Skip to content

Commit

Permalink
add docs for time control and timeout service
Browse files Browse the repository at this point in the history
  • Loading branch information
merowin committed Jan 1, 2024
1 parent 549c55d commit 29fc568
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/server/src/time-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { AbstractGame, GameResponse } from "@ogfcommunity/variants-shared";
import { TimeoutService } from './timeout';
import { getTimeoutService } from './index';

/**
* Validates whether game_config has type and
* properties for being a config with time control
*/
export function HasTimeControlConfig(
game_config: unknown,
): game_config is IConfigWithTimeControl {
Expand All @@ -20,6 +24,10 @@ export function HasTimeControlConfig(
);
}

/**
* Validates whether time_control_config has type and
* properties for being a time control config
*/
export function ValidateTimeControlConfig(
time_control_config: unknown,
): time_control_config is ITimeControlConfig {
Expand All @@ -31,6 +39,10 @@ export function ValidateTimeControlConfig(
);
}

/**
* Validates whether time_control has type and
* properties for being a basic time control data object
*/
export function ValidateTimeControlBase(
time_control: unknown,
): time_control is ITimeControlBase {
Expand All @@ -42,6 +54,10 @@ export function ValidateTimeControlBase(
);
}

/**
* Returns the initial state of a time control data object
* for a new game, if it has time control at all
*/
export function GetInitialTimeControl(variant: string, config: object): ITimeControlBase | null {
if (!HasTimeControlConfig(config)) return null;

Expand All @@ -51,15 +67,27 @@ export function GetInitialTimeControl(variant: string, config: object): ITimeCon

// validation of the config should happen before this is called
export interface ITimeHandler {

/**
* Returns the initial state of a time control data object
* that this handler works with
*/
initialState(variant: string, config: IConfigWithTimeControl): ITimeControlBase;

/**
* transition for the time control data when a move is played
*/
handleMove(
game: GameResponse,
game_obj: AbstractGame<unknown, unknown>,
playerNr: number,
move: string,
): ITimeControlBase;

/**
* Returns the time in milliseconds until this player
* times out, provided no moves are played
*/
getMsUntilTimeout(game: GameResponse, playerNr: number): number;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/server/src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export class TimeoutService {
constructor() {
}

/**
* initializes timeout schedules
*/
public async initialize(): Promise<void> {
// I would like to improve this by only querying unfinished games from db
// but currently I think its not possible, because of the game result
Expand Down Expand Up @@ -51,6 +54,10 @@ export class TimeoutService {
}
}

/**
* stores a timeout-id or null for this player + game
* if an id was previously stored, the timeout is cleared
*/
private setPlayerTimeout(gameId: string, playerNr: number, timeout: ReturnType<typeof setTimeout> | null): void {
let gameTimeouts = this.timeoutsByGame.get(gameId);

Expand All @@ -68,6 +75,9 @@ export class TimeoutService {
gameTimeouts[playerNr] = timeout;
}

/**
* clears the timeout-ids of this game
*/
public clearGameTimeouts(gameId: string): void {
const gameTimeouts = this.timeoutsByGame.get(gameId)

Expand All @@ -81,6 +91,11 @@ export class TimeoutService {
this.setPlayerTimeout(gameId, playerNr, null);
}

/**
* schedules a timeout for this player + game
* timeout is resolved by adding a timeout move
* and applying the time control handler again
*/
public scheduleTimeout(gameId: string, playerNr: number, inTimeMs: number): void {
const timeoutResolver = async () => {
const game = await getGame(gameId)
Expand Down

0 comments on commit 29fc568

Please sign in to comment.