208 lines
7.5 KiB
TypeScript
208 lines
7.5 KiB
TypeScript
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER } from './../consts';
|
||
import { EquipModel } from './../db/Equip';
|
||
import { CounterModel } from './../db/Counter';
|
||
import { decodeStr, resResult } from '../pubUtils/util';
|
||
import { getGoodById } 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';
|
||
const _ = require('underscore');
|
||
|
||
export async function handleFixedReward(roleId: string, roleName: string, rewardStr: string, multi: number) {
|
||
let reward = decodeStr('fixReward', rewardStr);
|
||
let rewards = [];
|
||
for(let obj of reward)
|
||
rewards.push({ ...obj, count: Math.ceil(obj.count * multi)});
|
||
const result = await handleReward(roleId, roleName, reward);
|
||
return result;
|
||
}
|
||
|
||
export async function handleReward(roleId: string, roleName: string, rewards: Array<{type?: number, gid: number, count: number, times?: number}>) {
|
||
|
||
let returnGoods = new Array(), getGold = 0, getCoin = 0, getAp = 0;
|
||
for(let goods of rewards) {
|
||
let goodInfo = getGoodById(goods.gid);
|
||
let result = new Array<any>()
|
||
if(goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
|
||
result = await rewardWeapons(roleId, roleName, goodInfo, {id: goods.gid, cnt: goods.count });
|
||
} else if(goodInfo.goodType == GOOD_TYPE.CONSUMES|| goodInfo.goodType == GOOD_TYPE.SCRIPT) { // 消耗品
|
||
let {type, isCurrency} = ITID.get(goodInfo.itid);
|
||
if(isCurrency) { // 货币
|
||
result = await rewardCurrency(roleId, goodInfo, {id: goods.gid, cnt: goods.count });
|
||
} else {
|
||
result = await rewardItems(roleId, roleName, goodInfo, type, {id: goods.gid, cnt: goods.count });
|
||
}
|
||
}
|
||
|
||
for(let obj of result) {
|
||
returnGoods.push({dropType: goods.type, times: goods.times, ...obj});
|
||
if(obj.isCurrency) {
|
||
if(obj.currencyType == CURRENCY_TYPE.GOLD) {
|
||
getGold += obj.count;
|
||
} else if (obj.currencyType == CURRENCY_TYPE.COIN) {
|
||
getCoin += obj.count;
|
||
} else if (obj.currencyType == CURRENCY_TYPE.ACTION_POINT) {
|
||
getAp += obj.count;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return {
|
||
goods: returnGoods,
|
||
getGold,
|
||
getCoin,
|
||
getAp
|
||
};
|
||
}
|
||
|
||
async function rewardWeapons (roleId: string, roleName: string, dicGood: any, weapon: {id:number,cnt:number }) {
|
||
|
||
let weaponsData = [];
|
||
let cnt = weapon.cnt;
|
||
while (cnt > 0) {
|
||
const seqId = await CounterModel.getNewCounter(COUNTER.EID);
|
||
const equipInfo = {
|
||
roleId,
|
||
roleName,
|
||
id: weapon.id,
|
||
name: dicGood.name,
|
||
seqId,
|
||
quality: dicGood.lv,
|
||
type: dicGood.goodType
|
||
}
|
||
const equip = await EquipModel.createEquip(equipInfo);
|
||
cnt -= 1;
|
||
weaponsData.push(equip);
|
||
}
|
||
return weaponsData;
|
||
}
|
||
|
||
// 消耗品
|
||
async function rewardItems (roleId: string, roleName: string, dicGood: any, type: number, data: {id:number,cnt:number }) {
|
||
|
||
let goods = new Array();
|
||
let {id, cnt} = data;
|
||
|
||
let result = await ItemModel.increaseItem(roleId, id, cnt, {roleId, roleName, itemName: dicGood.name, id, type, hid: dicGood.hid||0});
|
||
|
||
if(result) {
|
||
goods.push({
|
||
id: id,
|
||
name: dicGood.name,
|
||
count: cnt,
|
||
type: dicGood.goodType,
|
||
isCurrency: false
|
||
});
|
||
}
|
||
return goods;
|
||
}
|
||
|
||
// 消耗品
|
||
async function rewardCurrency (roleId: string, dicGood: any, data: {id:number,cnt:number }) {
|
||
|
||
let goods = new Array();
|
||
let {id, cnt} = data;
|
||
let dicCurrency = CURRENCY.get(id);
|
||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
|
||
await RoleModel.addGoldFree(roleId, cnt);
|
||
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
|
||
await RoleModel.addCoin(roleId, cnt);
|
||
} else if(dicCurrency.type == CURRENCY_TYPE.ACTION_POINT) { // 处理体力
|
||
await setAp(Date.now(), roleId, cnt);
|
||
}
|
||
|
||
goods.push({
|
||
id: id,
|
||
name: dicGood.name,
|
||
count: cnt,
|
||
type: dicGood.goodType,
|
||
isCurrency: true,
|
||
currencyType: dicCurrency.type
|
||
});
|
||
return goods;
|
||
}
|
||
|
||
export async function handleCost(roleId: string, sid: string, goods: Array<{id: number, count: number}>) {
|
||
// 检查道具数量
|
||
let role, costGold = 0, costCoin = 0, items = new Array<{id: number, count: number}>(), ids = new Array<number>() ;
|
||
for(let {id, count} of goods) {
|
||
let goodInfo = getGoodById(id);
|
||
if(goodInfo.goodType == GOOD_TYPE.CONSUMES|| goodInfo.goodType == GOOD_TYPE.SCRIPT) { // 消耗品
|
||
let {isCurrency} = ITID.get(goodInfo.itid);
|
||
if(isCurrency) { // 货币
|
||
if(!role) role = await RoleModel.findByRoleId(roleId);
|
||
let dicCurrency = CURRENCY.get(id);
|
||
if(dicCurrency.type == CURRENCY_TYPE.GOLD) { // 处理元宝
|
||
if(role.gold < count + costGold) return false;
|
||
costGold += count;
|
||
} else if(dicCurrency.type == CURRENCY_TYPE.COIN) { // 处理铜币
|
||
if(role.coin < count + costCoin) return false;
|
||
costCoin += count;
|
||
}
|
||
} else {
|
||
let findItem = items.find(cur => cur.id == id);
|
||
if(findItem) {
|
||
findItem.count += count;
|
||
} else {
|
||
items.push({id, count}); ids.push(id);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
for(let {id, count} of items) {
|
||
let item = await ItemModel.findbyRoleAndGidAndCount(roleId, id, count);
|
||
if(!item) return false
|
||
}
|
||
|
||
// 推送参数
|
||
let gold = 0, coin = 0, resultGoods = [];
|
||
if(costGold > 0) {
|
||
role = await RoleModel.costGold(roleId, costGold);
|
||
if(role) gold = role.gold;
|
||
}
|
||
if(costCoin > 0) {
|
||
role = await RoleModel.costCoin(roleId, costCoin);
|
||
if(role) coin = role.coin;
|
||
}
|
||
if(items.length > 0) {
|
||
let {hasError, result} = await ItemModel.decreaseItems(roleId, items);
|
||
if(hasError) return;
|
||
resultGoods = result;
|
||
}
|
||
|
||
let uids = [{uid: roleId, sid}];
|
||
pinus.app.get('channelService').pushMessageByUids('onItemUpdate', resResult(STATUS.SUCCESS, {goods: resultGoods, gold, coin} ), uids);
|
||
|
||
return true;
|
||
}
|
||
|
||
export async function addItems(roleId: string, sid: string, goods: Array<{id: number, count: number}>) {
|
||
let showItems = [];
|
||
let equips = [];
|
||
let currencys = [];
|
||
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) {
|
||
let {type, isCurrency} = ITID.get(goodInfo.itid);
|
||
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 {
|
||
|
||
}
|
||
}
|
||
}
|
||
} |