Browse Source

* Formatting / Linting

master
vvandenb 2 years ago
parent
commit
55caa807a9
  1. 2
      back/volume/src/pong/entity/result.entity.ts
  2. 5
      back/volume/src/pong/game/Game.ts
  3. 4
      back/volume/src/pong/pong.module.ts
  4. 23
      back/volume/src/pong/pong.service.ts
  5. 2
      back/volume/src/types.d.ts
  6. 66
      back/volume/src/users/users.controller.ts
  7. 6
      back/volume/src/users/users.module.ts
  8. 4
      back/volume/src/users/users.service.ts
  9. 5
      front/volume/src/components/Pong/Ball.ts
  10. 2
      front/volume/src/components/Pong/ColorPicker.svelte
  11. 7
      front/volume/src/components/Pong/Game.ts
  12. 16
      front/volume/src/components/Pong/MapCustomization.svelte
  13. 5
      front/volume/src/components/Pong/utils.ts

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

@ -14,7 +14,7 @@ export default class Result {
@PrimaryGeneratedColumn()
id: number
@ManyToMany(() => User, (player: User) => player.results, { cascade: true})
@ManyToMany(() => User, (player: User) => player.results, { cascade: true })
players: Array<User | null> // TODO: change to User[] for final version
@Column('text', { array: true })

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

@ -16,7 +16,6 @@ import { type MapDtoValidated } from '../dtos/MapDtoValidated'
import { type GameUpdate } from '../dtos/GameUpdate'
import { type GameInfo } from '../dtos/GameInfo'
import { type PongService } from '../pong.service'
import { Injectable, Inject } from '@nestjs/common'
function gameLoop (game: Game): void {
const canvasRect: Rect = new Rect(
@ -34,7 +33,7 @@ function gameLoop (game: Game): void {
game.players[indexPlayerScored].score += 1
if (game.players[indexPlayerScored].score >= DEFAULT_WIN_SCORE) {
console.log(`${game.players[indexPlayerScored].name} won!`)
game.stop()
void game.stop()
}
}
@ -116,7 +115,7 @@ export class Game {
if (playerIndex !== -1) {
this.players.splice(playerIndex, 1)
if (this.players.length < 2) {
this.stop()
void this.stop()
}
}
}

4
back/volume/src/pong/pong.module.ts

@ -6,9 +6,7 @@ import { PongService } from './pong.service'
import { UsersModule } from 'src/users/users.module'
@Module({
imports: [
forwardRef(() => UsersModule),
TypeOrmModule.forFeature([Result])],
imports: [forwardRef(() => UsersModule), TypeOrmModule.forFeature([Result])],
providers: [PongGateway, PongService],
exports: [PongService]
})

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

@ -1,4 +1,4 @@
import { Inject, Injectable, forwardRef } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { UsersService } from 'src/users/users.service'
@ -14,28 +14,28 @@ export class PongService {
private readonly usersService: UsersService
) {}
async updatePlayer (i: number, result: Result) {
async updatePlayer (i: number, result: Result): Promise<void> {
const player: User | null = result.players[i]
if (player == null) return
player.matchs++
if (result.score[i] > result.score[Math.abs(i - 1)]) player.wins++
else player.looses++
player.winrate = (100 * player.wins) / player.matchs
player.rank = await this.usersService.getRank(player.ftId) + 1
//player.results.push(result)
player.rank = (await this.usersService.getRank(player.ftId)) + 1
// player.results.push(result)
this.usersService.save(player)
}
async saveResult (players: Player[]) {
let result = new Result()
let ply = new Array<User | null>
async saveResult (players: Player[]): Promise<void> {
const result = new Result()
const ply = new Array<User | null>()
ply.push(await this.usersService.findUserByName(players[0].name))
ply.push(await this.usersService.findUserByName(players[1].name))
result.players = ply;
result.players = ply
result.score = [players[0].score, players[1].score]
this.resultsRepository.save(result)
this.updatePlayer(0, result)
this.updatePlayer(1, result)
await this.resultsRepository.save(result)
await this.updatePlayer(0, result)
await this.updatePlayer(1, result)
}
async getHistory (): Promise<Result[]> {
@ -48,5 +48,4 @@ export class PongService {
const results = await this.usersService.getResults(ftId)
return results.sort((a, b) => (a.date < b.date ? 1 : -1))
}
}

2
back/volume/src/types.d.ts

@ -1,5 +1,5 @@
declare module 'passport-42' {
export type Profile = any
export type VerifyCallback = any
export class Strategy {}
export class Strategy {}!
}

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

@ -11,7 +11,8 @@ import {
Res,
StreamableFile,
BadRequestException,
Redirect
Redirect,
type NotFoundException
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
@ -30,59 +31,59 @@ import { ApiBody, ApiConsumes } from '@nestjs/swagger'
import { type Request, Response } from 'express'
import { createReadStream } from 'fs'
import { join } from 'path'
import type Result from 'src/pong/entity/result.entity'
@Controller()
export class UsersController {
constructor(
constructor (
private readonly usersService: UsersService,
private readonly pongService: PongService
) { }
) {}
@Get('all')
async getAllUsers(): Promise<User[]> {
async getAllUsers (): Promise<User[]> {
return await this.usersService.findUsers()
}
@Get('online')
async getOnlineUsers(): Promise<User[]> {
async getOnlineUsers (): Promise<User[]> {
return await this.usersService.findOnlineUsers()
}
@Get('friends')
@UseGuards(AuthenticatedGuard)
async getFriends(@FtUser() profile: Profile) {
async getFriends (@FtUser() profile: Profile): Promise<User[]> {
return await this.usersService.getFriends(profile.id)
}
@Get('invits')
@UseGuards(AuthenticatedGuard)
async getInvits(@FtUser() profile: Profile) {
async getInvits (@FtUser() profile: Profile): Promise<User[]> {
return await this.usersService.getInvits(profile.id)
}
@Get('leader')
@UseGuards(AuthenticatedGuard)
async getLeader() {
async getLeader (): Promise<User[]> {
return await this.usersService.getLeader()
}
@Get('leader/:id')
@UseGuards(AuthenticatedGuard)
async getRank(@Param('id', ParseIntPipe) id: number) {
async getRank (@Param('id', ParseIntPipe) id: number): Promise<number> {
return await this.usersService.getRank(id)
}
@Get('history')
@UseGuards(AuthenticatedGuard)
async getHistory() {
async getHistory (): Promise<Result[]> {
return await this.pongService.getHistory()
}
@Get('history/:id')
@UseGuards(AuthenticatedGuard)
async getHistoryById(@Param('id', ParseIntPipe) id: number) {
return this.pongService.getHistoryById(id)
async getHistoryById (@Param('id', ParseIntPipe) id: number): Promise<Result[]> {
return await this.pongService.getHistoryById(id)
}
@Post('avatar')
@ -107,43 +108,43 @@ export class UsersController {
description: 'A new avatar for the user',
type: AvatarUploadDto
})
async addAvatar(
async addAvatar (
@FtUser() profile: Profile,
@UploadedFile() file: Express.Multer.File
) {
@UploadedFile() file: Express.Multer.File
): Promise<void> {
await this.usersService.addAvatar(profile.id, file.filename)
}
@Get('avatar')
@UseGuards(AuthenticatedGuard)
async getAvatar(
async getAvatar (
@FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response
) {
@Res({ passthrough: true }) response: Response
): Promise<StreamableFile | null> {
return await this.getAvatarById(profile.id, response)
}
@Get('user/:name')
async getUserByName(@Param('name') username: string): Promise<User | null> {
async getUserByName (@Param('name') username: string): Promise<User | null> {
return await this.usersService.findUserByName(username)
}
@Get('invit/:id')
@UseGuards(AuthenticatedGuard)
async invitUser(
async invitUser (
@FtUser() profile: Profile,
@Param('id', ParseIntPipe) id: number
) {
@Param('id', ParseIntPipe) id: number
): Promise<NotFoundException | null | undefined> {
return await this.usersService.invit(profile.id, id)
}
@Get('avatar/:id')
async getAvatarById(
async getAvatarById (
@Param('id', ParseIntPipe) ftId: number,
@Res({ passthrough: true }) response: Response
) {
@Res({ passthrough: true }) response: Response
): Promise<StreamableFile | null> {
const user = await this.usersService.findUser(ftId)
if (user == null) return
if (user == null) return null
const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({
@ -154,15 +155,15 @@ export class UsersController {
}
@Get(':id')
async getUserById(
async getUserById (
@Param('id', ParseIntPipe) ftId: number
): Promise<User | null> {
return await this.usersService.findUser(ftId)
}
@Post(":id")
@Post(':id')
@UseGuards(AuthenticatedGuard)
async createById(@Body() payload: UserDto) {
async createById (@Body() payload: UserDto): Promise<User | null> {
const user = await this.usersService.findUser(payload.ftId)
if (user != null) {
return await this.usersService.update(user, payload)
@ -173,13 +174,13 @@ export class UsersController {
@Get()
@UseGuards(AuthenticatedGuard)
async getUser(@FtUser() profile: Profile): Promise<User | null> {
async getUser (@FtUser() profile: Profile): Promise<User | null> {
return await this.usersService.findUser(profile.id)
}
@Post()
@UseGuards(AuthenticatedGuard)
async create(@Body() payload: UserDto, @FtUser() profile: Profile) {
async create (@Body() payload: UserDto, @FtUser() profile: Profile): Promise<User | null> {
const user = await this.usersService.findUser(profile.id)
if (user != null) {
return await this.usersService.update(user, payload)
@ -187,5 +188,4 @@ export class UsersController {
return await this.usersService.create(payload)
}
}
}

6
back/volume/src/users/users.module.ts

@ -6,11 +6,9 @@ import { UsersService } from './users.service'
import { PongModule } from 'src/pong/pong.module'
@Module({
imports: [
forwardRef(() => PongModule),
TypeOrmModule.forFeature([User])],
imports: [forwardRef(() => PongModule), TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService]
})
export class UsersModule { }
export class UsersModule {}

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

@ -4,13 +4,13 @@ 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 Result from 'src/pong/entity/result.entity'
import type Result from 'src/pong/entity/result.entity'
@Injectable()
@Catch(QueryFailedError, EntityNotFoundError)
export class UsersService {
constructor (
@InjectRepository(User) private readonly usersRepository: Repository<User>,
@InjectRepository(User) private readonly usersRepository: Repository<User>
) {}
save (user: User) {

5
front/volume/src/components/Pong/Ball.ts

@ -4,10 +4,7 @@ export class Ball {
rect: Rect;
speed: Point;
constructor(
spawn: Point,
size: Point = new Point(20, 20)
) {
constructor(spawn: Point, size: Point = new Point(20, 20)) {
this.rect = new Rect(spawn, size);
}

2
front/volume/src/components/Pong/ColorPicker.svelte

@ -3,7 +3,7 @@
</script>
<div>
<input type="color" bind:value={color}>
<input type="color" bind:value={color} />
</div>
<style>

7
front/volume/src/components/Pong/Game.ts

@ -20,7 +20,12 @@ export class Game {
elementsColor: string;
backgroundColor: string;
constructor(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D, elementsColor: string, backgroundColor: string) {
constructor(
canvas: HTMLCanvasElement,
context: CanvasRenderingContext2D,
elementsColor: string,
backgroundColor: string
) {
this.canvas = canvas;
this.context = context;
this.players = [];

16
front/volume/src/components/Pong/MapCustomization.svelte

@ -72,9 +72,21 @@
<h1>Map Customization:</h1>
<div>
Width:
<input type="range" min={DEFAULT_MAP_SIZE.x} max=1000 bind:value={map.size.x} on:input={sizeChange} />
<input
type="range"
min={DEFAULT_MAP_SIZE.x}
max="1000"
bind:value={map.size.x}
on:input={sizeChange}
/>
Height:
<input type="range" min={DEFAULT_MAP_SIZE.y} max=800 bind:value={map.size.y} on:input={sizeChange} />
<input
type="range"
min={DEFAULT_MAP_SIZE.y}
max="800"
bind:value={map.size.y}
on:input={sizeChange}
/>
</div>
<canvas
bind:this={canvas}

5
front/volume/src/components/Pong/utils.ts

@ -32,10 +32,7 @@ export class Rect {
this.size = size;
}
draw(
context: CanvasRenderingContext2D,
color: string
) {
draw(context: CanvasRenderingContext2D, color: string) {
const offset: Point = new Point(this.size.x / 2, this.size.y / 2);
context.fillStyle = color;

Loading…
Cancel
Save