Skip to content

Commit

Permalink
feat: Add player update functionality (#172)
Browse files Browse the repository at this point in the history
Co-authored-by: 유미라 <[email protected]>
  • Loading branch information
ssum1ra and 유미라 authored Dec 2, 2024
1 parent d8323e4 commit 603888b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server/src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ export class GameGateway implements OnGatewayDisconnect {
client.to(roomId).emit('settingsUpdated', { settings: updatedSettings });
}

@SubscribeMessage('updatePlayer')
async handle(@ConnectedSocket() client: Socket, @MessageBody() data: { player: Partial<Player> }) {
const { playerId, roomId } = client.data;
if (!roomId || !playerId) throw new BadRequestException('Room ID and Player ID are required');

const updatedPlayer = await this.gameService.updatePlayer(roomId, playerId, data.player);

client.to(roomId).emit('playerUpdated', { player: updatedPlayer });
}

@SubscribeMessage('gameStart')
async handleGameStart(@ConnectedSocket() client: Socket) {
const { playerId, roomId } = client.data;
Expand Down
12 changes: 12 additions & 0 deletions server/src/game/game.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ export class GameRepository {
await multi.exec();
}

async getPlayer(roomId: string, playerId: string) {
const player = await this.redisService.hgetall(`room:${roomId}:players:${playerId}`);
if (!player || Object.keys(player).length === 0) return null;

return {
...player,
role: player.role === '' ? null : player.role,
profileImage: player.userImg === '' ? null : player.userImg,
score: parseInt(player.score, 10) || 0,
} as Player;
}

async updatePlayer(roomId: string, playerId: string, player: Partial<Player>) {
await this.redisService.hset(`room:${roomId}:players:${playerId}`, player);
}
Expand Down
13 changes: 13 additions & 0 deletions server/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ export class GameService {
return updatedSettings;
}

async updatePlayer(roomId: string, playerId: string, data: Partial<Player>) {
const room = await this.gameRepository.getRoom(roomId);
if (!room) throw new RoomNotFoundException('Room not found');

const player = await this.gameRepository.getPlayer(roomId, playerId);
if (!player) throw new PlayerNotFoundException('Player not found');

const updatedPlayer = { ...player, ...data };
await this.gameRepository.updatePlayer(roomId, playerId, updatedPlayer);

return updatedPlayer;
}

async startGame(roomId: string, playerId: string) {
const room = await this.gameRepository.getRoom(roomId);
if (!room) throw new RoomNotFoundException('Room not found');
Expand Down

0 comments on commit 603888b

Please sign in to comment.