From e1928f26584e7553aa6cdd22a302f8eaed569bd0 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 15 Jan 2021 19:15:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA=E7=9A=84=E6=98=BE=E7=A4=BA=E8=AE=A1=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/GConfig.ts | 2 ++ src/constants/BaseConst.ts | 1 + src/global.d.ts | 1 + src/rooms/GeneralRoom.ts | 7 ++++++- src/rooms/RankedLobbyRoom.ts | 21 ++++++++++++++++++--- src/rooms/commands/GameResultCommand.ts | 1 + src/rooms/commands/OnJoinCommand.ts | 16 +++++++++++++++- src/rooms/schema/Player.ts | 6 ++++++ src/utils/number.util.ts | 10 ++++++++++ 9 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 src/utils/number.util.ts diff --git a/src/common/GConfig.ts b/src/common/GConfig.ts index 2a55b66..ca2f4af 100644 --- a/src/common/GConfig.ts +++ b/src/common/GConfig.ts @@ -7,6 +7,7 @@ import {SystemCardCfg} from "../cfg/parsers/SystemCardCfg"; import {UnitCfg} from "../cfg/parsers/UnitCfg"; import {BaseConst} from "../constants/BaseConst"; import SkillMan from "../rooms/logic/skill/SkillMan"; +import {FormulaCfg} from "../cfg/parsers/FormulaCfg"; export function initData() { @@ -17,6 +18,7 @@ export function initData() { rP(BaseConst.SKILL, SkillCfg); rP(BaseConst.SYSTEMCARD, SystemCardCfg); rP(BaseConst.UNIT, UnitCfg); + rP(BaseConst.FORMULA, FormulaCfg); DataParser.loadAll(); let map = global.$cfg.get(BaseConst.SKILL); diff --git a/src/constants/BaseConst.ts b/src/constants/BaseConst.ts index 902f562..dd64fdb 100644 --- a/src/constants/BaseConst.ts +++ b/src/constants/BaseConst.ts @@ -67,4 +67,5 @@ export class BaseConst { public static readonly SKILL = "skill"; public static readonly SYSTEMCARD = "systemcard"; public static readonly UNIT = "unit"; + public static readonly FORMULA = "formula" } diff --git a/src/global.d.ts b/src/global.d.ts index 118ee07..c26ba9e 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -32,6 +32,7 @@ declare module "colyseus" { mainClock: Delayed; robotCount: number; match: boolean; + score: number; /** * 根据sessionId获取client * @param player 玩家id或者玩家的对象 diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index f7f30d6..34bbbe2 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -25,6 +25,7 @@ import {createRobot} from "../common/WebApi"; export class GeneralRoom extends Room { dispatcher = new Dispatcher(this); maxClients = 4; + score = 0; battleMan = new BattleHandler(); // 用于游戏过程中各种计时器, 使用该计时器的前提是, 只针对当前操作玩家 gameClock: Map = new Map(); @@ -50,6 +51,9 @@ export class GeneralRoom extends Room { if (options.match) { this.match = options.match; } + if (options.score) { + this.score = options.score; + } this.battleMan.init(cs, this); this.clock.start(); this.state.gameState = GameStateConst.STATE_WAIT_JOIN; @@ -113,7 +117,8 @@ export class GeneralRoom extends Room { let data = { client: client, accountId: options.accountid, - seat: options.seat + seat: options.seat, + score: options.score, }; this.dispatcher.dispatch(new OnJoinCommand(), data); } diff --git a/src/rooms/RankedLobbyRoom.ts b/src/rooms/RankedLobbyRoom.ts index 709c05e..5fabc06 100644 --- a/src/rooms/RankedLobbyRoom.ts +++ b/src/rooms/RankedLobbyRoom.ts @@ -1,4 +1,5 @@ import {Client, generateId, matchMaker, Room} from "colyseus"; +import {BaseConst} from "../constants/BaseConst"; interface MatchmakingGroup { averageRank: number; @@ -109,6 +110,7 @@ export class RankedLobbyRoom extends Room { * 如果找到的记录, clients已经查过2条, 则也插入一条记录 * 如果找到的记录, clients是1, 则把当前client添加到该记录中, 同时更新rank */ + const fc = global.$cfg.get(BaseConst.FORMULA); if (options.group) { let length = this.stats.length; let groupData; @@ -142,7 +144,7 @@ export class RankedLobbyRoom extends Room { }); } else { groupData.clients.set(client.sessionId, {client, options}); - groupData.rank = (groupData.rank + options.rank) / 2 + groupData.rank = (groupData.rank + options.rank) / 2 * (1 + fc.get(70027).number / 100); } } client.send("clients", groupData.clients.size); @@ -299,11 +301,24 @@ export class RankedLobbyRoom extends Room { .map(async (group) => { if (group.ready) { group.confirmed = 0; - + let score = 0; + let count = 0; + group.clients.map(client => { + for (let [,data] of client.clients) { + score += (data.options?.score || 0); + count ++; + } + }) + let avaScore = score / count; /** * Create room instance in the server. */ - const room = await matchMaker.createRoom(this.roomToCreate, {rank: true, count: this.numClientsToMatch - group.count}); + const room = await matchMaker.createRoom(this.roomToCreate, { + match: true, + rank: group.averageRank, + score: avaScore, + count: this.numClientsToMatch - group.count + }); // TODO: 预处理数据, 确定座次 let hasGroup = false; for (let client of group.clients) { diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index 7eed480..765c085 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -201,6 +201,7 @@ export class GameResultCommand extends Command { team: player.team, heroid: player.heroId, statdata: dataObj, + score: player.score, cards: cards }); } diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index cd48ce1..75e1a12 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -4,6 +4,8 @@ import {Player} from "../schema/Player"; import {Client} from "colyseus"; import {GameStateConst} from "../../constants/GameStateConst"; import {GameEnv} from "../../cfg/GameEnv"; +import {BaseConst} from "../../constants/BaseConst"; +import {getRandom} from "../../utils/number.util"; /** * 玩家成功加入房间 @@ -12,13 +14,16 @@ export class OnJoinCommand extends Command { - execute({client, accountId, seat} = this.payload) { + execute({client, accountId, seat, score} = this.payload) { let count = this.state.players.size; if (count >= this.room.maxClients) { return; } + let isRobot = false; if (accountId && accountId == 'robot') { + isRobot = true; accountId = `robot_${client.sessionId}`; } else if (!accountId) { accountId = `player_${client.sessionId}`; @@ -32,6 +37,15 @@ export class OnJoinCommand extends Command Date: Mon, 18 Jan 2021 15:58:29 +0800 Subject: [PATCH 2/2] =?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=9A=84=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rooms/commands/GameResultCommand.ts | 148 ++++++++++++++++-------- 1 file changed, 100 insertions(+), 48 deletions(-) diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index 765c085..fb1a35d 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -10,8 +10,28 @@ import {Card} from "../schema/Card"; import {MapSchema, SetSchema} from "@colyseus/schema"; import {StateTypeEnum} from "../enums/StateTypeEnum"; import {reportGameResult} from "../../common/WebApi"; +import {BaseConst} from "../../constants/BaseConst"; +import {Player} from "../schema/Player"; +class GameResult{ + public id: string + public team: number + public hp: number + public alive: boolean + public ap: number + public mvpScore: number + public scoreChange: number + public scoreOld: number + constructor(player: Player) { + this.id = player.id; + this.team = player.team; + this.hp = player.hp; + this.alive = player.state != PlayerStateConst.PLAYER_DEAD; + this.ap = gameUtil.calcTotalAp(player); + this.scoreOld = player.score; + } +} /** * 游戏结束 */ @@ -19,67 +39,80 @@ export class GameResultCommand extends Command { async execute() { // 计算最终结果 - let hp0 = 0; - let hp1 = 0; - let ap0 = 0; - let ap1 = 0; - let petAp0 = 0; - let petAp1 = 0; - let resultArr = []; + let hp: number[] = [0, 0]; + let ap: number[] = [0, 0]; + let petAp: number[] = [0, 0]; + let score: number[] = [0, 0]; + let results: Map = new Map(); + const fc = global.$cfg.get(BaseConst.FORMULA); for (let [, player] of this.state.players) { - let data = { - id: player.id, - team: player.team, - hp: player.hp, - alive: player.state != PlayerStateConst.PLAYER_DEAD, - ap: gameUtil.calcTotalAp(player) - }; - resultArr.push(data); - if (player.team == 0) { - hp0 += player.hp; - if (player.state != PlayerStateConst.PLAYER_DEAD) { - ap0 += data.ap; - for (let [,pet] of player.pets) { - petAp0 += (pet.ap + pet.extAp); - } - } - } - if (player.team == 1) { - hp1 += player.hp; - if (player.state != PlayerStateConst.PLAYER_DEAD) { - ap1 += data.ap; - for (let [,pet] of player.pets) { - petAp1 += (pet.ap + pet.extAp); - } + let data = new GameResult(player); + results.set(player, data); + score[player.team] += player.score; + hp[player.team] += player.hp; + if (player.state != PlayerStateConst.PLAYER_DEAD) { + ap[player.team] += data.ap; + for (let [,pet] of player.pets) { + petAp[player.team] += (pet.ap + pet.extAp); } } } - let winner = -1; // 平局 - if (hp0 > hp1) { + /** + * 判断哪队是赢家 + * @type {number} + */ + let winner = 0; + if (hp[0] > hp[1]) { winner = 0; - } else if (hp0 < hp1) { + } else if (hp[0] < hp[1]) { winner = 1; } else { - if (ap0 > ap1) { + if (ap[0] > ap[1]) { winner = 0; - } else if (ap0 < ap1){ + } else if (ap[0] < ap[1]){ winner = 1; } else { - if (petAp0 > petAp1) { + if (petAp[0] >= petAp[1]) { winner = 0; - } else if (petAp0 < petAp1) { + } else if (petAp[0] < petAp[1]) { winner = 1; } } } - let resultData: any = { - winner: winner, - results: resultArr - }; + const scores = [ + fc.get(70043).number, + fc.get(70046).number, + fc.get(70044).number, + fc.get(70045).number, + fc.get(70047).number, + fc.get(70048).number + ]; let resultMap: Map = new Map(); + let scoreMap: Map = new Map(); for (let [, player] of this.state.players) { + let otherTeam = 1 - player.team; + let result = results.get(player); + if (winner == player.team) { + if (score[player.team] >= score[otherTeam]) { + result.scoreChange = 100/(1+Math.pow(10, ((score[player.team]-score[otherTeam])/2500))); + } else { + result.scoreChange = 100-100/(1+Math.pow(10, ((score[otherTeam]-score[player.team])/2500))); + } + } else { + if (score[player.team] >= score[otherTeam]) { + result.scoreChange = -(80-80/(1+Math.pow(10, ((score[player.team]-score[otherTeam])/2500)))); + } else { + result.scoreChange = -(80/(1+Math.pow(10, ((score[otherTeam]-score[player.team])/2500)))); + } + } + let s = 0; for (let [type, val] of player.statData) { + if (type >= scores.length) { + continue; + } + val = val * scores[type]; + s += val; if (resultMap.has(type)) { let current = resultMap.get(type); if (current[1] < val) { @@ -89,16 +122,31 @@ export class GameResultCommand extends Command { resultMap.set(type, [player.id, val]); } } + scoreMap.set(player, s); } let statics: any = []; + for (let [type, val] of resultMap) { - statics.push({ - type, - player: val[0], - val: val[1] - }) + if (type < scores.length) { + statics.push({ + type, + player: val[0], + val: val[1] + }) + } + } - resultData['statics'] = statics; + for (let [p, val] of scoreMap) { + let mvpRate = p.team == winner ? fc.get(70051).number : fc.get(70052).number; + let data = results.get(p); + data.mvpScore = val * mvpRate; // mvp分 + } + let resultData: any = { + winner: winner, + results: [...results.values()], + statics: statics + }; + let self = this; this.room.bGameResult(resultData); this.state.updateGameState(GameStateConst.STATE_GAME_OVER); @@ -179,6 +227,10 @@ export class GameResultCommand extends Command { pet.extSkills.length = 0; } + /** + * 向info-svr上报游戏结果 + * @param {number} winner + */ reportGameResult(winner: number) { let data: any = { roomid: this.room.roomId,