diff --git a/src/api/room/ApiChangeState.ts b/src/api/room/ApiChangeState.ts new file mode 100644 index 0000000..420baf7 --- /dev/null +++ b/src/api/room/ApiChangeState.ts @@ -0,0 +1,12 @@ +import { ApiCall, BaseConnection } from "tsrpc"; +import Room from "../../component/Room/Room"; +import { ReqChangeState, ResChangeState } from "../../shared/protocols/room/PtlChangeState"; +import { ServiceType } from "../../shared/protocols/serviceProto"; + +export default async function (call: ApiCall) { + const { state } = call.req; + const conn: BaseConnection = call.conn; + const room: Room = conn.Room; + room.ChangeState(conn, state); + call.succ(0); +} \ No newline at end of file diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index b0511c2..99fd6d4 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -1,6 +1,7 @@ import { BaseConnection, WsConnection } from "tsrpc"; import { server } from "../.."; import { RandomEx } from "../../Engine/Utils/Number/RandomEx"; +import { EGameState } from "../../shared/protocols/define/enum"; import { ServiceType } from "../../shared/protocols/serviceProto"; @@ -26,7 +27,9 @@ export default class Room { private conns: BaseConnection[] = []; - private nowPlayer: [index: number, name: string] = undefined; + private ready: boolean[] = []; + + private nowPlayer: [conn: BaseConnection, state: number] = undefined; //#endregion @@ -39,8 +42,6 @@ export default class Room { // 给每个新房间生成一个唯一的 ID this.RoomId = ++Room.maxRoomId; Room.rooms[this.RoomId] = this; - - const now: number = RandomEx.GetInt(0, 2); } //#endregion @@ -54,12 +55,7 @@ export default class Room { //#endregion - //#region Custom - - /** Join */ - public Join(conn: BaseConnection): void { - this.conns.push(conn); - } + //#region API /** Exit */ public Exit(conn: BaseConnection): void { @@ -71,6 +67,39 @@ export default class Room { server.broadcastMsg("room/GoToGame", 0, []>this.conns); } + /** ChangeState */ + public ChangeState(conn: BaseConnection, state: EGameState): void { + if (state === EGameState.Loading) { + this.SetReady(conn); + } + const index: number = this.conns.indexOf(conn); + const nowPlayer: WsConnection[] = []>this.conns.filter((conn, i) => i === index); + const othersPlayer: WsConnection[] = []>this.conns.filter((conn, i) => i !== index); + server.broadcastMsg("room/ChangeState", state + 100, nowPlayer); + server.broadcastMsg("room/ChangeState", state + 200, othersPlayer); + } + + //#endregion + + //#region Custom + + /** Join */ + public Join(conn: BaseConnection): void { + this.conns.push(conn); + this.ready.push(false); + } + + /** SetReady */ + public SetReady(conn: BaseConnection): void { + const index: number = this.conns.indexOf(conn); + this.ready[index] = true; + if (!this.ready.includes(false)) { + const now: number = RandomEx.GetInt(0, 2); + this.nowPlayer = [this.conns[now], EGameState.TurnCards]; + this.ChangeState(...this.nowPlayer); + } + } + /** ConnCount */ public ConnCount(): number { return this.conns.length;