diff --git a/frontend/src/pages/pong/pong.ts b/frontend/src/pages/pong/pong.ts index 91d7eb7..fb80108 100644 --- a/frontend/src/pages/pong/pong.ts +++ b/frontend/src/pages/pong/pong.ts @@ -230,15 +230,15 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn set_pretty(batRight, playerR, playerL, SELF_COLOR); } else showError("couldn't find your id in game"); - rdy_btn.classList.remove('hidden'); rdy_btn.innerText = ReadyState.readyUp; - - setTimeout(() => { - rdy_btn.classList.add('hidden');}, 2000); // 1500 : pong.CONCEDED_TIMEOUT + }); + socket.on('rdyEnd', () => { + rdy_btn.classList.add('hidden'); }); socket.on("gameEnd", (winner) => { + rdy_btn.classList.remove('hidden'); queueBtn.innerHTML = QueueState.Iddle; queueBtn.style.color = 'white'; diff --git a/frontend/src/pages/pong/socket.ts b/frontend/src/pages/pong/socket.ts index 2800909..387f2c7 100644 --- a/frontend/src/pages/pong/socket.ts +++ b/frontend/src/pages/pong/socket.ts @@ -45,6 +45,7 @@ export interface ClientToServer { export interface ServerToClient { forceDisconnect: (reason: string) => void; queueEvent: (msg: 'registered' | 'unregistered') => void; + rdyEnd:() => void, updateInformation: (info: UpdateInfo) => void, newGame: (initState: GameUpdate) => void, // <- consider this the gameProc eg not start of game but wait for client to "ready up" gameUpdate: (state: GameUpdate) => void, diff --git a/src/pong/src/game.ts b/src/pong/src/game.ts index 71ded29..cb93941 100644 --- a/src/pong/src/game.ts +++ b/src/pong/src/game.ts @@ -132,6 +132,7 @@ export class Pong { Pong.BALL_START_ANGLES[this.ballAngleIdx++], ); + public sendSig : boolean = false; public ready_checks: [boolean, boolean] = [false, false]; public game_creation : number = Date.now(); @@ -158,7 +159,7 @@ export class Pong { { if (uid === this.userLeft) { this.ready_checks[LEFT] = true; - } else if (uid === this.userRight) { + } if (uid === this.userRight) { this.ready_checks[RIGHT] = true; } } @@ -169,12 +170,12 @@ export class Pong { if (uid === this.userLeft) this.ready_checks[LEFT] = false; - else if (uid === this.userRight) + if (uid === this.userRight) this.ready_checks[RIGHT] = false; } public tick() { - if (this.ready_checks[LEFT] !== true || this.ready_checks[RIGHT] !== true) + if (!this.local && (this.ready_checks[LEFT] !== true || this.ready_checks[RIGHT] !== true)) return; if (this.paddleCollision(this.leftPaddle, 'left')) { this.ball.collided( @@ -287,7 +288,7 @@ export class Pong { if (this.score[LEFT] >= 5) return 'left'; if (this.score[RIGHT] >= 5) return 'right'; - if (this.game_creation !== -1 && Date.now() - this.game_creation > Pong.CONCEDED_TIMEOUT && (!this.ready_checks[0] || !this.ready_checks[1])) { + if (this.local !== true && this.game_creation !== -1 && Date.now() - this.game_creation > Pong.CONCEDED_TIMEOUT * 10 && (!this.ready_checks[0] || !this.ready_checks[1])) { if (!this.ready_checks[0] && !this.ready_checks[1]) return (randomInt(1) == 1 ? 'left' : 'right'); if (!this.ready_checks[0]) return ('right'); if (!this.ready_checks[1]) return ('left'); diff --git a/src/pong/src/socket.ts b/src/pong/src/socket.ts index ab9b066..1126c2a 100644 --- a/src/pong/src/socket.ts +++ b/src/pong/src/socket.ts @@ -44,6 +44,7 @@ export interface ClientToServer { export interface ServerToClient { forceDisconnect: (reason: string) => void; queueEvent: (msg: 'registered' | 'unregistered') => void; + rdyEnd:() => void, updateInformation: (info: UpdateInfo) => void, newGame: (initState: GameUpdate) => void, gameUpdate: (state: GameUpdate) => void, diff --git a/src/pong/src/state.ts b/src/pong/src/state.ts index 3293568..3995565 100644 --- a/src/pong/src/state.ts +++ b/src/pong/src/state.ts @@ -74,11 +74,18 @@ class StateI { // wait for ready up // --- g.tick(); - this.gameUpdate(gameId, u1.socket); - this.gameUpdate(gameId, u2.socket); - if (g.checkWinner() !== null) { - this.cleanupGame(gameId, g); + if (g.sendSig === false && g.ready_checks[0] === true && g.ready_checks[1] === true) + { + u1.socket.emit('rdyEnd'); + u2.socket.emit('rdyEnd'); + g.sendSig = true; } + if (g.ready_checks[0] === true && g.ready_checks[1] === true) + { + this.gameUpdate(gameId, u1.socket); + this.gameUpdate(gameId, u2.socket); + } + if (g.checkWinner() !== null) {this.cleanupGame(gameId, g); } }, 1000 / StateI.UPDATE_INTERVAL_FRAMES); } } @@ -98,8 +105,11 @@ class StateI { g.gameUpdate = setInterval(() => { g.tick(); - if (g.ready_checks[0] && g.ready_checks[1]) - this.gameUpdate(gameId, user.socket); + this.gameUpdate(gameId, user.socket); + if (g.sendSig === false) { + user.socket.emit('rdyEnd'); + g.sendSig = true; + } if (g.checkWinner() !== null) { this.cleanupGame(gameId, g); } }, 1000 / StateI.UPDATE_INTERVAL_FRAMES);