From 68994e551eba4301b0dcd02708f8af3d3a5fae66 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sat, 11 Mar 2023 14:35:21 +0100 Subject: [PATCH 1/2] fixed avatar possible 404 and get others users socketKey --- back/volume/src/users/users.controller.ts | 20 ++++++----- back/volume/src/users/users.service.ts | 41 ++++++++++++++-------- front/volume/src/components/Profile.svelte | 3 +- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 876174e..897d37e 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -121,13 +121,15 @@ export class UsersController { async getAvatar ( @FtUser() profile: Profile, @Res({ passthrough: true }) response: Response - ): Promise { + ): Promise { return await this.getAvatarById(profile.id, response) } @Get('user/:name') - async getUserByName (@Param('name') username: string): Promise { - return await this.usersService.findUserByName(username) + async getUserByName (@Param('name') username: string): Promise { + const user = await this.usersService.findUserByName(username) + user.socketKey = '' + return user } @Get('invit/:username') @@ -135,7 +137,7 @@ export class UsersController { async invitUser ( @FtUser() profile: Profile, @Param('username') username: string - ): Promise { + ): Promise { const target: User | null = await this.usersService.findUserByName( username ) @@ -143,16 +145,16 @@ export class UsersController { if (profile.id === target.ftId) { throw new BadRequestException("You can't invit yourself.") } - return await this.usersService.invit(profile.id, target.id) + await this.usersService.invit(profile.id, target.id) } @Get('avatar/:id') async getAvatarById ( @Param('id', ParseIntPipe) ftId: number, @Res({ passthrough: true }) response: Response - ): Promise { + ): Promise { const user = await this.usersService.findUser(ftId) - if (user == null) return null + if (!user) throw new BadRequestException('User unknown.') const filename = user.avatar const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) response.set({ @@ -166,7 +168,9 @@ export class UsersController { async getUserById ( @Param('id', ParseIntPipe) ftId: number ): Promise { - return await this.usersService.findUser(ftId) + const user = await this.usersService.findUser(ftId) + user.socketKey = '' + return user } @Post(':id') diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index 24ad901..bdd69e7 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -19,14 +19,19 @@ export class UsersService { } async findUsers (): Promise { - return await this.usersRepository.find({}) + const users = await this.usersRepository.find({}) + users.forEach((usr) => { + usr.socketKey = '' + }) + return users } - async findUserByName (username: string): Promise { + async findUserByName (username: string): Promise { const user = await this.usersRepository.findOne({ where: { username }, relations: { results: true } }) + if (user == null) throw new BadRequestException('User not found.') return user } @@ -43,20 +48,26 @@ export class UsersService { }) } - async findUser (ftId: number): Promise { + async findUser (ftId: number): Promise { const user = await this.usersRepository.findOneBy({ ftId }) - if (user == null) return null + if (user == null) throw new BadRequestException('User not found.') user.lastAccess = Date.now() - user.status = 'online' + if (user.status === 'offline') user.status = 'online' await this.usersRepository.save(user) return user } async findOnlineUsers (): Promise { - return await this.usersRepository.find({ where: { status: 'online' } }) + const users = await this.usersRepository.find({ + where: { status: 'online' } + }) + users.forEach((usr) => { + usr.socketKey = '' + }) + return users } - async create (userData: UserDto): Promise { + async create (userData: UserDto): Promise { try { const newUser = this.usersRepository.create(userData) return await this.usersRepository.save(newUser) @@ -87,8 +98,8 @@ export class UsersService { where: { ftId }, relations: { friends: true } }) - if (user != null) return user.friends - return [] + if (user == null) throw new BadRequestException('User not found.') + return user.friends } async getInvits (ftId: number): Promise { @@ -98,8 +109,8 @@ export class UsersService { followers: true } }) - if (user != null) return user.followers - return [] + if (user == null) throw new BadRequestException('User not found.') + return user.followers } async getResults (ftId: number): Promise { @@ -111,8 +122,8 @@ export class UsersService { } } }) - if (user != null) return user.results - return [] + if (user == null) throw new BadRequestException('User not found.') + return user.results } async getLeaderboard (): Promise { @@ -128,7 +139,7 @@ export class UsersService { return leader.findIndex((user) => user.ftId === ftId) } - async invit (ftId: number, targetFtId: number): Promise { + async invit (ftId: number, targetFtId: number): Promise { const user: User | null = await this.usersRepository.findOne({ where: { ftId }, relations: { @@ -138,7 +149,7 @@ export class UsersService { }) if (user == null) throw new BadRequestException('User not found.') if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { - return new BadRequestException('You are already friends.') + throw new BadRequestException('You are already friends.') } const target: User | null = await this.usersRepository.findOne({ where: { ftId: targetFtId }, diff --git a/front/volume/src/components/Profile.svelte b/front/volume/src/components/Profile.svelte index 68aea1e..fdc83f3 100644 --- a/front/volume/src/components/Profile.svelte +++ b/front/volume/src/components/Profile.svelte @@ -50,7 +50,7 @@ avatar {:else}
From a72da047ca7fa69a0be991a922738a08d090f1c4 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sat, 11 Mar 2023 15:00:38 +0100 Subject: [PATCH 2/2] add in-game status --- back/volume/src/pong/game/Game.ts | 1 + back/volume/src/pong/game/Player.ts | 1 + back/volume/src/pong/pong.service.ts | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 890d08c..b3c3b99 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -100,6 +100,7 @@ export class Game { if (this.timer === null && this.players.length === 2) { this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) this.players.forEach((p) => { + this.pongService.setInGame(p.name) p.newGame() }) this.playing = true diff --git a/back/volume/src/pong/game/Player.ts b/back/volume/src/pong/game/Player.ts index 934838f..5727d51 100644 --- a/back/volume/src/pong/game/Player.ts +++ b/back/volume/src/pong/game/Player.ts @@ -32,5 +32,6 @@ export class Player { newGame (): void { this.score = 0 this.paddle = new Paddle(this.paddleCoords, this.mapSize) + } } diff --git a/back/volume/src/pong/pong.service.ts b/back/volume/src/pong/pong.service.ts index df1e42c..8845150 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -23,6 +23,13 @@ export class PongService { player.winrate = (100 * player.wins) / player.matchs player.rank = (await this.usersService.getRank(player.ftId)) + 1 player.results.push(result) + player.status = "online" + await this.usersService.save(player) + } + + async setInGame (playerName: string): Promise { + const player = await this.usersService.findUserByName(playerName) + player.status = "in-game" await this.usersService.save(player) }