fix bug 升级更新错误
This commit is contained in:
@@ -5,9 +5,10 @@ import { resResult, getItems, decodeStr } from '../../../pubUtils/util';
|
|||||||
import { STATUS } from '../../../consts/statusCode';
|
import { STATUS } from '../../../consts/statusCode';
|
||||||
import {HeroModel} from '../../../db/Hero';
|
import {HeroModel} from '../../../db/Hero';
|
||||||
import {CURRENCY_BY_TYPE, CURRENCY_TYPE, ITID, CONSUME_TYPE, HERO_GROW_MAX, HERO_SYSTEM_TYPE} from '../../../consts/consts';
|
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 { ABI_STAGE } from '../../../consts/abilityConst';
|
||||||
import { RoleModel } from '../../../db/Role';
|
import { RoleModel } from '../../../db/Role';
|
||||||
|
import Item, { ItemModel } from '../../../db/Item';
|
||||||
|
|
||||||
const _ = require('underscore');
|
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 roleId: string = session.get('roleId');
|
||||||
let sid: string = session.get('sid');
|
let sid: string = session.get('sid');
|
||||||
|
|
||||||
let { hid, type, material } = msg;
|
let { hid, type } = msg;
|
||||||
|
|
||||||
let addLv = 0;
|
let addLv = 0;
|
||||||
if(type == 1) {
|
if(type == 1) {
|
||||||
@@ -78,31 +79,37 @@ export class HeroHandler {
|
|||||||
} else {
|
} else {
|
||||||
return resResult(STATUS.ROLE_HERO_LV_TYPE_ERROR);
|
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);
|
let hero = await HeroModel.findByHidAndRole(hid, roleId, false);
|
||||||
if(!hero) return resResult(STATUS.ROLE_HERO_NOT_EXISTS);
|
if(!hero) return resResult(STATUS.ROLE_HERO_NOT_EXISTS);
|
||||||
let {lv: playerLv} = await RoleModel.findByRoleId(roleId);
|
let {lv: playerLv} = await RoleModel.findByRoleId(roleId);
|
||||||
let {lv: oldLv, exp: oldExp} = hero;
|
let {lv: oldLv, exp: oldExp} = hero;
|
||||||
if(oldLv + addLv > playerLv ) return resResult(STATUS.ROLE_HERO_LV_OVER);
|
if(oldLv + addLv > playerLv ) return resResult(STATUS.ROLE_HERO_LV_OVER);
|
||||||
oldExp += allExp;
|
let nextExp = getHeroExpByLv(oldLv + addLv - 1);
|
||||||
let newExp = oldExp + allExp; // 加上经验书可以达到的经验
|
let needExp = nextExp - oldExp;
|
||||||
let newLv = getHeroLvByExp(newExp);
|
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);
|
return resResult(STATUS.ROLE_EXP_NOT_ENOUGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let newLv = getHeroLvByExp(newExp);
|
||||||
|
|
||||||
let costResult = await handleCost(roleId, sid, material);
|
let costResult = await handleCost(roleId, sid, material);
|
||||||
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ export async function calPlayerCeAndSave(sid: string, roleId: string, heros: Arr
|
|||||||
for (let hero of heros) {
|
for (let hero of heros) {
|
||||||
let incHeroCe = calPlayerCe(hero, type, args);
|
let incHeroCe = calPlayerCe(hero, type, args);
|
||||||
incPlayerCe += incHeroCe;
|
incPlayerCe += incHeroCe;
|
||||||
|
console.log('*******', JSON.stringify(hero))
|
||||||
await hero.save();
|
await hero.save();
|
||||||
pushHeros.push({
|
pushHeros.push({
|
||||||
hid: hero.hid,
|
hid: hero.hid,
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,7 @@ export default class Item extends BaseModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async findByRoleAndType(roleId: string, type: number, lean = true) {
|
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;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -382,7 +382,7 @@ export default class Role extends BaseModel {
|
|||||||
|
|
||||||
public static async updateRoleInfo(roleId: string, roleUpdate:roleUpdate, lean = true) {
|
public static async updateRoleInfo(roleId: string, roleUpdate:roleUpdate, lean = true) {
|
||||||
delete roleUpdate._id;
|
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||{};
|
return result||{};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user