From 3f6f34503c48de79fef4de3febbaf91d7c37a707 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Sun, 12 Mar 2023 09:04:44 +0100 Subject: [PATCH] * Linting + database result entity fix --- back/volume/src/pong/entity/result.entity.ts | 2 +- back/volume/src/pong/game/Game.ts | 2 +- back/volume/src/pong/game/Player.ts | 1 - back/volume/src/pong/pong.service.ts | 15 +- back/volume/src/users/entity/user.entity.ts | 2 +- back/volume/src/users/users.controller.ts | 21 +- back/volume/src/users/users.service.ts | 211 ++++++++++--------- 7 files changed, 128 insertions(+), 126 deletions(-) diff --git a/back/volume/src/pong/entity/result.entity.ts b/back/volume/src/pong/entity/result.entity.ts index 863f2ff..037dc33 100644 --- a/back/volume/src/pong/entity/result.entity.ts +++ b/back/volume/src/pong/entity/result.entity.ts @@ -13,7 +13,7 @@ export default class Result { @PrimaryGeneratedColumn() id: number - @Column() + @Column({ default: false }) ranked: boolean @ManyToMany(() => User, (player: User) => player.results, { cascade: true }) diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 2a63ad8..3a43c65 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -100,7 +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) + void 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 5727d51..934838f 100644 --- a/back/volume/src/pong/game/Player.ts +++ b/back/volume/src/pong/game/Player.ts @@ -32,6 +32,5 @@ 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 989342e..74a55d2 100644 --- a/back/volume/src/pong/pong.service.ts +++ b/back/volume/src/pong/pong.service.ts @@ -21,18 +21,19 @@ export class PongService { player.winrate = (100 * player.wins) / player.matchs player.rank = (await this.usersService.getRank(player.ftId)) + 1 } - async updatePlayer (i: number, result: Result) { + + async updatePlayer (i: number, result: Result): Promise { const player: User | null = result.players[i] if (player == null) return - if (result.ranked) this.updateStats(player, i, result) + if (result.ranked) await this.updateStats(player, i, result) player.results.push(result) - player.status = "online" + player.status = 'online' await this.usersService.save(player) } async setInGame (playerName: string): Promise { const player = await this.usersService.findUserByName(playerName) - player.status = "in-game" + player.status = 'in-game' await this.usersService.save(player) } @@ -41,7 +42,7 @@ export class PongService { const ply = new Array() ply.push(await this.usersService.findUserByName(players[0].name)) ply.push(await this.usersService.findUserByName(players[1].name)) - result.ranked = ranked; + result.ranked = ranked result.players = ply result.score = [players[0].score, players[1].score] await this.resultsRepository.save(result) @@ -51,9 +52,9 @@ export class PongService { async getRankedHistory (): Promise { return await this.resultsRepository.find({ - where: { ranked: true}, + where: { ranked: true }, relations: { - players: true, + players: true }, order: { date: 'DESC' } }) diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index 5b853ea..5464921 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -22,7 +22,7 @@ export class User { @Column({ unique: true }) ftId: number - @Column({ unique: true}) + @Column({ unique: true }) socketKey: string @Column({ unique: true }) diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 874f9aa..c7da6de 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -138,12 +138,13 @@ export class UsersController { @FtUser() profile: Profile, @Param('username') username: string ): Promise { - const target: User | null = await this.usersService.findUserByName( username ) - if (!target) throw new BadRequestException(`User ${username} not found.`) - if (+profile.id === target.ftId) - throw new BadRequestException("You can't invit yourself.") - const ret: string = await this.usersService.invit(profile.id, target.ftId); - if (ret !== "OK") throw new BadRequestException(ret) + const target: User | null = await this.usersService.findUserByName(username) + if (target === null) throw new BadRequestException(`User ${username} not found.`) + if (+profile.id === target.ftId) { + throw new BadRequestException('You can\'t invite yourself.') + } + const ret: string = await this.usersService.invit(profile.id, target.ftId) + if (ret !== 'OK') throw new BadRequestException(ret) } @Get('avatar/:id') @@ -151,12 +152,12 @@ export class UsersController { @Param('id', ParseIntPipe) ftId: number, @Res({ passthrough: true }) response: Response ): Promise { - const user = await this.usersService.findUser(ftId) - if (!user) throw new BadRequestException('User unknown.') + const user: User | null = await this.usersService.findUser(ftId) + if (user === null) throw new BadRequestException('User unknown.') const filename = user.avatar const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) response.set({ - 'Content-Diposition': `inline; filename="${filename}"`, + 'Content-Diposition': `inline; filename='${filename}'`, 'Content-Type': 'image/jpg' }) return new StreamableFile(stream) @@ -167,7 +168,7 @@ export class UsersController { @Param('id', ParseIntPipe) ftId: number ): Promise { const user = await this.usersService.findUser(ftId) - if (!user) throw new BadRequestException('User unknown.') + if (user == null) throw new BadRequestException('User unknown.') user.socketKey = '' return user } diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index a38ea6a..b887671 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -1,180 +1,181 @@ -import { BadRequestException, Catch, Injectable } from "@nestjs/common"; -import { InjectRepository } from "@nestjs/typeorm"; -import { EntityNotFoundError, QueryFailedError, Repository } from "typeorm"; -import { User } from "./entity/user.entity"; -import { type UserDto } from "./dto/user.dto"; -import { type Channel } from "src/chat/entity/channel.entity"; -import type Result from "src/pong/entity/result.entity"; -import { Cron } from "@nestjs/schedule"; -import { randomUUID } from "crypto"; +import { BadRequestException, Catch, Injectable } from '@nestjs/common' +import { InjectRepository } from '@nestjs/typeorm' +import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm' +import { User } from './entity/user.entity' +import { type UserDto } from './dto/user.dto' +import { type Channel } from 'src/chat/entity/channel.entity' +import type Result from 'src/pong/entity/result.entity' +import { Cron } from '@nestjs/schedule' +import { randomUUID } from 'crypto' @Injectable() @Catch(QueryFailedError, EntityNotFoundError) export class UsersService { - constructor( + constructor ( @InjectRepository(User) private readonly usersRepository: Repository ) {} - async save(user: User): Promise { - await this.usersRepository.save(user); + async save (user: User): Promise { + await this.usersRepository.save(user) } - async findUsers(): Promise { - const users = await this.usersRepository.find({}); + async findUsers (): Promise { + const users = await this.usersRepository.find({}) users.forEach((usr) => { - usr.socketKey = ""; - }); - return users; + 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; + relations: { results: true } + }) + if (user == null) throw new BadRequestException('User not found.') + return user } - @Cron("0 * * * * *") - async updateStatus(): Promise { - const users = await this.usersRepository.find({}); + @Cron('0 * * * * *') + async updateStatus (): Promise { + const users = await this.usersRepository.find({}) users.forEach((usr) => { if (Date.now() - usr.lastAccess > 60000) { - usr.status = "offline"; + usr.status = 'offline' this.usersRepository.save(usr).catch((err) => { - console.log(err); - }); + console.log(err) + }) } - }); + }) } - async findUser(ftId: number): Promise { - const user = await this.usersRepository.findOneBy({ ftId }); - if (user == null) return null; - user.lastAccess = Date.now(); - if (user.status === "offline") user.status = "online"; - await this.usersRepository.save(user); - return user; + async findUser (ftId: number): Promise { + const user = await this.usersRepository.findOneBy({ ftId }) + if (user == null) return null + user.lastAccess = Date.now() + if (user.status === 'offline') user.status = 'online' + await this.usersRepository.save(user) + return user } - async findOnlineUsers(): Promise { + async findOnlineUsers (): Promise { const users = await this.usersRepository.find({ - where: { status: "online" }, - }); + where: { status: 'online' } + }) users.forEach((usr) => { - usr.socketKey = ""; - }); - return users; + usr.socketKey = '' + }) + return users } - async create(userData: UserDto): Promise { + async create (userData: UserDto): Promise { try { - const newUser = this.usersRepository.create(userData); + const newUser = this.usersRepository.create(userData) newUser.socketKey = randomUUID() - return await this.usersRepository.save(newUser); + return await this.usersRepository.save(newUser) } catch (err) { - throw new BadRequestException("User already exists."); + throw new BadRequestException('User already exists.') } } - async findOnlineInChannel(channel: Channel): Promise { + async findOnlineInChannel (channel: Channel): Promise { return await this.usersRepository - .createQueryBuilder("user") - .where("user.channel = :chan", { chan: channel }) - .andWhere("user.status := status)", { status: "online" }) - .getMany(); + .createQueryBuilder('user') + .where('user.channel = :chan', { chan: channel }) + .andWhere('user.status := status)', { status: 'online' }) + .getMany() } - async update(user: User, changes: UserDto): Promise { - this.usersRepository.merge(user, changes); - return await this.usersRepository.save(user); + async update (user: User, changes: UserDto): Promise { + this.usersRepository.merge(user, changes) + return await this.usersRepository.save(user) } - async addAvatar(ftId: number, filename: string): Promise { - await this.usersRepository.update({ ftId }, { avatar: filename }); + async addAvatar (ftId: number, filename: string): Promise { + await this.usersRepository.update({ ftId }, { avatar: filename }) } - async getFriends(ftId: number): Promise { + async getFriends (ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, - relations: { friends: true }, - }); - if (user == null) throw new BadRequestException("User not found."); - return user.friends; + relations: { friends: true } + }) + if (user == null) throw new BadRequestException('User not found.') + return user.friends } - async getInvits(ftId: number): Promise { + async getInvits (ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, relations: { - followers: true, - }, - }); - if (user == null) throw new BadRequestException("User not found."); - return user.followers; + followers: true + } + }) + if (user == null) throw new BadRequestException('User not found.') + return user.followers } - async getResultsById(ftId: number): Promise { + async getResultsById (ftId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, - relations: { results: { players: true } }, - }); - if (user == null) throw new BadRequestException("User not found."); - return user.results; + relations: { results: { players: true } } + }) + if (user == null) throw new BadRequestException('User not found.') + return user.results } - async getLeaderboard(): Promise { - let leaderboard = await this.usersRepository.find({ + async getLeaderboard (): Promise { + const leaderboard = await this.usersRepository.find({ order: { - winrate: "DESC", - }, - }); - return leaderboard.filter((user) => user.rank !== 0); + winrate: 'DESC' + } + }) + return leaderboard.filter((user) => user.rank !== 0) } - async getRank(ftId: number): Promise { - let leaderboard = await this.usersRepository.find({ + async getRank (ftId: number): Promise { + const leaderboard = await this.usersRepository.find({ order: { - winrate: "DESC", - }, - }); - return leaderboard.findIndex((user) => user.ftId === ftId); + winrate: 'DESC' + } + }) + return leaderboard.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: { followers: true, - friends: true, - }, - }); - if (!user) throw new BadRequestException("User not found."); + friends: true + } + }) + if (user === null) throw new BadRequestException('User not found.') if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { - return "You are already friends."; + return 'You are already friends.' } const target: User | null = await this.usersRepository.findOne({ where: { ftId: targetFtId }, relations: { followers: true, - friends: true, - }, - }); - if (!target) return "Target not found."; + friends: true + } + }) + if (target === null) return 'Target not found.' const id = user.followers.findIndex( (follower) => follower.ftId === targetFtId - ); + ) if (target.followers.findIndex((follower) => follower.ftId === user.ftId) !== -1) { - return "Invitation already sent."; - }else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) { - user.friends.push(target); - target.friends.push(user); - user.followers.slice(id, 1); - await this.usersRepository.save(user); - } else - target.followers.push(user); - await this.usersRepository.save(target); - return "OK" + return 'Invitation already sent.' + } else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) { + user.friends.push(target) + target.friends.push(user) + user.followers.slice(id, 1) + await this.usersRepository.save(user) + } else { + target.followers.push(user) + } + await this.usersRepository.save(target) + return 'OK' } }