-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* expand game info block * add event stages * add timer calculation and test * Expand GameStartType * resolve review changes * resolve review changes * update game timer and tests * rectify gameMode and inGameMode * return enums instead of strings * fix test pointing to removed attribute * small fixes * rename constant Co-authored-by: Vince Au <vince@canva.com>
- Loading branch information
Showing
10 changed files
with
350 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Based on https://github.com/wilsonzlin/edgesearch/blob/d03816dd4b18d3d2eb6d08cb1ae14f96f046141d/demo/wiki/client/src/util/util.ts | ||
|
||
// Ensures value is not null or undefined. | ||
// != does no type validation so we don't need to explcitly check for undefined. | ||
export function exists<T>(value: T | null | undefined): value is T { | ||
return value != null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { TimerType } from "../types"; | ||
import { frameToGameTimer } from "./gameTimer"; | ||
|
||
describe("when calculating the in-game timer", () => { | ||
it("should return unknown if no starting timer is provided", () => { | ||
const gameTimer = frameToGameTimer(1234, { | ||
timerType: TimerType.DECREASING, | ||
startingTimerSeconds: null, | ||
}); | ||
expect(gameTimer).toBe("Unknown"); | ||
}); | ||
|
||
it("should support increasing timers", () => { | ||
const gameTimer = frameToGameTimer(2014, { | ||
timerType: TimerType.INCREASING, | ||
startingTimerSeconds: 0, | ||
}); | ||
expect(gameTimer).toBe("00:33.57"); | ||
}); | ||
|
||
it("should support decreasing timers", () => { | ||
const gameTimer = frameToGameTimer(4095, { | ||
timerType: TimerType.DECREASING, | ||
startingTimerSeconds: 180, | ||
}); | ||
|
||
expect(gameTimer).toBe("01:51.76"); | ||
}); | ||
|
||
it("should support when the exact limit is hit", () => { | ||
const gameTimer = frameToGameTimer(10800, { | ||
timerType: TimerType.DECREASING, | ||
startingTimerSeconds: 180, | ||
}); | ||
|
||
expect(gameTimer).toBe("00:00.00"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { format } from "date-fns"; | ||
|
||
import type { GameStartType } from "../types"; | ||
import { TimerType } from "../types"; | ||
import { exists } from "./exists"; | ||
|
||
export function frameToGameTimer( | ||
frame: number, | ||
options: Pick<GameStartType, "timerType" | "startingTimerSeconds">, | ||
): string { | ||
const { timerType, startingTimerSeconds } = options; | ||
|
||
if (timerType === TimerType.DECREASING) { | ||
if (!exists(startingTimerSeconds)) { | ||
return "Unknown"; | ||
} | ||
const centiseconds = Math.ceil((((60 - (frame % 60)) % 60) * 99) / 59); | ||
const date = new Date(0, 0, 0, 0, 0, startingTimerSeconds - frame / 60, centiseconds * 10); | ||
return format(date, "mm:ss.SS"); | ||
} | ||
|
||
if (timerType === TimerType.INCREASING) { | ||
const centiseconds = Math.floor(((frame % 60) * 99) / 59); | ||
const date = new Date(0, 0, 0, 0, 0, frame / 60, centiseconds * 10); | ||
return format(date, "mm:ss.SS"); | ||
} | ||
|
||
return "Infinite"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.