Skip to content

Commit

Permalink
fix: splice and change idx 2 but matching interval idx not changed no…
Browse files Browse the repository at this point in the history
…w Good Working (#12)
  • Loading branch information
minsikim-42 committed Dec 3, 2022
1 parent 26560be commit dadc39f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
12 changes: 8 additions & 4 deletions backend/src/core/game/game-queue.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { Injectable } from "@nestjs/common";
export class GameQueueRepository {
private gameQue: any[] = [];
private queLoop: NodeJS.Timer[] = [];
private idx: number = 0;
private idx: number = -1;
constructor(
private gameRoomRepository: GameRoomRepository,
private userRepository: UserRepository,
) { }

addQue(userId: string, rating: number, _mode: GameMode) {
this.gameQue[this.idx] = { userId: userId, ladder: rating, mode: _mode, wait: 0 };
this.gameQue[++this.idx] = { userId: userId, ladder: rating, mode: _mode, wait: 0 };
console.log(this.gameQue);
}

Expand Down Expand Up @@ -58,8 +58,12 @@ export class GameQueueRepository {
return this.gameQue[idx].wait;
}

nextIdx(): number {
return this.idx++;
getIdx(): number {
return this.idx;
}

setIdx(i: number) {
this.idx = i;
}

async checkQue(myId: string, _wait: number): Promise<GameRoom> {
Expand Down
23 changes: 16 additions & 7 deletions backend/src/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
// if user == L ? R
if (room.gameRoomDto.leftUser.id == client.user.id) {
room.gameRoomDto.gameData.p1.in = false;
clearInterval(room.startTimer);
clearInterval(room.gameLoop);
let countDown = 60;
room.p1EndTimer = setInterval(() => {
Expand Down Expand Up @@ -230,31 +231,38 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@MessageBody() mode: GameMode,
) {
// console.log(client.user.id);
const is_join = await this.gameRoomRepository.findByUserId(client.user.id);
if (is_join) {
console.log(`already you game`);
return ;
}
const user = await this.userRepository.findOneBy({ id: client.user.id });
if (user) {
// await this.userRepository.update(client.user.id, {
// ladder: user.ladder,
// });
this.gameQueueRepository.addQue(client.user.id, user.ladder, mode);
let idx = this.gameQueueRepository.nextIdx();
let wait = 0;
await this.func(10000, client, idx, wait);
await this.func(10000, client, wait);
}
}
async func(time, client, idx, wait) {
if (await this.matching(client, idx, wait) == false) {
async func(time, client, wait) {
if (await this.matching(client, wait) == false) {
let idx = this.gameQueueRepository.getIdx();
clearTimeout(this.gameQueueRepository.getQueLoop(idx));
this.gameQueueRepository.setQueLoop(idx, setTimeout(() => {
this.func(time + 10000, client, idx, wait + 1)
this.func(time + 10000, client, wait + 1)
}, time));
}
}

async matching(client, idx, wait): Promise<boolean> {
async matching(client, wait): Promise<boolean> {
let room = await this.gameQueueRepository.checkQue(client.user.id, wait);
console.log('events find room');
console.log(room);
let idx = this.gameQueueRepository.getIdx();
if (room) {
// clearInterval(this.gameQueueRepository.getQueLoop(idx));
clearTimeout(this.gameQueueRepository.getQueLoop(idx));
console.log('really find Que!!');
console.log(client.user.id);
console.log(room);
Expand All @@ -273,6 +281,7 @@ export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
.emit('goToGameRoom', room.gameRoomDto.id);
return true;
} else {
console.log(`setWait idx: ${idx}`);
this.gameQueueRepository.setWait(idx, wait);
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/pages/clients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ function GoToGameRoom({ gameRoom }: any) {

function onClickGameRoom() {
console.log(`come in room~`);
console.log(gameRoom);
console.log(gameRoom.leftUser.id);
console.log(gameRoom.leftUser.id);
console.log(socket.id);
if (gameRoom.leftUser.id == socket.id ||
gameRoom.rightUser.id == socket.id) {
user_data.is_player = 1;
Expand Down
2 changes: 2 additions & 0 deletions frontend/pages/game/[room_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ export default function GameRoom() {
socket.on(`countDown1`, (count: number) => {
console.log(`countDown1: ${count}`);
data.p1.countDown = count;
data.countDown = -1;
});
socket.on(`countDown2`, (count: number) => {
console.log(`countDown2: ${count}`);
data.countDown = -1;
data.p2.countDown = count;
});
socket.on(`game[${roomId}]`, (_data) => {
Expand Down

0 comments on commit dadc39f

Please sign in to comment.