Skip to content

Commit

Permalink
Added game end reasoning
Browse files Browse the repository at this point in the history
Allows queue to move hosts when the game ends.
Tested with 5 clients.

Need to:
Allow users to choose a name.
make queue ui look better
  • Loading branch information
ymmot239 committed Nov 22, 2024
1 parent dab1559 commit 636c577
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/server/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export let gameManager: GameManager | null = null;

const queue = new PriorityQueue<string>();
const names = new Map<string, string>();

//let the queue be moved once per game
let onlyOnce = true;

/**
* An endpoint used to establish a websocket connection with the server.
*
Expand All @@ -50,6 +54,31 @@ const names = new Map<string, string>();
export const websocketHandler: WebsocketRequestHandler = (ws, req) => {
ws.on("close", () => {
socketManager.handleSocketClosed(req.cookies.id);

//if you reload and the game is over
if (gameManager?.isGameEnded() && onlyOnce) {
onlyOnce = false;
for (const x in [1, 2]) {
console.log(x);
if (!queue.isEmpty()) {
const newHost = clientManager.getIds();
clientManager.removeHost();
clientManager.removeClient();
newHost ?
clientManager.assignPlayer(newHost[1])
: undefined;
const newPlayer = queue.pop();
if (newPlayer) {
//transfer them from spectator to the newly-opened spot and remove them from queue
clientManager.removeSpectator(newPlayer);
clientManager.assignPlayer(newPlayer);
names.delete(newPlayer);
socketManager.sendToAll(new GameStartedMessage());
}
}
}
}

//wait in case the client is just reloading or disconnected instead of leaving
setTimeout(() => {
if (socketManager.getSocket(req.cookies.id) === undefined) {
Expand Down Expand Up @@ -195,6 +224,7 @@ apiRouter.get("/game-state", (req, res) => {
});

apiRouter.post("/start-computer-game", (req, res) => {
onlyOnce = true;
const side = req.query.side as Side;
const difficulty = parseInt(req.query.difficulty as string) as Difficulty;
gameManager = new ComputerGameManager(
Expand All @@ -208,6 +238,7 @@ apiRouter.post("/start-computer-game", (req, res) => {
});

apiRouter.post("/start-human-game", (req, res) => {
onlyOnce = true;
const side = req.query.side as Side;
gameManager = new HumanGameManager(
new ChessEngine(),
Expand Down

0 comments on commit 636c577

Please sign in to comment.