fix:local, fix:timer, fix:evt rdyEnd
This commit is contained in:
parent
b5c80d649e
commit
adb89aecbe
5 changed files with 27 additions and 14 deletions
|
|
@ -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';
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue