[add] Room OK

This commit is contained in:
建喵 2023-09-11 12:14:17 +08:00
parent 93f66106fc
commit 011667a064
4 changed files with 479 additions and 34 deletions

View File

@ -5,15 +5,18 @@
"version": "1.0.0"
},
"paths": {
"/Send": {
"/account/Login": {
"post": {
"operationId": "Send",
"tags": [
"account"
],
"operationId": "account/Login",
"requestBody": {
"description": "Req<Send>",
"description": "Req<Login>",
"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<ResSend>",
"description": "ApiReturn<ResLogin>",
"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<ChangeState>",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/room_PtlChangeState_ReqChangeState"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "ApiReturn<ResChangeState>",
"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<Create>",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/room_PtlCreate_ReqCreate"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "ApiReturn<ResCreate>",
"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<Exit>",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/room_PtlExit_ReqExit"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "ApiReturn<ResExit>",
"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<Join>",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/room_PtlJoin_ReqJoin"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "ApiReturn<ResJoin>",
"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<List>",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/room_PtlList_ReqList"
}
}
}
},
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"type": "object",
"description": "ApiReturn<ResList>",
"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"
},

View File

@ -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 <a id="/Send"></a>
## account
### Login <a id="/account/Login"></a>
**路径**
- 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 <a id="/room/ChangeState"></a>
**路径**
- POST `/room/ChangeState`
**请求**
```ts
interface ReqChangeState {
state: number
}
```
**响应**
```ts
type ResChangeState = number
```
---
### Create <a id="/room/Create"></a>
**路径**
- POST `/room/Create`
**请求**
```ts
interface ReqCreate {
null: string
}
```
**响应**
```ts
type ResCreate = number
```
---
### Exit <a id="/room/Exit"></a>
**路径**
- POST `/room/Exit`
**请求**
```ts
interface ReqExit {
null: string
}
```
**响应**
```ts
type ResExit = number
```
---
### Join <a id="/room/Join"></a>
**路径**
- POST `/room/Join`
**请求**
```ts
interface ReqJoin {
roomId: number
}
```
**响应**
```ts
type ResJoin = number
```
---
### List <a id="/room/List"></a>
**路径**
- POST `/room/List`
**请求**
```ts
interface ReqList {
null: string
}
```
**响应**
```ts
type ResList = any[]
```

View File

@ -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<BaseConnection, RoomConnData>();
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<ServiceType>[] = <WsConnection<ServiceType>[]>this.conns.filter((conn, i) => i === index);
const othersPlayer: WsConnection<ServiceType>[] = <WsConnection<ServiceType>[]>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<ServiceType>[] = <WsConnection<ServiceType>[]>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;

View File

@ -0,0 +1,8 @@
// #region Interface
export interface RoomConnData {
state: number
card?: number
}
// #endregion