From b7e4627b2f200c741346d82770f4d0da1442d2fd Mon Sep 17 00:00:00 2001 From: JianMiau Date: Mon, 11 Sep 2023 15:27:45 +0800 Subject: [PATCH] =?UTF-8?q?[add]=20=E5=85=88=E4=B8=80=E7=89=88demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/room/ApiChangeState.ts | 2 +- src/component/Room/Room.ts | 38 ++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/api/room/ApiChangeState.ts b/src/api/room/ApiChangeState.ts index 0526603..811b724 100644 --- a/src/api/room/ApiChangeState.ts +++ b/src/api/room/ApiChangeState.ts @@ -8,5 +8,5 @@ export default async function (call: ApiCall) { const { state } = call.req; const conn: BaseConnection = call.conn; const room: Room = conn.Room; - room.ChangeState(state, conn); + room.ChangeState(false, state, conn); } \ No newline at end of file diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index 02d9ba7..c53567b 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -2,6 +2,7 @@ import { BaseConnection, WsConnection } from "tsrpc"; import { server } from "../.."; 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 { RoomConnData } from "./RoomUtils"; @@ -67,15 +68,28 @@ export default class Room { } /** ChangeState */ - public ChangeState(state?: EGameState, conn?: BaseConnection): void { + 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) { - const player: WsConnection[] = []>this.conns.filter((conn) => conn === key); - const apiNum: number = conn === key ? 100 : 200; - server.broadcastMsg("room/ChangeState", [(connData.state ?? EGameState.Ready) + apiNum], player); + if (state) { + target = []>this.conns.filter((conn) => conn === key); + const apiNum: number = conn === key ? 100 : 200; + data = [state + apiNum]; + } else if (conn) { + target = []>this.conns.filter((conn) => conn === key); + const apiNum: number = conn === key ? 100 : 200; + data = [(connData.state ?? EGameState.Ready) + apiNum]; + } } else { - server.broadcastMsg("room/ChangeState", [state], []>this.conns); + target = []>this.conns.filter((conn) => conn === key); + data = [connData.state]; } + if (needData) { + data.push(connData.card); + } + server.broadcastMsg("room/ChangeState", data, target); }); switch (state) { case EGameState.Ready: @@ -112,24 +126,26 @@ export default class Room { const now: number = RandomEx.GetInt(0, 2); this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { const card: number = RandomEx.GetInt(0, cardCount); - connData.state = key === self.conns[now] ? EGameState.TurnCards : EGameState.Wait; + connData.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait; connData.card = card; self.connData.set(key, connData); }); - this.ChangeState(EGameState.TurnCards, self.conns[now]); + this.ChangeState(true); } } /** TurnEnd */ public TurnEnd(conn: BaseConnection): void { - const data: RoomConnData = this.connData.get(conn); - data.state = EGameState.TurnEnd; - this.connData.set(conn, data); + const self: this = this; + this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { + connData.state = EGameState.MyWait; + self.connData.set(key, connData); + }); 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); - nextData.state = EGameState.TurnCards; + nextData.state = EGameState.MyTurnCards; this.connData.set(nextConn, nextData); this.ChangeState(); }