104 lines
3.8 KiB
TypeScript
104 lines
3.8 KiB
TypeScript
/**
|
||
* 战场奖励发放 对象
|
||
* 支持战场相关的奖励格式,目前包括fixedReward, randomReward, conditionReward
|
||
*/
|
||
|
||
import { BattleDropModel } from '../db/BattleDrop';
|
||
import { getWarById } from '../pubUtils/gamedata';
|
||
import { decodeStr } from '../pubUtils/util';
|
||
import { BATTLE_REWARD_TYPE } from '../consts/consts';
|
||
import { handleReward } from './rewardService';
|
||
|
||
export class WarReward {
|
||
roleId: string;
|
||
roleName: string;
|
||
battleId: number;
|
||
condition: Map<number, boolean>;
|
||
warInfo: any;
|
||
isSuccess: boolean;
|
||
rewards: Array<{type: number, gid: number, count: number, times: number}>;
|
||
|
||
constructor(roleId: string, roleName: string, battleId: number, isSuccess: boolean) {
|
||
this.roleId = roleId;
|
||
this.roleName = roleName;
|
||
this.battleId = battleId;
|
||
this.condition = new Map();
|
||
this.warInfo = getWarById(battleId);
|
||
this.isSuccess = isSuccess;
|
||
}
|
||
|
||
public setCondition(id: number, isOk: boolean) {
|
||
this.condition.set(id, isOk);
|
||
}
|
||
|
||
private handleFixReward(num: number) {
|
||
if(this.isSuccess) { // 成功了才给固定奖励
|
||
let {fixReward = ''} = this.warInfo;
|
||
let reward = decodeStr('fixReward', fixReward);
|
||
for(let i = 0; i < num; i++) {
|
||
for(let obj of reward) {
|
||
this.rewards.push({type: BATTLE_REWARD_TYPE.FIX_REWARD, times: i +1, ...obj, count: obj.count });
|
||
}
|
||
}
|
||
}
|
||
}
|
||
private handleConditionReward(num: number) {
|
||
if(this.isSuccess) {
|
||
let {conditionReward = ''} = this.warInfo;
|
||
let reward = decodeStr('conditionReward', conditionReward);
|
||
for(let i = 0; i < num; i++) {
|
||
for(let obj of reward) {
|
||
if(this.condition.get(obj.condition)) {
|
||
this.rewards.push({type: BATTLE_REWARD_TYPE.CONDITION_REWARD, times: i +1, ...obj, count: obj.count});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private async handleRandomReward(num: number) {
|
||
if(this.isSuccess) {
|
||
let {RandomReward:randomReward = ''} = this.warInfo;
|
||
|
||
let reward = decodeStr('randomReward', randomReward);
|
||
for(let obj of reward) {
|
||
let { gid, frequency } = obj;
|
||
let dropHistory = await BattleDropModel.findByGid(this.roleId, this.battleId, gid);
|
||
let { getNum = 0, allNum = 0, getSum = 0, allSum = 0 } = dropHistory;
|
||
for(let i = 0; i < num; i ++) {
|
||
let flag = false; // 是否可以获得
|
||
if(allNum + 1 > frequency) {
|
||
allNum = 0; getNum = 0;
|
||
}
|
||
allNum++; allSum++;
|
||
if(getNum == 0) {
|
||
let r = Math.random();
|
||
if(r <= 1/frequency*allNum || (allNum >= frequency) ) {
|
||
flag = true; // 独立概率随机
|
||
}
|
||
}
|
||
if(flag) {
|
||
getNum ++; getSum++;
|
||
this.rewards.push({type: BATTLE_REWARD_TYPE.RANDOM_REWARD, times: i +1, ...obj});
|
||
}
|
||
}
|
||
await BattleDropModel.updateByGid(this.roleId, this.battleId, gid, {
|
||
getNum, allNum, getSum, allSum
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
public async saveReward(num: number) {
|
||
this.rewards = new Array();
|
||
// let warType = this.warInfo.warType;
|
||
|
||
this.handleFixReward(num);
|
||
this.handleConditionReward(num);
|
||
await this.handleRandomReward(num);
|
||
|
||
let returnGoods = await handleReward(this.roleId, this.roleName, this.rewards);
|
||
return returnGoods;
|
||
}
|
||
|
||
} |