Skip to content

Commit

Permalink
Merge pull request #104 from tscenping/game
Browse files Browse the repository at this point in the history
fix: Game 찐 최종
  • Loading branch information
cjho0316 authored Dec 29, 2023
2 parents 6c6a19a + 3d7473f commit 76b0980
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class AppService {

// push data to redis using zadd
async updateRanking(score: number, id: number) {
Logger.log(`Redis updateRanking: ${score}, ${id}`)
// Logger.log(`Redis updateRanking: ${score}, ${id}`);
await this.redis.zadd('rankings', score, id);
}
}
2 changes: 2 additions & 0 deletions src/game/dto/emit-event-match-end-param.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GameType } from '../../common/enum';

export class EmitEventMatchEndParamDto {
gameType: GameType;
rivalName: string;
rivalAvatar: string;
rivalScore: number | null;
myScore: number | null;
isWin: boolean | null;
Expand Down
57 changes: 34 additions & 23 deletions src/game/dto/game.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class GameDto {
gameStatus: GameStatus;
viewMap: ViewMapDto;
readyCnt: number;
gameInterrupted: boolean;
interrupted: boolean;

constructor(game: Game, readonly maxScore = 7) {
this.setGameId(game.id);
Expand All @@ -32,7 +32,7 @@ export class GameDto {
this.gameStatus = game.gameStatus;
this.viewMap = new ViewMapDto(game.ballSpeed, game.racketSize);
this.readyCnt = 0;
this.gameInterrupted = false;
this.interrupted = false;
}

getGameId() {
Expand All @@ -58,36 +58,47 @@ export class GameDto {
await this.viewMap.initObjects();
}

async setResult() {
if (this.gameType === GameType.NONE) {
async setGameOver() {
if (this.scoreLeft === this.maxScore) {
this.winnerScore = this.scoreLeft;
this.winnerId = this.playerLeftId;
this.winnerScore = 0;
this.loserScore = this.scoreRight;
this.loserId = this.playerRightId;
this.loserScore = 0;
return;
} else if (this.scoreRight === this.maxScore) {
this.winnerScore = this.scoreRight;
this.winnerId = this.playerRightId;
this.loserScore = this.scoreLeft;
this.loserId = this.playerLeftId;
}
}

async setGameInterrupted() {
// 게임 중에 비정상 종료돼서 몰수패 처리
if (this.loserId) {
this.loserScore = 0;
this.winnerScore = this.maxScore;
this.winnerId =
this.loserId === this.playerLeftId
? this.playerRightId
: this.playerLeftId;
} else {
if (this.scoreLeft === this.maxScore) {
this.winnerScore = this.scoreLeft;
this.winnerId = this.playerLeftId;
this.loserScore = this.scoreRight;
this.loserId = this.playerRightId;
} else if (this.scoreRight === this.maxScore) {
this.winnerScore = this.scoreRight;
const isLoserLeft = this.loserId === this.playerLeftId;
if (isLoserLeft) {
this.scoreLeft = this.loserScore = 0;
this.scoreRight = this.winnerScore = this.maxScore;
this.winnerId = this.playerRightId;
this.loserScore = this.scoreLeft;
this.loserId = this.playerLeftId;
} else {
this.scoreRight = this.loserScore = 0;
this.scoreLeft = this.winnerScore = this.maxScore;
this.winnerId = this.playerLeftId;
}
}
}

async setNone() {
// 게임 시작 전 비정상 종료돼서 게임 자체를 무효 처리
this.scoreLeft = this.scoreRight = 0;
this.winnerId = this.playerLeftId;
this.winnerScore = this.loserScore = 0;
this.loserId = this.playerRightId;
this.loserScore = 0;
this.gameType = GameType.NONE;
this.gameStatus = GameStatus.FINISHED;
}

private setGameId(gameId: number) {
this.gameId = gameId;
}
Expand Down
90 changes: 55 additions & 35 deletions src/game/dto/view-map.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,17 @@ export class ViewMapDto {
this.racketRight.y = this.canvasHeight / 2 - this.racketHeight / 2;
}

private async updateBall() {
private async calculateNextBallLocation() {
const ball = this.ball;
const dt = this.deltaTime;

// 공의 위치 업데이트
const x = ball.x + ball.xVelocity * dt + ball.accel * dt * dt * 0.5;
const y = ball.y + ball.yVelocity * dt + ball.accel * dt * dt * 0.5;
// 공의 속력 업데이트
// ball.xVelocity += ball.accel * dt * (ball.xVelocity > 0 ? 1 : -1);
// ball.yVelocity += ball.accel * dt * (ball.yVelocity > 0 ? 1 : -1);

this.ball.vx = x - ball.x;
this.ball.vy = y - ball.y;
this.ball.x = x;
this.ball.y = y;
// 공의 위치 변화량 계산
ball.vx =
ball.xVelocity * dt +
(ball.xVelocity < 0 ? ball.accel * -1 : ball.accel) * dt * dt * 0.5;
ball.vy =
ball.yVelocity * dt +
(ball.yVelocity < 0 ? ball.accel * -1 : ball.accel) * dt * dt * 0.5;
}

updateRacketLeft(action: KEYNAME) {
Expand Down Expand Up @@ -174,10 +170,30 @@ export class ViewMapDto {
async changes() {
const updateDto = this.updateDto;
const ball = this.ball;
await this.updateBall();

// racket, 천장, 바닥에 부딪히는지
await this.calculateNextBallLocation();

const xChange = this.ballRadius;
const piece = Math.abs(ball.vx / xChange);
const xRemain = Math.abs(ball.vx % xChange);
const yChange = Math.abs(xChange * (ball.vy / ball.vx));
const yRemain = Math.abs(ball.vy % yChange);

for (let i = 0; i < Math.floor(piece); i++) {
ball.x += xChange * (ball.xVelocity > 0 ? 1 : -1);
ball.y += yChange * (ball.yVelocity > 0 ? 1 : -1);
// console.log(`\nbefore piece ball.x: ${ball.x}`);
// console.log(`before piece xVelocity: ${ball.xVelocity}`);
await this.detectCollision();
// console.log(`after piece ball.x: ${ball.x}`);
// console.log(`after piece xVelocity: ${ball.xVelocity}`);
}
ball.x += xRemain * (ball.xVelocity > 0 ? 1 : -1);
ball.y += yRemain * (ball.yVelocity > 0 ? 1 : -1);
// console.log(`before remain ball.x: ${ball.x}`);
// console.log(`before remain xVelocity: ${ball.xVelocity}`);
await this.detectCollision();
// console.log(`after remain ball.x: ${ball.x}`);
// console.log(`after remain xVelocity: ${ball.xVelocity}`);

//score
if (ball.x + this.ballRadius >= this.canvasWidth)
Expand Down Expand Up @@ -211,22 +227,24 @@ export class ViewMapDto {
dy = Math.abs(ball.y - this.getRacketRightCenter().cy);
if (
dx <= this.ballRadius + this.racketWidth / 2 &&
dy <= this.ballRadius + this.racketHeight / 2
dy <= this.ballRadius + this.racketHeight / 2 &&
ball.xVelocity > 0
) {
ball.xVelocity *= -1;
console.log('\nbefore speed: ', Math.abs(ball.xVelocity));
// console.log('\nbefore speed: ', Math.abs(ball.xVelocity));
ball.xVelocity +=
ball.accel *
2 *
this.deltaTime *
(ball.xVelocity > 0 ? 1 : -1);
console.log(
'collision right -> ',
ball.accel *
2 *
this.deltaTime *
(ball.xVelocity > 0 ? 1 : -1),
);

// console.log(
// 'collision right -> ',
// ball.accel *
// 2 *
// this.deltaTime *
// (ball.xVelocity > 0 ? 1 : -1),
// );
console.log('accel, deltaTime: ', ball.accel, this.deltaTime);
console.log('after speed: ', Math.abs(ball.xVelocity));
}
Expand All @@ -235,31 +253,33 @@ export class ViewMapDto {
dy = Math.abs(ball.y - this.getRacketLeftCenter().cy);
if (
dx <= this.ballRadius + this.racketWidth / 2 &&
dy <= this.ballRadius + this.racketHeight / 2
dy <= this.ballRadius + this.racketHeight / 2 &&
ball.xVelocity < 0
) {
ball.xVelocity *= -1;
console.log('\nbefore speed: ', Math.abs(ball.xVelocity));
// console.log('\nbefore speed: ', Math.abs(ball.xVelocity));
ball.xVelocity +=
ball.accel *
2 *
this.deltaTime *
(ball.xVelocity > 0 ? 1 : -1);
console.log(
'collision left -> ',
ball.accel *
2 *
this.deltaTime *
(ball.xVelocity > 0 ? 1 : -1),
);
// console.log(
// 'collision left -> ',
// ball.accel *
// 2 *
// this.deltaTime *
// (ball.xVelocity > 0 ? 1 : -1),
// );
console.log('accel, deltaTime: ', ball.accel, this.deltaTime);
console.log('after speed: ', Math.abs(ball.xVelocity));
}
}

// 바닥, 천장
if (
ball.y + this.ballRadius >= this.canvasHeight ||
ball.y - this.ballRadius < 0
(ball.y + this.ballRadius >= this.canvasHeight &&
ball.yVelocity > 0) ||
(ball.y - this.ballRadius < 0 && ball.yVelocity < 0)
)
ball.yVelocity *= -1;
}
Expand Down
Loading

0 comments on commit 76b0980

Please sign in to comment.