diff --git a/src/api/room/ApiJoin.ts b/src/api/room/ApiJoin.ts index 2f0d52a..20d2bc2 100644 --- a/src/api/room/ApiJoin.ts +++ b/src/api/room/ApiJoin.ts @@ -14,7 +14,7 @@ export default async function (call: ApiCall) { } room.Join(conn); conn.Room = room; - call.succ(0); + call.succ(room.ConnCount()); if (room.ConnCount() >= 2) { room.GotoGame(); diff --git a/src/api/room/ApiSendAns.ts b/src/api/room/ApiSendAns.ts new file mode 100644 index 0000000..a31a7fb --- /dev/null +++ b/src/api/room/ApiSendAns.ts @@ -0,0 +1,12 @@ +import { ApiCall, BaseConnection } from "tsrpc"; +import Room from "../../component/Room/Room"; +import { ReqSendAns, ResSendAns } from "../../shared/protocols/room/PtlSendAns"; +import { ServiceType } from "../../shared/protocols/serviceProto"; + +export default async function (call: ApiCall) { + const { answer } = call.req; + const conn: BaseConnection = call.conn; + const room: Room = conn.Room; + const data: boolean = room.SendAns(conn, answer); + call.succ(data); +} \ No newline at end of file diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index c53567b..ae65cb5 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -4,6 +4,7 @@ import { RandomEx } from "../../Engine/Utils/Number/RandomEx"; import { EGameState } from "../../shared/protocols/define/enum"; import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState"; import { ServiceType } from "../../shared/protocols/serviceProto"; +import { sleep } from "../../utils"; import { RoomConnData } from "./RoomUtils"; @@ -67,27 +68,32 @@ export default class Room { server.broadcastMsg("room/GoToGame", 0, []>this.conns); } - /** ChangeState */ + /** + * ChangeState + * @param needData true就是要帶connData.answer + * @param state 沒帶就是使用connData.state ?? EGameState.Ready + * @param conn 沒帶就是會+apiNum + */ public ChangeState(needData: boolean = false, state?: EGameState, conn?: BaseConnection): void { this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { let target: WsConnection[] = []; let data: MsgChangeState = []; if (conn) { if (state) { - target = []>this.conns.filter((conn) => conn === key); + target = []>this.conns.filter((filterConn) => filterConn === key); const apiNum: number = conn === key ? 100 : 200; data = [state + apiNum]; } else if (conn) { - target = []>this.conns.filter((conn) => conn === key); + target = []>this.conns.filter((filterConn) => filterConn === key); const apiNum: number = conn === key ? 100 : 200; data = [(connData.state ?? EGameState.Ready) + apiNum]; } } else { - target = []>this.conns.filter((conn) => conn === key); + target = []>this.conns.filter((filterConn) => filterConn === key); data = [connData.state]; } if (needData) { - data.push(connData.card); + data.push(connData.answer); } server.broadcastMsg("room/ChangeState", data, target); }); @@ -105,6 +111,20 @@ export default class Room { } } + /** SendAns */ + public SendAns(conn: BaseConnection, answer: number): boolean { + const index: number = this.conns.indexOf(conn); + const nextIndex: number = (index + 1) % this.conns.length; + const nextConn: BaseConnection = this.conns[nextIndex]; + const nextData: RoomConnData = this.connData.get(nextConn); + const nextAns: number = nextData.answer; + const data: boolean = nextAns === answer; + if (data) { + this.GameOver(); + } + return data; + } + //#endregion //#region ChangeState @@ -127,7 +147,7 @@ export default class Room { this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { const card: number = RandomEx.GetInt(0, cardCount); connData.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait; - connData.card = card; + connData.answer = card; self.connData.set(key, connData); }); this.ChangeState(true); @@ -159,11 +179,17 @@ export default class Room { this.conns.push(conn); const data: RoomConnData = { state: undefined, - card: undefined, + answer: undefined, }; this.connData.set(conn, data); } + /** GameOver */ + public async GameOver(): Promise { + await sleep(500); + this.ChangeState(false, EGameState.GameOver); + } + /** RestartGame */ public RestartGame(): void { diff --git a/src/component/Room/RoomUtils.ts b/src/component/Room/RoomUtils.ts index ee737ae..0f74fd9 100644 --- a/src/component/Room/RoomUtils.ts +++ b/src/component/Room/RoomUtils.ts @@ -2,7 +2,7 @@ export interface RoomConnData { state: number - card?: number + answer?: number } // #endregion \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..343e0b6 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,11 @@ +export function sleep(ms: any): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +export function getArray(count: number): number[] { + const array: number[] = []; + for (let i: number = 0; i < count; i++) { + array.push(i); + } + return array; +} \ No newline at end of file