Files
ZYZ/game-server/app/services/playerCeService.ts
2022-07-07 13:27:43 +08:00

440 lines
19 KiB
TypeScript

/**
* 体力系统
*/
import { EPlace, HeroModel, HeroType, HeroUpdate } from '../db/Hero';
import { RoleUpdate, RoleType, RoleModel } from '../db/Role';
import { Rank } from './rankService';
import { HERO_SYSTEM_TYPE, PUSH_ROUTE, REDIS_KEY } from '../consts';
import { updateRoleOnlineInfo, updateUserInfo } from './redisService';
import { GuildModel, GuildType } from '../db/Guild';
import { CalCe } from './role/calCe';
import { RoleCeModel, RoleCeUpdate } from '../db/RoleCe';
import { PvpDefenseModel } from '../db/PvpDefense';
import { saveCeChangeLog } from '../pubUtils/logUtil';
import { JewelType } from '../db/Jewel';
import { SchoolModel, SchoolType } from '../db/School';
import { AttributeCal } from '../domain/roleField/attribute';
import { sendMessageToUserWithSuc } from './pushService';
import { SkinType } from '../db/Skin';
interface Param {
isInitRole?: boolean,
hid?: number,
hero?: HeroType,
isUpStar?: boolean,
shipId?: number,
isFavourLvUp?: boolean,
ePlaceId?: number,
ePlaceIds?: number[],
jewels?: JewelType[],
jewel?: JewelType,
teraphId?: number,
schoolId?: number,
schoolHid?: number,
preSchoolHid?: number,
skinId?: number,
roleUpdate?: RoleUpdate,
role?: RoleType,
roleIncUpdate?: RoleUpdate;
heroes?: HeroType[],
schools?: SchoolType[];
skins?: SkinType[]
}
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.roleUpdate||{}, param.roleIncUpdate||{}, param);
let curHero = heroes.find(cur => cur.hid == hid);
return { heroes, curHero, curRole }
}
export async function calculateCeWithRole(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(roleId);
let roleCe = await RoleCeModel.findByRoleId(roleId);
calCe.setRoleCe(roleCe);
switch (type) {
case HERO_SYSTEM_TYPE.INIT:
{
for(let [hid, { skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, skins }] of heroUpdates) {
calCe.setHeroBase(hid, skinId);
calCe.setHeroLv(hid, lv);
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
calCe.setJob(hid, job, jobStage);
calCe.setTalent(hid, skins)
}
// console.log('####### roleUpdate', param.isInitRole, roleUpdate)
if(param.isInitRole) {
let { title, teraphs, lv } = roleUpdate;
calCe.setRoleLv(lv)
calCe.setTitle(title);
calCe.setTeraph(teraphs);
}
break;
}
case HERO_SYSTEM_TYPE.LVUP: // 1. 升级
{
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, { quality, colorStar, colorStarStage }] of heroUpdates) {
let { hero: { star, starStage, job }, isUpStar } = param;
await treatHeroStar(calCe, roleId, hid, star, starStage, quality, colorStar, colorStarStage, job, isUpStar);
}
break;
}
case HERO_SYSTEM_TYPE.TRAIN: // 5. 训练
case HERO_SYSTEM_TYPE.STAGEUP: // 6. 职业进阶
{
for(let [hid, { job, jobStage }] of heroUpdates) {
calCe.setJob(hid, job, jobStage);
}
break;
}
case HERO_SYSTEM_TYPE.SKIN: // 7. 穿皮肤
{
let { hero: { quality, star, starStage, colorStar, colorStarStage, jobStage } } = param;
for(let [hid, { skinId, job, ePlace, skins }] of heroUpdates) {
calCe.setHeroBase(hid, skinId);
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
calCe.setJob(hid, job, jobStage);
for(let { id, equipId, quality, qualityStage, lv, star, starStage } of ePlace) {
calCe.setEquipQuality(hid, id, equipId, quality, qualityStage);
calCe.setEquipStrength(hid, id, equipId, lv);
calCe.setEquipStar(hid, id, equipId, star, starStage);
}
calCe.setEquipSuit(hid, skinId, ePlace);
calCe.setTalent(hid, skins);
}
break;
}
case HERO_SYSTEM_TYPE.FAVOUR: // 8. 好感度
{
let { hero: { connections }, isFavourLvUp } = param;
if(isFavourLvUp) {
for(let [hid, { favourLv }] of heroUpdates) {
calCe.setFavour(hid, favourLv, connections);
}
}
break;
}
case HERO_SYSTEM_TYPE.CONNECT: // 9. 羁绊
{
for(let [ hid, { connections } ] of heroUpdates) {
calCe.setConnection(hid, connections);
}
break;
}
case HERO_SYSTEM_TYPE.ADD_SKIN: // 15. 第一次获得皮肤
{
let { skinId } = param;
calCe.setAddSkin(skinId);
break;
}
case HERO_SYSTEM_TYPE.SCHOOL: // 16. 放百家学宫
{
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;
}
case HERO_SYSTEM_TYPE.SCROLL: // 17. 名将谱
{
for(let [hid, { scrollStar, scrollQuality, scrollColorStar }] of heroUpdates) {
calCe.setScroll(hid, scrollStar, scrollQuality, scrollColorStar);
}
break;
}
case HERO_SYSTEM_TYPE.TITLE: // 18. 爵位
{
let { title } = roleUpdate;
calCe.setTitle(title);
break;
}
case HERO_SYSTEM_TYPE.TERAPH: // 19. 神像
case HERO_SYSTEM_TYPE.TERAPH_UP: // 20. 神像进阶
{
let { teraphs } = roleUpdate;
calCe.setTeraph(teraphs);
break;
}
case HERO_SYSTEM_TYPE.COMPOSE_EQUIP: // 21. 合成装备
{
let { ePlaceId, skinId } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, equipId, quality, qualityStage, lv, star, starStage } of ePlace) {
if(ePlaceId == id) {
calCe.setEquipQuality(hid, id, equipId, quality, qualityStage);
calCe.setEquipStrength(hid, id, equipId, lv);
calCe.setEquipStar(hid, id, equipId, star, starStage);
}
}
calCe.setEquipSuit(hid, skinId, ePlace);
}
break;
}
case HERO_SYSTEM_TYPE.EQUIP_STRENGTH: // 22. 装备强化
{
let { ePlaceIds } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, equipId, lv } of ePlace) {
if(ePlaceIds.indexOf(id) != -1) {
calCe.setEquipStrength(hid, id, equipId, lv);
}
}
}
break;
}
case HERO_SYSTEM_TYPE.EQUIP_QUALITY: // 23. 装备升品
{
let { ePlaceId } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, equipId, quality, qualityStage } of ePlace) {
if(ePlaceId == id) {
calCe.setEquipQuality(hid, id, equipId, quality, qualityStage);
}
}
}
break;
}
case HERO_SYSTEM_TYPE.EQUIP_STAR: // 24. 装备升星
{
let { ePlaceId, skinId } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, equipId, star, starStage } of ePlace) {
if(ePlaceId == id) {
calCe.setEquipStar(hid, id, equipId, star, starStage);
}
}
calCe.setEquipSuit(hid, skinId, ePlace);
}
break;
}
case HERO_SYSTEM_TYPE.EQUIP_JEWEL: // 25. 装备天晶
case HERO_SYSTEM_TYPE.JEWEL_RESET_RANDSE: // 27. 洗练
case HERO_SYSTEM_TYPE.JEWEL_QUENCH: // 28. 淬炼
{
let { ePlaceId, jewel: curJewel, skinId } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, stones } of ePlace) {
if(ePlaceId == id) {
calCe.setJewel(hid, id, stones, curJewel);
}
}
calCe.setEquipSuit(hid, skinId, ePlace);
}
break;
}
case HERO_SYSTEM_TYPE.EQUIP_STONE: // 26. 装备地玉
{
let { ePlaceId, jewel: curJewel, skinId } = param;
for(let [hid, { ePlace = [] }] of heroUpdates) {
for(let { id, stones } of ePlace) {
if(ePlaceId == id) {
calCe.setStone(hid, ePlaceId, stones);
calCe.setJewel(hid, id, stones, curJewel);
}
}
calCe.setEquipSuit(hid, skinId, ePlace);
}
break;
}
case HERO_SYSTEM_TYPE.REBIRTH: // 29. 重生
{
let { schoolId } = param;
for(let [hid, { skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, skins, scrollStar, scrollQuality, scrollColorStar }] of heroUpdates) {
calCe.setHeroBase(hid, skinId);
calCe.setHeroLv(hid, lv);
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
calCe.setJob(hid, job, jobStage);
calCe.clearEquip(hid);
calCe.setTalent(hid, skins);
calCe.setScroll(hid, scrollStar, scrollQuality, scrollColorStar);
if(schoolId) calCe.setSchool(true, hid, schoolId, star, colorStar, quality);
}
break;
}
case HERO_SYSTEM_TYPE.TALENT: // 30. 天赋
{
for(let [hid, { skins }] of heroUpdates) {
calCe.setTalent(hid, skins);
}
break;
}
case HERO_SYSTEM_TYPE.RE_CAL: // 31. 玩家等级
{
let { role, schools, jewels, heroes, skins } = param;
calCe.clearRoleCe();
for(let { hid, skinId, lv, quality, star, starStage, colorStar, colorStarStage, job, jobStage, connections, favourLv, skins, scrollStar, scrollQuality, scrollColorStar, ePlace } of heroes) {
calCe.setHeroBase(hid, skinId);
calCe.setHeroLv(hid, lv);
calCe.setHeroStar(hid, job, quality, star, starStage, colorStar, colorStarStage);
calCe.setJob(hid, job, jobStage);
calCe.setFavour(hid, favourLv, connections);
calCe.setConnection(hid, connections);
calCe.setTalent(hid, skins);
for(let { id, equipId, star, starStage, quality, qualityStage, lv: equipLv, stones, jewel } of ePlace) {
calCe.setEquipQuality(hid, id, equipId, quality, qualityStage);
calCe.setEquipStrength(hid, id, equipId, equipLv);
calCe.setEquipStar(hid, id, equipId, star, starStage);
let curJewel = jewels.find(cur => cur.seqId == jewel);
calCe.setJewel(hid, id, stones, curJewel);
calCe.setStone(hid, id, stones);
calCe.setTitle(role.title);
calCe.setTeraph(role.teraphs);
}
calCe.setEquipSuit(hid, skinId, ePlace);
calCe.setScroll(hid, scrollStar, scrollQuality, scrollColorStar);
}
for(let { schoolId, hid, } of schools) {
let curHero = heroes.find(cur => cur.hid == hid);
if(curHero) calCe.setSchool(true, hid, schoolId, curHero.star, curHero.colorStar, curHero.quality);
}
for(let { id } of skins) {
calCe.setAddSkin(id);
}
break;
}
}
let { heroCe, roleInc } = calCe.getCeInc(); // 计算战力,获得有变化的武将战力
let changeHids: number[] = [];
let pushHeros = new Array<{ hid: number, ce: number, incHeroCe: number }>();
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, ce); // 更新pvp防守阵战力
pushHeros.push({ hid, ce, incHeroCe: inc });
changeHids.push(hid);
} else {
let hero = await HeroModel.updateHeroInfo(roleId, hid, heroUpdate);
heroes.push(hero);
}
}
for(let [hid, { ce, inc }] of heroCe) {
if(changeHids.indexOf(hid) == -1) {
let hero = await HeroModel.updateHeroInfo(roleId, hid, { ce });
await PvpDefenseModel.updateCe(roleId, hid, ce); // 更新pvp防守阵战力
pushHeros.push({ hid, ce: hero.ce, incHeroCe: inc });
changeHids.push(hid);
}
}
let { topLineup, topLineupCe, hasTopCeChange } = calCe.getTopLineup();
let roleCeUpdate: RoleCeUpdate = calCe.getRoleCeTable();
if(topLineupCe > roleCe?.historyLineupCe??0) {
roleCeUpdate = { ...roleCeUpdate, historyLineupCe: topLineupCe };
}
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, changeHids);
updateRank(roleId, serverId, topLineupCe, role, pushHeros, guild);
sendMessageToUserWithSuc(roleId, PUSH_ROUTE.PLAYER_CE_UPDATE, { ce: role.ce, heros: pushHeros, topLineupCe }, sid);
if(hasTopCeChange) await updateRoleOnlineInfo(roleId, { topLineupCe });
if(guild) {
await updateUserInfo(REDIS_KEY.GUILD_INFO, guild.code, [{ field: 'guildCe', value: guild.guildCe }]);
}
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);
}
}
// 更新排行榜数据
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 });
await r.setRankWithRoleInfo(roleId, topLineupCe, 0, role);
// 最强武将
for(let { hid, ce } of pushHeros) {
let r2 = new Rank(REDIS_KEY.TOP_HERO_RANK, { serverId });
await r2.setRankWithHeroInfo(roleId, hid, ce, 0);
let r4 = new Rank(REDIS_KEY.HERO_RANK, { serverId, hid });
await r4.setRankWithHeroInfo(roleId, hid, ce, 0);
}
// 总战力
let r3 = new Rank(REDIS_KEY.SUM_CE_RANK, { serverId });
await r3.setRankWithRoleInfo(roleId, role.ce, 0, role);
// 更新最强五人阵容信息
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 }]);
}
// 武将数量
let r6 = new Rank(REDIS_KEY.HERO_NUM_RANK, { serverId });
await r6.setRankWithRoleInfo(roleId, role.heroNum, role.heroNumUpdatedAt, role);
}
export async function getSumCe(roleId: string) {
let roleCe = await RoleCeModel.findByRoleId(roleId);
return roleCe.historyLineupCe;
}
export async function getHeroesAttributes(roleId: string) {
let roleCe = await RoleCeModel.findByRoleId(roleId);
let attrByHid = new Map<number, AttributeCal>();
let heroAttrs = roleCe?.heroAttrs??[];
for(let { hid, attrs } of roleCe?.attributes??[]) {
let cal = new AttributeCal();
let heroAttr = heroAttrs.find(cur => cur.hid == hid);
if(heroAttr) cal.setLv(heroAttr.lv);
cal.setByWarJson(attrs);
attrByHid.set(hid, cal);
}
return attrByHid;
}