vvandenb 2 years ago
parent
commit
6c3f3a8540
  1. 1
      back/volume/src/pong/game/Game.ts
  2. 1
      back/volume/src/pong/game/Player.ts
  3. 7
      back/volume/src/pong/pong.service.ts
  4. 20
      back/volume/src/users/users.controller.ts
  5. 41
      back/volume/src/users/users.service.ts
  6. 3
      front/volume/src/components/Profile.svelte

1
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

1
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)
}
}

7
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<void> {
const player = await this.usersService.findUserByName(playerName)
player.status = "in-game"
await this.usersService.save(player)
}

20
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<StreamableFile | null> {
): Promise<StreamableFile> {
return await this.getAvatarById(profile.id, response)
}
@Get('user/:name')
async getUserByName (@Param('name') username: string): Promise<User | null> {
return await this.usersService.findUserByName(username)
async getUserByName (@Param('name') username: string): Promise<User> {
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<BadRequestException | null> {
): Promise<void> {
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<StreamableFile | null> {
): Promise<StreamableFile> {
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<User | null> {
return await this.usersService.findUser(ftId)
const user = await this.usersService.findUser(ftId)
user.socketKey = ''
return user
}
@Post(':id')

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

@ -19,14 +19,19 @@ export class UsersService {
}
async findUsers (): Promise<User[]> {
return await this.usersRepository.find({})
const users = await this.usersRepository.find({})
users.forEach((usr) => {
usr.socketKey = ''
})
return users
}
async findUserByName (username: string): Promise<User | null> {
async findUserByName (username: string): Promise<User> {
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<User | null> {
async findUser (ftId: number): Promise<User> {
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<User[]> {
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<BadRequestException | User> {
async create (userData: UserDto): Promise<User | null> {
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<User[]> {
@ -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<Result[]> {
@ -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<User[]> {
@ -128,7 +139,7 @@ export class UsersService {
return leader.findIndex((user) => user.ftId === ftId)
}
async invit (ftId: number, targetFtId: number): Promise<any> {
async invit (ftId: number, targetFtId: number): Promise<void> {
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 },

3
front/volume/src/components/Profile.svelte

@ -50,7 +50,7 @@
<img src={API_URL + "/avatar"} alt="avatar" class="profile-img" />
{:else}
<form
action={`${API_URL}/avatar/${user.id}`}
action={`${API_URL}/avatar`}
method="post"
enctype="multipart/form-data"
id="upload_avatar"
@ -59,6 +59,7 @@
type="file"
id="avatar-input"
name="avatar"
accept="image/png, image/gif, image/jpeg"
on:change={submitAvatar}
/>
</form>

Loading…
Cancel
Save