[add] 遊戲Type

This commit is contained in:
建喵 2024-01-10 11:46:57 +08:00
parent 14505e26ee
commit d7671c7d80
4 changed files with 17 additions and 8 deletions

View File

@ -1,2 +1,3 @@
src/utils
src/Engine
src/shared/protocols/serviceProto.ts

View File

@ -4,8 +4,9 @@ import { ReqCreate, ResCreate } from "../../shared/protocols/room/PtlCreate";
import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqCreate, ResCreate>) {
const { type } = call.req;
const conn: BaseConnection<ServiceType> = call.conn;
const room: Room = new Room();
const room: Room = new Room(type);
room.Join(conn);
conn.Room = room;
call.succ(room.RoomId);

View File

@ -1,5 +1,6 @@
import { ApiCall, BaseConnection } from "tsrpc";
import Room from "../../component/Room/Room";
import { EGameType } from "../../shared/protocols/define/enum";
import { ReqJoin, ResJoin } from "../../shared/protocols/room/PtlJoin";
import { ServiceType } from "../../shared/protocols/serviceProto";
@ -12,9 +13,9 @@ export default async function (call: ApiCall<ReqJoin, ResJoin>) {
call.error("房間已滿");
return;
}
room.Join(conn);
const type: EGameType = room.Join(conn);
conn.Room = room;
call.succ(room.ConnCount());
call.succ([room.ConnCount(), type]);
if (room.ConnCount() >= 2) {
room.GotoGame();

View File

@ -1,8 +1,9 @@
import { BaseConnection, WsConnection } from "tsrpc";
import { server } from "../..";
import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
import { EGameState } from "../../shared/protocols/define/enum";
import { EGameState, EGameType } from "../../shared/protocols/define/enum";
import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState";
import { MsgGoToGame } from "../../shared/protocols/room/MsgGoToGame";
import { ServiceType } from "../../shared/protocols/serviceProto";
import { sleep } from "../../utils";
import { RoomConnData } from "./RoomUtils";
@ -32,6 +33,8 @@ export default class Room {
private connData: Map<BaseConnection<any>, RoomConnData> = new Map<BaseConnection, RoomConnData>();
private type: EGameType;
//#endregion
//#region Lifecycle
@ -39,10 +42,11 @@ export default class Room {
/**
*
*/
constructor() {
constructor(type: EGameType) {
// 给每个新房间生成一个唯一的 ID
this.RoomId = ++Room.maxRoomId;
Room.rooms[this.RoomId] = this;
this.type = type;
}
//#endregion
@ -68,7 +72,8 @@ export default class Room {
/** GotoGame */
public GotoGame(): void {
server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>this.conns);
let data: MsgGoToGame = this.type;
server.broadcastMsg("room/GoToGame", data, <WsConnection<ServiceType>[]>this.conns);
}
/**
@ -211,13 +216,14 @@ export default class Room {
}
/** Join */
public Join(conn: BaseConnection): void {
public Join(conn: BaseConnection): EGameType {
this.conns.push(conn);
const data: RoomConnData = {
state: undefined,
answer: undefined,
};
this.connData.set(conn, data);
return this.type;
}
/** GameOver */