219 lines
8.3 KiB
TypeScript
219 lines
8.3 KiB
TypeScript
import { ITID, CONSUME_TYPE, getCurNameById, ITEM_TABLE } from './../consts';
|
||
import { EquipModel } from './../db/Equip';
|
||
import { resResult, parseGoodStr } from '../pubUtils/util';
|
||
import { getGoodById } from '../pubUtils/gamedata';
|
||
import { RoleModel } from '../db/Role';
|
||
import { setAp } from './actionPointService';
|
||
import { calAllHeroCe } from './playerCeService';
|
||
import { ItemModel } from '../db/Item';
|
||
import { STATUS } from '../consts/statusCode';
|
||
import { pinus } from 'pinus';
|
||
import { addEquips, addBags, addSkins } from '../pubUtils/itemUtils';
|
||
import { EquipInter, ItemInter, BagInter } from '../pubUtils/interface';
|
||
import { gameData } from '../pubUtils/data';
|
||
const _ = require('underscore');
|
||
|
||
export async function handleFixedReward(roleId: string, roleName: string, sid: string, rewardStr: string, multi: number) {
|
||
let reward = parseGoodStr(rewardStr);
|
||
let rewards = [];
|
||
for(let obj of reward)
|
||
rewards.push({ ...obj, count: Math.ceil(obj.count * multi)});
|
||
const result = await addItems(roleId, roleName, sid, reward);
|
||
return result;
|
||
}
|
||
|
||
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;
|
||
// 检查货币是否充足
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
//检查装备是否存在
|
||
if (!!equips.length) {
|
||
let resEquips = await EquipModel.getEquips(equips);
|
||
if (resEquips.length < equips.length)
|
||
return false;
|
||
}
|
||
//检查并修改道具
|
||
if(bags.length > 0) {
|
||
let {hasError, result} = await ItemModel.decreaseItems(roleId, bags);
|
||
if(hasError) return false;
|
||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: result} ), uids);
|
||
}
|
||
|
||
//删除装备
|
||
if (!!equips.length) {
|
||
await EquipModel.deleteEquips(equips);
|
||
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);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
function sortConsumes(goods: Array<ItemInter>, bags: Array<ItemInter>, currencysMap: any, equips: Array<number>) {
|
||
for (let good of goods) {
|
||
let goodInfo = getGoodById(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.HERO) {
|
||
return false
|
||
} else if (table == ITEM_TABLE.ROLE) {
|
||
let curname = getCurNameById(goodInfo.good_id);
|
||
if (!!curname) {
|
||
if (curname != 'ap') {
|
||
currencysMap[curname] = (currencysMap[curname]||0) + good.count;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
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 uids = [{uid: roleId, sid}];
|
||
sortItems(goods, bags, skins, currencysMap, equips, showItems);
|
||
let equipInfos = [];
|
||
for (let equip of equips) {
|
||
let equipInfo = await addEquips(roleId, roleName, equip);
|
||
showItems.push({seqId: equipInfo.seqId, id: equip.id, count: 1});
|
||
equipInfos.push(equipInfo);
|
||
}
|
||
//装备推送
|
||
if (!!equipInfos.length)
|
||
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, {equipInfos}), uids);
|
||
|
||
//货币推送
|
||
if (!!Object.keys(currencysMap).length) {
|
||
let role = await RoleModel.findByRoleId(roleId);
|
||
for (let key in currencysMap) {
|
||
if (key == 'ap') {
|
||
let {ap} = await setAp(Date.now(), roleId, currencysMap[key]);
|
||
currencysMap.ap = ap;
|
||
} else {
|
||
currencysMap[key] += role[key];
|
||
}
|
||
}
|
||
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);
|
||
}
|
||
//背包除去装备推送
|
||
if (!!bagInfos.length)
|
||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: bagInfos}), uids);
|
||
|
||
let skinInfos = [];
|
||
let addSkinIds = [];
|
||
for (let skinId of skins) {//皮肤推送
|
||
let result = await addSkins(roleId, skinId);
|
||
if (!!result) {
|
||
showItems.push({id: skinId, count: 1});
|
||
skinInfos.push(result);
|
||
addSkinIds.push(skinId);
|
||
}
|
||
}
|
||
if (!!skinInfos.length) {
|
||
calAllHeroCe(sid, roleId, 1, addSkinIds)
|
||
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, {skinInfos}), uids);
|
||
}
|
||
return showItems;
|
||
}
|
||
|
||
function sortItems (goods: Array<ItemInter>, bags: Array<BagInter>, skins: Array<number>, currencysMap: any, equips: Array<EquipInter>, showItems: Array<ItemInter>) {
|
||
for (let good of goods) {
|
||
let goodInfo = gameData.goods.get(good.id);
|
||
// 道具不存在时先跳过
|
||
if (!goodInfo) {
|
||
console.log('sortItems 道具不存在:', 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, ...goodInfo});
|
||
}
|
||
} 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({id: good.id, count: good.count, itemName: goodInfo.name, hid: goodInfo.hid||0, type});
|
||
}
|
||
|
||
} else if (table == ITEM_TABLE.HERO) {
|
||
if (type == CONSUME_TYPE.SKIN) {
|
||
let index = _.indexOf(skins, 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);
|
||
}
|
||
currencysMap[curname] = (currencysMap[curname]||0 )+ good.count;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
export async function decreaseItems(roleId: string, sid: string, bags: Array<{id: number, count:number, ratio?: number}>) {
|
||
let uids = [{uid: roleId, sid}];
|
||
if(bags.length > 0) {
|
||
let {hasError, result} = await ItemModel.decreaseItems(roleId, bags);
|
||
if(hasError) return true;
|
||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: result} ), uids);
|
||
}
|
||
} |