[wip] - frontend meh/20 back-end meh/20 still, works. works badly but does work...
This commit is contained in:
parent
f87a991441
commit
572392f640
5 changed files with 176 additions and 53 deletions
|
|
@ -1,5 +1,7 @@
|
|||
import { UserId } from '@shared/database/mixin/user';
|
||||
|
||||
|
||||
|
||||
export class Paddle {
|
||||
public static readonly DEFAULT_SPEED = 10;
|
||||
public static readonly DEFAULT_HEIGHT = 80;
|
||||
|
|
@ -94,8 +96,16 @@ function makeAngle(i: number): [number, number, number, number] {
|
|||
-Math.PI / i + Math.PI,
|
||||
];
|
||||
}
|
||||
const LEFT :number = 0;
|
||||
const RIGHT :number = 1;
|
||||
enum ReadyState {
|
||||
noState,
|
||||
readyUp,
|
||||
readyDown,
|
||||
}
|
||||
|
||||
export class Pong {
|
||||
|
||||
public gameUpdate: NodeJS.Timeout | null = null;
|
||||
|
||||
public static readonly CONCEDED_TIMEOUT: number = 1500;
|
||||
|
|
@ -126,6 +136,8 @@ export class Pong {
|
|||
Pong.BALL_START_ANGLES[this.ballAngleIdx++],
|
||||
);
|
||||
|
||||
public ready_checks: [ReadyState, ReadyState] = [ReadyState.noState, ReadyState.noState];
|
||||
|
||||
public score: [number, number] = [0, 0];
|
||||
public local: boolean = false;
|
||||
|
||||
|
|
@ -145,7 +157,44 @@ export class Pong {
|
|||
public userRight: UserId,
|
||||
) { }
|
||||
|
||||
public readyup(uid : UserId)
|
||||
{
|
||||
// debug
|
||||
console.log(this.userLeft + " | " + this.userRight);
|
||||
if (uid === this.userLeft) {
|
||||
console.log("rdy.up : lft " + uid);
|
||||
} else if (uid === this.userRight) {
|
||||
console.log("rdy.up : rgt " + uid);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// 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?
|
||||
if (this.ready_checks[LEFT] === ReadyState.readyUp && this.ready_checks[RIGHT] === ReadyState.readyUp) return ;
|
||||
|
||||
if (uid === this.userLeft)
|
||||
this.ready_checks[LEFT] = ReadyState.readyDown;
|
||||
else if (uid === this.userRight)
|
||||
this.ready_checks[RIGHT] = ReadyState.readyDown;
|
||||
}
|
||||
|
||||
public tick() {
|
||||
if (this.ready_checks[LEFT] !== ReadyState.readyUp || this.ready_checks[RIGHT] !== ReadyState.readyUp)
|
||||
return;
|
||||
if (this.paddleCollision(this.leftPaddle, 'left')) {
|
||||
this.ball.collided(
|
||||
'left',
|
||||
|
|
@ -254,8 +303,8 @@ export class Pong {
|
|||
|
||||
public checkWinner(): 'left' | 'right' | null {
|
||||
const checkInner = () => {
|
||||
if (this.score[0] >= 5) return 'left';
|
||||
if (this.score[1] >= 5) return 'right';
|
||||
if (this.score[LEFT] >= 5) return 'left';
|
||||
if (this.score[RIGHT] >= 5) return 'right';
|
||||
|
||||
if (
|
||||
this.leftLastSeen !== -1 &&
|
||||
|
|
|
|||
|
|
@ -69,11 +69,10 @@ class StateI {
|
|||
u1.currentGame = gameId;
|
||||
u2.currentGame = gameId;
|
||||
|
||||
// ---
|
||||
// wait for ready up
|
||||
// ---
|
||||
|
||||
g.gameUpdate = setInterval(() => {
|
||||
// ---
|
||||
// wait for ready up
|
||||
// ---
|
||||
g.tick();
|
||||
this.gameUpdate(gameId, u1.socket);
|
||||
this.gameUpdate(gameId, u2.socket);
|
||||
|
|
@ -152,6 +151,9 @@ class StateI {
|
|||
socket.on('enqueue', () => this.enqueueUser(socket));
|
||||
socket.on('dequeue', () => this.dequeueUser(socket));
|
||||
|
||||
socket.on('readyUp', () => this.readyupUser(socket));
|
||||
socket.on('readyDown', () => this.readydownUser(socket));
|
||||
|
||||
socket.on('gameMove', (e) => this.gameMove(socket, e));
|
||||
socket.on('localGame', () => this.newLocalGame(socket));
|
||||
}
|
||||
|
|
@ -215,6 +217,27 @@ class StateI {
|
|||
socket.emit('queueEvent', 'unregistered');
|
||||
}
|
||||
|
||||
private readydownUser(socket: SSocket) : void { //
|
||||
// do we know this user ?
|
||||
if (!this.users.has(socket.authUser.id)) return;
|
||||
const user = this.users.get(socket.authUser.id)!;
|
||||
// does the user have a game and do we know such game ?
|
||||
if (user.currentGame === null || !this.games.has(user.currentGame)) return;
|
||||
const game = this.games.get(user.currentGame)!;
|
||||
// is this a local game?
|
||||
if (game.local === true) return;
|
||||
game.readydown(user.id);
|
||||
}
|
||||
private readyupUser(socket: SSocket) : void { //
|
||||
// do we know this user ?
|
||||
if (!this.users.has(socket.authUser.id)) return;
|
||||
const user = this.users.get(socket.authUser.id)!;
|
||||
// does the user have a game and do we know such game ?
|
||||
if (user.currentGame === null || !this.games.has(user.currentGame)) return;
|
||||
const game = this.games.get(user.currentGame)!;
|
||||
if (game.local === true) return;
|
||||
game.readyup(user.id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue