添加主线关卡和每日关卡
This commit is contained in:
166
game-server/app/servers/battle/handler/battleUtils.ts
Normal file
166
game-server/app/servers/battle/handler/battleUtils.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
|
||||
import { ActionPointModel } from '../../../db/ActionPoint';
|
||||
import { BattleDropModel } from '../../../db/BattleDrop';
|
||||
import { CounterModel } from '../../../db/Counter';
|
||||
import { EquipModel } from '../../../db/Equip';
|
||||
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { decodeStr } from '../../../util/util';
|
||||
import { ACTION_POIN, BATTLE_REWARD_TYPE, GOOD_TYPE } from '../../../consts/consts';
|
||||
|
||||
export async function getAp(now: number, roleId: string) {
|
||||
let dataAp = await ActionPointModel.getAp(roleId);
|
||||
const maxAp = ACTION_POIN.MAX; // 最大体力值
|
||||
const per = ACTION_POIN.PER; // 恢复时间(ms)
|
||||
let {ap, refTime} = dataAp;
|
||||
if(ap >= maxAp) {
|
||||
// 体力溢出不需要做时间的处理
|
||||
return { ap, maxAp, refTime: now, apRemainTime:0, isOver: true }
|
||||
} else {
|
||||
if(refTime > now) refTime = now; // refTime:每次记录时候最近的一个整的时间点,绝对不会大于now
|
||||
let n = Math.floor((now - refTime)/per); // 上次记录到现在可以增长多少体力
|
||||
ap += n; // 增加上
|
||||
if(ap >= maxAp) { // 加上后溢出了
|
||||
ap = maxAp;
|
||||
return { ap, maxAp, refTime: now, apRemainTime:0, isOver: true }
|
||||
} else {
|
||||
refTime += n * per; // 更新refTime到离现在最近的一个整的时间点
|
||||
let apRemainTime = Math.floor((refTime + per - now)/1000); // 恢复下一点需要多少时间
|
||||
|
||||
return { ap, maxAp, refTime, apRemainTime, isOver: false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function setAp(now: number, roleId: string, changeAp: number) {
|
||||
let ApResult = await getAp(now, roleId);
|
||||
let { ap, maxAp, refTime, apRemainTime, isOver } = ApResult; // 更新ap
|
||||
ap += changeAp;
|
||||
if(ap >= maxAp) { // 溢出
|
||||
refTime = now;
|
||||
apRemainTime = 0;
|
||||
}
|
||||
if(ap < 0) return null // 体力不足
|
||||
await ActionPointModel.saveAp(roleId, ap, refTime);
|
||||
|
||||
if(isOver && ap < maxAp) apRemainTime = Math.floor(ACTION_POIN.PER / 1000); // 特殊处理
|
||||
return {ap, maxAp, refTime, apRemainTime}
|
||||
}
|
||||
|
||||
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}>;
|
||||
|
||||
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 obj of reward) this.rewards.push({type: BATTLE_REWARD_TYPE.FIX_REWARD, ...obj, count: obj.count * num});
|
||||
}
|
||||
}
|
||||
private handleConditionReward(num: number) {
|
||||
if(this.isSuccess) {
|
||||
let {conditionReward} = this.warInfo;
|
||||
let reward = decodeStr('conditionReward', conditionReward);
|
||||
for(let obj of reward) {
|
||||
if(this.condition.get(obj.condition)) {
|
||||
this.rewards.push({type: BATTLE_REWARD_TYPE.CONDITION_REWARD, ...obj, count: obj.count * num});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleRandomReward(num: number) {
|
||||
if(this.isSuccess) {
|
||||
let {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, ...obj});
|
||||
}
|
||||
}
|
||||
await BattleDropModel.updateByGid(this.roleId, this.battleId, gid, {
|
||||
getNum, allNum, getSum, allSum
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async saveReward(num: number) {
|
||||
this.rewards = new Array();
|
||||
await this.handleFixReward(num);
|
||||
await this.handleConditionReward(num);
|
||||
await this.handleRandomReward(num);
|
||||
|
||||
let returnGoods = new Array();
|
||||
for(let goods of this.rewards) {
|
||||
let goodInfo = getGoodById(goods.gid);
|
||||
if(goodInfo.good_type == GOOD_TYPE.EQUIP) { // 装备
|
||||
let result = await this.rewardWeapons(goodInfo, {id: goods.gid, cnt: goods.count });
|
||||
for(let obj of result) {
|
||||
returnGoods.push({dropType: goods.type, ...obj})
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnGoods;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async rewardWeapons (dicGood: any, weapon: {id:number,cnt:number }) {
|
||||
|
||||
let weaponsData = [];
|
||||
let cnt = weapon.cnt;
|
||||
while (cnt > 0) {
|
||||
const seqId = await CounterModel.getNewCounter('eid');
|
||||
const equipInfo = {
|
||||
roleId: this.roleId,
|
||||
roleName: this.roleName,
|
||||
eid: weapon.id,
|
||||
eName: dicGood.name,
|
||||
seqId,
|
||||
quality: dicGood.lv,
|
||||
type: dicGood.good_type
|
||||
}
|
||||
const equip = await EquipModel.createEquip(equipInfo);
|
||||
cnt -= 1;
|
||||
weaponsData.push(equip);
|
||||
}
|
||||
return weaponsData;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user