Files
ZYZ/game-server/app/services/rewardService.ts
2020-11-25 20:31:42 +08:00

122 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { GOOD_TYPE, ITID, CURRENCY, CURRENCY_TYPE, COUNTER } from './../consts/consts';
import { EquipModel } from './../db/Equip';
import { CounterModel } from './../db/Counter';
import { decodeStr } from '../pubUtils/util';
import { getGoodById } from '../pubUtils/gamedata';
import { RoleModel } from '../db/Role';
import { setAp } from './actionPointService';
import { ItemModel } from '../db/Item';
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: 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;
}