[add] SendAns
This commit is contained in:
parent
cce15b366d
commit
7f491ebddf
@ -14,7 +14,7 @@ export default async function (call: ApiCall<ReqJoin, ResJoin>) {
|
|||||||
}
|
}
|
||||||
room.Join(conn);
|
room.Join(conn);
|
||||||
conn.Room = room;
|
conn.Room = room;
|
||||||
call.succ(0);
|
call.succ(room.ConnCount());
|
||||||
|
|
||||||
if (room.ConnCount() >= 2) {
|
if (room.ConnCount() >= 2) {
|
||||||
room.GotoGame();
|
room.GotoGame();
|
||||||
|
12
src/api/room/ApiSendAns.ts
Normal file
12
src/api/room/ApiSendAns.ts
Normal 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);
|
||||||
|
}
|
@ -4,6 +4,7 @@ import { RandomEx } from "../../Engine/Utils/Number/RandomEx";
|
|||||||
import { EGameState } from "../../shared/protocols/define/enum";
|
import { EGameState } from "../../shared/protocols/define/enum";
|
||||||
import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState";
|
import { MsgChangeState } from "../../shared/protocols/room/MsgChangeState";
|
||||||
import { ServiceType } from "../../shared/protocols/serviceProto";
|
import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||||
|
import { sleep } from "../../utils";
|
||||||
import { RoomConnData } from "./RoomUtils";
|
import { RoomConnData } from "./RoomUtils";
|
||||||
|
|
||||||
|
|
||||||
@ -67,27 +68,32 @@ export default class Room {
|
|||||||
server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>this.conns);
|
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 {
|
public ChangeState(needData: boolean = false, state?: EGameState, conn?: BaseConnection): void {
|
||||||
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
||||||
let target: WsConnection<ServiceType>[] = [];
|
let target: WsConnection<ServiceType>[] = [];
|
||||||
let data: MsgChangeState = [];
|
let data: MsgChangeState = [];
|
||||||
if (conn) {
|
if (conn) {
|
||||||
if (state) {
|
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;
|
const apiNum: number = conn === key ? 100 : 200;
|
||||||
data = [state + apiNum];
|
data = [state + apiNum];
|
||||||
} else if (conn) {
|
} 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;
|
const apiNum: number = conn === key ? 100 : 200;
|
||||||
data = [(connData.state ?? EGameState.Ready) + apiNum];
|
data = [(connData.state ?? EGameState.Ready) + apiNum];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
target = <WsConnection<ServiceType>[]>this.conns.filter((conn) => conn === key);
|
target = <WsConnection<ServiceType>[]>this.conns.filter((filterConn) => filterConn === key);
|
||||||
data = [connData.state];
|
data = [connData.state];
|
||||||
}
|
}
|
||||||
if (needData) {
|
if (needData) {
|
||||||
data.push(connData.card);
|
data.push(connData.answer);
|
||||||
}
|
}
|
||||||
server.broadcastMsg("room/ChangeState", data, target);
|
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
|
//#endregion
|
||||||
|
|
||||||
//#region ChangeState
|
//#region ChangeState
|
||||||
@ -127,7 +147,7 @@ export default class Room {
|
|||||||
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
this.connData.forEach((connData: RoomConnData, key: BaseConnection) => {
|
||||||
const card: number = RandomEx.GetInt(0, cardCount);
|
const card: number = RandomEx.GetInt(0, cardCount);
|
||||||
connData.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait;
|
connData.state = key === self.conns[now] ? EGameState.MyTurnCards : EGameState.MyWait;
|
||||||
connData.card = card;
|
connData.answer = card;
|
||||||
self.connData.set(key, connData);
|
self.connData.set(key, connData);
|
||||||
});
|
});
|
||||||
this.ChangeState(true);
|
this.ChangeState(true);
|
||||||
@ -159,11 +179,17 @@ export default class Room {
|
|||||||
this.conns.push(conn);
|
this.conns.push(conn);
|
||||||
const data: RoomConnData = {
|
const data: RoomConnData = {
|
||||||
state: undefined,
|
state: undefined,
|
||||||
card: undefined,
|
answer: undefined,
|
||||||
};
|
};
|
||||||
this.connData.set(conn, data);
|
this.connData.set(conn, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** GameOver */
|
||||||
|
public async GameOver(): Promise<void> {
|
||||||
|
await sleep(500);
|
||||||
|
this.ChangeState(false, EGameState.GameOver);
|
||||||
|
}
|
||||||
|
|
||||||
/** RestartGame */
|
/** RestartGame */
|
||||||
public RestartGame(): void {
|
public RestartGame(): void {
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
export interface RoomConnData {
|
export interface RoomConnData {
|
||||||
state: number
|
state: number
|
||||||
card?: number
|
answer?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
// #endregion
|
// #endregion
|
11
src/utils/index.ts
Normal file
11
src/utils/index.ts
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user