[add] Room SetReady,ChangeState

This commit is contained in:
建喵 2023-09-10 19:33:19 +08:00
parent 3dfff7ea54
commit 93f66106fc
2 changed files with 50 additions and 9 deletions

View File

@ -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<ReqChangeState, ResChangeState>) {
const { state } = call.req;
const conn: BaseConnection<ServiceType> = call.conn;
const room: Room = conn.Room;
room.ChangeState(conn, state);
call.succ(0);
}

View File

@ -1,6 +1,7 @@
import { BaseConnection, WsConnection } from "tsrpc"; import { BaseConnection, WsConnection } from "tsrpc";
import { server } from "../.."; import { server } from "../..";
import { RandomEx } from "../../Engine/Utils/Number/RandomEx"; import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
import { EGameState } from "../../shared/protocols/define/enum";
import { ServiceType } from "../../shared/protocols/serviceProto"; import { ServiceType } from "../../shared/protocols/serviceProto";
@ -26,7 +27,9 @@ export default class Room {
private conns: BaseConnection[] = []; private conns: BaseConnection[] = [];
private nowPlayer: [index: number, name: string] = undefined; private ready: boolean[] = [];
private nowPlayer: [conn: BaseConnection, state: number] = undefined;
//#endregion //#endregion
@ -39,8 +42,6 @@ export default class Room {
// 给每个新房间生成一个唯一的 ID // 给每个新房间生成一个唯一的 ID
this.RoomId = ++Room.maxRoomId; this.RoomId = ++Room.maxRoomId;
Room.rooms[this.RoomId] = this; Room.rooms[this.RoomId] = this;
const now: number = RandomEx.GetInt(0, 2);
} }
//#endregion //#endregion
@ -54,12 +55,7 @@ export default class Room {
//#endregion //#endregion
//#region Custom //#region API
/** Join */
public Join(conn: BaseConnection): void {
this.conns.push(conn);
}
/** Exit */ /** Exit */
public Exit(conn: BaseConnection): void { public Exit(conn: BaseConnection): void {
@ -71,6 +67,39 @@ export default class Room {
server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>this.conns); server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>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<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn, i) => i === index);
const othersPlayer: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>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 */ /** ConnCount */
public ConnCount(): number { public ConnCount(): number {
return this.conns.length; return this.conns.length;