206 lines
7.8 KiB
TypeScript
206 lines
7.8 KiB
TypeScript
/**
|
||
* 战场奖励发放 对象
|
||
* 支持战场相关的奖励格式,目前包括fixedReward, randomReward, conditionReward
|
||
*/
|
||
|
||
import { BattleDropModel } from '../db/BattleDrop';
|
||
import { getWarById, getBluePrtByQuality, getGamedata } from '../pubUtils/gamedata';
|
||
import { decodeStr, getRefTime, getRandomWithWeight, getRandomByLen } from '../pubUtils/util';
|
||
import { BATTLE_REWARD_TYPE, BLUEPRT_CONST } from '../consts';
|
||
import { addItems } from './rewardService';
|
||
import { BattleBlueprtDropModel } from '../db/BattleBlueprtDrop'
|
||
import { RoleModel } from '../db/Role';
|
||
|
||
export class WarReward {
|
||
private roleId: string;
|
||
private roleName: string;
|
||
private sid: string;
|
||
private battleId: number;
|
||
private condition: Map<number, boolean>;
|
||
private warInfo: any;
|
||
private isSuccess: boolean;
|
||
private rewards: Array<{type: number, id: number, count: number, times: number}>;
|
||
private fixReward: string;
|
||
private conditionReward: string;
|
||
private randomReward: string;
|
||
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 = getWarById(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: string) {
|
||
let str = this.fixReward;
|
||
let last = str.substr(str.length-1, 1);
|
||
if(last == '&') str = str.substr(0, str.length - 1);
|
||
if(str.length) {
|
||
this.fixReward = str + '|' + reward;
|
||
} else {
|
||
this.fixReward = str + reward;
|
||
}
|
||
}
|
||
|
||
public resetFixReward(reward: string) {
|
||
this.fixReward = reward;
|
||
}
|
||
|
||
public setConditionReward(reward: string) {
|
||
let str = this.conditionReward;
|
||
let last = str.substr(str.length-1, 1);
|
||
if(last == '&') str = str.substr(0, str.length - 1);
|
||
if(str.length) {
|
||
this.conditionReward = str + '|' + reward;
|
||
} else {
|
||
this.conditionReward = str + reward;
|
||
}
|
||
}
|
||
|
||
public setRandomReward(reward: string) {
|
||
let str = this.randomReward;
|
||
let last = str.substr(str.length-1, 1);
|
||
if(last == '&') str = str.substr(0, str.length - 1);
|
||
if(str.length) {
|
||
this.randomReward = str + '|' + reward;
|
||
} else {
|
||
this.randomReward = str + reward;
|
||
}
|
||
}
|
||
|
||
private handleFixReward(num: number, fixReward: string) {
|
||
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, conditionReward: string) {
|
||
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, randomReward: string) {
|
||
|
||
let reward = decodeStr('randomReward', randomReward);
|
||
for(let obj of reward) {
|
||
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
|
||
});
|
||
}
|
||
}
|
||
|
||
private async handlerBlueprtReward(num: number) {
|
||
const refTime = getRefTime(new Date(), BLUEPRT_CONST.REFRESH_TIME);
|
||
const battleBlueprtDrop = await BattleBlueprtDropModel.findByTime(this.roleId, refTime);
|
||
if(battleBlueprtDrop) {
|
||
let { getNum = 0, curCostAp = 0, getSum = 0, costAp = 0 } = battleBlueprtDrop;
|
||
for(let i = 0; i < num; i ++) {
|
||
let flag = false; // 是否可以获得
|
||
if( curCostAp >= BLUEPRT_CONST.PER_AP) {
|
||
curCostAp = curCostAp - BLUEPRT_CONST.PER_AP; getNum = 0;
|
||
}
|
||
costAp += this.costAp; curCostAp += this.costAp;
|
||
if(getNum == 0) {
|
||
let r = Math.random();
|
||
console.log(r, 1/BLUEPRT_CONST.PER_AP*curCostAp);
|
||
if(r <= 1/BLUEPRT_CONST.PER_AP*curCostAp || (curCostAp >= BLUEPRT_CONST.PER_AP) ) {
|
||
flag = true; // 独立概率随机
|
||
}
|
||
}
|
||
if(getSum >= BLUEPRT_CONST.DAILY_CNT) {
|
||
flag = false;
|
||
}
|
||
if(flag) {
|
||
getNum ++; getSum++;
|
||
const obj = await this.randomBlueprt();
|
||
if(obj) {
|
||
this.rewards.push({type: BATTLE_REWARD_TYPE.RANDOM_REWARD, times: i +1, ...obj});
|
||
}
|
||
}
|
||
}
|
||
await BattleBlueprtDropModel.updateByTime(this.roleId, refTime, {
|
||
getNum, curCostAp, getSum, costAp
|
||
});
|
||
}
|
||
}
|
||
|
||
private async randomBlueprt() {
|
||
const { lv } = await RoleModel.findByRoleId(this.roleId);
|
||
const dicPossibility = getGamedata('dic_blueprt_possibility');
|
||
|
||
const result = dicPossibility.find(cur => {return cur.min <= lv && cur.max >= lv});
|
||
|
||
if(result) {
|
||
const dicOdds = decodeStr('possibility', result.possibility);
|
||
const {dic: {id}} = getRandomWithWeight(dicOdds);
|
||
|
||
const blueprtList = getBluePrtByQuality(id);
|
||
const gid = getRandomByLen(blueprtList);
|
||
return {id: gid, count:1}
|
||
} else {
|
||
return null
|
||
}
|
||
}
|
||
|
||
public async saveReward(num: number) {
|
||
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.sort((a, b) => a.times - b.times)
|
||
await addItems(this.roleId, this.roleName, this.sid, rewards);
|
||
return rewards;
|
||
}
|
||
|
||
} |