fix bug 升级更新错误

This commit is contained in:
luying
2020-12-14 21:10:35 +08:00
parent 763b61b867
commit 2a268b2158
4 changed files with 29 additions and 21 deletions

View File

@@ -5,9 +5,10 @@ import { resResult, getItems, decodeStr } from '../../../pubUtils/util';
import { STATUS } from '../../../consts/statusCode';
import {HeroModel} from '../../../db/Hero';
import {CURRENCY_BY_TYPE, CURRENCY_TYPE, ITID, CONSUME_TYPE, HERO_GROW_MAX, HERO_SYSTEM_TYPE} from '../../../consts/consts';
import {getJobInfoById, getMaxGradeByjobClass, getHidAndLevelByShipId, getHeroInfoById, getGoodById, getHeroExpByLv, getGamedata, getJobByGradeAndClass, getFriendShipById, getFriendShipLevels, getFashionsById, getHeroLvByExp} from '../../../pubUtils/gamedata';
import {getJobInfoById, getMaxGradeByjobClass, getHidAndLevelByShipId, getHeroInfoById, getGoodById, getHeroExpByLv, getGamedata, getJobByGradeAndClass, getFriendShipById, getFriendShipLevels, getFashionsById, getHeroLvByExp, getExpByLv} from '../../../pubUtils/gamedata';
import { ABI_STAGE } from '../../../consts/abilityConst';
import { RoleModel } from '../../../db/Role';
import Item, { ItemModel } from '../../../db/Item';
const _ = require('underscore');
@@ -64,11 +65,11 @@ export class HeroHandler {
}
// 武将升级
public async lvUp(msg: { hid: number, type: number, material: Array<{id: number, count: number}>}, session: BackendSession) {
public async lvUp(msg: { hid: number, type: number}, session: BackendSession) {
let roleId: string = session.get('roleId');
let sid: string = session.get('sid');
let { hid, type, material } = msg;
let { hid, type } = msg;
let addLv = 0;
if(type == 1) {
@@ -78,31 +79,37 @@ export class HeroHandler {
} else {
return resResult(STATUS.ROLE_HERO_LV_TYPE_ERROR);
}
// 计算得材料可转换的经验
let allExp = 0;
for(let {id, count} of material) {
let dicGoods = getGoodById(id);
if(!dicGoods) return resResult(STATUS.ROLE_INFO_NOT_FOUND);
let dicItid = ITID.get(dicGoods.itid);
if(!dicItid || dicItid.type != CONSUME_TYPE.EXP) {
return resResult(STATUS.ROLE_METERIAL_ERROR);
}
allExp += count * dicGoods.value;
}
// 计算武将可以升的级数
let hero = await HeroModel.findByHidAndRole(hid, roleId, false);
if(!hero) return resResult(STATUS.ROLE_HERO_NOT_EXISTS);
let {lv: playerLv} = await RoleModel.findByRoleId(roleId);
let {lv: oldLv, exp: oldExp} = hero;
if(oldLv + addLv > playerLv ) return resResult(STATUS.ROLE_HERO_LV_OVER);
oldExp += allExp;
let newExp = oldExp + allExp; // 加上经验书可以达到的经验
let newLv = getHeroLvByExp(newExp);
let nextExp = getHeroExpByLv(oldLv + addLv - 1);
let needExp = nextExp - oldExp;
let newExp = oldExp;
if(newLv < oldLv + addLv) {
// 计算得材料可转换的经验
let originalConsumes: Array<any> = await ItemModel.findByRoleAndType(roleId, CONSUME_TYPE.EXP);
let material = new Array<{id: number, count: number}>();
for(let {id, count} of originalConsumes) {
let dicGoods = getGoodById(id);
if(!dicGoods) return resResult(STATUS.ROLE_INFO_NOT_FOUND);
let _count = Math.ceil(needExp/dicGoods.value);
if(_count < count) {
material.push({id, count: _count});
newExp += dicGoods.value * _count;
break;
} else {
material.push({id, count});
newExp += dicGoods.value * count;
}
}
if(newExp < needExp) {
return resResult(STATUS.ROLE_EXP_NOT_ENOUGH);
}
let newLv = getHeroLvByExp(newExp);
let costResult = await handleCost(roleId, sid, material);
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);

View File

@@ -62,6 +62,7 @@ export async function calPlayerCeAndSave(sid: string, roleId: string, heros: Arr
for (let hero of heros) {
let incHeroCe = calPlayerCe(hero, type, args);
incPlayerCe += incHeroCe;
console.log('*******', JSON.stringify(hero))
await hero.save();
pushHeros.push({
hid: hero.hid,

View File

@@ -32,7 +32,7 @@ export default class Item extends BaseModel {
}
public static async findByRoleAndType(roleId: string, type: number, lean = true) {
const items = await ItemModel.find({ roleId, type }).select('id count type').lean(lean);
const items = await ItemModel.find({ roleId, type }).select('id count type').sort({id: 1}).lean(lean);
return items;
}

View File

@@ -382,7 +382,7 @@ export default class Role extends BaseModel {
public static async updateRoleInfo(roleId: string, roleUpdate:roleUpdate, lean = true) {
delete roleUpdate._id;
let result = await HeroModel.findOneAndUpdate({roleId}, {$set:roleUpdate}).lean(lean);
let result = await RoleModel.findOneAndUpdate({roleId}, {$set:roleUpdate}).lean(lean);
return result||{};
}
}