[mod] Room功能

This commit is contained in:
建喵 2023-09-11 14:56:12 +08:00
parent 011667a064
commit fedf77eb75
5 changed files with 67 additions and 31 deletions

10
.vscode/launch.json vendored
View File

@ -12,8 +12,14 @@
"address": "127.0.0.1:9229/87f42d5b-97bf-4d25-a4d7-37306985459a", "address": "127.0.0.1:9229/87f42d5b-97bf-4d25-a4d7-37306985459a",
"port": 9229, "port": 9229,
"localRoot": "${workspaceFolder}", "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", // "remoteRoot": "/volume1/homes/JianMiau/www/line-bot-ts",
"skipFiles": [ "skipFiles": [
"<node_internals>/**" "<node_internals>/**"

View File

@ -10,11 +10,10 @@ export default async function (call: ApiCall<ReqLogin, ResLogin>) {
} }
// Success // Success
const { sn, req } = call; const { name } = call.req;
const { name } = req;
const conn: BaseConnection<ServiceType> = call.conn; const conn: BaseConnection<ServiceType> = call.conn;
console.log(`name: ${name} is Login`); console.log(`name: ${name} is Login`);
conn.UserId = sn; conn.UserId = conn.id;
conn.NickName = name; conn.NickName = name;
call.succ(0); call.succ(0);
} }

View File

@ -4,9 +4,9 @@ import { ReqChangeState, ResChangeState } from "../../shared/protocols/room/PtlC
import { ServiceType } from "../../shared/protocols/serviceProto"; import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqChangeState, ResChangeState>) { export default async function (call: ApiCall<ReqChangeState, ResChangeState>) {
call.succ(0);
const { state } = call.req; const { state } = call.req;
const conn: BaseConnection<ServiceType> = call.conn; const conn: BaseConnection<ServiceType> = call.conn;
const room: Room = conn.Room; const room: Room = conn.Room;
room.ChangeState(conn, state); room.ChangeState(state, conn);
call.succ(0);
} }

View File

@ -28,9 +28,7 @@ export default class Room {
private conns: BaseConnection[] = []; private conns: BaseConnection[] = [];
private connData = new Map<BaseConnection, RoomConnData>(); private connData: Map<BaseConnection<any>, RoomConnData> = new Map<BaseConnection, RoomConnData>();
private nowPlayer: [conn: BaseConnection, state: number] = undefined;
//#endregion //#endregion
@ -69,33 +67,36 @@ export default class Room {
} }
/** ChangeState */ /** ChangeState */
public ChangeState(conn: BaseConnection, state: EGameState): void { public ChangeState(state?: EGameState, conn?: BaseConnection): void {
if (state === EGameState.Ready) {
this.SetReady(conn);
}
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => { this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
const player: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key); if (conn) {
const apiNum: number = conn === key ? 100 : 200; const player: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
server.broadcastMsg("room/ChangeState", [connData.state + apiNum], player); const apiNum: number = conn === key ? 100 : 200;
server.broadcastMsg("room/ChangeState", [(connData.state ?? EGameState.Ready) + apiNum], player);
} else {
server.broadcastMsg("room/ChangeState", [state], <WsConnection<ServiceType>[]>this.conns);
}
}); });
switch (state) {
case EGameState.Ready:
this.Ready(conn);
break;
case EGameState.TurnEnd:
this.TurnEnd(conn);
break;
default:
break;
}
} }
//#endregion //#endregion
//#region Custom //#region ChangeState
/** Join */ /** Ready */
public Join(conn: BaseConnection): void { public Ready(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 {
const self: this = this; const self: this = this;
const cardCount: number = 24; const cardCount: number = 24;
const data: RoomConnData = this.connData.get(conn); const data: RoomConnData = this.connData.get(conn);
@ -113,10 +114,40 @@ export default class Room {
const card: number = RandomEx.GetInt(0, cardCount); const card: number = RandomEx.GetInt(0, cardCount);
connData.state = key === self.conns[now] ? EGameState.TurnCards : EGameState.Wait; connData.state = key === self.conns[now] ? EGameState.TurnCards : EGameState.Wait;
connData.card = card; 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 */ /** RestartGame */
public RestartGame(): void { public RestartGame(): void {

View File

@ -8,7 +8,7 @@ export default class User {
//#region User //#region User
public UserId: number = undefined; public UserId: string = undefined;
public NickName: string = undefined; public NickName: string = undefined;
//#endregion //#endregion