172 lines
8.6 KiB
TypeScript
172 lines
8.6 KiB
TypeScript
import { Application, BackendSession, HandlerService, } from 'pinus';
|
|
import { getInitEplace, getJewelDataMap, getResonanceDataMap, refreshResonanceData } from '../../../services/role/resonanceService';
|
|
import { resResult } from '../../../pubUtils/util'
|
|
import { HERO_SYSTEM_TYPE, ITEM_CHANGE_REASON, LINEUP_NUM, RESONANCE, STATUS } from '../../../consts';
|
|
import { ResonanceModel } from '../../../db/Resonance';
|
|
import { gameData } from '../../../pubUtils/data';
|
|
import { RoleModel } from '../../../db/Role';
|
|
import { handleCost } from '../../../services/role/rewardService';
|
|
import { HeroModel } from '../../../db/Hero';
|
|
import { ArtifactModel, ArtifactModelType } from '../../../db/Artifact';
|
|
import { calculateCeWithHero } from '../../../services/playerCeService';
|
|
import { HeroParam } from '../../../domain/roleField/hero';
|
|
import { pick } from 'underscore';
|
|
import { ResonanceHistoryModel } from '../../../db/ResonanceHistory';
|
|
|
|
export default function (app: Application) {
|
|
new HandlerService(app, {});
|
|
return new HeroHandler(app);
|
|
}
|
|
|
|
export class HeroHandler {
|
|
constructor(private app: Application) {
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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);
|
|
|
|
const dicResonance = gameData.resonance.get(position);
|
|
if (!dicResonance) return resResult(STATUS.RESONANCE_POSITION_NOT_FOUND);
|
|
|
|
const role = await RoleModel.findByRoleId(roleId);
|
|
if (!role || !role.mainWarId || role.mainWarId < (dicResonance?.openLimit || 0)) return resResult(STATUS.RESONANCE_POSITION_LV_NOT_ENOUGH);
|
|
|
|
let costResult = await handleCost(roleId, sid, dicResonance.openConsume, ITEM_CHANGE_REASON.RESONANCE_LOCK_POSITION);
|
|
if (!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
|
|
let result = await ResonanceModel.updateByPosition(roleId, position, {});
|
|
|
|
if (!result || !result.position) return resResult(STATUS.RESONANCE_POSITION_LOCK_FAIL);
|
|
|
|
return resResult(STATUS.SUCCESS, { position: result.position });
|
|
}
|
|
|
|
async heroPutPosition(msg: { position: number, hid: number }, session: BackendSession) {
|
|
const { position, hid } = msg;
|
|
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);
|
|
let { dbResonanceMap } = await getResonanceDataMap(roleId);
|
|
let dbRole = await RoleModel.findByRoleId(roleId);
|
|
if (dbResonanceMap.size + LINEUP_NUM >= (dbRole?.heroNum || 0)) return resResult(STATUS.RESONANCE_PUT_NOT_POSITION)
|
|
|
|
let dbResonance = await ResonanceModel.findByPosition(roleId, position);
|
|
if (!dbResonance) return resResult(STATUS.RESONANCE_POSITION_UNLOCK);
|
|
if (dbResonance.hid) return resResult(STATUS.RESONANCE_POSITION_EXIST_HID);
|
|
|
|
let dbHeroes = await HeroModel.findByHidAndRole(hid, roleId);
|
|
if (!dbHeroes) return resResult(STATUS.ROLE_HERO_NOT_EXISTS);
|
|
|
|
let { lv = 1, exp = 0, job = 0, jobStage = 0, skinId, skins = [], connections = [], ePlace = [], seqId, ce } = dbHeroes;
|
|
|
|
//检测养成维度
|
|
if (lv > 1) return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_LV);
|
|
// if (jobStage) return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_JOBSTAGE);
|
|
if (skins.find(cur => (cur?.usedTalentPoint || 0) > 0)) return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_TALENT);
|
|
if (connections.length > 0) return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_CONNECT);
|
|
// if (ePlace.length > 0) return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_EQUIP);
|
|
if (ePlace.length > 0) {
|
|
for (let obj of ePlace) {
|
|
let newEplace = await getInitEplace(obj.id, skinId);
|
|
if (obj.lv != newEplace.lv || obj.quality != newEplace.quality || obj.qualityStage != newEplace.qualityStage ||
|
|
obj.star != newEplace.star || obj.starStage != newEplace.starStage || obj.jewel != newEplace.jewel) {
|
|
return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_EQUIP);
|
|
}
|
|
for (let st of obj.stones) {
|
|
if (st.stone != (newEplace.stones.find(cur => cur.id == st.id)?.stone || 0)) {
|
|
return resResult(STATUS.RESONANCE_PUT_POSITION_EXIST_EQUIP);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let result = await ResonanceModel.updateByPosition(roleId, position, { hid, seqId, lv, exp, job, jobStage, skinId, skins, connections, ePlace, ce });
|
|
if (!result) return resResult(STATUS.RESONANCE_PUT_POSITION_FAIL);
|
|
|
|
const resonances = await refreshResonanceData(roleId, serverId, sid);
|
|
|
|
return resResult(STATUS.SUCCESS, resonances)
|
|
}
|
|
|
|
async heroOffPosition(msg: { position: number, hid: number }, session: BackendSession) {
|
|
const { position, hid } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const sid: string = session.get('sid');
|
|
const serverId: number = session.get('serverId');
|
|
|
|
let dbResonance = await ResonanceModel.findByPositionAndHid(roleId, position, hid);
|
|
if (!dbResonance) return resResult(STATUS.RESONANCE_NOT_PUT_POSITION);
|
|
|
|
let dbHero = await HeroModel.findByHidAndRole(hid, roleId);
|
|
if (!dbHero) return resResult(STATUS.ROLE_HERO_NOT_EXISTS);
|
|
|
|
let { lv = 1, exp = 0, job = 0, jobStage = 0, skinId, skins = [], connections = [], ePlace = [], seqId } = dbResonance;
|
|
for (let obj of dbHero.skins) {
|
|
let newSkin = skins.find(cur => cur.id == obj.id);
|
|
if (newSkin) {
|
|
obj.skinId = newSkin.skinId;
|
|
obj.enable = newSkin.enable;
|
|
obj.talent = newSkin.talent;
|
|
obj.usedTalentPoint = newSkin.usedTalentPoint;
|
|
}
|
|
}
|
|
for (let obj of dbHero.ePlace) {
|
|
let tempEPlace = ePlace.find(cur => cur.id == obj.id);
|
|
if (!tempEPlace && (!RESONANCE.JEWEL || !RESONANCE.STONE) && (obj.jewel || obj.stones.find(cur => cur.stone != 0))) {
|
|
ePlace.push({ ...await getInitEplace(obj.id, skinId) });
|
|
tempEPlace = ePlace.find(cur => cur.id == obj.id);
|
|
}
|
|
if (!RESONANCE.JEWEL && obj.jewel) {
|
|
tempEPlace.jewel = obj.jewel;
|
|
}
|
|
if (!RESONANCE.STONE && obj.stones.find(cur => cur.stone != 0)) {
|
|
tempEPlace.stones = obj.stones;
|
|
}
|
|
}
|
|
let heroResult = await HeroModel.updateHeroInfo(roleId, hid, { lv, exp, job, jobStage, skinId, skins: dbHero.skins, connections, ePlace });
|
|
if (!heroResult) return resResult(STATUS.RESONANCE_OFF_POSITION_FAIL);
|
|
|
|
// 重新计算战力
|
|
let dbJewelMap = await getJewelDataMap([heroResult]);
|
|
let artifact = await ArtifactModel.findbySeqId(roleId, seqId);
|
|
let artifacts: ArtifactModelType[] = [];
|
|
if (artifact) {
|
|
artifacts = [artifact]
|
|
}
|
|
let { curHero } = await calculateCeWithHero(HERO_SYSTEM_TYPE.RESONANCE_CAL, roleId, serverId, sid, hid, heroResult, { jewels: [...dbJewelMap.values()], heroes: [heroResult], artifacts });
|
|
|
|
|
|
await ResonanceModel.deletePosition(roleId, position);
|
|
let result = await ResonanceModel.updateByPosition(roleId, position, {});
|
|
if (!result || result.hid) return resResult(STATUS.RESONANCE_OFF_POSITION_FAIL);
|
|
await ResonanceHistoryModel.updateByPosition(roleId, position, { ...dbResonance });
|
|
|
|
const resonances = await refreshResonanceData(roleId, serverId, sid);
|
|
|
|
return resResult(STATUS.SUCCESS, {
|
|
curHero: { isResonance: false, ...pick(new HeroParam(curHero), ['hid', 'seqId', 'lv', 'exp', 'job', 'jobStage', 'skinId', 'skins', 'talent', 'usedTalentPoint', 'totalTalentPoint', 'connections', 'ePlace', 'ce']) },
|
|
...resonances,
|
|
})
|
|
}
|
|
|
|
} |