From 70b286f59e7f1b4c420713c24e31eeddf7d85b5f Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 10 Mar 2021 14:55:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8E=A9=E5=AE=B6=E6=AD=BB?= =?UTF-8?q?=E4=BA=A1=E5=90=8E=E9=80=80=E5=87=BA=E6=B8=B8=E6=88=8F=E8=A7=A3?= =?UTF-8?q?=E9=94=81game=20locker=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/WebApi.ts | 27 +++++++++++++++++++++++++++ src/rooms/GeneralRoom.ts | 19 +++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/common/WebApi.ts b/src/common/WebApi.ts index 93c10fa..b798a69 100644 --- a/src/common/WebApi.ts +++ b/src/common/WebApi.ts @@ -161,4 +161,31 @@ export async function createRobot(data: any) { }) } +/** + * 用户主动离开游戏 + * @param {string} accountid + * @param {string} roomid + * @return {Promise} + */ +export async function leftGame(accountid: string, roomid: string) { + debugRoom(`player dead and left game: ${roomid}, ${accountid}`) + const data = { roomid } + let dataStr = JSON.stringify(data) + const infoHost = await new Service().getInfoSvr() + if (!infoHost) { + error('no info host found!!!') + } + let reqConfig = { + method: 'post', + url: `${ infoHost }/${ accountid }/leftgame`, + headers: { + 'Content-Type': 'application/json' + }, + data: dataStr + } + + // @ts-ignore + return axios(reqConfig) +} + diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 8588157..a67258f 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -19,10 +19,11 @@ import { GameStateConst } from '../constants/GameStateConst' import { GameRestartCommand } from './commands/GameRestartCommand' import { RobotClient } from '../robot/RobotClient' import { ChangePetCommand } from './commands/ChangePetCommand' -import { createRobot } from '../common/WebApi' +import { createRobot, leftGame } from '../common/WebApi' import { Service } from '../service/Service' import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand' import { RoomOptions } from '../cfg/RoomOptions' +import { PlayerStateConst } from '../constants/PlayerStateConst' export class GeneralRoom extends Room { dispatcher = new Dispatcher(this) @@ -186,17 +187,21 @@ export class GeneralRoom extends Room { } this.bUserLeft(client.sessionId) } else { - this.state.players.get(client.sessionId).online = false + let player = this.state.players.get(client.sessionId) + player.online = false let assistClient = this.getAssistClient(client.sessionId) assistClient.active = true try { - if (consented) { + // 20210310 添加, 如果该玩家已死亡, 则上报下, 清除redis中的锁定键 + if (this.state.mode == 1 && !player.robot && player.state == PlayerStateConst.PLAYER_DEAD) { + await leftGame(player.id, this.roomId) + } else if (consented) { throw new Error('consented leave') } else { await this.allowReconnection(client, 10 * 60) debugRoom(`${ client.sessionId } 重连`) assistClient.active = false - this.state.players.get(client.sessionId).online = true + player.online = true } } catch (e) { debugRoom(`player realy level :${ client.sessionId }, try add robot`) @@ -371,6 +376,7 @@ export class GeneralRoom extends Room { getAssistClient(sessionId: string): RobotClient { return this.assistMap.get(sessionId) } + /** * 根据指定的座次号获取玩家信息 */ @@ -400,10 +406,11 @@ export class GeneralRoom extends Room { let opposIdx = ((this.maxClients / 2 | 0) + idx) % this.maxClients return this.getPlayerByIdx(opposIdx) } + getOtherTeamPlayers(srcPlayer: string, exPlayer?: string): Player[] { let team = this.state.players.get(srcPlayer).team - let results:Player[] = [] - for (let [,player] of this.state.players) { + let results: Player[] = [] + for (let [, player] of this.state.players) { if (player.team !== team) { if (exPlayer && player.id != exPlayer) { results.push(player)