Skip to content

Commit

Permalink
Allow beatmap to be optional when converting a replay
Browse files Browse the repository at this point in the history
  • Loading branch information
kionell committed Jun 18, 2023
1 parent 28838c7 commit b65d668
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/Replays/ReplayConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class ReplayConverter {
* @param original Any kind of a replay.
* @returns The converted replay.
*/
convertReplay(original: IReplay, beatmap: IBeatmap): Replay {
convertReplay(original: IReplay, beatmap?: IBeatmap): Replay {
const converted = this.createReplay();

converted.gameVersion = original.gameVersion;
Expand All @@ -34,11 +34,11 @@ export abstract class ReplayConverter {
return new Replay();
}

*convertFrames(frames: IReplayFrame[], beatmap: IBeatmap): Generator<ReplayFrame> {
*convertFrames(frames: IReplayFrame[], beatmap?: IBeatmap): Generator<ReplayFrame> {
let lastFrame: ReplayFrame | null = null;

for (const frame of frames) {
const convertedFrame = this._convertFrame(frame, beatmap, lastFrame);
const convertedFrame = this._convertFrame(frame, lastFrame, beatmap);

yield convertedFrame;

Expand All @@ -48,8 +48,8 @@ export abstract class ReplayConverter {

protected _convertFrame(
frame: IReplayFrame,
beatmap: IBeatmap,
lastFrame: IReplayFrame | null,
beatmap?: IBeatmap,
): ReplayFrame {
if (this._isConvertedReplayFrame(frame)) {
return frame;
Expand All @@ -58,12 +58,15 @@ export abstract class ReplayConverter {
const convertedFrame = this._createConvertibleReplayFrame();

if (convertedFrame && frame instanceof LegacyReplayFrame) {
return convertedFrame.fromLegacy(frame, beatmap, lastFrame);
return convertedFrame.fromLegacy(frame, lastFrame, beatmap);
}

throw new Error('Replay can not be converted to this ruleset!');
}

/**
* @returns A new instance of convertible replay frame.
*/
protected abstract _createConvertibleReplayFrame(): IConvertibleReplayFrame | null;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Replays/Types/IConvertibleReplayFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export interface IConvertibleReplayFrame extends IReplayFrame {
*/
fromLegacy(
currentFrame: LegacyReplayFrame,
beatmap: IBeatmap,
lastFrame: IReplayFrame | null
lastFrame: IReplayFrame | null,
beatmap?: IBeatmap,
): this;

/**
* Populates this {@link ReplayFrame} using values from a {@link LegacyReplayFrame}.
* @param beatmap The beatmap of the replay which is used to get some data.
* @returns A new instance of legacy replay frame.
*/
toLegacy(beatmap: IBeatmap): LegacyReplayFrame;
toLegacy(beatmap?: IBeatmap): LegacyReplayFrame;
}
2 changes: 1 addition & 1 deletion src/Rulesets/IRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface IRuleset {
* @param beatmap The beatmap of the replay which is used to get some data.
* @returns A new instance of the replay with applied ruleset.
*/
applyToReplay(replay: IReplay, beatmap: IBeatmap): Replay;
applyToReplay(replay: IReplay, beatmap?: IBeatmap): Replay;

/**
* Resets a mod combination from a beatmap.
Expand Down
4 changes: 2 additions & 2 deletions src/Rulesets/Ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ export abstract class Ruleset implements IRuleset {
* @param beatmap The beatmap of the replay which is used to get some data.
* @returns A new instance of the replay with applied ruleset.
*/
applyToReplay(replay: IReplay, beatmap: IBeatmap): Replay {
if (replay.mode !== beatmap.mode) {
applyToReplay(replay: IReplay, beatmap?: IBeatmap): Replay {
if (beatmap && replay.mode !== beatmap.mode) {
throw new Error('Replay and beatmap mode does not match!');
}

Expand Down

0 comments on commit b65d668

Please sign in to comment.