物品:获取装备上限

This commit is contained in:
luying
2021-08-02 20:47:49 +08:00
parent abb74b9e6b
commit 2e961691b5
14 changed files with 315 additions and 222 deletions
@@ -549,7 +549,7 @@ export class EquipHandler {
goods = goods.concat(goodInfo.decomposeItem);
}
let uids = [{ uid: roleId, sid }];
await EquipModel.deleteEquips(originalEquip);
await EquipModel.deleteEquips(roleId, originalEquip);
this.app.get('channelService').pushMessageByUids('onEquipDel', resResult(STATUS.SUCCESS, { equips: originalEquip }), uids);
await addItems(roleId, roleName, sid, jewels); // 把原本镶嵌在装备上的宝石返还了
let result = await addItems(roleId, roleName, sid, goods);
@@ -11,7 +11,7 @@ import { getMainTask, refDailyTaskBox, removeHistoryTask, getCurTask, checkTask
import { TASK } from "../../../pubUtils/dicParam";
import { newHeroGiftPoint } from "../../../services/activity/newHeroGiftsService";
import { ActivityTaskPointModel, ActivityTaskPointModelType } from "../../../db/ActivityTaskPoint";
import { ItemInter } from "../../../pubUtils/interface";
import { ItemInter, RewardInter } from "../../../pubUtils/interface";
import { RoleModel } from "../../../db/Role";
import { roleLevelup } from "../../../services/normalBattleService";
@@ -51,7 +51,7 @@ export class ShopHandler {
rec = await UserTaskRecModel.receiveTask(roleId, type, taskType, group, id);
// 每日、成就增加积分
let point = 0, weeklyPoint = 0, expItem: ItemInter;
let point = 0, weeklyPoint = 0, expItem: RewardInter;
if (type == TASK_FUN_TYPE.DAILY) { // 增加积分
let userTask = await UserTaskModel.findByRole(roleId);
let dic = <DicDailyTask>dicTask;
+171 -163
View File
@@ -1,4 +1,4 @@
import { ITID, CONSUME_TYPE, getCurNameById, ITEM_TABLE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, FIGURE_UNLOCK_CONDITION, REDIS_KEY, TASK_TYPE } from './../consts';
import { ITID, CONSUME_TYPE, getCurNameById, ITEM_TABLE, HERO_SYSTEM_TYPE, CURRENCY_BY_TYPE, FIGURE_UNLOCK_CONDITION, REDIS_KEY, TASK_TYPE, CURRENCY, CURRENCY_TYPE, MAIL_TYPE } from './../consts';
import { EquipModel } from './../db/Equip';
import { resResult } from '../pubUtils/util';
import { RoleModel, RoleType } from '../db/Role';
@@ -8,7 +8,7 @@ import { ItemModel } from '../db/Item';
import { STATUS } from '../consts/statusCode';
import { pinus } from 'pinus';
import { addEquips, addBags, addSkin, addFigure, unlockFigure as pubUnlockFigure, createHeroes as pubCreateHeroes, transPiece } from '../pubUtils/itemUtils';
import { EquipInter, ItemInter, BagInter } from '../pubUtils/interface';
import { ItemInter, RewardInter, } from '../pubUtils/interface';
import { gameData } from '../pubUtils/data';
import { uniq, indexOf, findIndex } from 'underscore';
import { HeroModel, HeroType } from '../db/Hero';
@@ -17,168 +17,141 @@ import { Rank } from './rankService';
import { checkActivityTask, pushActivityUpdate, pushTaskUpdate } from './taskService';
import { CreateHeroParam } from '../domain/roleField/hero';
import { HeroSkin } from '../db/Hero';
import { errlogger } from '../util/logger';
import { BAG } from '../pubUtils/dicParam';
import { sendMailByContent } from './mailService';
export async function handleCost(roleId: string, sid: string, goods: Array<ItemInter>) {
let currencysMap: any = {};
let equips: Array<number> = [];
let bags: Array<{ id: number, count: number }> = [];
let uids = [{ uid: roleId, sid }];
if (!sortConsumes(goods, bags, currencysMap, equips))
return false;
let { items, equips, gold, coin } = sortItems(goods, 2);
let equipSeqIds = equips.map(cur => cur.seqId);
// 检查货币是否充足
if (!!Object.keys(currencysMap).length) {
let role = await RoleModel.findByRoleId(roleId);
for (let key in currencysMap) {
currencysMap[key] = role[key] - currencysMap[key];
if (currencysMap[key] < 0) {
return false;
}
}
let role = await RoleModel.findByRoleId(roleId);
if (gold.length > 0 || coin > 0) {
let { gold: originGold, coin: originCoin } = role;
for(let {count} of gold) { originGold -= count };
if(originGold < 0 || originCoin < coin) return false;
}
//检查装备是否存在
if (!!equips.length) {
let resEquips = await EquipModel.getEquips(equips);
if (equips.length > 0) {
let resEquips = await EquipModel.getEquips(equipSeqIds);
if (resEquips.length < equips.length)
return false;
}
//检查并修改道具
if (bags.length > 0) {
let { hasError, result } = await ItemModel.decreaseItems(roleId, bags);
if (items.length > 0) {
let { hasError, result } = await ItemModel.decreaseItems(roleId, items);
if (hasError) return false;
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, { goods: result }), uids);
}
//删除装备
if (!!equips.length) {
await EquipModel.deleteEquips(equips);
if (equips.length > 0) {
await EquipModel.deleteEquips(roleId, equipSeqIds);
pinus.app.get('channelService').pushMessageByUids('onEquipDel', resResult(STATUS.SUCCESS, { equips }), uids);
}
//消耗玩家货币
if (!!Object.keys(currencysMap).length) {
await RoleModel.updateRoleInfo(roleId, currencysMap);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, currencysMap), uids);
if (gold.length > 0 || coin > 0) {
role = await RoleModel.decreaseGoldAndCoin(roleId, gold, coin);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, {
gold: role.gold, coin: role.coin
}), uids);
}
return true;
}
function sortConsumes(goods: Array<ItemInter>, bags: Array<ItemInter>, currencysMap: any, equips: Array<number>) {
for (let good of goods) {
if (good.count == 0) continue;
let goodInfo = gameData.goods.get(good.id);
let { type, table } = ITID.get(goodInfo.itid);
if (table == ITEM_TABLE.EQUIP) {
if (!!good.seqId) {
equips.push(good.seqId);
} else {
return false;
}
} else if (table == ITEM_TABLE.ITEM) {
let index = indexOf(bags, { id: good.id });
if (index > 0) {
bags[index].count = bags[index].count + good.count;
} else {
bags.push(good);
}
} else if (table == ITEM_TABLE.SKIN) {
return false
} else if (table == ITEM_TABLE.ROLE) {
let curname = getCurNameById(goodInfo.good_id);
if (!!curname) {
if (curname != 'ap' && curname != 'kingExp') {
currencysMap[curname] = (currencysMap[curname] || 0) + good.count;
} else {
return false;
}
}
}
}
return true;
}
// TODO: sid 在方法内部获取,且不一定存在
export async function addItems(roleId: string, roleName: string, sid: string, goods: Array<ItemInter>) {
let showItems: Array<ItemInter> = [];
let currencysMap: any = {};
let equips: Array<EquipInter> = [];
let bags: Array<BagInter> = [];
let skins: Array<number> = [];
let figures: Array<number> = [];
let uids = [{ uid: roleId, sid }];
sortItems(goods, bags, skins, figures, currencysMap, equips, showItems);
let equipInfos = [], taskPushMessage = [];
for (let equip of equips) {
let { equipInfo, pushMessage } = await addEquips(roleId, roleName, equip);
showItems.push({ seqId: equipInfo.seqId, id: equip.id, count: 1 });
equipInfos.push(equipInfo);
taskPushMessage.concat(pushMessage);
}
//装备推送
if (!!equipInfos.length)
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, { equipInfos }), uids);
pushTaskUpdate(roleId, sid, null, taskPushMessage);
//统计装备
if (equipInfos.length > 0) {
let { serverId } = await RoleModel.findByRoleId(roleId);
await checkActivityTask(serverId, sid, [], roleId, TASK_TYPE.EQUIP_QUALITY_COUNT, equipInfos.length, { equips: equipInfos.map(obj => { return { quality: obj.quality } }) })
}
//货币推送
if (!!Object.keys(currencysMap).length) {
let role = await RoleModel.findByRoleId(roleId);
for (let key in currencysMap) {
if (key == 'ap') {
let { ap } = await setAp(roleId, role.lv, currencysMap[key], sid, []);
currencysMap.ap = ap;
} else {
currencysMap[key] += role[key];
}
let { items, equips, gold, coin, ap, skins, figures } = sortItems(goods, 1);
let showItems: { id: number, seqId?: number, count: number }[] = [];
let role = await RoleModel.findByRoleId(roleId);
// 1. 装备处理
if(equips.length > 0) {
let { equipCount = 0 } = role;
let incEquips = equips, mailEquips: { id?: number, hid?: number, seqId?: number }[] = [];
if(equips.length + equipCount > BAG.BAG_EQUIP_UPLIMITED) { // 装备上限
equips.reverse();
mailEquips = equips.slice(0, equipCount + equips.length - BAG.BAG_EQUIP_UPLIMITED);
incEquips = equips.slice(equipCount + equips.length - BAG.BAG_EQUIP_UPLIMITED);
mailEquips.reverse(); incEquips.reverse();
}
// 直接加的
let { equips: equipInfos, pushMessages } = await addEquips(roleId, roleName, <{id: number, hid?: number}[]>incEquips);
for (let equip of equipInfos) {
showItems.push({ seqId: equip.seqId, id: equip.id, count: 1 });
}
//装备推送
if (!!equipInfos.length)
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, { equipInfos }), uids);
pushTaskUpdate(roleId, sid, null, pushMessages);
//统计装备
if (equipInfos.length > 0) {
let { serverId } = await RoleModel.findByRoleId(roleId);
await checkActivityTask(serverId, sid, [], roleId, TASK_TYPE.EQUIP_QUALITY_COUNT, equipInfos.length, { equips: equipInfos.map(obj => { return { quality: obj.quality } }) })
}
// 发邮件的
if(mailEquips.length > 0) {
await sendMailByContent(MAIL_TYPE.EQUIP_OVER, roleId, { goods: combineEquips(mailEquips) });
}
await RoleModel.updateRoleInfo(roleId, currencysMap);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, currencysMap), uids);
}
let bagInfos = [];
for (let item of bags) {
let bagInfo = await addBags(roleId, roleName, item);
showItems.push({ id: item.id, count: item.count });
bagInfos.push(bagInfo);
// 2. 道具处理
if(items.length > 0) {
let { items: itemInfos } = await addBags(roleId, roleName, items);
for (let item of itemInfos) {
showItems.push({ id: item.id, count: item.count });
}
//背包除去装备推送
if (!!itemInfos.length)
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, { goods: itemInfos }), uids);
}
//背包除去装备推送
if (!!bagInfos.length)
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, { goods: bagInfos }), uids);
// 3. 货币推送
if(gold.length > 0 || coin > 0 || ap > 0) {
let { ap: addAp } = await setAp(roleId, role.lv, ap, sid, []);
let heroskins: {skins: HeroSkin[], hid: number}[] = []; // 皮肤推送信息
let skinInfos: {id: number, hid: number }[] = [];
role = await RoleModel.increaseGoldAndCoin(roleId, gold, coin);
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, {
gold: role.gold, coin: role.coin, ap: addAp
}), uids);
}
// 4. 皮肤处理
let figureInfos:{ heads: Figure[], frames: Figure[], spines: Figure[] }[] = []; // 头像变化推送信息
let calAllHeroResult = undefined; // 全局战力变化推送
for (let skinId of skins) {//皮肤推送
let result = await addSkin(roleId, roleName, skinId, false);
console.log(result)
if (!!result) {
showItems.push({ id: skinId, count: 1 });
figureInfos.push(result.figureInfo);
if(result.hero) {
heroskins.push({ skins: result.hero.skins, hid: result.hero.hid });
if(result.calAllHeroResult) calAllHeroResult = result.calAllHeroResult;
if(skins.length > 0) {
let heroskins: {skins: HeroSkin[], hid: number}[] = []; // 皮肤推送信息
let skinInfos: {id: number, hid: number }[] = [];
let calAllHeroResult = undefined; // 全局战力变化推送
for (let skinId of skins) {//皮肤推送
let result = await addSkin(roleId, roleName, skinId, false);
console.log(result)
if (!!result) {
showItems.push({ id: skinId, count: 1 });
figureInfos.push(result.figureInfo);
if(result.hero) {
heroskins.push({ skins: result.hero.skins, hid: result.hero.hid });
if(result.calAllHeroResult) calAllHeroResult = result.calAllHeroResult;
}
skinInfos.push({ id: skinId, hid: result.hero?.hid||0 })
}
skinInfos.push({ id: skinId, hid: result.hero?.hid||0 })
}
}
if (!!skinInfos.length) {
pushHeroSkinMsg(heroskins, skinInfos, uids); // 推送onHeroSkinChange
}
// 推送全局加成信息
if(calAllHeroResult) await pushCalAllHeroCe(roleId, sid, calAllHeroResult);
if (!!skinInfos.length) {
pushHeroSkinMsg(heroskins, skinInfos, uids); // 推送onHeroSkinChange
}
// 推送全局加成信息
if(calAllHeroResult) await pushCalAllHeroCe(roleId, sid, calAllHeroResult);
}
// 获得头像和相框等
if(!!figures && figures.length > 0) {
// 5. 获得头像和相框等
if(figures.length > 0) {
let figureInfo = await addFigure(roleId, figures);
if(figureInfo) figureInfos.push(figureInfo);
for (let id of figures) {//皮肤推送
@@ -196,59 +169,94 @@ export async function addItems(roleId: string, roleName: string, sid: string, go
return showItems;
}
function sortItems(goods: Array<ItemInter>, bags: Array<BagInter>, skins: Array<number>, figures: Array<number>, currencysMap: any, equips: EquipInter[], showItems: Array<ItemInter>) {
for (let good of goods) {
let goodInfo = gameData.goods.get(good.id);
// 道具不存在时先跳过
if (!goodInfo) {
console.log('sortItems 道具不存在:', good.id);
function combineEquips(equips: { id?: number }[]) {
let result: { id: number, count: number }[] = [];
for(let { id } of equips) {
let index = result.findIndex(cur => cur.id == id);
if(index == -1) {
result.push({ id, count: 1 });
} else {
result[index].count ++;
}
}
return result;
}
function sortItems(goods: ItemInter[], handleType: 1|2) {
let items: { id: number, count: number }[] = []; // 可叠加道具
let equips: { seqId?: number, id?: number, hid?: number }[] = []; // 不可叠加装备
let gold: { count: number, isPay: boolean }[] = []; // 金币
let coin: number = 0;
let ap: number = 0;
let skins: number[] = [];
let figures: number[] = [];
for(let good of goods) {
let dicGood = gameData.goods.get(good.id);
if(!dicGood) {
errlogger.error(`物品 ${good.id} 未配置`);
continue;
}
let { type, table, isCurrency } = ITID.get(goodInfo.itid);
if (table == ITEM_TABLE.EQUIP) {
for (let i = 0; i < good.count; i++) {
equips.push({ id: good.id });
}
} else if (table == ITEM_TABLE.ITEM) {
let index = findIndex(bags, { id: good.id });
if (index > 0) {
bags[index].count = bags[index].count + good.count;
let dicItid = ITID.get(dicGood.itid);
if(!dicItid) {
errlogger.error(`itid ${dicGood.itid} 未配置`);
continue;
}
let { type, table, isCurrency } = dicItid;
if(table == ITEM_TABLE.EQUIP) { // 装备
if(handleType == 1) {
for(let i = 0; i < good.count; i++) {
equips.push({ id: good.id, hid: good.hid })
}
} else {
bags.push({ id: good.id, count: good.count, itemName: goodInfo.name, hid: goodInfo.hid || 0, type });
if(!!good.seqId) {
equips.push({ seqId: good.seqId });
}
}
} else if (table == ITEM_TABLE.SKIN) {
if (type == CONSUME_TYPE.SKIN) {
let index = indexOf(skins, good.id);
} else if (table == ITEM_TABLE.ITEM) { // 可叠加道具
let index = items.findIndex(cur => cur.id == good.id);
if(index > 0) {
items[index].count += good.count;
} else {
items.push({ id: good.id, count: good.count });
}
} else if (table == ITEM_TABLE.SKIN) { // 皮肤,不可重复获得,不可删
if(handleType == 1) {
let index = skins.indexOf(good.id);
if (index == -1) {
skins.push(good.id);
}
}
} else if (table == ITEM_TABLE.ROLE) {
if (!!isCurrency) {
let curname = getCurNameById(goodInfo.good_id);
if (!!curname) {
let index = findIndex(showItems, { id: good.id });
if (index > 0) {
showItems[index].count = showItems[index].count + good.count;
} else {
showItems.push(good);
if(isCurrency) { // 3种货币
let dicCurrency = CURRENCY.get(good.id);
if(dicCurrency) {
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 金币,区分付费和免费,默认免费
let index = gold.findIndex(cur => cur.isPay == !!good.isPay);
if(index > 0) {
gold[index].count += good.count;
} else {
gold.push({ count: good.count, isPay: !!good.isPay });
}
} else if (dicCurrency.type == CURRENCY_TYPE.COIN) { // 铜钱
coin += good.count;
} else if (dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 体力
ap += good.count;
}
currencysMap[curname] = (currencysMap[curname] || 0) + good.count;
}
} else {
if (type == CONSUME_TYPE.HEAD || type == CONSUME_TYPE.FRAME || type == CONSUME_TYPE.SPINE) {
let index = indexOf(figures, good.id);
} else {
if (type == CONSUME_TYPE.HEAD || type == CONSUME_TYPE.FRAME || type == CONSUME_TYPE.SPINE) { // 头像等,不可重复获得,不可删
let index = figures.indexOf(good.id);
if (index == -1) {
figures.push(good.id);
}
}
}
}
}
return { items, equips, gold, coin, ap, skins, figures }
}
export async function decreaseItems(roleId: string, sid: string, bags: Array<{ id: number, count: number, ratio?: number }>) {
@@ -346,7 +354,7 @@ export async function createHeroes(roleId: string, roleName: string, sid: string
}
}
let resultHeroes: HeroType[] = [], resultItems: ItemInter[] = [];
let resultHeroes: HeroType[] = [], resultItems: RewardInter[] = [];
if (newHeroInfo.length > 0) {
let { heroes, role, figureInfo, calHeroResults, calAllHeroResult, taskPushMessage, activityTaskPushMessage } = await pubCreateHeroes(roleId, roleName, serverId, newHeroInfo, funcs);