Files
ZYZ/game-server/app/services/normalBattleService.ts
2020-12-15 16:02:59 +08:00

75 lines
2.6 KiB
TypeScript

import { HeroModel } from '../db/Hero';
import Role, { RoleModel } from '../db/Role'
import { getLvByExp, getExpByLv } from '../pubUtils/gamedata';
import { redisUserInfoUpdate } from './redisService';
import { switchOnFunc } from './funcSwitchService';
import { FUNC_OPT_TYPE } from '../consts';
import { Application, BackendSession } from 'pinus';
export async function roleLevelup(roleId: string, kingExp: number, app: Application, session: BackendSession) {
let role = await RoleModel.findByRoleId(roleId);
let {lv = 1, exp = 0} = role;
let newExp = exp + kingExp;
let newLv = getLvByExp(newExp);
let nextExpObj = getExpByLv(newLv + 1);
let curExpObj = getExpByLv(newLv);
if(curExpObj && !nextExpObj && newExp > curExpObj.sum) {
newExp = curExpObj.sum;
}
role = await RoleModel.levelup(roleId, newLv, newExp);
if(newLv > lv) { // 升级
await switchOnFunc(roleId, FUNC_OPT_TYPE.LEVEL_UP, newLv, app, session);
await redisUserInfoUpdate(roleId, [{field: 'lv', value: newLv}])
}
let actordata = [];
for(let i = lv; i <= newLv; i++) {
let lvObj = getExpByLv(i);
if(!lvObj) break;
let {sum, cur} = lvObj;
let lastSum = sum - cur;
// console.log(sum, cur, lastSum, exp, newExp);
let startExp = exp > lastSum? exp - lastSum: 0;
let getExp = newExp > sum? cur - startExp: newExp - lastSum - startExp;
actordata.push({
lv : i,
exp : startExp, // 升级前经验
getExp : getExp, // 获得的经验
mostExp : cur
});
}
return {actordata, lv: newLv, exp: newExp, kingExp: newExp - exp};
}
export async function checkBattleHeroes(roleId: string, heroes: Array<number>) {
let flag = true;
if(heroes.length <= 0) flag = false;
const findHeroes = await HeroModel.findBySeqIdRange(heroes, roleId);
if(findHeroes.length != heroes.length) flag = false;
for(let seqId of heroes) {
let hero = findHeroes.find(cur => cur.seqId == seqId);
if(!hero) flag = false;
}
return flag;
}
export async function updateWarStar(roleId: string, battleId: number, warType: number, star: number) {
let role = await RoleModel.findByRoleId(roleId);
let { warStar = new Array<{id: number, warType: number, star: number}>() } = role;
let curStar = warStar.find(cur => cur.id == battleId);
let result: Role;
if(curStar) {
if(star > curStar.star) {
result = await Role.updateWarStar(roleId, battleId, star);
}
} else {
result = await RoleModel.pushWarStar(roleId, battleId, warType, star);
}
return result
}