강동현
Builds for 1 pipeline failed in 1 minute 25 seconds

게임 시작, 종료 구현

......@@ -47,6 +47,9 @@ export class ServerInboundMessageRecordMap {
ready: Boolean,
});
// 방장이 게임을 시작합니다.
startGame = Record({});
// drawer가 단어를 선택합니다.
chooseWord = Record({
word: String,
......
......@@ -177,7 +177,7 @@ export class WorldGuessingGame implements Game {
private finishGame(): void {
this.room.broadcast("finishGame", {});
// TODO
this.room.finishGame();
}
private forceFinishGame() {
......
......@@ -9,6 +9,8 @@ import {
} from "../../common";
import { RoomDescription, RoomInfo, UserData } from "../../common/dataType";
import { RoomManager } from "./RoomManager";
import { Game } from "../game/Game";
import { WorldGuessingGame } from "../game/WordGuessingGame";
export class Room {
public readonly uuid: string;
......@@ -22,6 +24,8 @@ export class Room {
public usersReady: User[] = [];
public admin?: User;
public game?: Game;
public closed: boolean = false;
public handler: MessageHandler;
......@@ -60,6 +64,17 @@ export class Room {
this.setReady(user, message.ready);
return { ok: true };
},
startGame: (user, message) => {
if (user !== this.admin) {
return { ok: false };
}
const result = this.canStart();
if (!result.ok) {
return result;
}
this.startGame();
return { ok: true };
},
});
if (this.admin) {
......@@ -149,16 +164,31 @@ export class Room {
return this.usersReady.includes(user);
}
public canStart(): boolean {
public canStart(): { ok: boolean; reason?: string } {
if (this.isPlayingGame()) {
return { ok: false, reason: "이미 게임이 진행 중입니다." };
}
if (this.users.length < 2) {
return false;
return { ok: false, reason: "최소 2명의 플레이어가 필요합니다." };
}
for (let i = 0; i < this.users.length; i++) {
if (!this.isAdmin(this.users[i]) && !this.isReady(this.users[i])) {
return false;
return { ok: false, reason: "모든 플레이어가 준비해야 합니다." };
}
}
return true;
return { ok: true };
}
private startGame(): void {
this.game = new WorldGuessingGame(this);
}
public finishGame(): void {
this.game = undefined;
}
public isPlayingGame(): boolean {
return this.game !== undefined;
}
public sendChat(user: User, message: string): void {
......