Browse Source

* Linting + database result entity fix

master
vvandenb 2 years ago
parent
commit
3f6f34503c
  1. 2
      back/volume/src/pong/entity/result.entity.ts
  2. 2
      back/volume/src/pong/game/Game.ts
  3. 1
      back/volume/src/pong/game/Player.ts
  4. 13
      back/volume/src/pong/pong.service.ts
  5. 19
      back/volume/src/users/users.controller.ts
  6. 175
      back/volume/src/users/users.service.ts

2
back/volume/src/pong/entity/result.entity.ts

@ -13,7 +13,7 @@ export default class Result {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number id: number
@Column() @Column({ default: false })
ranked: boolean ranked: boolean
@ManyToMany(() => User, (player: User) => player.results, { cascade: true }) @ManyToMany(() => User, (player: User) => player.results, { cascade: true })

2
back/volume/src/pong/game/Game.ts

@ -100,7 +100,7 @@ export class Game {
if (this.timer === null && this.players.length === 2) { 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.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2))
this.players.forEach((p) => { this.players.forEach((p) => {
this.pongService.setInGame(p.name) void this.pongService.setInGame(p.name)
p.newGame() p.newGame()
}) })
this.playing = true this.playing = true

1
back/volume/src/pong/game/Player.ts

@ -32,6 +32,5 @@ export class Player {
newGame (): void { newGame (): void {
this.score = 0 this.score = 0
this.paddle = new Paddle(this.paddleCoords, this.mapSize) this.paddle = new Paddle(this.paddleCoords, this.mapSize)
} }
} }

13
back/volume/src/pong/pong.service.ts

@ -21,18 +21,19 @@ export class PongService {
player.winrate = (100 * player.wins) / player.matchs player.winrate = (100 * player.wins) / player.matchs
player.rank = (await this.usersService.getRank(player.ftId)) + 1 player.rank = (await this.usersService.getRank(player.ftId)) + 1
} }
async updatePlayer (i: number, result: Result) {
async updatePlayer (i: number, result: Result): Promise<void> {
const player: User | null = result.players[i] const player: User | null = result.players[i]
if (player == null) return 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.results.push(result)
player.status = "online" player.status = 'online'
await this.usersService.save(player) await this.usersService.save(player)
} }
async setInGame (playerName: string): Promise<void> { async setInGame (playerName: string): Promise<void> {
const player = await this.usersService.findUserByName(playerName) const player = await this.usersService.findUserByName(playerName)
player.status = "in-game" player.status = 'in-game'
await this.usersService.save(player) await this.usersService.save(player)
} }
@ -41,7 +42,7 @@ export class PongService {
const ply = new Array<User | null>() const ply = new Array<User | null>()
ply.push(await this.usersService.findUserByName(players[0].name)) ply.push(await this.usersService.findUserByName(players[0].name))
ply.push(await this.usersService.findUserByName(players[1].name)) ply.push(await this.usersService.findUserByName(players[1].name))
result.ranked = ranked; result.ranked = ranked
result.players = ply result.players = ply
result.score = [players[0].score, players[1].score] result.score = [players[0].score, players[1].score]
await this.resultsRepository.save(result) await this.resultsRepository.save(result)
@ -53,7 +54,7 @@ export class PongService {
return await this.resultsRepository.find({ return await this.resultsRepository.find({
where: { ranked: true }, where: { ranked: true },
relations: { relations: {
players: true, players: true
}, },
order: { date: 'DESC' } order: { date: 'DESC' }
}) })

19
back/volume/src/users/users.controller.ts

@ -139,11 +139,12 @@ export class UsersController {
@Param('username') username: string @Param('username') username: string
): Promise<void> { ): Promise<void> {
const target: User | null = await this.usersService.findUserByName(username) const target: User | null = await this.usersService.findUserByName(username)
if (!target) throw new BadRequestException(`User ${username} not found.`) if (target === null) throw new BadRequestException(`User ${username} not found.`)
if (+profile.id === target.ftId) if (+profile.id === target.ftId) {
throw new BadRequestException("You can't invit yourself.") 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) const ret: string = await this.usersService.invit(profile.id, target.ftId)
if (ret !== 'OK') throw new BadRequestException(ret)
} }
@Get('avatar/:id') @Get('avatar/:id')
@ -151,12 +152,12 @@ export class UsersController {
@Param('id', ParseIntPipe) ftId: number, @Param('id', ParseIntPipe) ftId: number,
@Res({ passthrough: true }) response: Response @Res({ passthrough: true }) response: Response
): Promise<StreamableFile> { ): Promise<StreamableFile> {
const user = await this.usersService.findUser(ftId) const user: User | null = await this.usersService.findUser(ftId)
if (!user) throw new BadRequestException('User unknown.') if (user === null) throw new BadRequestException('User unknown.')
const filename = user.avatar const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename)) const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({ response.set({
'Content-Diposition': `inline; filename="${filename}"`, 'Content-Diposition': `inline; filename='${filename}'`,
'Content-Type': 'image/jpg' 'Content-Type': 'image/jpg'
}) })
return new StreamableFile(stream) return new StreamableFile(stream)
@ -167,7 +168,7 @@ export class UsersController {
@Param('id', ParseIntPipe) ftId: number @Param('id', ParseIntPipe) ftId: number
): Promise<User | null> { ): Promise<User | null> {
const user = await this.usersService.findUser(ftId) const user = await this.usersService.findUser(ftId)
if (!user) throw new BadRequestException('User unknown.') if (user == null) throw new BadRequestException('User unknown.')
user.socketKey = '' user.socketKey = ''
return user return user
} }

175
back/volume/src/users/users.service.ts

@ -1,12 +1,12 @@
import { BadRequestException, Catch, Injectable } from "@nestjs/common"; import { BadRequestException, Catch, Injectable } from '@nestjs/common'
import { InjectRepository } from "@nestjs/typeorm"; import { InjectRepository } from '@nestjs/typeorm'
import { EntityNotFoundError, QueryFailedError, Repository } from "typeorm"; import { EntityNotFoundError, QueryFailedError, Repository } from 'typeorm'
import { User } from "./entity/user.entity"; import { User } from './entity/user.entity'
import { type UserDto } from "./dto/user.dto"; import { type UserDto } from './dto/user.dto'
import { type Channel } from "src/chat/entity/channel.entity"; import { type Channel } from 'src/chat/entity/channel.entity'
import type Result from "src/pong/entity/result.entity"; import type Result from 'src/pong/entity/result.entity'
import { Cron } from "@nestjs/schedule"; import { Cron } from '@nestjs/schedule'
import { randomUUID } from "crypto"; import { randomUUID } from 'crypto'
@Injectable() @Injectable()
@Catch(QueryFailedError, EntityNotFoundError) @Catch(QueryFailedError, EntityNotFoundError)
@ -16,130 +16,130 @@ export class UsersService {
) {} ) {}
async save (user: User): Promise<void> { async save (user: User): Promise<void> {
await this.usersRepository.save(user); await this.usersRepository.save(user)
} }
async findUsers (): Promise<User[]> { async findUsers (): Promise<User[]> {
const users = await this.usersRepository.find({}); const users = await this.usersRepository.find({})
users.forEach((usr) => { users.forEach((usr) => {
usr.socketKey = ""; usr.socketKey = ''
}); })
return users; return users
} }
async findUserByName (username: string): Promise<User> { async findUserByName (username: string): Promise<User> {
const user = await this.usersRepository.findOne({ const user = await this.usersRepository.findOne({
where: { username }, where: { username },
relations: { results: true }, relations: { results: true }
}); })
if (user == null) throw new BadRequestException("User not found."); if (user == null) throw new BadRequestException('User not found.')
return user; return user
} }
@Cron("0 * * * * *") @Cron('0 * * * * *')
async updateStatus (): Promise<void> { async updateStatus (): Promise<void> {
const users = await this.usersRepository.find({}); const users = await this.usersRepository.find({})
users.forEach((usr) => { users.forEach((usr) => {
if (Date.now() - usr.lastAccess > 60000) { if (Date.now() - usr.lastAccess > 60000) {
usr.status = "offline"; usr.status = 'offline'
this.usersRepository.save(usr).catch((err) => { this.usersRepository.save(usr).catch((err) => {
console.log(err); console.log(err)
}); })
} }
}); })
} }
async findUser (ftId: number): Promise<User | null> { async findUser (ftId: number): Promise<User | null> {
const user = await this.usersRepository.findOneBy({ ftId }); const user = await this.usersRepository.findOneBy({ ftId })
if (user == null) return null; if (user == null) return null
user.lastAccess = Date.now(); user.lastAccess = Date.now()
if (user.status === "offline") user.status = "online"; if (user.status === 'offline') user.status = 'online'
await this.usersRepository.save(user); await this.usersRepository.save(user)
return user; return user
} }
async findOnlineUsers (): Promise<User[]> { async findOnlineUsers (): Promise<User[]> {
const users = await this.usersRepository.find({ const users = await this.usersRepository.find({
where: { status: "online" }, where: { status: 'online' }
}); })
users.forEach((usr) => { users.forEach((usr) => {
usr.socketKey = ""; usr.socketKey = ''
}); })
return users; return users
} }
async create (userData: UserDto): Promise<User | null> { async create (userData: UserDto): Promise<User | null> {
try { try {
const newUser = this.usersRepository.create(userData); const newUser = this.usersRepository.create(userData)
newUser.socketKey = randomUUID() newUser.socketKey = randomUUID()
return await this.usersRepository.save(newUser); return await this.usersRepository.save(newUser)
} catch (err) { } catch (err) {
throw new BadRequestException("User already exists."); throw new BadRequestException('User already exists.')
} }
} }
async findOnlineInChannel (channel: Channel): Promise<User[]> { async findOnlineInChannel (channel: Channel): Promise<User[]> {
return await this.usersRepository return await this.usersRepository
.createQueryBuilder("user") .createQueryBuilder('user')
.where("user.channel = :chan", { chan: channel }) .where('user.channel = :chan', { chan: channel })
.andWhere("user.status := status)", { status: "online" }) .andWhere('user.status := status)', { status: 'online' })
.getMany(); .getMany()
} }
async update (user: User, changes: UserDto): Promise<User | null> { async update (user: User, changes: UserDto): Promise<User | null> {
this.usersRepository.merge(user, changes); this.usersRepository.merge(user, changes)
return await this.usersRepository.save(user); return await this.usersRepository.save(user)
} }
async addAvatar (ftId: number, filename: string): Promise<void> { async addAvatar (ftId: number, filename: string): Promise<void> {
await this.usersRepository.update({ ftId }, { avatar: filename }); await this.usersRepository.update({ ftId }, { avatar: filename })
} }
async getFriends (ftId: number): Promise<User[]> { async getFriends (ftId: number): Promise<User[]> {
const user = await this.usersRepository.findOne({ const user = await this.usersRepository.findOne({
where: { ftId }, where: { ftId },
relations: { friends: true }, relations: { friends: true }
}); })
if (user == null) throw new BadRequestException("User not found."); if (user == null) throw new BadRequestException('User not found.')
return user.friends; return user.friends
} }
async getInvits (ftId: number): Promise<User[]> { async getInvits (ftId: number): Promise<User[]> {
const user = await this.usersRepository.findOne({ const user = await this.usersRepository.findOne({
where: { ftId }, where: { ftId },
relations: { relations: {
followers: true, followers: true
}, }
}); })
if (user == null) throw new BadRequestException("User not found."); if (user == null) throw new BadRequestException('User not found.')
return user.followers; return user.followers
} }
async getResultsById (ftId: number): Promise<Result[]> { async getResultsById (ftId: number): Promise<Result[]> {
const user = await this.usersRepository.findOne({ const user = await this.usersRepository.findOne({
where: { ftId }, where: { ftId },
relations: { results: { players: true } }, relations: { results: { players: true } }
}); })
if (user == null) throw new BadRequestException("User not found."); if (user == null) throw new BadRequestException('User not found.')
return user.results; return user.results
} }
async getLeaderboard (): Promise<User[]> { async getLeaderboard (): Promise<User[]> {
let leaderboard = await this.usersRepository.find({ const leaderboard = await this.usersRepository.find({
order: { order: {
winrate: "DESC", winrate: 'DESC'
}, }
}); })
return leaderboard.filter((user) => user.rank !== 0); return leaderboard.filter((user) => user.rank !== 0)
} }
async getRank (ftId: number): Promise<number> { async getRank (ftId: number): Promise<number> {
let leaderboard = await this.usersRepository.find({ const leaderboard = await this.usersRepository.find({
order: { order: {
winrate: "DESC", winrate: 'DESC'
}, }
}); })
return leaderboard.findIndex((user) => user.ftId === ftId); return leaderboard.findIndex((user) => user.ftId === ftId)
} }
async invit (ftId: number, targetFtId: number): Promise<string> { async invit (ftId: number, targetFtId: number): Promise<string> {
@ -147,34 +147,35 @@ export class UsersService {
where: { ftId }, where: { ftId },
relations: { relations: {
followers: true, followers: true,
friends: true, friends: true
}, }
}); })
if (!user) throw new BadRequestException("User not found."); if (user === null) throw new BadRequestException('User not found.')
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { 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({ const target: User | null = await this.usersRepository.findOne({
where: { ftId: targetFtId }, where: { ftId: targetFtId },
relations: { relations: {
followers: true, followers: true,
friends: true, friends: true
}, }
}); })
if (!target) return "Target not found."; if (target === null) return 'Target not found.'
const id = user.followers.findIndex( const id = user.followers.findIndex(
(follower) => follower.ftId === targetFtId (follower) => follower.ftId === targetFtId
); )
if (target.followers.findIndex((follower) => follower.ftId === user.ftId) !== -1) { if (target.followers.findIndex((follower) => follower.ftId === user.ftId) !== -1) {
return "Invitation already sent."; return 'Invitation already sent.'
} else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) { } else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) {
user.friends.push(target); user.friends.push(target)
target.friends.push(user); target.friends.push(user)
user.followers.slice(id, 1); user.followers.slice(id, 1)
await this.usersRepository.save(user); await this.usersRepository.save(user)
} else } else {
target.followers.push(user); target.followers.push(user)
await this.usersRepository.save(target); }
return "OK" await this.usersRepository.save(target)
return 'OK'
} }
} }

Loading…
Cancel
Save