From 4ef6e558d2317e1bd51d144a8a0b3894183b6c65 Mon Sep 17 00:00:00 2001 From: zhangxk Date: Fri, 20 Oct 2023 20:24:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix(=E5=85=B1=E9=B8=A3):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../servers/role/handler/resonanceHandler.ts | 27 +++- .../app/services/role/resonanceService.ts | 124 ++++++------------ game-server/test/Client.ts | 2 +- game-server/test/qps.test.ts | 90 +++++++------ 4 files changed, 109 insertions(+), 134 deletions(-) diff --git a/game-server/app/servers/role/handler/resonanceHandler.ts b/game-server/app/servers/role/handler/resonanceHandler.ts index f9331ac68..61648fe09 100644 --- a/game-server/app/servers/role/handler/resonanceHandler.ts +++ b/game-server/app/servers/role/handler/resonanceHandler.ts @@ -22,24 +22,33 @@ export class HeroHandler { constructor(private app: Application) { } + /** + * 获取最新数据(会刷新) + * @param msg + * @param session + * @returns + */ async getData(msg: {}, session: BackendSession) { const roleId: string = session.get('roleId'); const sid: string = session.get('sid'); const serverId: number = session.get('serverId'); - // if (!await getStartLimt(roleId)) return resResult(STATUS.RESONANCE_NO_START); const resonances = await refreshResonanceData(roleId, serverId, sid); return resResult(STATUS.SUCCESS, resonances) } + /** + * 解锁阵位 + * @param msg + * @param session + * @returns + */ async unlockPosition(msg: { position: number }, session: BackendSession) { const { position } = msg; const roleId: string = session.get('roleId'); const sid: string = session.get('sid'); - // if (!await getStartLimt(roleId)) return resResult(STATUS.RESONANCE_NO_START); - let dbResonance = await ResonanceModel.findByPosition(roleId, position); if (dbResonance) return resResult(STATUS.RESONANCE_POSITION_LOCK); @@ -59,6 +68,12 @@ export class HeroHandler { return resResult(STATUS.SUCCESS, { position: result.position }); } + /** + * 武将上阵 + * @param msg + * @param session + * @returns + */ async heroPutPosition(msg: { position: number, hid: number }, session: BackendSession) { const { position, hid } = msg; const roleId: string = session.get('roleId'); @@ -108,6 +123,12 @@ export class HeroHandler { return resResult(STATUS.SUCCESS, resonances) } + /** + * 武将下阵 + * @param msg + * @param session + * @returns + */ async heroOffPosition(msg: { position: number, hid: number }, session: BackendSession) { const { position, hid } = msg; const roleId: string = session.get('roleId'); diff --git a/game-server/app/services/role/resonanceService.ts b/game-server/app/services/role/resonanceService.ts index 093c5d7bb..c421881a7 100644 --- a/game-server/app/services/role/resonanceService.ts +++ b/game-server/app/services/role/resonanceService.ts @@ -1,6 +1,6 @@ import { EQUIP_EPLACEID, EQUIP_STONE, FRIENDSHIP_INDEX, HERO_SYSTEM_TYPE, LINEUP_NUM, RESONANCE, RESONANCE_SORT_TYPE } from "../../consts"; import { ArtifactModel } from "../../db/Artifact"; -import { EPlace, HeroModel, HeroType, HeroUpdate } from "../../db/Hero"; +import { EPlace, HeroModel, HeroType } from "../../db/Hero"; import { JewelModel, JewelType } from "../../db/Jewel"; import { ResonanceModel, ResonanceType } from "../../db/Resonance"; import { RoleModel } from "../../db/Role"; @@ -12,35 +12,28 @@ import { getNewJob, initSkinTalent } from "../roleService"; import { pick } from 'underscore'; import * as util from 'util'; -// export async function getStartLimt(roleId: string) { -// const role = await RoleModel.findByRoleId(roleId); -// if (!role || !role.mainWarId || role.mainWarId < RESONANCE.START_MAIN_WARId) return false; -// return true; -// } - export async function refreshResonanceData(roleId: string, serverId: number, sid: string) { let resonanceDatas: ReturnResonanceParam[] = []; let resonanceLv = {}, resonanceJob = {}, resonanceCon = [], resonanceEquip = [], resonanceEpQuality = [], resonanceEpStar = [], resonanceJewel = [], resonanceStone = []; - let dbHeroes: HeroType[] = await HeroModel.findByRole(roleId, [{ field: 'ce', sortBy: -1 }]); let role = await RoleModel.findByRoleId(roleId); + let topHids = role.topLineup.map(cur => cur.hid); + if (topHids.length < LINEUP_NUM) return { resonanceDatas }; + + let { dbResonanceMap, newPositionArr, posHids } = await getResonanceDataMap(roleId); + resonanceDatas = newPositionArr; + + let dbHeroes: HeroType[] = await HeroModel.findByHidsAndRole(roleId, [...topHids, ...posHids]); + let topLineHeroes: HeroType[] = []; - for (let top of role.topLineup) { + for (const top of role.topLineup) { topLineHeroes.push(dbHeroes.find(cur => cur.hid == top.hid)); } - if (topLineHeroes.length < LINEUP_NUM || dbHeroes.length < LINEUP_NUM) return { resonanceDatas }; - - - let { dbResonanceMap, newPositionArr } = await getResonanceDataMap(roleId); - for (let position of newPositionArr) { - resonanceDatas.push({ position }); - } - // if (dbResonanceMap.size == 0) return { resonanceDatas }; let dbJewelMap = await getJewelDataMap(dbHeroes); // 武将等级 - let topLineHero: HeroType = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.LV); + let topLineHero: HeroType = sortData(topLineHeroes, RESONANCE_SORT_TYPE.LV); resonanceLv = { hid: topLineHero.hid, lv: topLineHero.lv }; for (let [hid] of dbResonanceMap) { let hero = dbHeroes.find(cur => cur.hid == hid); @@ -49,7 +42,7 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid } // 职业(职阶、天赋) - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.JOBSTAGE); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.JOBSTAGE); resonanceJob = { hid: topLineHero.hid, job: topLineHero.job, jobStage: topLineHero.jobStage }; for (let [hid] of dbResonanceMap) { let hero = dbHeroes.find(cur => cur.hid == hid); @@ -65,23 +58,15 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid // 羁绊1, 2, 3 for (let index = 1; index <= FRIENDSHIP_INDEX.THREE; index++) { - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.CONNECT, index); - + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.CONNECT, index); const topDicShipId = gameData.friendShipByIndex.get(`${topLineHero.hid}_${index}`); const topShipData = (topLineHero.connections || []).find(cur => cur.shipId == topDicShipId); resonanceCon.push({ hid: topLineHero.hid, index, level: topShipData?.level || 0 }); if (!topShipData) continue; - for (let [hid] of dbResonanceMap) { let hero = dbHeroes.find(cur => cur.hid == hid); const dicShipId = gameData.friendShipByIndex.get(`${hero.hid}_${index}`) let shipData = hero.connections.find(cur => cur.shipId == dicShipId); - - console.log('x-x-x-x-x- hid', hid) - console.log('x-x-x-x-x- dicShipId', dicShipId) - console.log('x-x-x-x-x- shipData?.shipId', shipData?.shipId) - console.log('x-x-x-x-x- shipData?.level', shipData?.level) - const dicShipData = gameData.friendShipsByLv.get(`${hid}_${dicShipId}_${topShipData?.level || 0}`); let sign = true; for (let hi of dicShipData?.hids || []) { @@ -103,9 +88,8 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid // 装备 1武器,2衣甲,3帽子,4鞋子 for (let id = 1; id <= EQUIP_EPLACEID.SHOE_ID; id++) { - //装备强化 - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_LV, id); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_LV, id); let topLineHeroEplace = topLineHero.ePlace.find(cur => cur.id == id); if (!topLineHeroEplace) resonanceEquip.push({ hid: topLineHero.hid, id, lv: 0 }); @@ -117,16 +101,15 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid let hero = dbHeroes.find(cur => cur.hid == hid); let ePlaceData = hero.ePlace.find(cur => cur.id == id); if (!ePlaceData) { - hero.ePlace.push({ ...await getInitEplace(id, hero.skinId) }); + hero.ePlace.push({ ...getInitEplace(id, hero.skinId) }); ePlaceData = hero.ePlace.find(cur => cur.id == id); } ePlaceData.lv = topLineHeroEplace.lv; } //装备升品 - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_QUALITY, id); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_QUALITY, id); topLineHeroEplace = topLineHero.ePlace.find(cur => cur.id == id); - if (!topLineHeroEplace) resonanceEpQuality.push({ hid: topLineHero.hid, id, quality: 0, qualityStage: 0 }) else @@ -137,7 +120,7 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid let hero = dbHeroes.find(cur => cur.hid == hid); let ePlaceData = hero.ePlace.find(cur => cur.id == id); if (!ePlaceData) { - hero.ePlace.push({ ...await getInitEplace(id, hero.skinId) }); + hero.ePlace.push({ ...getInitEplace(id, hero.skinId) }); ePlaceData = hero.ePlace.find(cur => cur.id == id); }; ePlaceData.quality = topLineHeroEplace.quality; @@ -145,9 +128,8 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid } //装备精练 - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_STAR, id); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.EQUIP_STAR, id); topLineHeroEplace = topLineHero.ePlace.find(cur => cur.id == id); - if (!topLineHeroEplace) resonanceEpStar.push({ hid: topLineHero.hid, id, star: 0, starStage: 0 }) else @@ -158,7 +140,7 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid let hero = dbHeroes.find(cur => cur.hid == hid); let ePlaceData = hero.ePlace.find(cur => cur.id == id); if (!ePlaceData) { - hero.ePlace.push({ ...await getInitEplace(id, hero.skinId) }); + hero.ePlace.push({ ...getInitEplace(id, hero.skinId) }); ePlaceData = hero.ePlace.find(cur => cur.id == id); }; ePlaceData.star = topLineHeroEplace.star; @@ -167,9 +149,8 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid //天晶 if (RESONANCE.JEWEL) { - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.JEWEL, id, dbJewelMap); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.JEWEL, id, dbJewelMap); topLineHeroEplace = topLineHero.ePlace.find(cur => cur.id == id); - if (!topLineHeroEplace) resonanceJewel.push({ hid: topLineHero.hid, id, jewel: 0 }); else @@ -181,7 +162,7 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid let hero = dbHeroes.find(cur => cur.hid == hid); let ePlaceData = hero.ePlace.find(cur => cur.id == id); if (!ePlaceData) { - hero.ePlace.push({ ...await getInitEplace(id, hero.skinId) }); + hero.ePlace.push({ ...getInitEplace(id, hero.skinId) }); ePlaceData = hero.ePlace.find(cur => cur.id == id); }; ePlaceData.jewel = topLineHeroEplace.jewel; @@ -193,9 +174,8 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid //破,御,护,命 1,2,3 resonanceStone.push({ id, stones: [] }); for (let index = 1; index <= EQUIP_STONE.THREE; index++) { - topLineHero = sortData(dbResonanceMap, topLineHeroes, RESONANCE_SORT_TYPE.STONE, id, null, index); + topLineHero = sortData(topLineHeroes, RESONANCE_SORT_TYPE.STONE, id, null, index); topLineHeroEplace = topLineHero.ePlace.find(cur => cur.id == id); - resonanceStone.find(cur => cur.id == id).stones.push({ hid: topLineHero.hid, id: index, stone: 0 }); if (topLineHeroEplace && topLineHeroEplace.stones) { const topLineHeroStone = topLineHeroEplace.stones.find(cur => cur.id == index); @@ -203,16 +183,14 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid resonanceStone.find(cur => cur.id == id).stones.find(cur => cur.id == index).stone = topLineHeroStone.stone; } - for (let [hid] of dbResonanceMap) { if (!topLineHeroEplace || !topLineHeroEplace.stones) continue; const topLineHeroStone = topLineHeroEplace.stones.find(cur => cur.id == index); if (!topLineHeroStone || !topLineHeroStone.stone) continue; - let hero = dbHeroes.find(cur => cur.hid == hid); let ePlaceData = hero.ePlace.find(cur => cur.id == id); if (!ePlaceData) { - hero.ePlace.push({ ...await getInitEplace(id, hero.skinId) }); + hero.ePlace.push({ ...getInitEplace(id, hero.skinId) }); ePlaceData = hero.ePlace.find(cur => cur.id == id); }; ePlaceData.stones.find(cur => cur.id == index).stone = topLineHeroStone.stone; @@ -222,33 +200,27 @@ export async function refreshResonanceData(roleId: string, serverId: number, sid } - let updateHeroes: HeroUpdate[] = [], newHeroes: HeroType[] = [], newHeroIds: number[] = []; - for (let [hid] of dbResonanceMap) { - let hero = dbHeroes.find(cur => cur.hid == hid); - updateHeroes.push({ ...pick(hero, ['roleId', 'hid', 'lv', 'exp', 'job', 'jobStage', 'connections', 'skins', 'jobStage', 'ePlace']) }) - newHeroes.push(hero); - newHeroIds.push(hid); + let newHeroes: HeroType[] = []; + for (let hero of dbHeroes) { + if (dbResonanceMap.has(hero.hid)) newHeroes.push(hero); } - await HeroModel.bulkWriteUpdate(updateHeroes) // 重新计算战力 if (newHeroes.length > 0) { - let artifacts = await ArtifactModel.findbyHids(roleId, newHeroIds); + let artifacts = await ArtifactModel.findbyHids(roleId, posHids); let { heroes } = await calculateCeWithHeroes(HERO_SYSTEM_TYPE.RESONANCE_CAL, roleId, serverId, sid, newHeroes, { jewels: [...dbJewelMap.values()], heroes: newHeroes, artifacts }); for (let hero of (heroes || [])) { const { hid } = hero; const heroResult = new HeroParam(hero); - if (!dbResonanceMap.has(hid)) continue; resonanceDatas.push({ isResonance: true, position: dbResonanceMap.get(hid).position, ...pick(heroResult, ['hid', 'seqId', 'lv', 'exp', 'job', 'jobStage', 'talent', 'usedTalentPoint', 'totalTalentPoint', 'connections', 'ePlace', 'ce']) }); } } - return { resonanceDatas, resonanceLv, resonanceJob, resonanceCon, resonanceEquip, resonanceEpQuality, resonanceEpStar, resonanceJewel, resonanceStone }; } -export function sortData(dbResonanceMap: Map, heroes: HeroType[], sortType: number, findType?: number, jewelMap?: Map, extendValue?: number) { +export function sortData(heroes: HeroType[], sortType: number, findType?: number, jewelMap?: Map, extendValue?: number) { switch (sortType) { case RESONANCE_SORT_TYPE.LV: { @@ -303,9 +275,6 @@ export function sortData(dbResonanceMap: Map, heroes: Her case RESONANCE_SORT_TYPE.JEWEL: { heroes = sortByEquipJewel(heroes, findType, jewelMap); - // if (findType == 3) { - // console.log('-x-x--x-x-x-x-x-x-x-x-x- topLineHeroes', util.inspect(heroes, { depth: null })); - // } break; } case RESONANCE_SORT_TYPE.STONE: @@ -316,15 +285,8 @@ export function sortData(dbResonanceMap: Map, heroes: Her } let topLineHeroes: HeroType[] = heroes; - // for (let hero of heroes) { - // const { hid } = hero; - // if (!dbResonanceMap.has(hid) && topLineHeroes.length < LINEUP_NUM) { - // topLineHeroes.push(hero); - // } - // } + // console.log('-x-x--x-x-x-x-x-x-x-x-x- topLineHeroes', util.inspect(heroes, { depth: null })); - - return topLineHeroes[LINEUP_NUM - 1]; } @@ -449,21 +411,6 @@ export function sortByEquipJewel(heroes: HeroType[], eplaceId: number, jewelMap: } } } - // if (eplaceId == 3) { - // console.log('-------------------------------------------------------------------'); - - // console.log('--------------------------------hid', a.hid); - // console.log('--------------------------------dicJewelALv', dicJewelALv); - // console.log('--------------------------------valA', valA); - // console.log('--------------------------------ceA', a.ce); - // console.log('-------------------------------------------------------------------'); - - // console.log('--------------------------------hid', b.hid); - // console.log('--------------------------------dicJewelBLv', dicJewelBLv); - // console.log('--------------------------------valB', valB); - // console.log('--------------------------------ceB', b.ce); - // } - if (dicJewelALv != dicJewelBLv) { return dicJewelBLv - dicJewelALv; @@ -500,13 +447,16 @@ export function sortByEquipStone(heroes: HeroType[], id: number, extendValue: nu export async function getResonanceDataMap(roleId: string) { let dbResonance = await ResonanceModel.findByRoleId(roleId); let dbResonanceMap = new Map(); - let newPositionArr: number[] = []; + let newPositionArr: ReturnResonanceParam[] = [], posHids: number[] = []; for (let obj of dbResonance) { - if (obj.hid) dbResonanceMap.set(obj.hid, obj); - else newPositionArr.push(obj.position); + if (obj.hid) { + dbResonanceMap.set(obj.hid, obj); + posHids.push(obj.hid); + } + else newPositionArr.push({ position: obj.position }); } - return { dbResonanceMap, newPositionArr }; + return { dbResonanceMap, newPositionArr, posHids }; } export async function getJewelDataMap(heroes: HeroType[]) { @@ -531,7 +481,7 @@ export async function getJewelDataMap(heroes: HeroType[]) { return dbJewelMap; } -export async function getInitEplace(id: number, skinId: number) { +export function getInitEplace(id: number, skinId: number) { const dicHero = gameData.hero.get(skinId) const dicEquip = getEquipByJobClassAndEPlace(dicHero?.jobClass, id); return new EPlace(id, dicEquip.id); diff --git a/game-server/test/Client.ts b/game-server/test/Client.ts index a50ed9e0c..d92cbd50e 100644 --- a/game-server/test/Client.ts +++ b/game-server/test/Client.ts @@ -13,7 +13,7 @@ export class Client { private ENCRYPT_KEY = 'fiqaxijabbantusmprc234fj'; private serverId = 1; - constructor(tel: string = '13636354764') { + constructor(tel: string = '18165059853') { const param = {tel, deviceId: genCode(10), code: '', platform: 'ios', pkgName: 'com.bantu.zyz', serverType: 'dev'}; const paramStr = this.aesEncrypt(JSON.stringify(param), this.ENCRYPT_KEY, this.ENCRYPT_IV); request.post('http://127.0.0.1:7001/user/smslogin', { diff --git a/game-server/test/qps.test.ts b/game-server/test/qps.test.ts index 7ffcd641c..329803e09 100644 --- a/game-server/test/qps.test.ts +++ b/game-server/test/qps.test.ts @@ -93,52 +93,51 @@ describe('共鸣', function () { // 在测试开始前记录开始时间 const startTime = Date.now(); - // it('', function (done) { - // const NUM_CONCURRENT_REQUESTS = 7; - // let totalRequests = 0; - // let successfulRequests = 0; + it('', function (done) { + const NUM_CONCURRENT_REQUESTS = 8; + let totalRequests = 0; + let successfulRequests = 0; - // function makeRequest(callback, requestNumber) { - // const timerName = `RequestTime_${requestNumber}`; - // console.time(timerName); // 开始计时 - // pinusClient.request('role.resonanceHandler.getData', {}, (result) => { - // console.timeEnd(timerName); // 结束计时并输出耗时 - // totalRequests++; - // if (result && result.code == 0) { - // successfulRequests++; - // } - // callback(); - // }); - // } + function makeRequest(callback, requestNumber) { + const timerName = `RequestTime_${requestNumber}`; + console.time(timerName); // 开始计时 + pinusClient.request('role.resonanceHandler.getData', {}, (result) => { + console.timeEnd(timerName); // 结束计时并输出耗时 + totalRequests++; + if (result && result.code == 0) { + successfulRequests++; + } + callback(); + }); + } - // async.times(NUM_CONCURRENT_REQUESTS, (n, next) => { - // makeRequest(() => { - // next(); - // }, n); - // }, (err) => { - // if (err) { - // console.error('Error in making requests:', err); - // } else { - // // 在测试结束后记录结束时间 - // const endTime = Date.now(); - // const testDuration = (endTime - startTime- 500) / 1000; // 将毫秒转换为秒 - // console.log('testDuration', endTime - startTime) - // const qps = totalRequests / testDuration; - - // console.log('Test completed.'); - // console.log('Total requests:', totalRequests); - // console.log('Successful requests:', successfulRequests); - // console.log('QPS:', qps); - // } - // done(); - // }); - // }); + async.times(NUM_CONCURRENT_REQUESTS, (n, next) => { + makeRequest(() => { + next(); + }, n); + }, (err) => { + if (err) { + console.error('Error in making requests:', err); + } else { + // 在测试结束后记录结束时间 + const endTime = Date.now(); + const testDuration = (endTime - startTime - 500) / 1000; // 将毫秒转换为秒 + console.log('testDuration', endTime - startTime - 500) + const qps = totalRequests / testDuration; + console.log('Test completed.'); + console.log('Total requests:', totalRequests); + console.log('Successful requests:', successfulRequests); + // console.log('QPS:', qps); + } + done(); + }); + }); // it('', function (done) { - // const NUM_CONCURRENT_REQUESTS = 10; - // const REQUEST_INTERVAL = 125; // 125毫秒的间隔 + // const NUM_CONCURRENT_REQUESTS = 6; + // const REQUEST_INTERVAL = 170; // 125毫秒的间隔 // let totalRequests = 0; // let successfulRequests = 0; // let currentRequest = 0; @@ -160,15 +159,20 @@ describe('共鸣', function () { // if (currentRequest < NUM_CONCURRENT_REQUESTS) { // makeRequest(() => { // currentRequest++; - // setTimeout(sendNextRequest, REQUEST_INTERVAL); // 添加间隔并继续下一个请求 // if (currentRequest === NUM_CONCURRENT_REQUESTS) { - // // 最后一个请求完成后调用 done() // done(); // } // }, currentRequest); // } // } - // sendNextRequest(); // 开始第一个请求 + // + // const requestIntervalId = setInterval(sendNextRequest, REQUEST_INTERVAL); + + // // 当所有请求完成后清除定时器 + // if (currentRequest === NUM_CONCURRENT_REQUESTS) { + // clearInterval(requestIntervalId); + // } // }); + });