[add] SendAns

This commit is contained in:
建喵 2023-09-15 11:13:21 +08:00
parent cce15b366d
commit 7f491ebddf
5 changed files with 58 additions and 9 deletions

View File

@ -14,7 +14,7 @@ export default async function (call: ApiCall<ReqJoin, ResJoin>) {
}
room.Join(conn);
conn.Room = room;
call.succ(0);
call.succ(room.ConnCount());
if (room.ConnCount() >= 2) {
room.GotoGame();

View File

@ -0,0 +1,12 @@
import { ApiCall, BaseConnection } from "tsrpc";
import Room from "../../component/Room/Room";
import { ReqSendAns, ResSendAns } from "../../shared/protocols/room/PtlSendAns";
import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqSendAns, ResSendAns>) {
const { answer } = call.req;
const conn: BaseConnection<ServiceType> = call.conn;
const room: Room = conn.Room;
const data: boolean = room.SendAns(conn, answer);
call.succ(data);
}

View File

@ -4,6 +4,7 @@ import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
import { EGameState } from "../../shared/protocols/define/enum";
import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState";
import { ServiceType } from "../../shared/protocols/serviceProto";
import { sleep } from "../../utils";
import { RoomConnData } from "./RoomUtils";
@ -67,27 +68,32 @@ export default class Room {
server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>this.conns);
}
/** ChangeState */
/**
* ChangeState
* @param needData true就是要帶connData.answer
* @param state 使connData.state ?? EGameState.Ready
* @param conn +apiNum
*/
public ChangeState(needData: boolean = false, state?: EGameState, conn?: BaseConnection): void {
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
let target: WsConnection<ServiceType>[] = [];
let data: MsgChangeState = [];
if (conn) {
if (state) {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
target = <WsConnection<ServiceType>[]>this.conns.filter((filterConn) => filterConn === key);
const apiNum: number = conn === key ? 100 : 200;
data = [state + apiNum];
} else if (conn) {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
target = <WsConnection<ServiceType>[]>this.conns.filter((filterConn) => filterConn === key);
const apiNum: number = conn === key ? 100 : 200;
data = [(connData.state ?? EGameState.Ready) + apiNum];
}
} else {
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
target = <WsConnection<ServiceType>[]>this.conns.filter((filterConn) => filterConn === key);
data = [connData.state];
}
if (needData) {
data.push(connData.card);
data.push(connData.answer);
}
server.broadcastMsg("room/ChangeState", data, target);
});
@ -105,6 +111,20 @@ export default class Room {
}
}
/** SendAns */
public SendAns(conn: BaseConnection, answer: number): boolean {
const index: number = this.conns.indexOf(conn);
const nextIndex: number = (index + 1) % this.conns.length;
const nextConn: BaseConnection = this.conns[nextIndex];
const nextData: RoomConnData = this.connData.get(nextConn);
const nextAns: number = nextData.answer;
const data: boolean = nextAns === answer;
if (data) {
this.GameOver();
}
return data;
}
//#endregion
//#region ChangeState
@ -127,7 +147,7 @@ export default class Room {
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
const card: number = RandomEx.GetInt(0, cardCount);
connData.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait;
connData.card = card;
connData.answer = card;
self.connData.set(key, connData);
});
this.ChangeState(true);
@ -159,11 +179,17 @@ export default class Room {
this.conns.push(conn);
const data: RoomConnData = {
state: undefined,
card: undefined,
answer: undefined,
};
this.connData.set(conn, data);
}
/** GameOver */
public async GameOver(): Promise<void> {
await sleep(500);
this.ChangeState(false, EGameState.GameOver);
}
/** RestartGame */
public RestartGame(): void {

View File

@ -2,7 +2,7 @@
export interface RoomConnData {
state: number
card?: number
answer?: number
}
// #endregion

11
src/utils/index.ts Normal file
View File

@ -0,0 +1,11 @@
export function sleep(ms: any): Promise<any> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function getArray(count: number): number[] {
const array: number[] = [];
for (let i: number = 0; i < count; i++) {
array.push(i);
}
return array;
}