diff --git a/docs/openapi.json b/docs/openapi.json index a597510..12f2b58 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -5,15 +5,18 @@ "version": "1.0.0" }, "paths": { - "/Send": { + "/account/Login": { "post": { - "operationId": "Send", + "tags": [ + "account" + ], + "operationId": "account/Login", "requestBody": { - "description": "Req", + "description": "Req", "content": { "application/json": { "schema": { - "$ref": "#/components/schemas/PtlSend_ReqSend" + "$ref": "#/components/schemas/account_PtlLogin_ReqLogin" } } } @@ -25,7 +28,7 @@ "application/json": { "schema": { "type": "object", - "description": "ApiReturn", + "description": "ApiReturn", "properties": { "isSucc": { "type": "boolean", @@ -35,7 +38,242 @@ "default": true }, "res": { - "$ref": "#/components/schemas/PtlSend_ResSend" + "$ref": "#/components/schemas/account_PtlLogin_ResLogin" + } + } + } + } + } + }, + "default": { + "description": "Error", + "$ref": "#/components/responses/error" + } + } + } + }, + "/room/ChangeState": { + "post": { + "tags": [ + "room" + ], + "operationId": "room/ChangeState", + "requestBody": { + "description": "Req", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/room_PtlChangeState_ReqChangeState" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "ApiReturn", + "properties": { + "isSucc": { + "type": "boolean", + "enum": [ + true + ], + "default": true + }, + "res": { + "$ref": "#/components/schemas/room_PtlChangeState_ResChangeState" + } + } + } + } + } + }, + "default": { + "description": "Error", + "$ref": "#/components/responses/error" + } + } + } + }, + "/room/Create": { + "post": { + "tags": [ + "room" + ], + "operationId": "room/Create", + "requestBody": { + "description": "Req", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/room_PtlCreate_ReqCreate" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "ApiReturn", + "properties": { + "isSucc": { + "type": "boolean", + "enum": [ + true + ], + "default": true + }, + "res": { + "$ref": "#/components/schemas/room_PtlCreate_ResCreate" + } + } + } + } + } + }, + "default": { + "description": "Error", + "$ref": "#/components/responses/error" + } + } + } + }, + "/room/Exit": { + "post": { + "tags": [ + "room" + ], + "operationId": "room/Exit", + "requestBody": { + "description": "Req", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/room_PtlExit_ReqExit" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "ApiReturn", + "properties": { + "isSucc": { + "type": "boolean", + "enum": [ + true + ], + "default": true + }, + "res": { + "$ref": "#/components/schemas/room_PtlExit_ResExit" + } + } + } + } + } + }, + "default": { + "description": "Error", + "$ref": "#/components/responses/error" + } + } + } + }, + "/room/Join": { + "post": { + "tags": [ + "room" + ], + "operationId": "room/Join", + "requestBody": { + "description": "Req", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/room_PtlJoin_ReqJoin" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "ApiReturn", + "properties": { + "isSucc": { + "type": "boolean", + "enum": [ + true + ], + "default": true + }, + "res": { + "$ref": "#/components/schemas/room_PtlJoin_ResJoin" + } + } + } + } + } + }, + "default": { + "description": "Error", + "$ref": "#/components/responses/error" + } + } + } + }, + "/room/List": { + "post": { + "tags": [ + "room" + ], + "operationId": "room/List", + "requestBody": { + "description": "Req", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/room_PtlList_ReqList" + } + } + } + }, + "responses": { + "200": { + "description": "Success", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "ApiReturn", + "properties": { + "isSucc": { + "type": "boolean", + "enum": [ + true + ], + "default": true + }, + "res": { + "$ref": "#/components/schemas/room_PtlList_ResList" } } } @@ -52,6 +290,20 @@ }, "components": { "schemas": { + "account_PtlLogin_ReqLogin": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + }, + "required": [ + "name" + ] + }, + "account_PtlLogin_ResLogin": { + "type": "number" + }, "MsgChat_MsgChat": { "type": "object", "properties": { @@ -68,29 +320,88 @@ "time" ] }, - "PtlSend_ReqSend": { + "room_MsgChangeState_MsgChangeState": { + "type": "number" + }, + "room_MsgExit_MsgExit": { + "type": "number" + }, + "room_MsgGoToGame_MsgGoToGame": { + "type": "number" + }, + "room_PtlChangeState_ReqChangeState": { "type": "object", "properties": { - "content": { + "state": { + "type": "number" + } + }, + "required": [ + "state" + ] + }, + "room_PtlChangeState_ResChangeState": { + "type": "number" + }, + "room_PtlCreate_ReqCreate": { + "type": "object", + "properties": { + "null": { "type": "string" } }, "required": [ - "content" + "null" ] }, - "PtlSend_ResSend": { + "room_PtlCreate_ResCreate": { + "type": "number" + }, + "room_PtlExit_ReqExit": { "type": "object", "properties": { - "time": { - "type": "string", - "format": "date-time" + "null": { + "type": "string" } }, "required": [ - "time" + "null" ] }, + "room_PtlExit_ResExit": { + "type": "number" + }, + "room_PtlJoin_ReqJoin": { + "type": "object", + "properties": { + "roomId": { + "type": "number" + } + }, + "required": [ + "roomId" + ] + }, + "room_PtlJoin_ResJoin": { + "type": "number" + }, + "room_PtlList_ReqList": { + "type": "object", + "properties": { + "null": { + "type": "string" + } + }, + "required": [ + "null" + ] + }, + "room_PtlList_ResList": { + "type": "array", + "items": { + "type": "object" + } + }, "?bson_ObjectID": { "type": "string" }, diff --git a/docs/tsapi.md b/docs/tsapi.md index 96ee360..06e1406 100644 --- a/docs/tsapi.md +++ b/docs/tsapi.md @@ -9,26 +9,130 @@ ## 目录 -- [Send](#/Send) +- account + - [Login](#/account/Login) +- room + - [ChangeState](#/room/ChangeState) + - [Create](#/room/Create) + - [Exit](#/room/Exit) + - [Join](#/room/Join) + - [List](#/room/List) --- -## Send +## account + +### Login **路径** -- POST `/Send` +- POST `/account/Login` **请求** ```ts -interface ReqSend { - content: string +interface ReqLogin { + name: string } ``` **响应** ```ts -interface ResSend { - time: /*datetime*/ string +type ResLogin = number +``` + +--- + +## room + +### ChangeState + +**路径** +- POST `/room/ChangeState` + +**请求** +```ts +interface ReqChangeState { + state: number } ``` +**响应** +```ts +type ResChangeState = number +``` + +--- + +### Create + +**路径** +- POST `/room/Create` + +**请求** +```ts +interface ReqCreate { + null: string +} +``` + +**响应** +```ts +type ResCreate = number +``` + +--- + +### Exit + +**路径** +- POST `/room/Exit` + +**请求** +```ts +interface ReqExit { + null: string +} +``` + +**响应** +```ts +type ResExit = number +``` + +--- + +### Join + +**路径** +- POST `/room/Join` + +**请求** +```ts +interface ReqJoin { + roomId: number +} +``` + +**响应** +```ts +type ResJoin = number +``` + +--- + +### List + +**路径** +- POST `/room/List` + +**请求** +```ts +interface ReqList { + null: string +} +``` + +**响应** +```ts +type ResList = any[] +``` + diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index 99fd6d4..c48bd72 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -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(); 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[] = []>this.conns.filter((conn, i) => i === index); - const othersPlayer: WsConnection[] = []>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[] = []>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; diff --git a/src/component/Room/RoomUtils.ts b/src/component/Room/RoomUtils.ts new file mode 100644 index 0000000..ee737ae --- /dev/null +++ b/src/component/Room/RoomUtils.ts @@ -0,0 +1,8 @@ +// #region Interface + +export interface RoomConnData { + state: number + card?: number +} + +// #endregion \ No newline at end of file