diff --git a/.vscode/launch.json b/.vscode/launch.json index 023f0dd..30d0683 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,8 +12,14 @@ "address": "127.0.0.1:9229/87f42d5b-97bf-4d25-a4d7-37306985459a", "port": 9229, "localRoot": "${workspaceFolder}", - "remoteRoot": "D:/Project/Test/Test_Web/GuessWhoIAmS", - // "remoteRoot": "E:/Project/Test/Test_NodeJS/GuessWhoIAmS", + + // Home + // "remoteRoot": "D:/Project/Test/Test_Web/GuessWhoIAmS", + + // Work + "remoteRoot": "E:/Project/Test/Test_NodeJS/GuessWhoIAmS", + + // Nas // "remoteRoot": "/volume1/homes/JianMiau/www/line-bot-ts", "skipFiles": [ "/**" diff --git a/src/api/account/ApiLogin.ts b/src/api/account/ApiLogin.ts index 3bf6b7b..05a0676 100644 --- a/src/api/account/ApiLogin.ts +++ b/src/api/account/ApiLogin.ts @@ -10,11 +10,10 @@ export default async function (call: ApiCall) { } // Success - const { sn, req } = call; - const { name } = req; + const { name } = call.req; const conn: BaseConnection = call.conn; console.log(`name: ${name} is Login`); - conn.UserId = sn; + conn.UserId = conn.id; conn.NickName = name; call.succ(0); } \ No newline at end of file diff --git a/src/api/room/ApiChangeState.ts b/src/api/room/ApiChangeState.ts index 420baf7..0526603 100644 --- a/src/api/room/ApiChangeState.ts +++ b/src/api/room/ApiChangeState.ts @@ -4,9 +4,9 @@ import { ReqChangeState, ResChangeState } from "../../shared/protocols/room/PtlC import { ServiceType } from "../../shared/protocols/serviceProto"; export default async function (call: ApiCall) { + call.succ(0); const { state } = call.req; const conn: BaseConnection = call.conn; const room: Room = conn.Room; - room.ChangeState(conn, state); - call.succ(0); + room.ChangeState(state, conn); } \ No newline at end of file diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index c48bd72..02d9ba7 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -28,9 +28,7 @@ export default class Room { private conns: BaseConnection[] = []; - private connData = new Map(); - - private nowPlayer: [conn: BaseConnection, state: number] = undefined; + private connData: Map, RoomConnData> = new Map(); //#endregion @@ -69,33 +67,36 @@ export default class Room { } /** ChangeState */ - public ChangeState(conn: BaseConnection, state: EGameState): void { - if (state === EGameState.Ready) { - this.SetReady(conn); - } + public ChangeState(state?: EGameState, conn?: BaseConnection): void { this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { - const player: WsConnection[] = []>this.conns.filter((conn) => conn === key); - const apiNum: number = conn === key ? 100 : 200; - server.broadcastMsg("room/ChangeState", [connData.state + apiNum], player); + 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); + } else { + server.broadcastMsg("room/ChangeState", [state], []>this.conns); + } }); + switch (state) { + case EGameState.Ready: + this.Ready(conn); + break; + + case EGameState.TurnEnd: + this.TurnEnd(conn); + break; + + default: + break; + } } //#endregion - //#region Custom + //#region ChangeState - /** Join */ - public Join(conn: BaseConnection): void { - this.conns.push(conn); - const data: RoomConnData = { - state: undefined, - card: undefined, - }; - this.connData.set(conn, data); - } - - /** SetReady */ - public SetReady(conn: BaseConnection): void { + /** Ready */ + public Ready(conn: BaseConnection): void { const self: this = this; const cardCount: number = 24; const data: RoomConnData = this.connData.get(conn); @@ -113,10 +114,40 @@ export default class Room { const card: number = RandomEx.GetInt(0, cardCount); connData.state = key === self.conns[now] ? EGameState.TurnCards : EGameState.Wait; connData.card = card; + self.connData.set(key, connData); }); + this.ChangeState(EGameState.TurnCards, self.conns[now]); } } + /** TurnEnd */ + public TurnEnd(conn: BaseConnection): void { + const data: RoomConnData = this.connData.get(conn); + data.state = EGameState.TurnEnd; + this.connData.set(conn, data); + 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; + this.connData.set(nextConn, nextData); + this.ChangeState(); + } + + //#endregion + + //#region Custom + + /** Join */ + public Join(conn: BaseConnection): void { + this.conns.push(conn); + const data: RoomConnData = { + state: undefined, + card: undefined, + }; + this.connData.set(conn, data); + } + /** RestartGame */ public RestartGame(): void { diff --git a/src/component/User/User.ts b/src/component/User/User.ts index 78a8ff9..f8b5afa 100644 --- a/src/component/User/User.ts +++ b/src/component/User/User.ts @@ -8,7 +8,7 @@ export default class User { //#region User - public UserId: number = undefined; + public UserId: string = undefined; public NickName: string = undefined; //#endregion