物品增加掉落

This commit is contained in:
mamengke01
2020-12-16 16:16:20 +08:00
parent d822f6b819
commit 829d43c7a8
8 changed files with 176 additions and 25770 deletions
@@ -1,5 +1,5 @@
import {Application, BackendSession, createTcpMailBox, ChannelService} from 'pinus';
import { handleCost } from '../../../services/rewardService';
import { handleCost, addItems } from '../../../services/rewardService';
import { calPlayerCeAndSave, getAllAttrStage } from '../../../services/playerCeService';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts/statusCode';
@@ -27,11 +27,12 @@ export class HeroHandler {
let {id, count} = msg;
let result = await handleCost(roleId, sid, [{id, count}] );
//let result = await handleCost(roleId, sid, [{id, count}] );
let result = await addItems(roleId, roleName, sid, [{id, count}] );
if(!result) {
return resResult(STATUS.BATTLE_CONSUMES_NOT_ENOUGH);
}
return resResult(STATUS.SUCCESS);
return resResult(STATUS.SUCCESS,{result});
}
@@ -448,5 +449,4 @@ export class HeroHandler {
await calPlayerCeAndSave(sid, roleId, [hero], HERO_SYSTEM_TYPE.SKIN, [id, lastSkinId]);
return resResult(STATUS.SUCCESS, {curHero: {hid : hero.hid, skins : hero.skins} });
}
}
+122 -16
View File
@@ -1,13 +1,15 @@
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER } from './../consts';
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER, CONSUME_TYPE } from './../consts/consts';
import { EquipModel } from './../db/Equip';
import { CounterModel } from './../db/Counter';
import { decodeStr, resResult } from '../pubUtils/util';
import { getGoodById } from '../pubUtils/gamedata';
import { getGoodById, getFashionsById } from '../pubUtils/gamedata';
import { RoleModel } from '../db/Role';
import { setAp } from './actionPointService';
import { ItemModel } from '../db/Item';
import { STATUS } from '../consts/statusCode';
import { pinus } from 'pinus';
import { HeroModel } from '../db/Hero';
import { treatTask } from './battleService';
const _ = require('underscore');
export async function handleFixedReward(roleId: string, roleName: string, rewardStr: string, multi: number) {
@@ -178,31 +180,135 @@ export async function handleCost(roleId: string, sid: string, goods: Array<{id:
return true;
}
export async function addItems(roleId: string, sid: string, goods: Array<{id: number, count: number}>) {
let showItems = [];
let equips = [];
let currencys = [];
interface Item {id: number, count: number};
interface Equip {id: number, name: string, quality: number, type: number};
interface Bag {id: number, itemName: string, count: number, type: number, hid:number};
export async function addItems(roleId: string, roleName: string, sid: string, goods: Array<Item>) {
let showItems: Array<Item> = [];
let currencys: Array<Item> = [];
let equips: Array<Equip> = [];
let bags: Array<Bag> = [];
let skins: Array<number> = [];
let uids = [{uid: roleId, sid}];
sortItems(goods, bags, skins, currencys, equips);
let equipInfos = [];
for (let equip of equips) {
let equipInfo = await addEquips(roleId, roleName, equip);
showItems.push({id: equip.id, count: 1});
equipInfos.push(equipInfo);
}
//装备推送
if (!!equipInfos.length) {
pinus.app.get('channelService').pushMessageByUids('onEquipAdd', resResult(STATUS.SUCCESS, {equipInfos}), uids);
}
let data = {};
for (let currency of currencys) {
await addCurrency(roleId, currency, data);
showItems.push(currency);
}
//货币推送
if (!!Object.keys(data).length) {
pinus.app.get('channelService').pushMessageByUids('onPlayerDataChange', resResult(STATUS.SUCCESS, data), 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 = [];
for (let skinId of skins) {//皮肤推送
let result = await addSkins(roleId, skinId);
if (!!result) {
showItems.push({id: skinId, count: 1});
skinInfos.push(result);
}
pinus.app.get('channelService').pushMessageByUids('onHeroSkinChange', resResult(STATUS.SUCCESS, {skinInfos}), uids);
}
return showItems;
}
function sortItems (goods: Array<Item>, bags: Array<Bag>, skins: Array<number>, currencys: Array<Item>, equips: Array<Equip>) {
for (let good of goods) {
let goodInfo = getGoodById(good.id);
if (goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
equips.push({
id: good.id,
name: goodInfo.name,
quality: goodInfo.lv,
type: goodInfo.goodType
});
} else if (goodInfo.goodType == GOOD_TYPE.CONSUMES|| goodInfo.goodType == GOOD_TYPE.SCRIPT) {
for (let i = 0; i < good.count; i++) {
equips.push({id: good.id, name: goodInfo.name, quality: goodInfo.lv, type: goodInfo.goodType});
}
} else {
let {type, isCurrency} = ITID.get(goodInfo.itid);
if (!!isCurrency) { // 货币
if (!!isCurrency) {
let index = _.indexOf(currencys, {id: good.id});
if (index > 0) {
currencys[index].count = currencys[index].count + good.count;
} else {
currencys.push(good);
}
} else if (goodInfo.itid == CONSUME_TYPE.SKIN) {
let index = _.indexOf(skins, good.id);
if (index == -1) {
skins.push(good.id);
}
} else {
let index = _.indexOf(skins, {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});
}
}
}
}
}
}
async function addSkins(roleId: string, id: number) {
let skinInfo = getFashionsById(id);
if (!skinInfo)
return false;
let hero = await HeroModel.findByHidAndRole(skinInfo.actorId, roleId, false);
if (!hero)
return false;
if (!!_.findWhere(hero.skins, {id}))
return false;
hero.skins.push({id: id, enable: false});
await HeroModel.updateHeroInfo(roleId, hero.hid, hero);
return {skins: hero.skins, hid: hero.hid};
}
async function addBags(roleId: string, roleName: string, data:Bag) {
let {id, count, itemName, type, hid} = data;
let item = await ItemModel.increaseItem(roleId, id, count, {roleId, roleName, itemName, id, type, hid});
return item;
}
async function addEquips (roleId: string, roleName: string, weapon: Equip) {
const seqId = await CounterModel.getNewCounter(COUNTER.EID);
let equip = Object.assign({seqId, roleId, roleName}, weapon);
return await EquipModel.createEquip(equip);
}
async function addCurrency(roleId: string, currency: {id: number, count: number }, data: any) {
let {id, count} = currency;
let dicCurrency = CURRENCY.get(id);
let result;
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
result = await RoleModel.addGoldFree(roleId, count);
data.giftGold = result.giftGold;
data.gold = result.gold;
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
result = await RoleModel.addCoin(roleId, count);
data.coin = result.coin;
} else if(dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 处理体力
result = await setAp(Date.now(), roleId, count);
data.ap = result.ap;
}
}