win cond fixed

This commit is contained in:
bgoulard 2026-01-06 15:36:17 +01:00 committed by Nigel
parent 572392f640
commit 155f4ec169
3 changed files with 21 additions and 38 deletions

View file

@ -20,6 +20,7 @@ declare module 'ft_state' {
enum QueueState { enum QueueState {
InQueu = "In Queue", InQueu = "In Queue",
InGame = "In Game", InGame = "In Game",
Ready = "Ready-ing",
Iddle = "Queue Up", Iddle = "Queue Up",
In_local = "In Local", In_local = "In Local",
}; };
@ -203,19 +204,16 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
showInfo("rdy-evt"); showInfo("rdy-evt");
switch (rdy_btn.innerText) { switch (rdy_btn.innerText) {
case ReadyState.readyUp: case ReadyState.readyUp:
showInfo("sent:rdyup");
socket.emit('readyUp'); socket.emit('readyUp');
rdy_btn.innerText = ReadyState.readyDown; rdy_btn.innerText = ReadyState.readyDown;
break ; break ;
case ReadyState.readyDown: case ReadyState.readyDown:
showInfo("sent:rdydwn");
socket.emit('readyDown'); socket.emit('readyDown');
rdy_btn.innerText = ReadyState.readyUp; rdy_btn.innerText = ReadyState.readyUp;
break ; break ;
default: default:
showError("error on ready btn"); showError("error on ready btn");
} }
}); });
socket.on('newGame', async (state) => { socket.on('newGame', async (state) => {

View file

@ -98,11 +98,6 @@ function makeAngle(i: number): [number, number, number, number] {
} }
const LEFT :number = 0; const LEFT :number = 0;
const RIGHT :number = 1; const RIGHT :number = 1;
enum ReadyState {
noState,
readyUp,
readyDown,
}
export class Pong { export class Pong {
@ -136,7 +131,8 @@ export class Pong {
Pong.BALL_START_ANGLES[this.ballAngleIdx++], Pong.BALL_START_ANGLES[this.ballAngleIdx++],
); );
public ready_checks: [ReadyState, ReadyState] = [ReadyState.noState, ReadyState.noState]; public ready_checks: [boolean, boolean] = [false, false];
public first_rdy : number = -1;
public score: [number, number] = [0, 0]; public score: [number, number] = [0, 0];
public local: boolean = false; public local: boolean = false;
@ -159,41 +155,27 @@ export class Pong {
public readyup(uid : UserId) public readyup(uid : UserId)
{ {
// debug if (this.first_rdy == -1)
console.log(this.userLeft + " | " + this.userRight); this.first_rdy = Date.now();
if (uid === this.userLeft) { if (uid === this.userLeft) {
console.log("rdy.up : lft " + uid); this.ready_checks[LEFT] = true;
} else if (uid === this.userRight) { } else if (uid === this.userRight) {
console.log("rdy.up : rgt " + uid); this.ready_checks[RIGHT] = true;
}
if (uid === this.userLeft) {
this.ready_checks[LEFT] = ReadyState.readyUp;
} else if (uid === this.userRight) {
this.ready_checks[RIGHT] = ReadyState.readyUp;
} }
} }
public readydown(uid : UserId) public readydown(uid : UserId)
{ {
// debug
console.log(this.userLeft + " | " + this.userRight);
if (uid === this.userLeft) {
console.log("rdy.down : lft " + uid);
} else if (uid === this.userRight) {
console.log("rdy.down : rgt " + uid);
}
// is everyone already ready? // is everyone already ready?
if (this.ready_checks[LEFT] === ReadyState.readyUp && this.ready_checks[RIGHT] === ReadyState.readyUp) return ; if (this.ready_checks[LEFT] === true && this.ready_checks[RIGHT] === true) return ;
if (uid === this.userLeft) if (uid === this.userLeft)
this.ready_checks[LEFT] = ReadyState.readyDown; this.ready_checks[LEFT] = false;
else if (uid === this.userRight) else if (uid === this.userRight)
this.ready_checks[RIGHT] = ReadyState.readyDown; this.ready_checks[RIGHT] = false;
} }
public tick() { public tick() {
if (this.ready_checks[LEFT] !== ReadyState.readyUp || this.ready_checks[RIGHT] !== ReadyState.readyUp) if (this.ready_checks[LEFT] !== true || this.ready_checks[RIGHT] !== true)
return; return;
if (this.paddleCollision(this.leftPaddle, 'left')) { if (this.paddleCollision(this.leftPaddle, 'left')) {
this.ball.collided( this.ball.collided(
@ -306,15 +288,17 @@ export class Pong {
if (this.score[LEFT] >= 5) return 'left'; if (this.score[LEFT] >= 5) return 'left';
if (this.score[RIGHT] >= 5) return 'right'; if (this.score[RIGHT] >= 5) return 'right';
if ( if ((this.leftLastSeen !== -1 &&
this.leftLastSeen !== -1 && Date.now() - this.leftLastSeen > Pong.CONCEDED_TIMEOUT) ||
Date.now() - this.leftLastSeen > Pong.CONCEDED_TIMEOUT (this.first_rdy !== -1 && (this.ready_checks[0] || this.ready_checks[1])
&& Date.now() - this.first_rdy > Pong.CONCEDED_TIMEOUT && this.ready_checks[0] == false)
) { ) {
return 'right'; return 'right';
} }
if ( if ((this.rightLastSeen !== -1 &&
this.rightLastSeen !== -1 && Date.now() - this.rightLastSeen > Pong.CONCEDED_TIMEOUT) ||
Date.now() - this.rightLastSeen > Pong.CONCEDED_TIMEOUT (this.first_rdy !== -1 && (this.ready_checks[0] || this.ready_checks[1])
&& Date.now() - this.first_rdy > Pong.CONCEDED_TIMEOUT && this.ready_checks[1] == false)
) { ) {
return 'left'; return 'left';
} }

View file

@ -98,6 +98,7 @@ class StateI {
g.gameUpdate = setInterval(() => { g.gameUpdate = setInterval(() => {
g.tick(); g.tick();
if (g.ready_checks[0] && g.ready_checks[1])
this.gameUpdate(gameId, user.socket); this.gameUpdate(gameId, user.socket);
if (g.checkWinner() !== null) { this.cleanupGame(gameId, g); } if (g.checkWinner() !== null) { this.cleanupGame(gameId, g); }
}, 1000 / StateI.UPDATE_INTERVAL_FRAMES); }, 1000 / StateI.UPDATE_INTERVAL_FRAMES);