Skip to content

Commit

Permalink
feat: add change story name
Browse files Browse the repository at this point in the history
  • Loading branch information
yociduo committed Jun 22, 2020
1 parent 692a22b commit 91bb2f9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
12 changes: 12 additions & 0 deletions server/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.6"
services:
db:
image: mysql:5.7
env_file:
- .env
ports:
- 3306:3306
volumes:
- ../mysql/data:/var/lib/mysql
- ../mysql/my.cnf:/etc/mysql/my.cnf:ro
- ../mysql/init:/docker-entrypoint-initdb.d/:ro
80 changes: 35 additions & 45 deletions server/src/controller/socket/PokerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,122 +19,112 @@ export class PokerController {

@OnConnect()
connection(@ConnectedSocket() socket: Socket) {
logger.info(`User ${socket.user.id} connected`);
return this.base(`User ${socket.user.id} connected`, async () => { });
}

@OnDisconnect()
async disconnect(@ConnectedSocket() socket: Socket) {
try {
logger.info(`User ${socket.user.id} disconnected`);
await Poker.disconnect(socket.user);
} catch (error) {
logger.error(`User ${socket.user.id} disconnected`, error);
}
return this.base(`User ${socket.user.id} disconnected`, async () => await Poker.disconnect(socket.user));
}

@OnMessage('[Poker] join room')
@EmitOnSuccess('[Poker] init')
async join(@ConnectedSocket() socket: Socket, @MessageBody() { id }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} join room ${id}`);
return this.base(`User ${socket.user.id} join room ${id}`, async () => {
const poker = await Poker.getPoker(id);
await poker.join(socket.user);
const { roomId, currentStory } = poker;
socket.join(roomId, () => socket.to(roomId).emit('[Poker] action', { id, currentStory }));
return poker.getRoom(socket.user);
} catch (error) {
logger.error(`User ${socket.user.id} join room ${id}`, error);
}
});
}

@OnMessage('[Poker] leave room')
async leave(@ConnectedSocket() socket: Socket, @MessageBody() { id }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} leave room ${id}`);
return this.base(`User ${socket.user.id} leave room ${id}`, async () => {
const poker = await Poker.getPoker(id);
await poker.leave(socket.user);
const { roomId, currentStory } = poker;
socket.leave(roomId, () => socket.to(roomId).emit('[Poker] action', { id, currentStory }));
} catch (error) {
logger.error(`User ${socket.user.id} leave room ${id}`, error);
}
});
}

@OnMessage('[Poker] select card')
async selectCard(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, card }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} selected card ${card}`);
return this.base(`User ${socket.user.id} selected card ${card}`, async () => {
const poker = await Poker.getPoker(id);
await poker.selectCard(socket.user, card);
const { roomId, currentScore, currentStory } = poker;
io.to(roomId).emit('[Poker] action', { id, currentScore, currentStory });
} catch (error) {
logger.error(`User ${socket.user.id} selected card ${card}`, error);
}
});
}

@OnMessage('[Poker] calc method')
async calcMethod(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, calcMethod }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} changed calc method ${calcMethod}`);
return this.base(`User ${socket.user.id} changed calc method ${calcMethod}`, async () => {
const poker = await Poker.getPoker(id);
await poker.calcMethod(calcMethod);
const { roomId, currentScore, room: { options } } = poker;
io.to(roomId).emit('[Poker] action', { id, currentScore, options });
} catch (error) {
logger.error(`User ${socket.user.id} changed calc method ${calcMethod}`, error);
}
});
}

@OnMessage('[Poker] current score')
async changeCurrentScore(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, currentScore }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} change current score to ${currentScore}`);
return this.base(`User ${socket.user.id} change current score to ${currentScore}`, async () => {
const poker = await Poker.getPoker(id);
await poker.changeCurrentScore(currentScore);
const { roomId, room: { options } } = poker;
io.to(roomId).emit('[Poker] action', { id, currentScore, options });
} catch (error) {
logger.error(`User ${socket.user.id} change current score to ${currentScore}`, error);
}
});
}

@OnMessage('[Poker] next story')
async nextStory(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} next story for room ${id}`);
return this.base(`User ${socket.user.id} next story for room ${id}`, async () => {
const poker = await Poker.getPoker(id);
await poker.nextStory();
const { roomId, currentStory, currentScore, stories } = poker;
io.to(roomId).emit('[Poker] action', { id, stories, currentScore, currentStory, selectedCard: null, loading: false });
} catch (error) {
logger.error(`User ${socket.user.id} next story for room ${id}`, error);
}
});
}

@OnMessage('[Poker] add story')
async addStory(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, storyNames }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} add story ${storyNames.join()} for room ${id}`);
return this.base(`User ${socket.user.id} add story ${storyNames.join()} for room ${id}`, async () => {
const poker = await Poker.getPoker(id);
await poker.addStories(storyNames, socket.user);
const { roomId, currentStory, currentScore } = poker;
io.to(roomId).emit('[Poker] action', { id, currentScore, currentStory, selectedCard: null, loading: false });
} catch (error) {
logger.info(`User ${socket.user.id} add story ${storyNames.join()} for room ${id}`, error);
}
});
}

@OnMessage('[Poker] show hide score')
async showHideScore(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, isNoymous }: PokerMessageBody) {
try {
logger.info(`User ${socket.user.id} ${isNoymous ? 'show' : 'hide' } score for room ${id}`);
return this.base(`User ${socket.user.id} ${isNoymous ? 'show' : 'hide' } score for room ${id}`, async () => {
const poker = await Poker.getPoker(id);
await poker.toggleShowHideScore(isNoymous);
const { roomId, room: { options } } = poker;
io.to(roomId).emit('[Poker] action', { id, options });
});
}

@OnMessage('[Poker] change story name')
async changeStoryName(@ConnectedSocket() socket: Socket, @SocketIO() io: Socket, @MessageBody() { id, name }: PokerMessageBody) {
return this.base(`User ${socket.user.id} change story name to ${name}`, async () => {
const poker = await Poker.getPoker(id);
await poker.changeStoryName(name);
const { roomId, currentStory } = poker;
io.to(roomId).emit('[Poker] action', { id, currentStory });
});
}

private async base<T>(message: string, exec: () => Promise<T>): Promise<T> {
try {
logger.info(message);
return await exec();
} catch (error) {
logger.info(`User ${socket.user.id} ${isNoymous ? 'show' : 'hide' } score for room ${id}`, error);
logger.error(message, error);
}
}

Expand Down
1 change: 1 addition & 0 deletions server/src/model/PokerMessageBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface PokerMessageBody {
isNoymous?: boolean;
currentScore?: number;
storyNames?: string[];
name?: string;
}
5 changes: 5 additions & 0 deletions server/src/util/Poker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ export class Poker {
await getManager().save(Room, this.room);
}

public async changeStoryName(name: string): Promise<void> {
this.currentStory.name = name;
await getManager().save(Story, this.currentStory);
}

private async handleTimer(user: User, userRoom: UserRoom): Promise<void> {
if (this.room.userRooms.every(r => r.isLeft)) {
if (this.currentStory) {
Expand Down

0 comments on commit 91bb2f9

Please sign in to comment.