战力:替换新方法

This commit is contained in:
陆莹
2022-03-26 19:19:51 +08:00
parent 68e4d2232d
commit f35af70adc
18 changed files with 1294 additions and 1249 deletions
@@ -1,7 +1,7 @@
import { ExpeditionPointModel } from '../db/ExpeditionPoint';
import Role, { CeAttrDataRole, RoleModel } from '../db/Role';
import { shouldRefresh, calculateSumCE, getRandSingleEelm } from '../pubUtils/util';
import { shouldRefresh, getRandSingleEelm } from '../pubUtils/util';
import { LINEUP_NUM, EXPEDITION_WAR_RECORD_STATUS } from '../consts';
import { ExpeditionWarRecordModel } from '../db/ExpeditionWarRecord';
import { CeAttrData, HeroType } from '../db/Hero';
@@ -10,6 +10,7 @@ import { getTimeFunD } from '../pubUtils/timeUtil';
import { ExpeditionRecordModel } from '../db/ExpeditionRecord';
import { Attribute, AttributeCal } from '../domain/roleField/attribute';
import * as dicParam from '../pubUtils/dicParam';
import { getSumCe } from './playerCeService';
/**
* 获取远征关卡列表
@@ -24,7 +25,7 @@ export async function getExpeditionStatus(roleId: string, roleName: string) {
let expeditionRecord = await ExpeditionRecordModel.getCurRecord(roleId);
if (!expeditionRecord) { // 首次新建一条记录
// 我方战力
let myCe = await calculateSumCE(roleId, 1, { num: LINEUP_NUM });
let myCe = await getSumCe(roleId, LINEUP_NUM);
expeditionRecord = await ExpeditionRecordModel.createRecord({
roleId, roleName, heroes: [], myCe
});
+90 -54
View File
@@ -5,74 +5,92 @@
import { pinus } from 'pinus';
import { STATUS } from '../consts/statusCode';
import { resResult, reduceCe } from '../pubUtils/util';
import { calPlayerCeAndSave as pubCalPlayerCeAndSave, reCalAllHeroCe } from '../pubUtils/playerCe';
import { HeroType, HeroUpdate } from '../db/Hero';
import { resResult, reduceCe, calculatetopLineup } from '../pubUtils/util';
import { HeroModel, HeroType, HeroUpdate } from '../db/Hero';
import { RoleUpdate, RoleType } from '../db/Role';
import { RoleUpdate, RoleType, RoleModel } from '../db/Role';
import { Rank } from './rankService';
import { REDIS_KEY } from '../consts';
import { HERO_SYSTEM_TYPE, REDIS_KEY } from '../consts';
import { updateUserInfo } from './redisService';
import { GuildType } from '../db/Guild';
import { GuildModel, GuildType } from '../db/Guild';
import { CalCe } from './role/calCe';
import { RoleCeModel } from '../db/RoleCe';
import { PvpDefenseModel } from '../db/PvpDefense';
import { saveCeChangeLog } from '../pubUtils/logUtil';
import { JewelType } from '../db/Jewel';
interface calPlayerReturn {
serverId: number;
role: RoleType;
guild?: GuildType;
hero?: HeroType;
pushHeros: {hid: number, ce: number, incHeroCe: number}[];
topLineupCe: number;
heros?: HeroType[]
interface Param {
hid?: number,
hero?: HeroType,
isUpStar?: boolean,
shipId?: number,
isFavourLvUp?: boolean,
ePlaceId?: number,
ePlaceIds?: number[],
oldJewel?: JewelType,
newJewel?: JewelType,
jewel?: JewelType,
teraphId?: number,
schoolId?: number,
schoolHid?: number,
preSchoolHid?: number,
skinId?: number,
}
//修改并下发战力
export async function calPlayerCeAndSave(type: number, sid: string, roleId: string, originHero: HeroType, update: HeroUpdate, args?: Array<number>, params?: any) {
let result = await pubCalPlayerCeAndSave(type, roleId, originHero, update, args, params);
return await pushCalPlayerCe(roleId, sid, result);
}
export async function calculateCe(type: HERO_SYSTEM_TYPE, roleId: string, serverId: number, sid: string, heroUpdate: HeroUpdate, roleUpdate: RoleUpdate, param: Param) {
let calCe = new CalCe();
let roleCe = await RoleCeModel.findByRoleId(roleId);
calCe.setRoleCe(roleCe);
switch (type) {
case HERO_SYSTEM_TYPE.LVUP: // 升级
{
calCe.setHeroLv(param.hid, heroUpdate.lv);
break;
}
case HERO_SYSTEM_TYPE.STAR: // 升星
{
let { star, starStage, colorStar, colorStarStage } = heroUpdate;
let { hid, hero: { quality, job } } = param;
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
break;
}
}
let { heroCe, roleInc } = calCe.getCeInc(); // 计算战力,获得有变化的武将战力
let pushHeros = new Array<{ hid: number, ce: number, incHeroCe: number }>();
let heroes: HeroType[] = [], curHero: HeroType;
for(let [ hid, { ce, inc } ] of heroCe) {
let hero: HeroType;
if(hid == param.hid) { // 更新的那一个武将
hero = await HeroModel.updateHeroInfo(roleId, hid, { ...heroUpdate, ce });
curHero = hero;
} else { // 由于百家学宫等问题牵扯到的其他武将
hero = await HeroModel.updateHeroInfo(roleId, hid, { ce });
}
calCe.setResultHero(hero);
heroes.push(hero);
await PvpDefenseModel.updateCe(roleId, hid, inc); // 更新pvp防守阵战力
pushHeros.push({ hid, ce, incHeroCe: inc })
}
if(!heroCe.has(param.hid)) {
curHero = await HeroModel.updateHeroInfo(roleId, param.hid, heroUpdate);
}
let { topLineup, topLineupCe } = calCe.getTopLineup();
let role = await RoleModel.incRoleInfo(roleId, { ce: roleInc }, { ...roleUpdate, topLineup, topLineupCe });
let guild = await GuildModel.updateCe(roleId, roleInc); // 公会更新战力
saveCeChangeLog(role, roleInc, role.ce, type, [...heroCe.keys()]);
updateRank(roleId, serverId, topLineupCe, role, pushHeros, guild);
// 修改后战力的推送
export async function pushCalPlayerCe(roleId: string, sid: string, calResult: calPlayerReturn) {
let {role, pushHeros, topLineupCe, hero, guild, serverId} = calResult;
// console.log(JSON.stringify(pushHeros))
//下发战力
let uids = [{ uid: roleId, sid }];
pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: reduceCe(role.ce) , heros: pushHeros, topLineupCe: reduceCe(topLineupCe) }), uids);
pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: role.ce, heros: pushHeros, topLineupCe }), uids);
if(guild) {
await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
}
updateRank(roleId, serverId, topLineupCe, role, pushHeros);
return hero;
return { heroes, curHero, curRole: role }
}
// 修改全局战力并下发
export async function calAllHeroCe(type:number, sid: string, roleId: string, update: RoleUpdate, args?:Array<number>, params?: any) {
let result = await reCalAllHeroCe(type, roleId, update, args, params);
if(result.pushHeros.length > 0) {
return await pushCalAllHeroCe(roleId, sid, result);
} else {
return result;
}
}
// 修改全局战力的推送
export async function pushCalAllHeroCe(roleId: string, sid: string, calResult: calPlayerReturn) {
let {role, pushHeros, topLineupCe, guild, serverId } = calResult;
let uids = [{ uid: roleId, sid }];
pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: reduceCe(role.ce), heros: pushHeros, topLineupCe: reduceCe(topLineupCe) }), uids);
if(guild) {
await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
}
updateRank(roleId, serverId, topLineupCe, role, pushHeros);
return calResult;
}
// 更新排行榜数据
async function updateRank(roleId: string, serverId: number, topLineupCe: number, role: RoleType, pushHeros: {hid: number, ce: number}[]) {
async function updateRank(roleId: string, serverId: number, topLineupCe: number, role: RoleType, pushHeros: {hid: number, ce: number}[], guild?: GuildType) {
// 最强阵容
let r = new Rank(REDIS_KEY.TOP_LINEUP_RANK, { serverId });
@@ -94,4 +112,22 @@ async function updateRank(roleId: string, serverId: number, topLineupCe: number,
// 更新最强五人阵容信息
let r5 = new Rank(REDIS_KEY.TOP_LINEUP_INFO, { serverId });
await r5.generParamAndSet(REDIS_KEY.TOP_LINEUP_INFO, { roleId }, { role });
if(guild) {
await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
}
}
export async function getSumCe(roleId: string, num: number) {
let roleCe = await RoleCeModel.findByRoleId(roleId);
let calCe = new CalCe();
calCe.setRoleCe(roleCe);
let arr = calCe.getResultCeArr();
let ce = 0;
for(let i = 0; i < num; i++) {
if(arr[i]) {
ce += arr[i].ce;
}
}
return ce;
}
+95 -35
View File
@@ -1,14 +1,15 @@
import { ABI_STAGE, ABI_STAGE_TO_TYPE, ABI_TYPE_MAIN, SEID_TYPE } from "../../consts";
import { HeroUpdate } from "../../db/Hero";
import { ABI_STAGE, ABI_STAGE_TO_TYPE, ABI_TYPE_MAIN, LINEUP_NUM, SEID_TYPE } from "../../consts";
import { HeroType, HeroUpdate } from "../../db/Hero";
import { RoleUpdate, Teraph } from "../../db/Role";
import { AttrCell, EquipAttr, HeroAttr, RoleCeType } from "../../db/RoleCe";
import { TopHero } from "../../domain/dbGeneral";
import { AttributeCal } from "../../domain/roleField/attribute";
import { gameData, getHeroStarByQuality, getHeroWakeByQuality, getTeraph } from "../../pubUtils/data";
import { DicRandomEffectPool } from "../../pubUtils/dictionary/DicRandomEffectPool";
import { DicSe } from "../../pubUtils/dictionary/DicSe";
import { addToMap, deepCopy } from "../../pubUtils/util";
class RoleCe {
export class CalCe {
roleId: string;
globalAttrs: Map<number, GlobalMainAttr|GlobalSubAttr> = new Map(); // attrId => GlobalAttr
heroAttrs: Map<string, HeroMainAttr|HeroSubAttr> = new Map(); // hid+attrId => HeroAttr
@@ -17,31 +18,13 @@ class RoleCe {
equipAttrsByHid: Map<number, string[]> = new Map(); // hid => [hid+eplaceId+attrId]
equipAttrsByHidAndEplace: Map<string, { hid: number, eplaceId: number, keys: string[]}> = new Map(); // hid+eplaceId =>+eplaceId+attrId]
heroLv: Map<number, number> = new Map();
heroHistoryCe: Map<number, number> = new Map();
heroObjectId: Map<number, string> = new Map();
equipLv: Map<string, number> = new Map(); // hid+eplaceId => lv
setRoleCe(roleCe: RoleCeType) {
this.roleId = roleCe.roleId;
for(let globalAttr of roleCe.globalAttrs) {
let obj = this.getGlobalAttrById(globalAttr.attrId);
obj.setByRoleCe(globalAttr);
}
for(let {hid, lv, attrs} of roleCe.heroAttrs) {
this.heroLv.set(hid, lv);
for(let cell of attrs) {
let obj = this.getHeroAttrByHidAndId(hid, cell.attrId);
obj.setByRoleCe(cell);
}
}
for(let { hid, eplaceId, lv, attrs } of roleCe.equipAttrs) {
this.equipLv.set(`${hid}_${eplaceId}`, lv);
for(let cell of attrs) {
let obj = this.getEquipAttrByHidAndId(hid, eplaceId, cell.attrId);
obj.setByRoleCe(cell);
}
}
}
originCes: Map<number, number> = new Map(); // hid => ce
resultCes: Map<number, number> = new Map(); // hid => ce
public getGlobalAttrById(attrId: number) {
private getGlobalAttrById(attrId: number) {
if(!this.globalAttrs.has(attrId)) {
if(ABI_TYPE_MAIN.indexOf(attrId) != -1) {
let obj = new GlobalMainAttr(attrId);
@@ -54,7 +37,7 @@ class RoleCe {
return this.globalAttrs.get(attrId);
}
public getHeroAttrByHidAndId(hid: number, attrId: number) {
private getHeroAttrByHidAndId(hid: number, attrId: number) {
let key = `${hid}_${attrId}`;
if(!this.heroAttrs.has(key)) {
if(ABI_TYPE_MAIN.indexOf(attrId) != -1) {
@@ -73,7 +56,7 @@ class RoleCe {
return this.heroAttrs.get(key);
}
public getEquipAttrByHidAndId(hid: number, eplaceId: number, attrId: number) {
private getEquipAttrByHidAndId(hid: number, eplaceId: number, attrId: number) {
let key = `${hid}_${eplaceId}_${attrId}`;
if(!this.equipAttrs.has(key)) {
if(ABI_TYPE_MAIN.indexOf(attrId) != -1) {
@@ -115,13 +98,35 @@ class RoleCe {
ces.get(hid).push({ id: attrId, val });
}
}
let result = new Map<number, number>();
for(let [hid, arr] of ces) {
let obj = new AttributeCal();
obj.setByWarJson(arr);
result.set(hid, obj.calCe());
let ce = obj.calCe();
this.resultCes.set(hid, ce);
}
return this.resultCes;
}
public getCeInc() {
let ceResult = this.calHeroCe();
let heroCe = new Map<number, { inc: number, origin: number, ce: number }>();
let roleInc = 0;
for(let [hid, ce] of ceResult) {
let originCe = this.originCes.get(hid)||0;
if(ce != originCe) {
heroCe.set(hid, { inc: ce - originCe, origin: originCe, ce });
roleInc += ce - originCe;
this.setHistoryCe(hid, ce);
}
}
return { heroCe, roleInc }
}
private setHistoryCe(hid: number, ce: number) {
let historyCe = this.heroHistoryCe.get(hid)||0;
if(ce > historyCe) {
this.heroHistoryCe.set(hid, ce);
}
return result;
}
public getRoleCeTable() {
@@ -132,13 +137,15 @@ class RoleCe {
let heroAttrs: HeroAttr[] = [];
for(let [hid, keys] of this.heroAttrsByHid) {
let lv = this.heroLv.get(hid);
let historyCe = this.heroHistoryCe.get(hid);
let objectId = this.heroObjectId.get(hid);
let attrs: AttrCell[] = [];
for(let key of keys) {
let heroAttr = this.heroAttrs.get(key);
attrs.push(heroAttr.getHeroAttrCell());
}
heroAttrs.push({
hid, lv, attrs
hid, lv, attrs, historyCe, objectId
});
}
let equipAttrs: EquipAttr[] = [];
@@ -157,10 +164,34 @@ class RoleCe {
roleId: this.roleId, globalAttrs, heroAttrs, equipAttrs
}
}
}
// 计算函数
export class CalCe extends RoleCe {
public setRoleCe(roleCe: RoleCeType) {
this.roleId = roleCe.roleId;
if(roleCe) {
for(let globalAttr of roleCe.globalAttrs) {
let obj = this.getGlobalAttrById(globalAttr.attrId);
obj.setByRoleCe(globalAttr);
}
for(let {hid, lv, attrs, historyCe, objectId } of roleCe.heroAttrs) {
this.heroLv.set(hid, lv);
this.heroHistoryCe.set(hid, historyCe);
this.heroObjectId.set(hid, objectId);
for(let cell of attrs) {
let obj = this.getHeroAttrByHidAndId(hid, cell.attrId);
obj.setByRoleCe(cell);
}
}
for(let { hid, eplaceId, lv, attrs } of roleCe.equipAttrs) {
this.equipLv.set(`${hid}_${eplaceId}`, lv);
for(let cell of attrs) {
let obj = this.getEquipAttrByHidAndId(hid, eplaceId, cell.attrId);
obj.setByRoleCe(cell);
}
}
}
this.originCes = this.calHeroCe();
}
public setInitHero(role: RoleUpdate, heroes: HeroUpdate[]) {
this.roleId = role.roleId;
for(let { hid, skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, } of heroes) {
@@ -173,6 +204,35 @@ export class CalCe extends RoleCe {
this.setTeraph(role.teraphs);
}
public setResultHero(hero: HeroType) {
this.heroObjectId.set(hero.hid, hero._id);
}
public getResultCeArr() {
let arr: { hid: number, ce: number }[] = [];
for(let [ hid, ce ] of this.resultCes) {
arr.push({ hid, ce });
}
arr.sort((a, b) => b.ce - a.ce);
return arr;
}
public getTopLineup() {
let topLineup: TopHero[] = [], topLineupCe = 0;
let arr = this.getResultCeArr();
for(let i = 0; i < LINEUP_NUM; i++) {
if(arr[i]) {
let { hid, ce } = arr[i];
topLineup.push({
hid, ce, hero: this.heroObjectId.get(hid)
});
topLineupCe += ce;
}
}
return { topLineup, topLineupCe };
}
// 武将基础&成长
public setHeroBase(hid: number, skinId: number) {
let dicHero = gameData.hero.get(skinId);
@@ -83,7 +83,6 @@ export class CreateHeroes {
let ces = calCe.calHeroCe(); // 战力
for(let hero of this.heroes) {
hero.ce = ces.get(hero.hid)||0;
hero.historyCe = hero.ce;
this.incRoleCe += hero.ce;
}
this.roleCe = calCe.getRoleCeTable();
@@ -2,7 +2,6 @@ import { ITID, CONSUME_TYPE, ITEM_TABLE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE, HAN
import { getDecimalCnt, getRandEelm, getRandEelmWithWeight, getRandSingleEelm, getRandValueByMinMax, resResult } from '../../pubUtils/util';
import { RoleModel, RoleType } from '../../db/Role';
import { setAp } from '../actionPointService';
import { pushCalAllHeroCe, calPlayerCeAndSave, calAllHeroCe } from '../playerCeService';
import { ItemModel, ItemType } from '../../db/Item';
import { STATUS } from '../../consts/statusCode';
import { pinus } from 'pinus';
@@ -27,6 +26,7 @@ import { checkPopUpConditionInCreateHero } from '../activity/popUpShopService';
import { CreateHeroes } from './createHero';
import { combineItems, getCoinEventProperties, getGoldEventProperties, sortItems } from './util';
import { nowSeconds } from '../../pubUtils/timeUtil';
import { calculateCe } from '../playerCeService';
@@ -82,7 +82,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
}
}
let { newEplace } = updateEplaces(hero.ePlace, update);
await calPlayerCeAndSave(HERO_SYSTEM_TYPE.EQUIP_STRENGTH, sid, roleId, hero, { ePlace: newEplace }, [...update.keys()]);
await calculateCe(HERO_SYSTEM_TYPE.EQUIP_STRENGTH, roleId, role.serverId, sid, { ePlace: newEplace }, {}, { ePlaceIds: [...update.keys()] });
}
@@ -216,8 +216,6 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
pushHeroSkinMsg(heroskins, skinInfos, uids); // 推送onHeroSkinChange
saveItemChangeLog(roleId, skinInfos, reason);
}
// 推送全局加成信息
if(calAllHeroResult) await pushCalAllHeroCe(roleId, sid, calAllHeroResult);
}
// 5. 获得头像和相框等
@@ -362,7 +360,7 @@ export async function addSkin(roleId: string, roleName: string, sid: string, ski
if(skin.hid && !hero) hero = await HeroModel.findByHidAndRole(skin.hid, roleId);
let condition = { type: FIGURE_UNLOCK_CONDITION.GET_SKIN, paramSkinId: skinId };
await unlockFigure(sid, roleId, [condition]); // 解锁头像
await calAllHeroCe(HERO_SYSTEM_TYPE.ADD_SKIN, sid, roleId, {}, [skinId]); // 全局加成
await calculateCe(HERO_SYSTEM_TYPE.ADD_SKIN, roleId, hero.serverId, sid, {}, {}, { skinId }); // 全局加成
if (hero) { // 有武将的,将皮肤链接到武将上
let curSkin = hero.skins.find(cur => cur.id == skinId);