战力:修复创建武将bug

This commit is contained in:
luying
2022-03-28 17:44:14 +08:00
parent f35af70adc
commit 14b28921af
13 changed files with 379 additions and 393 deletions
+107 -25
View File
@@ -5,7 +5,7 @@
import { pinus } from 'pinus';
import { STATUS } from '../consts/statusCode';
import { resResult, reduceCe, calculatetopLineup } from '../pubUtils/util';
import { resResult, reduceCe } from '../pubUtils/util';
import { HeroModel, HeroType, HeroUpdate } from '../db/Hero';
import { RoleUpdate, RoleType, RoleModel } from '../db/Role';
@@ -18,8 +18,11 @@ import { RoleCeModel } from '../db/RoleCe';
import { PvpDefenseModel } from '../db/PvpDefense';
import { saveCeChangeLog } from '../pubUtils/logUtil';
import { JewelType } from '../db/Jewel';
import { SchoolModel } from '../db/School';
import { HasJustId } from 'mongoose';
interface Param {
isInitRole?: boolean,
hid?: number,
hero?: HeroType,
isUpStar?: boolean,
@@ -37,56 +40,135 @@ interface Param {
skinId?: number,
}
export async function calculateCe(type: HERO_SYSTEM_TYPE, roleId: string, serverId: number, sid: string, heroUpdate: HeroUpdate, roleUpdate: RoleUpdate, param: Param) {
export async function calculateCeWithHero(type: HERO_SYSTEM_TYPE, roleId: string, serverId: number, sid: string, hid: number, heroUpdate: HeroUpdate, param: Param = {}) {
let heroUpdates = new Map<number, HeroUpdate>();
heroUpdates.set(hid, heroUpdate);
let { heroes, curRole } = await calculateCes(type, roleId, serverId, sid, heroUpdates, {}, {}, param);
let curHero = heroes.find(cur => cur.hid == hid);
return { heroes, curHero, curRole }
}
export async function calculateWithRole(type: HERO_SYSTEM_TYPE, roleId: string, serverId: number, sid: string, roleUpdate: RoleUpdate, param: Param = {}) {
let { heroes, curRole } = await calculateCes(type, roleId, serverId, sid, new Map(), roleUpdate, {}, param);
return { heroes, curRole };
}
export async function calculateCes(type: HERO_SYSTEM_TYPE, roleId: string, serverId: number, sid: string, heroUpdates: Map<number, HeroUpdate>, roleUpdate: RoleUpdate, roleIncUpdate: RoleUpdate, param: Param = {}) {
let calCe = new CalCe();
let roleCe = await RoleCeModel.findByRoleId(roleId);
calCe.setRoleCe(roleCe);
switch (type) {
case HERO_SYSTEM_TYPE.LVUP: // 升级
case HERO_SYSTEM_TYPE.INIT:
{
calCe.setHeroLv(param.hid, heroUpdate.lv);
for(let [hid, { skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, }] of heroUpdates) {
calCe.setHeroBase(hid, skinId);
calCe.setHeroLv(hid, lv);
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
calCe.setJob(hid, job, jobStage);
}
if(param.isInitRole) {
let { title, teraphs } = roleUpdate;
calCe.setTitle(title);
calCe.setTeraph(teraphs);
}
break;
}
case HERO_SYSTEM_TYPE.STAR: // 升星
case HERO_SYSTEM_TYPE.LVUP: // 1. 升级
{
let { star, starStage, colorStar, colorStarStage } = heroUpdate;
let { hid, hero: { quality, job } } = param;
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
for(let [hid, { lv }] of heroUpdates) {
calCe.setHeroLv(hid, lv);
}
break;
}
case HERO_SYSTEM_TYPE.STAR: // 2. 升星
{
for(let [hid, { star, starStage }] of heroUpdates) {
let { hero: { quality, job, colorStar, colorStarStage }, isUpStar } = param;
await treatHeroStar(calCe, roleId, hid, star, starStage, quality, colorStar, colorStarStage, job, isUpStar);
}
break;
}
case HERO_SYSTEM_TYPE.QUALITY: // 3. 升品
{
for(let [hid, { quality }] of heroUpdates) {
let { hero: { star, starStage, colorStar, colorStarStage, job } } = param;
await treatHeroStar(calCe, roleId, hid, star, starStage, quality, colorStar, colorStarStage, job, true);
}
break;
}
case HERO_SYSTEM_TYPE.COLORSTAR: // 4. 觉醒
{
for(let [hid, { colorStar, colorStarStage }] of heroUpdates) {
let { hero: { star, starStage, quality, job }, isUpStar } = param;
await treatHeroStar(calCe, roleId, hid, star, starStage, quality, colorStar, colorStarStage, job, isUpStar);
}
break;
}
case HERO_SYSTEM_TYPE.SCHOOL: // 放百家学宫
{
let { schoolId, schoolHid, preSchoolHid, hero } = param;
if(preSchoolHid) {
calCe.setSchool(false, preSchoolHid, schoolId, 0, 0, 0);
}
if(schoolHid) {
let { star, colorStar, quality } = hero;
calCe.setSchool(true, schoolHid, schoolId, star, colorStar, quality);
}
break;
}
}
let { heroCe, roleInc } = calCe.getCeInc(); // 计算战力,获得有变化的武将战力
let changeHids: number[] = [];
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 });
let heroes: HeroType[] = [];
for(let [hid, heroUpdate] of heroUpdates) {
if(heroCe.has(hid)) {
let { ce, inc } = heroCe.get(hid);
let heroUpdate = heroUpdates.get(hid)||{};
let hero = await HeroModel.updateHeroInfo(roleId, hid, { ...heroUpdate, ce });
calCe.setResultHero(hero);
heroes.push(hero);
await PvpDefenseModel.updateCe(roleId, hid, inc); // 更新pvp防守阵战力
pushHeros.push({ hid, ce, incHeroCe: inc });
changeHids.push(hid);
} else {
let hero = await HeroModel.updateHeroInfo(roleId, param.hid, heroUpdate);
heroes.push(hero);
}
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);
for(let [hid, { ce, inc }] of heroCe) {
if(changeHids.indexOf(hid) == -1) {
let hero = await HeroModel.updateHeroInfo(roleId, hid, { ce });
pushHeros.push({ hid, ce: hero.ce, incHeroCe: inc });
changeHids.push(hid);
}
}
let { topLineup, topLineupCe } = calCe.getTopLineup();
let role = await RoleModel.incRoleInfo(roleId, { ce: roleInc }, { ...roleUpdate, topLineup, topLineupCe });
let roleCeUpdate = calCe.getRoleCeTable();
roleCe = await RoleCeModel.updateRoleCe(roleId, roleCeUpdate);
let role = await RoleModel.incRoleInfo(roleId, { ...roleIncUpdate, ce: roleInc }, { ...roleUpdate, topLineup, topLineupCe });
let guild = await GuildModel.updateCe(roleId, roleInc); // 公会更新战力
saveCeChangeLog(role, roleInc, role.ce, type, [...heroCe.keys()]);
saveCeChangeLog(role, roleInc, role.ce, type, changeHids);
updateRank(roleId, serverId, topLineupCe, role, pushHeros, guild);
let uids = [{ uid: roleId, sid }];
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 }]);
}
return { heroes, curHero, curRole: role }
return { heroes, curRole: role }
}
async function treatHeroStar(calCe: CalCe, roleId: string, hid: number, star: number, starStage: number, quality: number, colorStar: number, colorStarStage: number, job: number, isUpStar: boolean) {
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
if(isUpStar) {
let school = await SchoolModel.findByHid(roleId, hid);
if(school) calCe.setSchool(true, hid, school.schoolId, star, colorStar, quality);
}
}
// 更新排行榜数据
+95 -50
View File
@@ -1,10 +1,10 @@
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 { AttrCell, EquipAttr, HeroAttr, RoleCeType, SchoolAttr } from "../../db/RoleCe";
import { TopHero } from "../../domain/dbGeneral";
import { AttributeCal } from "../../domain/roleField/attribute";
import { gameData, getHeroStarByQuality, getHeroWakeByQuality, getTeraph } from "../../pubUtils/data";
import { gameData, getHeroStarByQuality, getHeroWakeByQuality, getSchoolRateByStar, getTeraph } from "../../pubUtils/data";
import { DicRandomEffectPool } from "../../pubUtils/dictionary/DicRandomEffectPool";
import { DicSe } from "../../pubUtils/dictionary/DicSe";
import { addToMap, deepCopy } from "../../pubUtils/util";
@@ -23,6 +23,8 @@ export class CalCe {
equipLv: Map<string, number> = new Map(); // hid+eplaceId => lv
originCes: Map<number, number> = new Map(); // hid => ce
resultCes: Map<number, number> = new Map(); // hid => ce
schoolAttrs: Map<string, { hid: number, attrId: number, ce: number }> = new Map(); // hid + attr => ratio
schoolAttrsByAttrId: Map<number, string[]> = new Map(); // attrId => hid + attr
private getGlobalAttrById(attrId: number) {
if(!this.globalAttrs.has(attrId)) {
@@ -39,6 +41,7 @@ export class CalCe {
private getHeroAttrByHidAndId(hid: number, attrId: number) {
let key = `${hid}_${attrId}`;
console.log('$$$$$$$$ getHeroAttrByHidAndId', hid, attrId)
if(!this.heroAttrs.has(key)) {
if(ABI_TYPE_MAIN.indexOf(attrId) != -1) {
let obj = new HeroMainAttr(hid, attrId);
@@ -53,6 +56,7 @@ export class CalCe {
}
this.heroAttrsByHid.get(hid).push(key);
}
console.log('$$$$$$$$ heroAttrsByHid', this.heroAttrsByHid)
return this.heroAttrs.get(key);
}
@@ -80,6 +84,30 @@ export class CalCe {
return this.equipAttrs.get(key);
}
private setSchoolRate(hid: number, attrId: number, value: number) {
let key = `${hid}_${attrId}`;
if(!this.schoolAttrs.has(key)) {
this.schoolAttrs.set(key, { hid, attrId, ce: value });
if(!this.schoolAttrsByAttrId.has(attrId)) {
this.schoolAttrsByAttrId.set(attrId, []);
}
this.schoolAttrsByAttrId.get(attrId).push(key);
} else {
this.schoolAttrs.get(key).ce = value;
}
}
private calSchoolAttrsToGlobal(attrId: number) {
let keys = this.schoolAttrsByAttrId.get(attrId)||[];
let schoolResult = 0;
for(let key of keys) {
let { ce } = this.schoolAttrs.get(key);
schoolResult += ce;
}
let globalAttr = this.getGlobalAttrById(attrId);
globalAttr.school = schoolResult;
}
public calHeroCe() {
let ces = new Map<number, { id: number, val: number }[]>(); // hid => [{attrId, val}]
for(let [hid, keys] of this.heroAttrsByHid) {
@@ -98,17 +126,20 @@ export class CalCe {
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);
let ce = obj.calCe();
this.resultCes.set(hid, ce);
result.set(hid, ce);
}
return this.resultCes;
return result;
}
public getCeInc() {
let ceResult = this.calHeroCe();
console.log('##### ceResult', ceResult, 'originCes', this.originCes)
let heroCe = new Map<number, { inc: number, origin: number, ce: number }>();
let roleInc = 0;
for(let [hid, ce] of ceResult) {
@@ -137,7 +168,7 @@ export class CalCe {
let heroAttrs: HeroAttr[] = [];
for(let [hid, keys] of this.heroAttrsByHid) {
let lv = this.heroLv.get(hid);
let historyCe = this.heroHistoryCe.get(hid);
let historyCe = this.heroHistoryCe.get(hid)||0;
let objectId = this.heroObjectId.get(hid);
let attrs: AttrCell[] = [];
for(let key of keys) {
@@ -160,12 +191,18 @@ export class CalCe {
hid, eplaceId, lv, attrs
});
}
let schoolAttrs: SchoolAttr[] = [];
for(let [_, { hid, attrId, ce }] of this.schoolAttrs) {
schoolAttrs.push({ hid, attrId, value: ce });
}
return {
roleId: this.roleId, globalAttrs, heroAttrs, equipAttrs
roleId: this.roleId, globalAttrs, heroAttrs, equipAttrs, schoolAttrs
}
}
public setRoleCe(roleCe: RoleCeType) {
console.log('&&&&&&&& setRoleCe', roleCe)
this.roleId = roleCe.roleId;
if(roleCe) {
for(let globalAttr of roleCe.globalAttrs) {
@@ -188,23 +225,14 @@ export class CalCe {
obj.setByRoleCe(cell);
}
}
for(let { hid, attrId, value } of roleCe.schoolAttrs) {
this.setSchoolRate(hid, attrId, value);
}
}
this.originCes = this.calHeroCe();
console.log('############ setRoleCe', this.originCes)
}
public setInitHero(role: RoleUpdate, heroes: HeroUpdate[]) {
this.roleId = role.roleId;
for(let { hid, skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, } of heroes) {
this.setHeroBase(hid, skinId);
this.setHeroLv(hid, lv);
this.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
this.setJob(hid, job, jobStage);
}
this.setTitle(role.title);
this.setTeraph(role.teraphs);
}
public setResultHero(hero: HeroType) {
this.heroObjectId.set(hero.hid, hero._id);
}
@@ -267,7 +295,6 @@ export class CalCe {
let heroAttr = this.getHeroAttrByHidAndId(hid, attrId);
heroAttr.starUp = value;
};
}
// 职业基础
@@ -313,6 +340,23 @@ export class CalCe {
}
}
// 百家学宫
public setSchool(isPutOn: boolean, hid: number, schoolId: number, star: number, colorStar: number, quality: number) {
let dicSchool = gameData.school.get(schoolId);
let dicSchoolRate = getSchoolRateByStar(star, colorStar, quality);
console.log('#### setSchool', star, colorStar, quality, dicSchoolRate)
for (let attrId of dicSchool.upAttribute) {
if(ABI_TYPE_MAIN.includes(attrId)) { // 主属性
this.setSchoolRate(hid, attrId, isPutOn?dicSchoolRate.mainAttrAPerent: 0);
} else {
this.setSchoolRate(hid, attrId, isPutOn?dicSchoolRate.assiAttrAddValue: 0);
}
this.calSchoolAttrsToGlobal(attrId);
}
}
// 添加技能增加的被动属性
private addSeidEffect(seidList: number[]) {
let fixUp = new Map<number, number>(), ratioUp = new Map<number, number>();
@@ -383,9 +427,10 @@ abstract class GlobalAllAttr {
abstract getValues(): number[];
public getGlobalAttr() {
let values = this.getValues().map(cur => (cur||0));
return {
attrId: this.attrId,
values: this.getValues()
values
}
}
}
@@ -417,19 +462,19 @@ class GlobalMainAttr extends GlobalAllAttr {
for(let i = GLOBAL_MAIN_ATTR_INDEX.START; i < GLOBAL_MAIN_ATTR_INDEX.END; i++) {
switch(i) {
case GLOBAL_MAIN_ATTR_INDEX.SCHOOL:
if(this.school) values[i] = this.school;
values.push(this.school);
break;
case GLOBAL_MAIN_ATTR_INDEX.TERAPH:
if(this.teraph) values[i] = this.teraph;
values.push(this.teraph);
break;
case GLOBAL_MAIN_ATTR_INDEX.TITLE:
if(this.title) values[i] = this.title;
values.push(this.title);
break;
case GLOBAL_MAIN_ATTR_INDEX.SCROLL:
if(this.scroll) values[i] = this.scroll;
values.push(this.scroll);
break;
case GLOBAL_MAIN_ATTR_INDEX.SKIN:
if(this.skin) values[i] = this.skin;
values.push(this.skin);
break;
}
}
@@ -462,16 +507,16 @@ class GlobalSubAttr extends GlobalAllAttr {
for(let i = GLOBAL_MAIN_ATTR_INDEX.START; i < GLOBAL_MAIN_ATTR_INDEX.END; i++) {
switch(i) {
case GLOBAL_MAIN_ATTR_INDEX.SCHOOL:
if(this.school) values[i] = this.school;
values.push(this.school);
break;
case GLOBAL_MAIN_ATTR_INDEX.TERAPH:
if(this.teraph) values[i] = this.teraph;
values.push(this.teraph);
break;
case GLOBAL_MAIN_ATTR_INDEX.TITLE:
if(this.title) values[i] = this.title;
values.push(this.title);
break;
case GLOBAL_MAIN_ATTR_INDEX.SKIN:
if(this.skin) values[i] = this.skin;
values.push(this.skin);
break;
}
}
@@ -543,31 +588,31 @@ class HeroMainAttr extends HeroAllAttr {
for(let i = HERO_MAIN_ATTR_INDEX.START; i < HERO_MAIN_ATTR_INDEX.END; i++) {
switch(i) {
case HERO_MAIN_ATTR_INDEX.BASE:
if(this.base) values[i] = this.base;
values.push(this.base);
break;
case HERO_MAIN_ATTR_INDEX.JOB:
if(this.job) values[i] = this.job;
values.push(this.job);
break;
case HERO_MAIN_ATTR_INDEX.STAR_UP:
if(this.starUp) values[i] = this.starUp;
values.push(this.starUp);
break;
case HERO_MAIN_ATTR_INDEX.CONNECT:
if(this.connect) values[i] = this.connect;
values.push(this.connect);
break;
case HERO_MAIN_ATTR_INDEX.FAVOUR:
if(this.favour) values[i] = this.favour;
values.push(this.favour);
break;
case HERO_MAIN_ATTR_INDEX.TALENT:
if(this.talent) values[i] = this.talent;
values.push(this.talent);
break;
case HERO_MAIN_ATTR_INDEX.EQUIP:
if(this.equip) values[i] = this.equip;
values.push(this.equip);
break;
case HERO_MAIN_ATTR_INDEX.EQUIP_UP:
if(this.equipUp) values[i] = this.equipUp;
values.push(this.equipUp);
break;
case HERO_MAIN_ATTR_INDEX.EQUIP_STONE:
if(this.equipStone) values[i] = this.equipStone;
values.push(this.equipStone);
break;
}
}
@@ -599,16 +644,16 @@ class HeroSubAttr extends HeroAllAttr {
for(let i = HERO_SUB_ATTR_INDEX.START; i < HERO_SUB_ATTR_INDEX.END; i++) {
switch(i) {
case HERO_SUB_ATTR_INDEX.BASE:
if(this.base) values[i] = this.base;
values.push(this.base);
break;
case HERO_SUB_ATTR_INDEX.JOB:
if(this.job) values[i] = this.job;
values.push(this.job);
break;
case HERO_SUB_ATTR_INDEX.TALENT:
if(this.talent) values[i] = this.talent;
values.push(this.talent);
break;
case HERO_SUB_ATTR_INDEX.EQUIP:
if(this.equip) values[i] = this.equip;
values.push(this.equip);
break;
}
}
@@ -672,22 +717,22 @@ class EquipMainAttr extends EquipAllAttr {
for(let i = EQUIP_MAIN_ATTR_INDEX.START; i < EQUIP_MAIN_ATTR_INDEX.END; i++) {
switch(i) {
case EQUIP_MAIN_ATTR_INDEX.EQUIP_QUALITY:
if(this.equipQuality) values[i] = this.equipQuality;
values.push(this.equipQuality);
break;
case EQUIP_MAIN_ATTR_INDEX.EQUIP_STRENGTH:
if(this.equipStrengthen) values[i] = this.equipStrengthen;
values.push(this.equipStrengthen);
break;
case EQUIP_MAIN_ATTR_INDEX.EQUIP_STAR:
if(this.equipStar) values[i] = this.equipStar;
values.push(this.equipStar);
break;
case EQUIP_MAIN_ATTR_INDEX.EQUIP_SUIT:
if(this.equipSuit) values[i] = this.equipSuit;
values.push(this.equipSuit);
break;
case EQUIP_MAIN_ATTR_INDEX.JEWEL:
if(this.jewel) values[i] = this.jewel;
values.push(this.jewel);
break;
case EQUIP_MAIN_ATTR_INDEX.STONE:
if(this.stone) values[i] = this.stone;
values.push(this.stone);
break;
}
}
@@ -715,10 +760,10 @@ class EquipSubAttr extends EquipAllAttr {
for(let i = EQUIP_SUB_ATTR_INDEX.START; i < EQUIP_SUB_ATTR_INDEX.END; i++) {
switch(i) {
case EQUIP_SUB_ATTR_INDEX.EQUIP_STAR:
if(this.equipStar) values[i] = this.equipStar;
values.push(this.equipStar);
break;
case EQUIP_SUB_ATTR_INDEX.JEWEL:
if(this.jewel) values[i] = this.jewel;
values.push(this.jewel);
break;
}
}
+50 -183
View File
@@ -1,188 +1,19 @@
import { FIGURE_UNLOCK_CONDITION, ITEM_CHANGE_REASON, REDIS_KEY, STATUS, TASK_TYPE, HERO_SYSTEM_TYPE, LINEUP_NUM } from "../../consts";
import { FIGURE_UNLOCK_CONDITION, ITEM_CHANGE_REASON, REDIS_KEY, STATUS, TASK_TYPE, HERO_SYSTEM_TYPE, LINEUP_NUM, COUNTER } from "../../consts";
import { SkinModel } from "../../db/Skin";
import { HeroModel, HeroSkin, HeroType, HeroUpdate } from "../../db/Hero";
import { RoleModel, RoleType, RoleUpdate } from "../../db/Role";
import { SkinUpdate } from "../../db/Skin";
import { Figure, TopHero } from "../../domain/dbGeneral";
import { TaskListReturn } from "../../domain/roleField/task";
import { GuildModel, GuildType } from "../../db/Guild";
import { PvpDefenseModel } from "../../db/PvpDefense";
import { pick } from "underscore";
import { calculatetopLineup } from "../../pubUtils/util";
import { nowSeconds } from "../../pubUtils/timeUtil";
import { saveCeChangeLog } from "../../pubUtils/logUtil";
import { getRandSingleEelm, reduceCe, resResult } from "../../pubUtils/util";
import { AttributeCal } from "../../domain/roleField/attribute";
import { CreateHeroParam, HeroParam, HeroShowParam } from "../../domain/roleField/hero";
import { pinus } from "pinus";
import { Rank } from "../rankService";
import { checkTaskInCreateHero } from "../task/taskService";
import { ItemInter, RewardInter } from "../../pubUtils/interface";
import { transPiece } from "./util";
import { getInitHeroById } from "../roleService";
import { addItems, combineFigureInfo, unlockFigure, unlockFigureWithoutSave } from "./rewardService";
import { gameData } from "../../pubUtils/data";
import { CalCe } from "./calCe";
import RoleCe, { RoleCeModel, RoleCeUpdate } from "../../db/RoleCe";
export class CreateHeroes {
private roleId: string;
private roleName: string;
private serverId: number;
private incHeroNum: number = 0;
private incRoleCe: number = 0;
private role: RoleType;
private guild: GuildType;
// 推送信息
private figureConditions: { type: FIGURE_UNLOCK_CONDITION, paramHid: number }[] = [];
private skins: SkinUpdate[] = [];
private heroes: HeroUpdate[] = [];
private roleCe: RoleCeUpdate = {};
private resultHeroes: HeroType[] = [];
private topLineup: TopHero[] = [];
private topLineupCe: number = 0;
constructor(roleId: string, roleName: string, serverId: number) {
this.roleId = roleId;
this.roleName = roleName;
this.serverId = serverId;
}
public setRole(role: RoleType) {
this.role = role;
}
public async getRole() {
if(!this.role) {
this.role = await RoleModel.findByRoleId(this.roleId);
this.topLineup = this.role.topLineup;
this.topLineupCe = this.role.topLineupCe;
}
return this.role;
}
public async createHeroes(hids: number[]) {
let { roleId, roleName } = this;
for(let hid of hids) {
let initSkin = SkinModel.getInitInfo(hid);
let initHero = HeroModel.getInitInfo(hid, { roleId, roleName, skins: [new HeroSkin(initSkin)] });
this.heroes.push(initHero);
this.skins.push(initSkin);
this.figureConditions.push({ type: FIGURE_UNLOCK_CONDITION.GET_HERO, paramHid: hid });
this.incHeroNum ++;
}
await this.calCe(); // 计算战力
await this.saveCeToDb();
}
private async calCe() {
let role = await this.getRole();
let calCe = new CalCe();
calCe.setInitHero(role, this.heroes); // 设置武将初始信息
let ces = calCe.calHeroCe(); // 战力
for(let hero of this.heroes) {
hero.ce = ces.get(hero.hid)||0;
this.incRoleCe += hero.ce;
}
this.roleCe = calCe.getRoleCeTable();
}
// 更新战力相关的各个表
private async saveCeToDb() {
let role = await this.getRole();
// 更新武将表
for(let hero of this.heroes) {
let resultHero = await HeroModel.createHero(hero);
this.resultHeroes.push(resultHero);
await calculatetopLineup(role, resultHero.hid, resultHero.ce, resultHero._id);
}
this.heroes = [];
// 更新role表
role = await RoleModel.incRoleInfo(this.roleId, { heroNum: this.incHeroNum, ce: this.incRoleCe }, { heroNumUpdatedAt: nowSeconds(), });
this.setRole(role);
// 更新roleCe表
await RoleCeModel.updateRoleCe(this.roleId, this.roleCe);
// 更新皮肤表
await SkinModel.insertSkins(this.roleId, this.roleName, this.skins)
// 更新guild表
if(role.hasGuild) {
this.guild = await GuildModel.updateCe(this.roleId, this.incRoleCe, true); // 公会更新战力
}
for(let { hid, ce } of this.resultHeroes) {
await PvpDefenseModel.updateCe(this.roleId, hid, ce); // 更新pvp防守阵战力
}
saveCeChangeLog(role, this.incRoleCe, role.ce, HERO_SYSTEM_TYPE.INIT, this.resultHeroes.map(cur => cur.hid));
}
public async updateRedisRank() {
let { serverId, roleId, resultHeroes, role } = this;
// 更新军团信息
if(this.guild) {
let r = new Rank(REDIS_KEY.GUILD_INFO, { guildCode: this.guild.code });
await r.generParamAndSet(REDIS_KEY.GUILD_INFO, { guildCode: this.guild.code }, { guild: this.guild });
}
// 武将数量
if(this.incHeroNum > 0) {
let r = new Rank(REDIS_KEY.HERO_NUM_RANK, { serverId });
await r.setRankWithRoleInfo(roleId, role.heroNum, role.heroNumUpdatedAt, role);
}
// 最强阵容
let r = new Rank(REDIS_KEY.TOP_LINEUP_RANK, { serverId });
await r.setRankWithRoleInfo(roleId, reduceCe(role.topLineupCe), 0, role);
// 最强武将
for(let hero of resultHeroes) {
let r2 = new Rank(REDIS_KEY.TOP_HERO_RANK, { serverId });
await r2.setRankWithHeroInfo(roleId, hero.hid, hero.ce, 0, hero);
let r4 = new Rank(REDIS_KEY.HERO_RANK, { serverId, hid: hero.hid });
await r4.setRankWithHeroInfo(roleId, hero.hid, hero.ce, 0, hero);
}
// 总战力
let r3 = new Rank(REDIS_KEY.SUM_CE_RANK, { serverId });
await r3.setRankWithRoleInfo(roleId, reduceCe(role.ce), 0, role);
// 更新最强五人阵容信息
let r5 = new Rank(REDIS_KEY.TOP_LINEUP_INFO, { serverId });
await r5.generParamAndSet(REDIS_KEY.TOP_LINEUP_INFO, { roleId }, { role });
}
public async pushMessage(sid: string) {
let role = await this.getRole();
let uids = [{ uid: this.roleId, sid }];
pinus.app.get('channelService').pushMessageByUids('onPlayerCeUpdate', resResult(STATUS.SUCCESS, { ce: role.ce, heros: this.resultHeroes.map(({ hid, ce }) => ({ hid, ce, incHeroCe: ce})), topLineupCe: role.topLineupCe}), uids);
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, this.getSkinPushMsg()), uids);
pinus.app.get('channelService').pushMessageByUids('onHeroUpdate', resResult(STATUS.SUCCESS, { heroes: this.getResultHeroes() }), uids);
checkTaskInCreateHero(this.serverId, this.roleId, sid, this.incHeroNum, this.resultHeroes);
await unlockFigure(sid, this.roleId, this.figureConditions);
}
private getSkinPushMsg() {
let skinPushMessages: {heros: {skins: HeroSkin[], hid: number}[], skins: {id: number, hid: number, inc: number, reason: number }[]} = { heros: [], skins:[] }; // 皮肤推送信息
for(let { hid, skins } of this.resultHeroes) {
skinPushMessages.heros.push({ hid, skins });
}
for(let { id, hid } of this.skins) {
skinPushMessages.skins.push({ id, hid, inc: 1, reason: ITEM_CHANGE_REASON.GET_HERO_UNLOCK_SKIN })
}
return skinPushMessages;
}
public getResultHeroes() {
return this.resultHeroes;
}
public getShowHeroes() {
return this.resultHeroes.map(cur => {
let hero = new HeroShowParam(cur);
return { ...hero, ce: reduceCe(cur.ce)}
});
}
}
import { addItems, combineFigureInfo, unlockFigure, } from "./rewardService";
import { CounterModel } from "../../db/Counter";
import { calculateCes } from "../playerCeService";
import { RoleUpdate } from "../../db/Role";
/**
* 创建多个武将
@@ -191,7 +22,7 @@ export class CreateHeroes {
* @param serverId
* @param heroInfo
*/
export async function createHeroes(roleId: string, roleName: string, sid: string, serverId: number, heroInfo: CreateHeroParam[]) {
export async function createHeroes(roleId: string, roleName: string, sid: string, serverId: number, heroInfo: CreateHeroParam[], initRoleInfos?: RoleUpdate) {
let hids = heroInfo.map(cur => cur.hid);
let userHeroesMap = await HeroModel.findMapByHidRange(hids, roleId);
@@ -211,24 +42,60 @@ export class CreateHeroes {
}
}
let resultHeroes: HeroType[] = [], resultItems: RewardInter[] = [], heroes: HeroShowParam[] = [];
let resultHeroes: HeroType[] = [], resultItems: RewardInter[] = [], showHeroes: HeroShowParam[] = [];
let figureConditions: { type: FIGURE_UNLOCK_CONDITION, paramHid: number }[] = [];
let incHeroNum = 0;
let skins: SkinUpdate[] = [];
if (createHids.length > 0) {
let createHero = new CreateHeroes(roleId, roleName, serverId);
await createHero.createHeroes(createHids);
await createHero.pushMessage(sid);
await createHero.updateRedisRank();
heroes = createHero.getShowHeroes();
resultHeroes = createHero.getResultHeroes();
let heroInfos = new Map<number, HeroUpdate>();
for(let hid of createHids) {
let initSkin = SkinModel.getInitInfo(hid);
skins.push(initSkin); // 初始皮肤
let seqId = await CounterModel.getNewCounter(COUNTER.HID) || -1;
let initHero = HeroModel.getInitInfo(hid, { seqId, roleId, roleName, skins: [new HeroSkin(initSkin)] });
heroInfos.set(hid, initHero);
figureConditions.push({ type: FIGURE_UNLOCK_CONDITION.GET_HERO, paramHid: hid });
incHeroNum ++;
}
await unlockFigure(sid, roleId, figureConditions);
let roleUpdata: RoleUpdate = { heroNumUpdatedAt: nowSeconds() };
if(initRoleInfos) {
roleUpdata = { ...initRoleInfos, ...roleUpdata }
}
let { heroes } = await calculateCes(HERO_SYSTEM_TYPE.INIT, roleId, serverId, sid, heroInfos, roleUpdata, { heroNum: incHeroNum });
for(let hero of heroes) {
showHeroes.push(new HeroShowParam(hero));
resultHeroes.push(hero);
}
let uids = [{ uid: roleId, sid }];
await SkinModel.insertSkins(roleId, roleName, skins);
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, getSkinPushMsg(resultHeroes, skins)), uids);
pinus.app.get('channelService').pushMessageByUids('onHeroUpdate', resResult(STATUS.SUCCESS, { heroes }), uids);
}
if (pieces.length > 0) {
let goods = await addItems(roleId, roleName, sid, pieces, ITEM_CHANGE_REASON.HERO_TRANSFER_PIECE);
resultItems = goods;
}
return { heroes, resultHeroes, goods: resultItems }
return { heroes: showHeroes, resultHeroes, goods: resultItems }
}
export async function createHero(roleId: string, roleName: string, sid: string, serverId: number, heroInfo: CreateHeroParam) {
let result = await createHeroes(roleId, roleName, sid, serverId, [heroInfo]);
return result;
}
function getSkinPushMsg(resultHeroes: HeroType[], skins: SkinUpdate[]) {
let skinPushMessages: {heros: {skins: HeroSkin[], hid: number}[], skins: {id: number, hid: number, inc: number, reason: number }[]} = { heros: [], skins:[] }; // 皮肤推送信息
for(let { hid, skins } of resultHeroes) {
skinPushMessages.heros.push({ hid, skins });
}
for(let { id, hid } of skins) {
skinPushMessages.skins.push({ id, hid, inc: 1, reason: ITEM_CHANGE_REASON.GET_HERO_UNLOCK_SKIN })
}
return skinPushMessages;
}
@@ -22,11 +22,9 @@ import { reportTAEvent, reportTAUserSet } from '../sdkService';
import { saveCoinChangeLog, saveFigureInfoLog, saveGoldChangeLog, saveItemChangeLog } from '../../pubUtils/logUtil';
import { JewelModel, JewelType, jewelUpdate, RandSe } from '../../db/Jewel';
import { updateEplaces } from '../equipService';
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';
import { calculateCeWithHero } from '../playerCeService';
@@ -71,7 +69,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
heroMap.get(jewel.hid).jewels.push(jewel);
}
}
for(let [_hid, {hero, jewels} ] of heroMap) {
for(let [hid, {hero, jewels} ] of heroMap) {
// 脱下天晶石
let update = new Map<number, Partial<EPlace>>();
for(let jewel of jewels) {
@@ -82,7 +80,7 @@ export async function handleCost(roleId: string, sid: string, goods: Array<ItemI
}
}
let { newEplace } = updateEplaces(hero.ePlace, update);
await calculateCe(HERO_SYSTEM_TYPE.EQUIP_STRENGTH, roleId, role.serverId, sid, { ePlace: newEplace }, {}, { ePlaceIds: [...update.keys()] });
await calculateCeWithHero(HERO_SYSTEM_TYPE.EQUIP_STRENGTH, roleId, role.serverId, sid, hid, { ePlace: newEplace }, { ePlaceIds: [...update.keys()] });
}
@@ -360,7 +358,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 calculateCe(HERO_SYSTEM_TYPE.ADD_SKIN, roleId, hero.serverId, sid, {}, {}, { skinId }); // 全局加成
await calculateCeWithHero(HERO_SYSTEM_TYPE.ADD_SKIN, roleId, hero.serverId, sid, skin.hid, {}, { skinId }); // 全局加成
if (hero) { // 有武将的,将皮肤链接到武将上
let curSkin = hero.skins.find(cur => cur.id == skinId);