Skip to content

Commit

Permalink
enhance: only create a new game when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanger committed Jul 4, 2023
1 parent c220a2e commit dbb40ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions server/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "planck";
import type { WebSocket } from "uWebSockets.js";

import { createNewGame, endGame, type PlayerContainer } from "./server";
import { allowJoin, createNewGame, endGame, type PlayerContainer } from "./server";
import { Map } from "./map";

import { Player } from "./objects/player";
Expand Down Expand Up @@ -415,7 +415,8 @@ export class Game {
this.over = true;
setTimeout(() => {
endGame(this.id); // End this game
createNewGame(this.id); // Create a new game
const otherID = this.id === 0 ? 1 : 0;
if (!allowJoin(otherID)) createNewGame(this.id); // Create a new game if the other game isn't allowing players to join
}, 1000);
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function endGame(id: number): void {
log(`Game #${id} ended`);
}

function allowJoin(gameID: number): boolean {
export function allowJoin(gameID: number): boolean {
return Boolean(games[gameID]?.allowJoin);
}

Expand Down

0 comments on commit dbb40ae

Please sign in to comment.