140 lines
5.2 KiB
TypeScript
140 lines
5.2 KiB
TypeScript
/**
|
|
* 战场奖励发放 对象
|
|
* 支持战场相关的奖励格式,目前包括fixedReward, randomReward, conditionReward
|
|
*/
|
|
|
|
import { BattleDropModel } from '../db/BattleDrop';
|
|
import { getRandEelmWithWeight, getRandSingleEelm, getReasonByWarType } from '../pubUtils/util';
|
|
import { BATTLE_REWARD_TYPE, BLUEPRT_CONST } from '../consts';
|
|
import { addItems } from './role/rewardService';
|
|
import { RoleModel } from '../db/Role';
|
|
import { gameData } from '../pubUtils/data';
|
|
import { DicWar } from '../pubUtils/dictionary/DicWar';
|
|
import { RewardInter } from '../pubUtils/interface';
|
|
import { getZeroPointD } from '../pubUtils/timeUtil';
|
|
import { combineItemAndJewels } from './role/util';
|
|
|
|
export class WarReward {
|
|
private roleId: string;
|
|
private roleName: string;
|
|
private sid: string;
|
|
private battleId: number;
|
|
private condition: Map<number, boolean>;
|
|
private warInfo: DicWar;
|
|
private isSuccess: boolean;
|
|
private rewards: Array<{type?: number, id: number, count: number, times?: number}>;
|
|
private fixReward: Array<{id: number, count: number}>;
|
|
private conditionReward: Array<{id: number, count: number, condition: number}>;
|
|
private randomReward: Array<{id: number, count: number, frequency: number}>;
|
|
private costAp: number;
|
|
|
|
constructor(roleId: string, roleName: string, sid: string, battleId: number, isSuccess: boolean) {
|
|
this.roleId = roleId;
|
|
this.roleName = roleName;
|
|
this.sid = sid;
|
|
this.battleId = battleId;
|
|
this.condition = new Map();
|
|
this.warInfo = gameData.war.get(battleId);
|
|
|
|
let { fixReward, conditionReward, randomReward } = this.warInfo;
|
|
this.fixReward = fixReward;
|
|
this.conditionReward = conditionReward;
|
|
this.randomReward = randomReward;
|
|
|
|
this.isSuccess = isSuccess;
|
|
this.costAp = this.warInfo.cost;
|
|
}
|
|
|
|
public setCondition(id: number, isOk: boolean) {
|
|
this.condition.set(id, isOk);
|
|
}
|
|
|
|
public setFixReward(reward: RewardInter[]) {
|
|
for(let r of reward) {
|
|
this.fixReward.push(r);
|
|
}
|
|
}
|
|
|
|
public resetFixReward(reward: RewardInter[]) {
|
|
this.fixReward = reward;
|
|
}
|
|
|
|
public setConditionReward(reward: {id: number, count: number, condition: number}[]) {
|
|
for(let r of reward) {
|
|
this.conditionReward.push(r);
|
|
}
|
|
}
|
|
|
|
public setRandomReward(reward: {id: number, count: number, frequency: number}[]) {
|
|
for(let r of reward) {
|
|
this.randomReward.push(r);
|
|
}
|
|
}
|
|
|
|
private handleFixReward(num: number, fixReward: RewardInter[]) {
|
|
for(let i = 0; i < num; i++) {
|
|
for(let obj of fixReward) {
|
|
this.rewards.push({type: BATTLE_REWARD_TYPE.FIX_REWARD, times: i +1, ...obj, count: obj.count });
|
|
}
|
|
}
|
|
}
|
|
private handleConditionReward(num: number, conditionReward: {id: number, count: number, condition: number}[]) {
|
|
for(let i = 0; i < num; i++) {
|
|
for(let obj of conditionReward) {
|
|
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, randomReward: {id: number, count: number, frequency: number}[]) {
|
|
|
|
for(let obj of randomReward) {
|
|
let { id, frequency } = obj;
|
|
let dropHistory = await BattleDropModel.findByGid(this.roleId, this.battleId, id);
|
|
let { getNum = 0, allNum = 0, getSum = 0, allSum = 0 } = dropHistory;
|
|
for(let i = 0; i < num; i ++) {
|
|
let flag = false; // 是否可以获得
|
|
if(allNum >= 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, id, {
|
|
getNum, allNum, getSum, allSum
|
|
});
|
|
}
|
|
}
|
|
|
|
public async saveReward(num: number, combine: boolean = false) {
|
|
this.rewards = new Array();
|
|
// let warType = this.warInfo.warType;
|
|
|
|
if(this.isSuccess) { // 成功了才给固定奖励
|
|
console.log(this.fixReward)
|
|
if(this.fixReward) this.handleFixReward(num, this.fixReward);
|
|
if(this.conditionReward) this.handleConditionReward(num, this.conditionReward);
|
|
if(this.randomReward) await this.handleRandomReward(num, this.randomReward);
|
|
|
|
// if(this.costAp > 0) await this.handlerBlueprtReward(num);
|
|
}
|
|
|
|
let rewards = this.rewards;
|
|
if(combine) {
|
|
rewards = combineItemAndJewels(rewards);
|
|
}
|
|
await addItems(this.roleId, this.roleName, this.sid, rewards, getReasonByWarType(this.warInfo.warType));
|
|
return rewards;
|
|
}
|
|
} |