Skip to content

Commit

Permalink
Updated db if time over then game Over
Browse files Browse the repository at this point in the history
  • Loading branch information
Airbornharsh committed Apr 21, 2024
1 parent 2829c80 commit 89ae9f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
11 changes: 4 additions & 7 deletions apps/frontend/src/screens/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const Game = () => {
break;

case USER_TIMEOUT:
setResult(USER_TIMEOUT)
setResult(message.payload.win)
break;

case GAME_JOINED:
Expand Down Expand Up @@ -156,7 +156,6 @@ export const Game = () => {
const getTimer = (tempTime: number) => {
const minutes = Math.floor(tempTime / (1000 * 60));
const remainingSeconds = Math.floor((tempTime % (1000 * 60)) / 1000);
const remainingMilliseconds = tempTime % 1000;

return (
<div className="text-white">
Expand All @@ -171,11 +170,9 @@ export const Game = () => {

return <div className="">
{result && <div className="justify-center flex pt-4 text-white">
{result === USER_TIMEOUT && <div>
{
gameMetadata?.blackPlayer?.id === user.id ? "You lost on time" : "Opponent lost on time"
}
</div>}
{result === "WHITE_WINS" && "White wins"}
{result === "BLACK_WINS" && "Black wins"}
{result === "DRAW" && "Draw"}
</div>}
{
started && <div className="justify-center flex pt-4 text-white">
Expand Down
25 changes: 24 additions & 1 deletion apps/ws/src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,27 @@ export class Game {
return;
}

if (this.player1Time <= 0 || this.player2Time <= 0) {
SocketManager.getInstance().broadcast(
this.gameId,
JSON.stringify({
type: USER_TIMEOUT,
payload: {
win: this.player1Time <= 0 ? "BLACK_WINS" : "WHITE_WINS",
},
})
);
await db.game.update({
data: {
status: "COMPLETED",
result: this.player1Time <= 0 ? "BLACK_WINS" : "WHITE_WINS"
},
where: {
id: this.gameId
}
})
}

try {
if (isPromoting(this.board, move.from, move.to)) {
this.board.move({
Expand Down Expand Up @@ -214,7 +235,9 @@ export class Game {
this.gameId,
JSON.stringify({
type: USER_TIMEOUT,
payload: { result: USER_TIMEOUT },
payload: {
win: this.board.turn() === "b" ? "WHITE_WINS" : "BLACK_WINS",
},
})
);
await db.game.update({
Expand Down

0 comments on commit 89ae9f4

Please sign in to comment.