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