[add] 先一版demo

This commit is contained in:
建喵 2023-09-11 15:27:45 +08:00
parent fedf77eb75
commit b7e4627b2f
2 changed files with 28 additions and 12 deletions

View File

@ -8,5 +8,5 @@ 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(state, conn);
room.ChangeState(false, state, conn);
}

View File

@ -2,6 +2,7 @@ import { BaseConnection, WsConnection } from "tsrpc";
import { server } from "../..";
import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
import { EGameState } from "../../shared/protocols/define/enum";
import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState";
import { ServiceType } from "../../shared/protocols/serviceProto";
import { RoomConnData } from "./RoomUtils";
@ -67,15 +68,28 @@ export default class Room {
}
/** ChangeState */
public ChangeState(state?: EGameState, conn?: BaseConnection): void {
public ChangeState(needData: boolean = false, state?: EGameState, conn?: BaseConnection): void {
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
let target: WsConnection<ServiceType>[] = [];
let data: MsgChangeState = [];
if (conn) {
const player: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
if (state) {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
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);
data = [state + apiNum];
} else if (conn) {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
const apiNum: number = conn === key ? 100 : 200;
data = [(connData.state ?? EGameState.Ready) + apiNum];
}
} else {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
data = [connData.state];
}
if (needData) {
data.push(connData.card);
}
server.broadcastMsg("room/ChangeState", data, target);
});
switch (state) {
case EGameState.Ready:
@ -112,24 +126,26 @@ export default class Room {
const now: number = RandomEx.GetInt(0, 2);
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.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait;
connData.card = card;
self.connData.set(key, connData);
});
this.ChangeState(EGameState.TurnCards, self.conns[now]);
this.ChangeState(true);
}
}
/** TurnEnd */
public TurnEnd(conn: BaseConnection): void {
const data: RoomConnData = this.connData.get(conn);
data.state = EGameState.TurnEnd;
this.connData.set(conn, data);
const self: this = this;
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
connData.state = EGameState.MyWait;
self.connData.set(key, connData);
});
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;
nextData.state = EGameState.MyTurnCards;
this.connData.set(nextConn, nextData);
this.ChangeState();
}