From 447a88704c48523e1a7e196811228bffeb1d9214 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 7 Mar 2023 18:47:24 +0100 Subject: [PATCH] *some linting and redisigned history and leaderboard with tables --- back/volume/src/auth/auth.controller.ts | 16 +++--- back/volume/src/auth/session.serializer.ts | 3 +- back/volume/src/main.ts | 19 ++++--- back/volume/src/pong/entity/result.entity.ts | 3 +- back/volume/src/users/entity/user.entity.ts | 3 +- back/volume/src/users/users.controller.ts | 2 +- back/volume/src/users/users.service.ts | 16 +++--- .../volume/src/components/Leaderboard.svelte | 53 ++++++++++++++----- .../volume/src/components/MatchHistory.svelte | 50 ++++++++++------- 9 files changed, 105 insertions(+), 60 deletions(-) diff --git a/back/volume/src/auth/auth.controller.ts b/back/volume/src/auth/auth.controller.ts index 9c5c14c..72c7c75 100644 --- a/back/volume/src/auth/auth.controller.ts +++ b/back/volume/src/auth/auth.controller.ts @@ -13,26 +13,26 @@ export class AuthController { @Get('inReturn') @UseGuards(FtOauthGuard) - @Redirect('http://' + process.env.HOST + ':' + process.env.FRONT_PORT + '/') + @Redirect(`http://${process.env.HOST}:${process.env.FRONT_PORT}`) ftAuthCallback ( - @Res({ passthrough: true }) response: Response, - @Req() request: Request - ) { + @Res({ passthrough: true }) response: Response, + @Req() request: Request + ): any { console.log('cookie:', request.cookies['connect.sid']) response.cookie('connect.sid', request.cookies['connect.sid']) } @Get('profile') @UseGuards(AuthenticatedGuard) - profile (@FtUser() user: Profile) { + profile (@FtUser() user: Profile): any { return { user } } @Get('out') - @Redirect('http://' + process.env.HOST + ':' + process.env.FRONT_PORT + '/') - logOut (@Req() req: Request) { + @Redirect(`http://${process.env.HOST}:${process.env.FRONT_PORT}`) + logOut (@Req() req: Request): any { req.logOut(function (err) { - if (err) return err + if (err != null) return err }) } } diff --git a/back/volume/src/auth/session.serializer.ts b/back/volume/src/auth/session.serializer.ts index 0cb569e..05510eb 100644 --- a/back/volume/src/auth/session.serializer.ts +++ b/back/volume/src/auth/session.serializer.ts @@ -4,6 +4,7 @@ import { type Profile } from 'passport-42' @Injectable() export class SessionSerializer extends PassportSerializer { + // useless constructor? constructor () { super() } @@ -18,7 +19,7 @@ export class SessionSerializer extends PassportSerializer { deserializeUser ( payload: Profile, done: (err: Error | null, user: Profile) => void - ) { + ): any { done(null, payload) } } diff --git a/back/volume/src/main.ts b/back/volume/src/main.ts index 379c854..03c0cf0 100644 --- a/back/volume/src/main.ts +++ b/back/volume/src/main.ts @@ -1,18 +1,20 @@ import { WsAdapter } from '@nestjs/platform-ws' -import { Logger } from '@nestjs/common' +import { InternalServerErrorException, Logger } from '@nestjs/common' import { NestFactory } from '@nestjs/core' import { AppModule } from './app.module' import * as session from 'express-session' import * as passport from 'passport' import { type NestExpressApplication } from '@nestjs/platform-express' import * as cookieParser from 'cookie-parser' -import { Response } from 'express' -async function bootstrap () { +async function bootstrap (): Promise { const logger = new Logger() const app = await NestFactory.create(AppModule) - const port = process.env.BACK_PORT! + const port = + process.env.BACK_PORT && process.env.BACK_PORT !== '' + ? +process.env.BACK_PORT + : 3001 const cors = { origin: ['http://localhost:80', 'http://localhost', '*'], methods: 'GET, HEAD, PUT, PATCH, POST, DELETE, OPTIONS', @@ -25,7 +27,10 @@ async function bootstrap () { session({ resave: false, saveUninitialized: false, - secret: process.env.JWT_SECRET! + secret: + process.env.JWT_SECRET && process.env.JWT_SECRET !== '' + ? process.env.JWT_SECRET + : 'secret' }) ) app.use(cookieParser()) @@ -36,4 +41,6 @@ async function bootstrap () { await app.listen(port) logger.log(`Application listening on port ${port}`) } -bootstrap() +bootstrap().catch((e) => { + throw new InternalServerErrorException(e) +}) diff --git a/back/volume/src/pong/entity/result.entity.ts b/back/volume/src/pong/entity/result.entity.ts index 87ab54d..53232dc 100644 --- a/back/volume/src/pong/entity/result.entity.ts +++ b/back/volume/src/pong/entity/result.entity.ts @@ -3,8 +3,7 @@ import { PrimaryGeneratedColumn, Column, ManyToMany, - CreateDateColumn, - JoinTable + CreateDateColumn } from 'typeorm' import User from 'src/users/entity/user.entity' diff --git a/back/volume/src/users/entity/user.entity.ts b/back/volume/src/users/entity/user.entity.ts index 8254d17..9a4ffef 100644 --- a/back/volume/src/users/entity/user.entity.ts +++ b/back/volume/src/users/entity/user.entity.ts @@ -4,8 +4,7 @@ import { Column, OneToMany, ManyToMany, - JoinTable, - UpdateDateColumn + JoinTable } from 'typeorm' import { randomUUID } from 'crypto' diff --git a/back/volume/src/users/users.controller.ts b/back/volume/src/users/users.controller.ts index 718c9fb..8248e46 100644 --- a/back/volume/src/users/users.controller.ts +++ b/back/volume/src/users/users.controller.ts @@ -137,7 +137,7 @@ export class UsersController { @FtUser() profile: Profile, @Param('id', ParseIntPipe) id: number ): Promise { - if (profile.id == id) { + if (profile.id === id) { throw new BadRequestException("You can't invit yourself.") } return await this.usersService.invit(profile.id, id) diff --git a/back/volume/src/users/users.service.ts b/back/volume/src/users/users.service.ts index b3b6074..a40c26d 100644 --- a/back/volume/src/users/users.service.ts +++ b/back/volume/src/users/users.service.ts @@ -59,7 +59,7 @@ export class UsersService { const newUser = this.usersRepository.create(userData) return await this.usersRepository.save(newUser) } catch (err) { - throw new Error(`Error creating ${err} user ${err.message}`) + throw new Error(`Error creating user ${err}`) } } @@ -130,10 +130,10 @@ export class UsersService { async getRank (ftId: number): Promise { const leader = await this.getLeader() - return leader.findIndex((user) => user.ftId == ftId) + return leader.findIndex((user) => user.ftId === ftId) } - async invit (ftId: number, targetFtId: number) { + async invit (ftId: number, targetFtId: number): Promise { const user = await this.usersRepository.findOne({ where: { ftId }, relations: { @@ -144,7 +144,7 @@ export class UsersService { if (user == null) { return new NotFoundException(`Error: user id ${ftId} isn't in our db.`) } - if (user.friends.findIndex((friend) => friend.ftId === targetFtId) != -1) { + if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { return null } const target = await this.usersRepository.findOne({ @@ -162,18 +162,18 @@ export class UsersService { const id = user.followers.findIndex( (follower) => follower.ftId === targetFtId ) - if (id != -1) { + if (id !== -1) { console.log( `Friend relation complete between ${user.username} and ${target.username}` ) user.friends.push(target) - if (user.ftId != target.ftId) target.friends.push(user) + if (user.ftId !== target.ftId) target.friends.push(user) user.followers.slice(id, 1) - this.usersRepository.save(user) + await this.usersRepository.save(user) } else { console.log(`You asked ${target.username} to be your friend.`) target.followers.push(user) } - this.usersRepository.save(target) + await this.usersRepository.save(target) } } diff --git a/front/volume/src/components/Leaderboard.svelte b/front/volume/src/components/Leaderboard.svelte index 9b80fea..d231201 100644 --- a/front/volume/src/components/Leaderboard.svelte +++ b/front/volume/src/components/Leaderboard.svelte @@ -3,6 +3,7 @@ username: string; wins: number; looses: number; + matchs: number; winrate: number; rank: number; } @@ -14,20 +15,35 @@
-
- {#if leaderboard.length > 0} -

Leaderboard

- {#each leaderboard.slice(0, 10) as player} -
  • - {player.username} - {player.wins} - {player.looses} - {player.winrate}%wins - | rank #{player.rank} -
  • - {/each} - {:else} -

    No Players to display

    - {/if} -
    + {#if leaderboard.length > 0} + + + + + + + + + + + + + + + {#each leaderboard.slice(0, 10) as player} + + + + + + + + {/each} + +
    Leaderboard
    RankUsernamesWinsMatchsWinrates
    {player.rank}{player.username}{player.wins}{player.matchs}{player.winrate}%
    + {:else} +

    No Players to display

    + {/if}
    @@ -51,5 +67,14 @@ border-radius: 5px; padding: 1rem; width: 300px; + display: flex; + justify-content: center; + } + + td { + border: 1px solid #333; + text-align: center; + max-width: 12ch; + overflow: hidden; } diff --git a/front/volume/src/components/MatchHistory.svelte b/front/volume/src/components/MatchHistory.svelte index 03cdaf0..c068ee7 100644 --- a/front/volume/src/components/MatchHistory.svelte +++ b/front/volume/src/components/MatchHistory.svelte @@ -11,8 +11,9 @@ export let matches: Array = []; function displayDate(str: string) { const splitT = str.split("T"); - const splitdot = splitT[1].split("."); - return splitT[0] + "-" + splitdot[0]; + const splitDate = splitT[0].split('-') + const splitDot = splitT[1].split("."); + return `${splitDate[1]}/${splitDate[2]}-${splitDot[0]}`; } @@ -20,24 +21,27 @@
    {#if matches.length > 0} -

    Last 10 monkey games

    + + + + + + + + + + + + {#each matches.slice(0, 10) as match} -
  • - {displayDate(match.date.toString())}: {match.players[0].username} - {match.scores[0]} - {match.scores[1]} - {match.players[1].username} - -
  • + + + + + {/each} + +
    Last 10 monkey games
    DatePlayersScores
    {displayDate(match.date.toString())}{match.players[0].username}
    {match.players[1].username}
    {match.score[0]}
    {match.score[1]}
    {:else}

    No matches to display

    {/if} @@ -65,5 +69,15 @@ border-radius: 5px; padding: 1rem; width: 300px; + display:flex; + justify-content: center; + } + + td { + border:1px solid #111; + text-align: center; + max-width: 15ch; + overflow: hidden; } +