diff --git a/src/Client/Client.ts b/src/Client/Client.ts index bbce7da..edb3abe 100644 --- a/src/Client/Client.ts +++ b/src/Client/Client.ts @@ -81,9 +81,9 @@ export default class Client { if (true) { //@ts-ignore if (req.Data != null && req.Data != undefined && req.Data != NaN) { - console.log(`[RPC] 傳送client資料:(${req.WS.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`) + console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`) } else { - console.log(`[RPC] 傳送client資料:(${req.WS.clientCount}): ${req.Method}()`) + console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`) } } diff --git a/src/DataReceived/MainControlData.ts b/src/DataReceived/MainControlData.ts index da668f8..7627f0d 100644 --- a/src/DataReceived/MainControlData.ts +++ b/src/DataReceived/MainControlData.ts @@ -12,8 +12,10 @@ export default class MainControlData { if (req.IsValid) { switch (req.Method) { case "lobby.list": - Lobby.GetList(req); - // GiftData.Instance?.AddCommonList(resp.Data) + Lobby.List(req); + break + case "lobby.create": + Lobby.Create(req); break default: // if (GameMain.Instance && GameMain.Instance.node && GameMain.Instance.node.parent) { diff --git a/src/Engine/CatanEngine/NetManagerV2/Core/INetResponse.ts b/src/Engine/CatanEngine/NetManagerV2/Core/INetResponse.ts index 519a3b1..0ef4d15 100644 --- a/src/Engine/CatanEngine/NetManagerV2/Core/INetResponse.ts +++ b/src/Engine/CatanEngine/NetManagerV2/Core/INetResponse.ts @@ -2,7 +2,7 @@ import Client from "../../../../Client/Client" export interface INetResponse { readonly Method: string - readonly WS: Client + readonly WS?: Client readonly Status: number readonly Data: TResponse readonly IsValid?: boolean diff --git a/src/Engine/CatanEngine/NetManagerV2/NetResponse.ts b/src/Engine/CatanEngine/NetManagerV2/NetResponse.ts index 56e0ea2..edd38ec 100644 --- a/src/Engine/CatanEngine/NetManagerV2/NetResponse.ts +++ b/src/Engine/CatanEngine/NetManagerV2/NetResponse.ts @@ -1,18 +1,13 @@ -import Client from "../../../Client/Client" import { INetResponse } from "./Core/INetResponse" export abstract class NetResponse implements INetResponse { protected data: any = undefined protected method: string = "" - protected ws: Client = undefined protected status: number = undefined get Method(): string { return this.method } - get WS(): Client { - return this.ws - } get Status(): number { return this.status } @@ -20,9 +15,8 @@ export abstract class NetResponse implements INetResponse { return this.data } - constructor(ws: Client, data: any, status: number = 0) { - this.ws = ws - this.data = data + constructor(data: any = undefined, status: number = 0) { + this.data = data ?? "" this.status = status } } diff --git a/src/Lobby/Lobby.ts b/src/Lobby/Lobby.ts index 1b3d988..96d636d 100644 --- a/src/Lobby/Lobby.ts +++ b/src/Lobby/Lobby.ts @@ -1,4 +1,4 @@ -import { LobbyListRequest } from "../define/Request/LobbyRequest"; +import { LobbyCreateRequest, LobbyListRequest } from "../define/Request/LobbyRequest"; import { INetResponse } from "../Engine/CatanEngine/NetManagerV2/Core/INetResponse"; import Room from "../Room/Room"; @@ -10,22 +10,30 @@ export default class Lobby { //#region private private static list: Room[] = [] + private static serialNumber: number = 0 //#endregion //#region Custom /** GetList */ - public static GetList(req: INetResponse): void { - let resp: LobbyListRequest = new LobbyListRequest(req.WS, this.list, 0) + public static List(req: INetResponse): void { + const data = [] + for (let i = 0; i < this.list.length; i++) { + const room = this.list[i]; + data.push(room.SerialNumber) + } + const resp: LobbyListRequest = new LobbyListRequest(data, 0) req.WS.SendClient(resp) } /** Create */ public static Create(req: INetResponse): void { - let room = new Room() + const room: Room = new Room(Lobby.serialNumber, req.WS) + Lobby.serialNumber++; this.list.push(room) - // + const resp: LobbyCreateRequest = new LobbyCreateRequest() + req.WS.SendClient(resp) } /** Join */ diff --git a/src/Room/Room.ts b/src/Room/Room.ts index f06136d..d85bc69 100644 --- a/src/Room/Room.ts +++ b/src/Room/Room.ts @@ -1,12 +1,19 @@ -import BaseSingleton from "../Engine/Utils/Singleton/BaseSingleton" +import Client from "../Client/Client" /** * Room */ -export default class Room extends BaseSingleton() { +export default class Room { + + //#region public + + public SerialNumber: number = 0 + + //#endregion //#region private + private wsArr: Client[] = [] //#endregion @@ -15,9 +22,9 @@ export default class Room extends BaseSingleton() { /** * */ - constructor() { - super() - + constructor(serialNumber: number, ws: Client) { + this.SerialNumber = serialNumber + this.wsArr.push(ws) } //#endregion diff --git a/src/define/Request/LobbyRequest.ts b/src/define/Request/LobbyRequest.ts index 9707b59..9a974c8 100644 --- a/src/define/Request/LobbyRequest.ts +++ b/src/define/Request/LobbyRequest.ts @@ -1,13 +1,19 @@ import { NetResponse } from "../../Engine/CatanEngine/NetManagerV2/NetResponse" -import Room from "../../Room/Room" // #region Request -export type RpcLobbyListRequest = Room[] - +export type RpcLobbyListRequest = any[] export class LobbyListRequest extends NetResponse { - protected data: RpcLobbyListRequest = undefined + protected data: RpcLobbyListRequest protected method: string = "lobby.list" + constructor(data: RpcLobbyListRequest = undefined, status: number = 0) { super(data, status) } +} + +export type RpcLobbyCreateRequest = undefined +export class LobbyCreateRequest extends NetResponse { + protected data: RpcLobbyListRequest + protected method: string = "lobby.create" + constructor(data: RpcLobbyCreateRequest = undefined, status: number = 0) { super(data, status) } } // #endregion