[add] Room OK
This commit is contained in:
@@ -3,6 +3,7 @@ import { server } from "../..";
|
||||
import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
|
||||
import { EGameState } from "../../shared/protocols/define/enum";
|
||||
import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||
import { RoomConnData } from "./RoomUtils";
|
||||
|
||||
|
||||
/**
|
||||
@@ -27,7 +28,7 @@ export default class Room {
|
||||
|
||||
private conns: BaseConnection[] = [];
|
||||
|
||||
private ready: boolean[] = [];
|
||||
private connData = new Map<BaseConnection, RoomConnData>();
|
||||
|
||||
private nowPlayer: [conn: BaseConnection, state: number] = undefined;
|
||||
|
||||
@@ -69,14 +70,14 @@ export default class Room {
|
||||
|
||||
/** ChangeState */
|
||||
public ChangeState(conn: BaseConnection, state: EGameState): void {
|
||||
if (state === EGameState.Loading) {
|
||||
if (state === EGameState.Ready) {
|
||||
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);
|
||||
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -86,20 +87,41 @@ export default class Room {
|
||||
/** Join */
|
||||
public Join(conn: BaseConnection): void {
|
||||
this.conns.push(conn);
|
||||
this.ready.push(false);
|
||||
const data: RoomConnData = {
|
||||
state: undefined,
|
||||
card: undefined,
|
||||
};
|
||||
this.connData.set(conn, data);
|
||||
}
|
||||
|
||||
/** SetReady */
|
||||
public SetReady(conn: BaseConnection): void {
|
||||
const index: number = this.conns.indexOf(conn);
|
||||
this.ready[index] = true;
|
||||
if (!this.ready.includes(false)) {
|
||||
const self: this = this;
|
||||
const cardCount: number = 24;
|
||||
const data: RoomConnData = this.connData.get(conn);
|
||||
data.state = EGameState.Ready;
|
||||
this.connData.set(conn, data);
|
||||
let canStart: boolean = true;
|
||||
this.connData.forEach((connData: RoomConnData) => {
|
||||
if (connData.state === undefined || connData.state !== EGameState.Ready) {
|
||||
canStart = false;
|
||||
}
|
||||
});
|
||||
if (canStart) {
|
||||
const now: number = RandomEx.GetInt(0, 2);
|
||||
this.nowPlayer = [this.conns[now], EGameState.TurnCards];
|
||||
this.ChangeState(...this.nowPlayer);
|
||||
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
||||
const card: number = RandomEx.GetInt(0, cardCount);
|
||||
connData.state = key === self.conns[now] ? EGameState.TurnCards : EGameState.Wait;
|
||||
connData.card = card;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** RestartGame */
|
||||
public RestartGame(): void {
|
||||
|
||||
}
|
||||
|
||||
/** ConnCount */
|
||||
public ConnCount(): number {
|
||||
return this.conns.length;
|
||||
|
||||
8
src/component/Room/RoomUtils.ts
Normal file
8
src/component/Room/RoomUtils.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// #region Interface
|
||||
|
||||
export interface RoomConnData {
|
||||
state: number
|
||||
card?: number
|
||||
}
|
||||
|
||||
// #endregion
|
||||
Reference in New Issue
Block a user