Browse Source

* Pong backend: Fixed matchmaking bugs

* Auth backend: reduced logs size on login
master
vvandenb 2 years ago
parent
commit
cfdc1def48
  1. 3
      back/volume/src/auth/42.strategy.ts
  2. 13
      back/volume/src/pong/game/MatchmakingQueue.ts
  3. 12
      back/volume/src/pong/pong.gateway.ts

3
back/volume/src/auth/42.strategy.ts

@ -30,9 +30,8 @@ export class FtStrategy extends PassportStrategy(Strategy, '42') {
cb: VerifyCallback cb: VerifyCallback
): Promise<VerifyCallback> { ): Promise<VerifyCallback> {
request.session.accessToken = accessToken request.session.accessToken = accessToken
console.log('accessToken', accessToken, 'refreshToken', refreshToken)
const ftId = profile.id as number const ftId = profile.id as number
console.log(profile) console.log('Validated ', profile.username)
if ((await this.usersService.findUser(ftId)) === null) { if ((await this.usersService.findUser(ftId)) === null) {
const newUser = new User() const newUser = new User()
newUser.ftId = profile.id as number newUser.ftId = profile.id as number

13
back/volume/src/pong/game/MatchmakingQueue.ts

@ -12,23 +12,24 @@ export class MatchmakingQueue {
this.queue = [] this.queue = []
} }
addPlayer (name: string, socket: WebSocket, uuid: string): boolean { addPlayer (name: string, socket: WebSocket, uuid: string): void {
let succeeded: boolean = false if (!this.isInQueue(name)) {
if (!this.alreadyInQueue(name)) { console.log('Adding player to queue: ', name)
this.queue.push({ name, socket, uuid }) this.queue.push({ name, socket, uuid })
if (this.canCreateGame()) { if (this.canCreateGame()) {
this.createGame() this.createGame()
} }
succeeded = true
} }
return succeeded
} }
removePlayer (name: string): void { removePlayer (name: string): void {
if (this.isInQueue(name)) {
console.log('Removing player from queue: ', name)
this.queue = this.queue.filter((player) => player.name !== name) this.queue = this.queue.filter((player) => player.name !== name)
} }
}
alreadyInQueue (name: string): boolean { isInQueue (name: string): boolean {
return this.queue.some((player) => player.name === name) return this.queue.some((player) => player.name === name)
} }

12
back/volume/src/pong/pong.gateway.ts

@ -62,6 +62,7 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
} }
if (name !== undefined) { if (name !== undefined) {
console.log('Disconnected ', this.socketToPlayerName.get(client)) console.log('Disconnected ', this.socketToPlayerName.get(client))
this.matchmakingQueue.removePlayer(name)
this.socketToPlayerName.delete(client) this.socketToPlayerName.delete(client)
} }
} }
@ -202,20 +203,19 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
client: WebSocketWithId, client: WebSocketWithId,
@MessageBody() matchmakingUpdateData: MatchmakingDtoValidated @MessageBody() matchmakingUpdateData: MatchmakingDtoValidated
): { event: string, data: MatchmakingDtoValidated } { ): { event: string, data: MatchmakingDtoValidated } {
let isMatchmaking: boolean = false let matchmaking: boolean = false
const name: string | undefined = this.socketToPlayerName.get(client) const name: string | undefined = this.socketToPlayerName.get(client)
if (name !== undefined) { if (name !== undefined) {
if (matchmakingUpdateData.matchmaking) { if (matchmakingUpdateData.matchmaking && !this.games.isInAGame(name)) {
if (this.matchmakingQueue.addPlayer(name, client, client.id)) { this.matchmakingQueue.addPlayer(name, client, client.id)
isMatchmaking = true
}
} else { } else {
this.matchmakingQueue.removePlayer(name) this.matchmakingQueue.removePlayer(name)
} }
matchmaking = this.matchmakingQueue.isInQueue(name)
} }
return { return {
event: GAME_EVENTS.MATCHMAKING, event: GAME_EVENTS.MATCHMAKING,
data: { matchmaking: isMatchmaking } data: { matchmaking }
} }
} }

Loading…
Cancel
Save