From a577f28ceb1ba3b0850d9e90a4d83025922a4005 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 14 Jan 2021 10:13:52 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=B0=86=E4=B8=8A=E6=8A=A5=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E7=BB=93=E6=9E=9C=E7=9A=84=E6=8E=A5=E5=8F=A3=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E5=88=B0webapi=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/WebApi.ts | 27 +++++++++++++++++++++++++ src/rooms/commands/GameResultCommand.ts | 24 ++-------------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/common/WebApi.ts b/src/common/WebApi.ts index 236db53..1c95ffb 100644 --- a/src/common/WebApi.ts +++ b/src/common/WebApi.ts @@ -1,5 +1,6 @@ import axios from "axios"; import {Config} from "../cfg/Config"; +import {debugRoom, error} from "./Debug"; let config: Config = require('../../config/config.json'); @@ -33,4 +34,30 @@ export function requestUnlockHero(accountid: string, heroid: number | string) { return axios.post(`${config.info_svr}/${accountid}/hero/unlock/${heroid}`, data); } +/** + * 上报游戏结果 + * @param data + */ +export function reportGameResult(data: any) { + let dataStr = JSON.stringify(data); + + let reqConfig = { + method: 'post', + url: `${config.info_svr}/record/save`, + headers: { + 'Content-Type': 'application/json', + }, + data : dataStr + }; + + // @ts-ignore + axios(reqConfig) + .then(function (response) { + debugRoom(JSON.stringify(response.data)); + }) + .catch(function (err) { + error(err); + }); +} + diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index 760c4d3..ed8694b 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -9,11 +9,9 @@ import gameUtil from "../../utils/game.util"; import {Card} from "../schema/Card"; import {MapSchema, SetSchema} from "@colyseus/schema"; import {StateTypeEnum} from "../enums/StateTypeEnum"; -import axios from "axios"; -import {Config} from "../../cfg/Config"; +import {reportGameResult} from "../../common/WebApi"; -let config: Config = require('../../../config/config.json'); /** * 游戏结束 */ @@ -207,25 +205,7 @@ export class GameResultCommand extends Command { }); } data.players = players; - let dataStr = JSON.stringify(data); - - let reqConfig = { - method: 'post', - url: `${config.info_svr}/record/save`, - headers: { - 'Content-Type': 'application/json', - }, - data : dataStr - }; - - // @ts-ignore - axios(reqConfig) - .then(function (response) { - debugRoom(JSON.stringify(response.data)); - }) - .catch(function (err) { - error(err); - }); + reportGameResult(data); } } From 85998f3b355dbd3050a04abdf4012d545e01bc74 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 14 Jan 2021 10:58:25 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=B0=86=E5=88=9B=E5=BB=BA=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E4=BA=BA=E7=BB=9F=E4=B8=80=E5=88=B0webapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/WebApi.ts | 15 +++++++++++++++ src/rooms/GeneralRoom.ts | 35 ++--------------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/common/WebApi.ts b/src/common/WebApi.ts index 1c95ffb..4ba8039 100644 --- a/src/common/WebApi.ts +++ b/src/common/WebApi.ts @@ -60,4 +60,19 @@ export function reportGameResult(data: any) { }); } +/** + * 创建机器人 + * @param data + */ +export function createRobot(data: any) { + axios.get('http://127.0.0.1:2500/robot/create', { + params: data + }).then((res) => { + debugRoom(res.status); + debugRoom(res.data); + }).catch((err) => { + error(err); + }) +} + diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index e2bbc79..a372818 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -22,6 +22,7 @@ import {RobotClient} from "../robot/RobotClient"; import axios from 'axios'; import {wait} from "../decorators/cfg"; import {ChangePetCommand} from "./commands/ChangePetCommand"; +import {createRobot} from "../common/WebApi"; export class GeneralRoom extends Room { dispatcher = new Dispatcher(this); @@ -245,39 +246,7 @@ export class GeneralRoom extends Room { room: this.roomId, sessionId: playerId } - - axios.get('http://127.0.0.1:2500/robot/create', { - params: data - }).then((res) => { - debugRoom(res.status); - debugRoom(res.data); - }).catch((err) => { - error(err); - }) - - // const sessionId = playerId || generateId(); - // let client = new RobotClient(sessionId, this.state, this.clock, this['onMessageHandlers']); - // if (this.reservedSeatTimeouts[sessionId]) { - // clearTimeout(this.reservedSeatTimeouts[sessionId]); - // delete this.reservedSeatTimeouts[sessionId]; - // } - // // get seat reservation options and clear it - // const options = this.reservedSeats[sessionId]; - // delete this.reservedSeats[sessionId]; - // this.clients.push(client); - // client.ref.once('close', this['_onLeave'].bind(this, client)); - // // client.ref.on('message', this.onMessage.bind(this, client)); - // const reconnection = this.reconnections[sessionId]; - // if (reconnection) { - // reconnection.resolve(client); - // } - // else { - // if (this.onJoin) { - // this.onJoin(client, options); - // } - // delete this.reservedSeats[sessionId]; - // } - // this._events.emit('join', client); + createRobot(data); } addAssistClient(sessionId: string) { From 3d05eb8fc627a72542a2200e383510befe919a02 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 14 Jan 2021 11:29:31 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE=E6=97=B6,=20type?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rooms/commands/SelectHeroCommand.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rooms/commands/SelectHeroCommand.ts b/src/rooms/commands/SelectHeroCommand.ts index 9ee3c21..691ef5a 100644 --- a/src/rooms/commands/SelectHeroCommand.ts +++ b/src/rooms/commands/SelectHeroCommand.ts @@ -37,7 +37,7 @@ export class SelectHeroCommand extends Command Date: Thu, 14 Jan 2021 11:55:24 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rooms/GeneralRoom.ts | 4 +--- src/rooms/commands/GameResultCommand.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index a372818..6b47f8b 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -1,4 +1,4 @@ -import {Client, generateId, Room} from "colyseus"; +import {Client, Room} from "colyseus"; import {CardGameState} from "./schema/CardGameState"; import {OnJoinCommand} from "./commands/OnJoinCommand"; import {PlayReadyCommand} from "./commands/PlayReadyCommand"; @@ -19,8 +19,6 @@ import {GMCommand} from "./commands/GMCommand"; import {GameStateConst} from "../constants/GameStateConst"; import {GameRestartCommand} from "./commands/GameRestartCommand"; import {RobotClient} from "../robot/RobotClient"; -import axios from 'axios'; -import {wait} from "../decorators/cfg"; import {ChangePetCommand} from "./commands/ChangePetCommand"; import {createRobot} from "../common/WebApi"; diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index ed8694b..7eed480 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -98,7 +98,7 @@ export class GameResultCommand extends Command { val: val[1] }) } - resultData[statics] = statics; + resultData['statics'] = statics; let self = this; this.room.bGameResult(resultData); this.state.updateGameState(GameStateConst.STATE_GAME_OVER); From 7c5a74762fd62dc8a11b8b975c06238adf7ac76e Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 14 Jan 2021 13:25:22 +0800 Subject: [PATCH 5/5] =?UTF-8?q?player=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A0=87=E8=AE=B0=E5=BA=A7=E6=AC=A1=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.d.ts | 7 +++++++ src/rooms/GeneralRoom.ts | 19 ++++++++++++++----- src/rooms/commands/EatConfirmCommand.ts | 4 +++- src/rooms/commands/GMCommand.ts | 14 +++++--------- src/rooms/commands/NextTurnCommand.ts | 4 +++- src/rooms/commands/OnJoinCommand.ts | 6 +++--- src/rooms/commands/PlayReadyCommand.ts | 10 +++++----- src/rooms/commands/SelectHeroCommand.ts | 1 - src/rooms/schema/Player.ts | 8 ++++++-- 9 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index 4a027dd..fc5afeb 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -282,6 +282,13 @@ declare module "colyseus" { * @param srcPlayer */ getOppositePlayer(srcPlayer: string|Player): Player; + + /** + * 根据index获取玩家 + * @param {number} idx + * @return {Player} + */ + getPlayerByIdx(idx: number): Player; } } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 6b47f8b..0a41f7d 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -257,7 +257,17 @@ export class GeneralRoom extends Room { getAssistClient(sessionId: string): RobotClient { return this.assistMap.get(sessionId); } - + getPlayerByIdx(idx: number) { + for (let [, player] of this.state.players) { + if (player.idx == idx) { + return player; + } + } + } + /** + * 获取对位玩家 + * @param srcPlayer + */ getOppositePlayer(srcPlayer: string | Player): Player { let playerId; if (typeof srcPlayer === 'string') { @@ -268,9 +278,8 @@ export class GeneralRoom extends Room { if (!this.state.players.has(playerId)) { return null; } - const sessionIds = [...this.state.players.keys()]; - let idx = sessionIds.indexOf(playerId); - let playerId2 = sessionIds[(2 + idx) % 4]; - return this.state.players.get(playerId2); + let idx = this.state.players.get(playerId).idx; + let opposIdx = (2 + idx) % 4; + return this.getPlayerByIdx(opposIdx); } } diff --git a/src/rooms/commands/EatConfirmCommand.ts b/src/rooms/commands/EatConfirmCommand.ts index 4bb3a68..1268ffa 100644 --- a/src/rooms/commands/EatConfirmCommand.ts +++ b/src/rooms/commands/EatConfirmCommand.ts @@ -19,7 +19,9 @@ export class EatConfirmCommand extends Command a.idx - b.idx); + const sessionIds = players.map(p => p.id); // 将当前轮玩家移至第一位 let pids: string[] = sessionIds.moveElement(-sessionIds.indexOf(this.state.currentTurn)); diff --git a/src/rooms/commands/GMCommand.ts b/src/rooms/commands/GMCommand.ts index 4cdcdd3..b0797a7 100644 --- a/src/rooms/commands/GMCommand.ts +++ b/src/rooms/commands/GMCommand.ts @@ -45,10 +45,6 @@ export class GMCommand extends Command 2) { @@ -208,7 +204,7 @@ export class GMCommand extends Command { async execute(){ this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW); - const sessionIds = [...this.state.players.keys()]; + const players = [...this.state.players.values()]; + players.sort((a, b) => a.idx - b.idx); + const sessionIds = players.map(p => p.id); if (!this.state.currentTurn) { this.state.round = 0; } diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index bdfe9ad..aee1a82 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -13,9 +13,9 @@ export class OnJoinCommand extends Command { execute({client, accountId} = this.payload) { - let team = (this.state.players.size == 1 || this.state.players.size == 2)? 1 : 0; - // 实际的team会在PlayReadyCommand中设置 - let player = new Player(client.sessionId, 0, team); + let count = this.state.players.size; + let team = (count == 1 || count == 2)? 1 : 0; + let player = new Player(client.sessionId, count, team); if (accountId && accountId == 'robot') { accountId = `robot_${client.sessionId}`; } else if (!accountId) { diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index 6f60ceb..6df7e3b 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -30,11 +30,11 @@ export class PlayReadyCommand extends Command(); - constructor(id: string, heroId: number, team: number) { + constructor(id: string, idx: number, team: number) { super(); this.id = id; this.state = PlayerStateConst.PLAYER_NORMAL; this.hp = 0; - this.heroId = heroId; + this.idx = idx; + this.heroId = 0; this.team = team; this.countTotal = 0; this.countPresent = 0;