[add] Room SetReady,ChangeState
This commit is contained in:
parent
3dfff7ea54
commit
93f66106fc
12
src/api/room/ApiChangeState.ts
Normal file
12
src/api/room/ApiChangeState.ts
Normal 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);
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import { BaseConnection, WsConnection } from "tsrpc";
|
||||
import { server } from "../..";
|
||||
import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
|
||||
import { EGameState } from "../../shared/protocols/define/enum";
|
||||
import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||
|
||||
|
||||
@ -26,7 +27,9 @@ export default class Room {
|
||||
|
||||
private conns: BaseConnection[] = [];
|
||||
|
||||
private nowPlayer: [index: number, name: string] = undefined;
|
||||
private ready: boolean[] = [];
|
||||
|
||||
private nowPlayer: [conn: BaseConnection, state: number] = undefined;
|
||||
|
||||
//#endregion
|
||||
|
||||
@ -39,8 +42,6 @@ export default class Room {
|
||||
// 给每个新房间生成一个唯一的 ID
|
||||
this.RoomId = ++Room.maxRoomId;
|
||||
Room.rooms[this.RoomId] = this;
|
||||
|
||||
const now: number = RandomEx.GetInt(0, 2);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@ -54,12 +55,7 @@ export default class Room {
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Custom
|
||||
|
||||
/** Join */
|
||||
public Join(conn: BaseConnection): void {
|
||||
this.conns.push(conn);
|
||||
}
|
||||
//#region API
|
||||
|
||||
/** Exit */
|
||||
public Exit(conn: BaseConnection): void {
|
||||
@ -71,6 +67,39 @@ export default class Room {
|
||||
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 */
|
||||
public ConnCount(): number {
|
||||
return this.conns.length;
|
||||
|
Loading…
Reference in New Issue
Block a user