Skip to content

Commit

Permalink
Merge pull request #152 from govariantsteam/time_control
Browse files Browse the repository at this point in the history
add singleton service for timeout scheduling
  • Loading branch information
merowin authored Jan 5, 2024
2 parents ebe7c3a + 921c5f1 commit 69ee47c
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 93 deletions.
37 changes: 26 additions & 11 deletions packages/server/src/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
import { ObjectId, WithId, Document } from "mongodb";
import { getDb } from "./db";
import { io } from "./socket_io";
import { getTimeoutService } from "./index";
import {
GetInitialTimeControl,
HasTimeControlConfig,
timeControlHandlerMap,
ValidateTimeControlConfig,
Expand All @@ -35,21 +37,26 @@ export async function getGames(
return (await games).map(outwardFacingGame);
}

export async function getGamesWithTimeControl(): Promise<GameResponse[]> {
const games = gamesCollection()
.find({ time_control: { $ne: null } })
.toArray();
return (await games).map(outwardFacingGame);
}

export async function getGame(id: string): Promise<GameResponse> {
const db_game = await gamesCollection().findOne({
_id: new ObjectId(id),
});

// TODO: db_game might be undefined if unknown ID is provided

console.log(db_game);
const game = outwardFacingGame(db_game);
// Legacy games don't have a players field
// TODO: remove this code after doing proper db migration
if (!game.players) {
game.players = await BACKFILL_addEmptyPlayersArray(game);
}
console.log(game);

return game;
}
Expand All @@ -62,6 +69,7 @@ export async function createGame(
variant: variant,
moves: [] as MovesType[],
config: config,
time_control: GetInitialTimeControl(variant, config),
};

const result = await gamesCollection().insertOne(game);
Expand Down Expand Up @@ -94,33 +102,40 @@ export async function playMove(
throw Error("Game is already finished.");
}

const move = getOnlyMove(moves);
const { player: playerNr, move: new_move } = getOnlyMove(moves);

if (!game.players || game.players[move.player] == null) {
throw Error(`Seat ${move.player} not occupied!`);
if (!game.players || game.players[playerNr] == null) {
throw Error(`Seat ${playerNr} not occupied!`);
}

const expected_player = game.players[move.player];
const expected_player = game.players[playerNr];

if (expected_player.id !== user_id) {
throw Error(
`Not the right user: expected ${expected_player.id}, got ${user_id}`,
);
}

const { player, move: new_move } = getOnlyMove(moves);
game_obj.playMove(player, new_move);
game_obj.playMove(playerNr, new_move);

let timeControl = game.time_control;
if (
HasTimeControlConfig(game.config) &&
ValidateTimeControlConfig(game.config.time_control)
) {
const timeHandler = new timeControlHandlerMap[game.variant]();
await timeHandler.handleMove(game, game_obj, move.player, move.move);
if (game_obj.result !== "") {
getTimeoutService().clearGameTimeouts(game.id);
} else {
const timeHandler = new timeControlHandlerMap[game.variant]();
timeControl = timeHandler.handleMove(game, game_obj, playerNr, new_move);
}
}

gamesCollection()
.updateOne({ _id: new ObjectId(game_id) }, { $push: { moves: moves } })
.updateOne(
{ _id: new ObjectId(game_id) },
{ $push: { moves: moves }, $set: { time_control: timeControl } },
)
.catch(console.log);

game.moves.push(moves);
Expand Down
18 changes: 13 additions & 5 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { Strategy as LocalStrategy } from "passport-local";
import { UserResponse } from "@ogfcommunity/variants-shared";
import { router as apiRouter } from "./api";
import * as socket_io from "./socket_io";
import { TimeoutService } from "./timeout";

const LOCAL_ORIGIN = "http://localhost:3000";
const LOCAL_ORIGIN = "http://localhost:5173";

passport.use(
new LocalStrategy(async function (username, password, callback) {
Expand All @@ -36,11 +37,18 @@ passport.use(
}),
);

const timeoutService = new TimeoutService();
export function getTimeoutService(): TimeoutService {
return timeoutService;
}

// Initialize MongoDB
connectToDb().catch((e) => {
console.log("Unable to connect to the database.");
console.log(e);
});
connectToDb()
.then(() => timeoutService.initialize())
.catch((e) => {
console.log("Unable to connect to the database.");
console.log(e);
});

passport.use(
"guest",
Expand Down
Loading

0 comments on commit 69ee47c

Please sign in to comment.