diff --git a/src/common/WebApi.ts b/src/common/WebApi.ts index 236db53..4ba8039 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,45 @@ 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); + }); +} + +/** + * 创建机器人 + * @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/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 e2bbc79..0a41f7d 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,9 +19,8 @@ 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"; export class GeneralRoom extends Room { dispatcher = new Dispatcher(this); @@ -245,39 +244,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) { @@ -290,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') { @@ -301,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 { val: val[1] }) } - resultData[statics] = statics; + resultData['statics'] = statics; let self = this; this.room.bGameResult(resultData); this.state.updateGameState(GameStateConst.STATE_GAME_OVER); @@ -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); } } diff --git a/src/rooms/commands/NextTurnCommand.ts b/src/rooms/commands/NextTurnCommand.ts index 2571956..b4fe1c9 100644 --- a/src/rooms/commands/NextTurnCommand.ts +++ b/src/rooms/commands/NextTurnCommand.ts @@ -14,7 +14,9 @@ export class NextTurnCommand 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;