Skip to content

Commit

Permalink
Merge branch 'main' into DEV-GAMEINVITE
Browse files Browse the repository at this point in the history
  • Loading branch information
sedeve authored Nov 14, 2023
2 parents 4bd5f76 + 3bf1381 commit b39f08f
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 1,501 deletions.
192 changes: 134 additions & 58 deletions backend/code/src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export class Game {
) {}

private screenAdapter(player, x: number, y: number, ballsize: number) {
let scale_x = player.w / this.w;
let scale_y = player.h / this.h;
const scale_x = player.w / this.w;
const scale_y = player.h / this.h;

let new_x = x * scale_x;
let new_y = y * scale_y;
const new_x = x * scale_x;
const new_y = y * scale_y;

let new_ball_size = ballsize * Math.min(scale_x, scale_y);
const new_ball_size = ballsize * Math.min(scale_x, scale_y);
return {
x: new_x,
y: new_y,
Expand All @@ -31,15 +31,25 @@ export class Game {
p2PaddleY: number,
side: boolean,
) {
let scale = this.h / player1.h;

this.p1PaddleY = p1PaddleY * scale;
const scale = this.h / player1.h;
if (
p1PaddleY * scale - this.paddleHeight / 6 > 0 &&
p1PaddleY * scale + this.paddleHeight < this.h
) {
this.p1PaddleY = p1PaddleY * scale;
}

let scale2 = player2.h / this.h;
const scale2 = player2.h / this.h;

let newPos = this.p1PaddleY * scale2;
let scale_y = player2.h / this.h;
const newPos = this.p1PaddleY * scale2;
// let scale_y = player2.h / this.h;
// let center = this.paddleHeight * scale_y;

if (p2PaddleY - player2.h / 6 / 6 < 0) {
p2PaddleY = 0;
} else if (p2PaddleY + player2.h / 6 > player2.h) {
p2PaddleY = player2.h - player2.h / 6;
}
return { p1PaddleY: newPos, p2PaddleY: p2PaddleY, side: side };
}
private paddleAdapterP2toP1(
Expand All @@ -49,70 +59,96 @@ export class Game {
p2PaddleY: number,
side: boolean,
) {
let scale = this.h / player2.h;

this.p2PaddleY = p2PaddleY * scale;
const scale = this.h / player2.h;
if (
p2PaddleY * scale - this.paddleHeight / 6 >= 0 &&
p2PaddleY * scale + this.paddleHeight <= this.h
)
this.p2PaddleY = p2PaddleY * scale;

let scale2 = player1.h / this.h;
const scale2 = player1.h / this.h;

let newPos = this.p2PaddleY * scale2;
let scale_y = player1.h / this.h;
const newPos = this.p2PaddleY * scale2;
// let scale_y = player1.h / this.h;

// let center = this.paddleHeight * scale_y;

if (p1PaddleY - player1.h / 6 / 6 < 0) {
p1PaddleY = 0;
} else if (p1PaddleY + player1.h / 6 > player1.h) {
p1PaddleY = player1.h - player1.h / 6;
}
return { p1PaddleY: p1PaddleY, p2PaddleY: newPos, side: side };
}
private up1() {
if (this.p1PaddleY - this.paddleHeight / 6 >= 0) {
this.eventp1Paddle -= this.paddleHeight / 6;
this.eventp1Paddle -= this.p1Res.h / 6 / 6;
if (this.eventp1Paddle - this.p1Res.h / 6 / 6 < 0) {
this.eventp1Paddle = 0;
}
}

private down1() {
if (this.p1PaddleY + this.paddleHeight <= this.h) {
this.eventp1Paddle += this.paddleHeight / 6;
this.eventp1Paddle += this.p1Res.h / 6 / 6;
if (this.eventp1Paddle + this.p1Res.h / 6 > this.p1Res.h) {
this.eventp1Paddle = this.p1Res.h - this.p1Res.h / 6;
}
}
private up2() {
if (this.p2PaddleY - this.paddleHeight / 6 >= 0) {
this.eventp2Paddle -= this.paddleHeight / 6;
this.eventp2Paddle -= this.p2Res.h / 6 / 6;
if (this.eventp2Paddle - this.p2Res.h / 6 / 6 < 0) {
this.eventp2Paddle = 0;
}
}

private down2() {
if (this.p2PaddleY + this.paddleHeight <= this.h) {
this.eventp2Paddle += this.paddleHeight / 6;

this.eventp2Paddle += this.p2Res.h / 6 / 6;
if (this.eventp2Paddle - this.p2Res.h / 6 / 6 < 0) {
this.eventp2Paddle = 0;
}
}

private async loop() {
if (this.closeGame) return;
console.log('loop');
if (this.x + this.ballSize / 2 + this.dx >= this.w || this.x + this.dx <= 0)

if (
this.x + this.dx + this.ballSize / 2 >= this.w ||
this.x + this.dx - this.ballSize / 2 <= 0
)
this.dx *= -1;
if (this.y + this.ballSize / 2 + this.dy >= this.h || this.y + this.dy <= 0)
if (
this.y + this.dy + this.ballSize / 2 >= this.h ||
this.y + this.dy - this.ballSize / 2 <= 0
)
this.dy *= -1;

if (
this.y >= this.p1PaddleY &&
this.y <= this.p1PaddleY + this.paddleHeight &&
this.x + this.ballSize <= this.paddleWidth + 40
this.y > this.p1PaddleY &&
this.y < this.p1PaddleY + this.paddleHeight &&
this.x <= this.paddleWidth + this.gap + this.ballSize / 2
) {
this.dx *= -1;
this.dy *= Math.random() * 2.5;
this.dy = Math.random() * (4 - 1.5) + 1.5;
}

if (
this.y >= this.p2PaddleY &&
this.y <= this.p2PaddleY + this.paddleHeight &&
this.x + this.ballSize >= this.w - (this.paddleWidth + 20)

this.y > this.p1PaddleY &&
this.y < this.p1PaddleY + this.paddleHeight &&
this.x >= this.w - (this.paddleWidth + this.gap + this.ballSize / 2)
) {
this.dx *= -1;
this.dy *= Math.random() * 2.5;
this.dy = Math.random() * (4 - 1.5) + 1.5;
}
if (
(this.y < this.p2PaddleY ||
this.y > this.p2PaddleY + this.paddleHeight) &&
this.x + this.ballSize >= this.w - (this.paddleWidth + 5)

this.x + this.ballSize / 2 >= this.w - (this.paddleWidth + this.gap)
) {
console.log(`${this.p1PaddleY} ${this.x} ${this.y} ${this.ballSize}`);
this.p1Score += 1;
this.init();
this.checkForWinner();
Expand All @@ -121,8 +157,10 @@ export class Game {
if (
(this.y < this.p1PaddleY ||
this.y > this.p1PaddleY + this.paddleHeight) &&
this.x <= this.paddleWidth + 5

this.x - this.ballSize / 2 <= this.paddleWidth + this.gap
) {
console.log(`${this.p1PaddleY} ${this.x} ${this.y} ${this.ballSize}`);
this.p2Score += 1;
this.init();
this.checkForWinner();
Expand All @@ -131,16 +169,21 @@ export class Game {

console.log(this.x);
console.log(this.y);

// const forwardx = this.x + this.dx;
// const forwardy = this.y + this.dy
// if (forwardx > this.) {
// }
this.x += this.dx;
this.y += this.dy;
console.log(this.p1Res);
console.log(this.p2Res);

if (
parseFloat((this.p1Res.w / this.p1Res.h).toFixed(1)) !== 1.8 &&
parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.9
) {
this.p1socket.emit('screen Error');
this.emitGameEnd('end');

this.emitGameEnd('p1Leave');
this.p1socket.emit('lose', 'trying cheat');
this.p2socket.emit('win', 'you win other player try to cheat');
} else {
Expand All @@ -165,7 +208,8 @@ export class Game {
parseFloat((this.p2Res.w / this.p2Res.h).toFixed(1)) !== 1.9
) {
this.p1socket.emit('screen Error');
this.emitGameEnd('end');

this.emitGameEnd('p2Leave');
this.p1socket.emit('win', 'you win other player try to cheat');
this.p2socket.emit('lose', 'trying cheat');
} else {
Expand All @@ -185,7 +229,7 @@ export class Game {
);
}

await this.sleep(20);
await this.sleep(this.frames);

this.loop();
}
Expand All @@ -195,7 +239,8 @@ export class Game {

for (let i = 0; i < 6; i++) {
await new Promise((resolve) => setTimeout(resolve, 1000));
this.server.emit('timer', timer);

this.server.to(this.gameid).emit('timer', timer);
timer -= 1000;
}
}
Expand All @@ -221,6 +266,10 @@ export class Game {
console.log(p2Data);
this.server.emit('players', [p1Data, p2Data]);
console.log('newfunc');

setInterval(() => {
this.frames -= 1;
}, 2000);
this.p1socket.on('up', () => {
this.up1();
});
Expand Down Expand Up @@ -254,16 +303,15 @@ export class Game {
this.emitGameEnd('p2 disconnected');
});
this.p1socket.on('leave', () => {
this.emitGameEnd('end');

this.emitGameEnd('p1Leave');
this.p2socket.emit('win', 'you win other player leave the game');
this.p1socket.emit('lose', 'you win other player leave the game');
this.emitGameEnd('leave');
});
this.p2socket.on('leave', () => {
this.emitGameEnd('end');
this.emitGameEnd('p2Leave');
this.p1socket.emit('win', 'you win other player leave the game');
this.p2socket.emit('lose', 'you lost other player leave the game');
this.emitGameEnd('leave');
});
}
private checkForWinner() {
Expand All @@ -280,15 +328,38 @@ export class Game {
}
private emitGameEnd(message: string) {
console.log('game end');
this.eventEmitter.emit('game.end', {
message: message,
gameid: this.gameid,
p1Data: this.p1Data,
p2Data: this.p2Data,
p1Score: this.p1Score,
p2Score: this.p2Score,
});
this.closeGame = true;
if (message === 'p1Leave') {
this.eventEmitter.emit('game.end', {
resign: 1,
message: message,
gameid: this.gameid,
p1Data: this.p1Data,
p2Data: this.p2Data,
p1Score: this.p1Score,
p2Score: this.p2Score,
});
} else if (message === 'p2Leave') {
this.eventEmitter.emit('game.end', {
resign: 2,
message: message,
gameid: this.gameid,
p1Data: this.p1Data,
p2Data: this.p2Data,
p1Score: this.p1Score,
p2Score: this.p2Score,
});
} else {
this.eventEmitter.emit('game.end', {
resign: 0,
message: message,
gameid: this.gameid,
p1Data: this.p1Data,
p2Data: this.p2Data,
p1Score: this.p1Score,
p2Score: this.p2Score,
});
}
}

private sleep(ms: number) {
Expand All @@ -301,23 +372,28 @@ export class Game {
this.x = this.w / 2;
this.y = this.h / 2;
this.ballSize = this.w / 42;
this.dx = this.w / 200;
this.dy = this.w / 200;

this.dx = Math.random() > 0.5 ? this.w / 220 : (this.w / 220) * -1;
this.dy = Math.random() > 0.5 ? this.w / 220 : (this.w / 220) * -1;
this.p1PaddleY = this.h / 2;
this.p2PaddleY = this.h / 2;
this.frames = 25;
}
private gameid: string;
private p1socket: Socket;
private p2socket: Socket;
private p1Data: any;
private p2Data: any;
private frames: number = 25;
private w: number = 1067;
private h: number = 600;
private x: number = this.w / 2;
private y: number = this.h / 2;
private gap: number = this.w / 100;
private ballSize: number = this.w / 42;
private dx: number = this.w / 200;
private dy: number = this.w / 200;

private dx: number = Math.random() > 0.5 ? this.w / 220 : (this.w / 220) * -1;
private dy: number = Math.random() > 0.5 ? this.w / 220 : (this.w / 220) * -1;
private p1PaddleY: number = this.h / 2;
private p2PaddleY: number = this.h / 2;
private eventp1Paddle: number = 0;
Expand Down
Loading

0 comments on commit b39f08f

Please sign in to comment.