From 0b3df16bf57905d879bfa59ac9692352e8790d2c Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 23 Mar 2021 15:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=E7=8E=A9=E5=AE=B6?= =?UTF-8?q?=E9=80=83=E8=B7=91=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cfg/GameEnv.ts | 12 ++++++ src/common/WebApi.ts | 9 +++-- src/rooms/GeneralRoom.ts | 8 +++- src/rooms/commands/GameResultCommand.ts | 13 ++++--- src/rooms/commands/PlayLeftCommand.ts | 49 +++++++++++++++++++++++++ src/rooms/commands/PlayReadyCommand.ts | 2 +- src/rooms/schema/Player.ts | 5 +++ 7 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/rooms/commands/PlayLeftCommand.ts diff --git a/src/cfg/GameEnv.ts b/src/cfg/GameEnv.ts index e5a6fed..d76d9d6 100644 --- a/src/cfg/GameEnv.ts +++ b/src/cfg/GameEnv.ts @@ -87,6 +87,15 @@ export class GameEnv { // 等待玩家确认的最长时间 public maxWaitingTime: number + // pve模式是否允许中途退出 + public allowLeftPve: boolean + + // 休闲模式是否允许中途退出 + public allowLeftCasual: boolean + + // 匹配模式是否允许中途退出 + public allowLeftMatch: boolean + public init(data: Map) { this.initCardNum = parseInt(data.get(BaseConst.INIT_CARD_NUM).value) this.cardChangeNum = parseInt(data.get(BaseConst.CARD_CHANGE_NUM).value) @@ -146,6 +155,9 @@ export class GameEnv { +(data.get(99060)?.value || 0), ] this.maxWaitingTime = 60 + this.allowLeftPve = !!data.get(99062)?.value + this.allowLeftCasual = !!data.get(99063)?.value + this.allowLeftMatch = !!data.get(99064)?.value } /** diff --git a/src/common/WebApi.ts b/src/common/WebApi.ts index dd524b3..7187352 100644 --- a/src/common/WebApi.ts +++ b/src/common/WebApi.ts @@ -155,7 +155,7 @@ export async function createRobot(data: any) { axios.get(`${robotHost}/robot/create`, { params: data }).then((res) => { - debugRoom(`caeate robot result: `, res.data) + debugRoom(`create robot result: `, res.data) }).catch((err) => { error(err) }) @@ -165,11 +165,14 @@ export async function createRobot(data: any) { * 用户主动离开游戏 * @param {string} accountid * @param {string} roomid + * @param matchid + * @param dead + * @param scoreChange * @return {Promise} */ -export async function leftGame(accountid: string, roomid: string) { +export async function leftGame(accountid: string, roomid: string, matchid: string, dead: number, scoreChange: number = 0) { debugRoom(`player dead and left game: ${roomid}, ${accountid}`) - const data = { roomid, token: SERVER_TOKEN } + const data = { roomid, token: SERVER_TOKEN, dead, matchid, scoreChange } let dataStr = JSON.stringify(data) const infoHost = await new Service().getInfoSvr() if (!infoHost) { diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index dd5fcd1..d366a25 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -24,6 +24,7 @@ import { Service } from '../service/Service' import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand' import { RoomOptions } from '../cfg/RoomOptions' import { PlayerStateConst } from '../constants/PlayerStateConst' +import { PlayLeftCommand } from './commands/PlayLeftCommand' export class GeneralRoom extends Room { dispatcher = new Dispatcher(this) @@ -157,6 +158,11 @@ export class GeneralRoom extends Room { this.dispatcher.dispatch(new ManualTurnEndCommand(), { client }) }) + this.onMessage('player_left_c2s', (client) => { + msgLog('player_left_c2s from ', client.sessionId) + this.dispatcher.dispatch(new PlayLeftCommand(), { client }) + }) + this.onMessage('*', (client, type, message) => { // // Triggers when any other type of message is sent, @@ -194,7 +200,7 @@ export class GeneralRoom extends Room { try { // 20210310 添加, 如果该玩家已死亡, 则上报下, 清除redis中的锁定键 if (this.state.mode == 1 && !player.robot && player.state == PlayerStateConst.PLAYER_DEAD) { - await leftGame(player.accountId, this.roomId) + await leftGame(player.accountId, this.roomId, this.match, 1) } else if (consented) { throw new Error('consented leave') } else { diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index 910af3b..ee0f238 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -84,8 +84,7 @@ export class GameResultCommand extends Command { } } } - - const scores = [ + const scoresCfg = [ fc.get(70043).number, fc.get(70046).number, fc.get(70044).number, @@ -97,7 +96,8 @@ export class GameResultCommand extends Command { let scoreMap: Map = new Map() for (let [, player] of this.state.players) { let result = results.get(player) - if (winner == player.team) { + // 计算赛季排位分的改变 + if (winner == player.team && !player.escape) { if (score[player.team] == Math.max.apply(this, score)) { result.scoreChange = 100 / (1 + Math.pow(10, ((score[player.team] - Math.min.apply(this, score)) / 2500))) } else { @@ -112,11 +112,11 @@ export class GameResultCommand extends Command { } let s = 0 for (let [type, val] of player.statData) { - if (type >= scores.length) { + if (type >= scoresCfg.length) { continue } result.stat[type] = val - val = val * scores[type] + val = val * scoresCfg[type] s += val if (resultMap.has(type)) { let current = resultMap.get(type) @@ -132,7 +132,7 @@ export class GameResultCommand extends Command { let statics: any = [] for (let [type, val] of resultMap) { - if (type < scores.length) { + if (type < scoresCfg.length) { statics.push({ type, player: val[0], @@ -293,6 +293,7 @@ export class GameResultCommand extends Command { heroid: player.heroId, statdata: dataObj, score: player.score, + escape: player.escape, cards: cards, scoreChange: results.get(player).scoreChange, mvpscore: player.id == mvp ? mvpScore : 0 diff --git a/src/rooms/commands/PlayLeftCommand.ts b/src/rooms/commands/PlayLeftCommand.ts new file mode 100644 index 0000000..75dab68 --- /dev/null +++ b/src/rooms/commands/PlayLeftCommand.ts @@ -0,0 +1,49 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { PlayerStateConst } from '../../constants/PlayerStateConst' +import { Client } from 'colyseus' +import { leftGame } from '../../common/WebApi' +import { GameEnv } from '../../cfg/GameEnv' + +/** + * 玩家主动离开游戏 + */ +export class PlayLeftCommand extends Command { + + async execute({ client } = this.payload) { + const player = this.state.players.get(client.sessionId) + if (this.state.mode === 1) { + if (!new GameEnv().allowLeftMatch) { + client.send('player_left_s2c', {errcode: 11, errmsg: '当前配置不允许逃跑'}) + return + } + const dead = player.state === PlayerStateConst.PLAYER_DEAD ? 1 : 0 + if (!dead) { + player.escape = true + } + let teamSet = new Set() + for (let [, player] of this.state.players) { + teamSet.add(player.team) + } + const teamCount = teamSet.size + let score: number[] = new Array(teamCount).fill(0) + for (let [, player] of this.state.players) { + score[player.team] += player.score + } + let scoreChange + if (score[player.team] == Math.max.apply(this, score)) { + scoreChange = -(80 - 80 / (1 + Math.pow(10, ((score[player.team] - Math.min.apply(this, score)) / 2500)))) + } else { + scoreChange = -(80 / (1 + Math.pow(10, ((Math.max.apply(this, score) - score[player.team]) / 2500)))) + } + const res: any = await leftGame(player.accountId, this.room.roomId, this.room.match, dead, scoreChange) + let result = res.data + client.send('player_left_s2c', {errcode: result.errcode, + errmsg: result.errmsg, + scoreChange: + result?.data?.scoreChange, + score: result?.data?.score, + items: result?.data?.items}) + } + } +} diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index 11f5638..ea44c70 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -45,7 +45,7 @@ export class PlayReadyCommand extends Command