[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",
"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": [
"<node_internals>/**"

View File

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

View File

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

View File

@ -28,9 +28,7 @@ export default class Room {
private conns: BaseConnection[] = [];
private connData = new Map<BaseConnection, RoomConnData>();
private nowPlayer: [conn: BaseConnection, state: number] = undefined;
private connData: Map<BaseConnection<any>, RoomConnData> = new Map<BaseConnection, RoomConnData>();
//#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) => {
if (conn) {
const player: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
const apiNum: number = conn === key ? 100 : 200;
server.broadcastMsg("room/ChangeState", [connData.state + apiNum], player);
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
//#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 {

View File

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