添加主线关卡和每日关卡
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { Application, BackendSession } from 'pinus';
|
||||
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||||
import { BattleSweepRecordModel } from '../../../db/BattleSweepRecord';
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { CounterModel } from '../../../db/Counter';
|
||||
import { HeroModel } from '../../../db/Hero';
|
||||
import { EquipModel } from '../../../db/Equip';
|
||||
import { genCode } from '../../../util/util';
|
||||
import { getAp, setAp, WarReward } from './battleUtils';
|
||||
import { WAR_TYPE } from '../../../consts/consts';
|
||||
import { checkDaily, checkDailyAndIncrease } from './dailyBattleHandler';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new NormalBattleHandler(app);
|
||||
@@ -14,25 +15,44 @@ export class NormalBattleHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
// 进入关卡前,记录信息,生成唯一标识
|
||||
// 获取关卡列表
|
||||
async checkBattle(msg: {battleId: number, heroes: Array<any> }, session: BackendSession) {
|
||||
const { battleId, heroes } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let warInfo = getWarById(battleId);
|
||||
if(!warInfo) {
|
||||
return {
|
||||
code: 202,
|
||||
data: "缺少关卡信息"
|
||||
}
|
||||
return { code: 202, data: "缺少关卡信息" }
|
||||
}
|
||||
if(!warInfo.hasOwnProperty('cost')) {
|
||||
warInfo['cost'] = 0;
|
||||
}
|
||||
|
||||
let apJson = await getAp(Date.now(), roleId);
|
||||
let {ap} = apJson;
|
||||
if(ap < warInfo.cost) {
|
||||
return { code: 202, data: "体力不足" }
|
||||
}
|
||||
|
||||
// 前置关卡是否挑战过
|
||||
let previousGk = warInfo.previousGk;
|
||||
if(previousGk) {
|
||||
let preBattle = await BattleRecordModel.getBattleRecordByIdAndStatus(roleId, previousGk, 1);
|
||||
if(!preBattle) return {code: 202, data: '需要完成上一关才可以挑战'};
|
||||
}
|
||||
|
||||
let dailyNum = {};
|
||||
if(warInfo.war_type == WAR_TYPE.DAILY) {
|
||||
let checkResult = await checkDaily(roleId, battleId, 1);
|
||||
if(checkResult.status == -1) {
|
||||
return {code: 202, data: checkResult.msg}
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
}
|
||||
const battleCode = genCode(8);
|
||||
const BattleRecord = await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||||
$set: {
|
||||
roleId,
|
||||
roleName,
|
||||
battleId,
|
||||
roleId, roleName, battleId,
|
||||
status: 0,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.war_type,
|
||||
@@ -42,28 +62,49 @@ export class NormalBattleHandler {
|
||||
|
||||
let {status} = BattleRecord;
|
||||
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
battleId, battleCode, status
|
||||
battleId, battleCode, status, apJson, dailyNum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关卡结算,记录使用的武将,获得奖励
|
||||
async battleEnd(msg: {battleCode: string, battleId: number, isSuccess: boolean, heroes: Array<any>, }, session: BackendSession) {
|
||||
// 关卡列表
|
||||
async getBattleList(msg: {type: number }, session: BackendSession) {
|
||||
const { type } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
|
||||
const { battleCode, battleId, isSuccess, heroes } = msg;
|
||||
const BattleRecord = await BattleRecordModel.getBattleList(roleId, type);
|
||||
let result = []; // 去重
|
||||
for(let br of BattleRecord) {
|
||||
let index = result.findIndex(cur => cur.battleId == br.battleId);
|
||||
if(index == -1) {
|
||||
result.push(br);
|
||||
}
|
||||
}
|
||||
|
||||
return { code: 200, data: result }
|
||||
}
|
||||
|
||||
// 关卡结算,记录使用的武将,获得奖励
|
||||
async battleEnd(msg: {battleCode: string, battleId: number, isSuccess: boolean, star: number, heroes: Array<any>, }, session: BackendSession) {
|
||||
|
||||
const { battleCode, battleId, isSuccess, heroes, star } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let warInfo = getWarById(battleId);
|
||||
if(!warInfo) {
|
||||
return { code: 202, data: "缺少关卡信息" }
|
||||
}
|
||||
if(!warInfo.hasOwnProperty('cost')) {
|
||||
warInfo['cost'] = 0;
|
||||
}
|
||||
|
||||
const BattleRecord = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
||||
if(!BattleRecord || BattleRecord.status != 0) {
|
||||
return {
|
||||
code: 202,
|
||||
data: '关卡状态错误'
|
||||
}
|
||||
return { code: 202, data: '关卡状态错误' }
|
||||
}
|
||||
|
||||
let flag = 1; // 对比hero信息
|
||||
@@ -72,21 +113,44 @@ export class NormalBattleHandler {
|
||||
if(dbHeroes.indexOf(hid) == -1) flag = 0;
|
||||
}
|
||||
if(!flag) {
|
||||
return {
|
||||
code: 202,
|
||||
data: '关卡信息不同'
|
||||
}
|
||||
return { code: 202, data: '关卡信息不同' }
|
||||
}
|
||||
|
||||
let params = {}, reward: Array<any>;
|
||||
const now = Date.now(); // 当前时间戳
|
||||
let apJson = await setAp(now, roleId, -1 * warInfo.cost); // 扣除体力
|
||||
if(!apJson) {
|
||||
return { code: 202, data: '体力不足' }
|
||||
}
|
||||
|
||||
let dailyNum = {};
|
||||
if(warInfo.war_type == WAR_TYPE.DAILY) {
|
||||
let checkResult = await checkDailyAndIncrease(roleId, battleId, 1, false);
|
||||
if(checkResult.status == -1) {
|
||||
return {code: 202, data: checkResult.msg}
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
}
|
||||
|
||||
let warReward = new WarReward(roleId, roleName, battleId, isSuccess);
|
||||
let params = {};
|
||||
if(isSuccess) { // 挑战胜利
|
||||
params = {
|
||||
$set: {
|
||||
status: 1,
|
||||
star,
|
||||
record: { heroes }
|
||||
}
|
||||
}
|
||||
reward = await this.handleReward(roleId, roleName, warInfo.reward);
|
||||
|
||||
// 是否首通
|
||||
let condition1 = await BattleRecordModel.getBattleRecordByIdAndStatus(roleId, battleId, 1);
|
||||
if(!condition1) warReward.setCondition(0, true);
|
||||
// 是否首次3星
|
||||
if(star == 3) {
|
||||
let condition2 = await BattleRecordModel.getBattleRecordByIdAndStar(roleId, battleId, 3);
|
||||
if(!condition2) warReward.setCondition(1, true);
|
||||
}
|
||||
|
||||
} else { // 挑战失败
|
||||
params = {
|
||||
$set: {
|
||||
@@ -94,9 +158,10 @@ export class NormalBattleHandler {
|
||||
record: { heroes }
|
||||
}
|
||||
}
|
||||
reward = [];
|
||||
}
|
||||
|
||||
let reward = await warReward.saveReward(1);
|
||||
|
||||
const updateResult = await BattleRecordModel.updateBattleRecordByCode(battleCode, params, true);
|
||||
let { status } = updateResult;
|
||||
|
||||
@@ -104,78 +169,71 @@ export class NormalBattleHandler {
|
||||
code: 200,
|
||||
data: {
|
||||
battleCode, battleId, status,
|
||||
goods: reward
|
||||
goods: reward,
|
||||
apJson,
|
||||
dailyNum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async handleReward(roleId: string, roleName: string, rewardStr:string) {
|
||||
let {weapons, armors, items, souls} = this.decodeReward(rewardStr);
|
||||
let addWeapons = await this.rewardWeapons(roleId, roleName, weapons);
|
||||
// 暂时只处理装备
|
||||
// let addArmors = await this.rewardArmors(roleId, roleName, armors);
|
||||
// let addItems = await this.rewardItems(roleId, roleName, items);
|
||||
// let addSouls = await this.rewardSouls(roleId, roleName, souls);
|
||||
|
||||
let result = [].concat(addWeapons);
|
||||
return result;
|
||||
}
|
||||
|
||||
private async rewardWeapons (roleId: string, roleName:string, weapons: Array<{id:number,cnt:number, type: number}>) {
|
||||
|
||||
let weaponsData = [];
|
||||
for (let weapon of weapons) {
|
||||
let cnt = weapon.cnt;
|
||||
let g = getGoodById(weapon.id);
|
||||
while (cnt > 0) {
|
||||
const seqId = await CounterModel.getNewCounter('eid');
|
||||
const equipInfo = {
|
||||
roleId,
|
||||
roleName,
|
||||
eid: weapon.id,
|
||||
eName: g.name,
|
||||
seqId,
|
||||
type: weapon.type,
|
||||
lv: g.lv
|
||||
}
|
||||
const equip = await EquipModel.createEquip(equipInfo);
|
||||
cnt -= 1;
|
||||
weaponsData.push(equip);
|
||||
}
|
||||
async battleSweep(msg: {battleId: number, count: number }, session: BackendSession) {
|
||||
|
||||
const { battleId, count } = msg;
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
let warInfo = getWarById(battleId);
|
||||
if(!warInfo) {
|
||||
return { code: 202, data: "缺少关卡信息" }
|
||||
}
|
||||
// 校验是否三星通关过
|
||||
let condition1 = await BattleRecordModel.getBattleRecordByIdAndStar(roleId, battleId, 3);
|
||||
if(!condition1) {
|
||||
return { code: 202, data: "需要3星通过关卡才可以扫荡" }
|
||||
}
|
||||
return weaponsData;
|
||||
}
|
||||
|
||||
private decodeReward(rewardStr: string, multiple=1) {
|
||||
let weapons = [];
|
||||
let armors = [];
|
||||
let items = [];
|
||||
let souls = [];
|
||||
rewardStr.split('|').forEach((rStr) => {
|
||||
// r[0]: type, r[1]: id, r[2]: count
|
||||
let r = rStr.split('&');
|
||||
let type = parseInt(r[0] || '')||0;
|
||||
let id = parseInt(r[1] || '') || 0;
|
||||
let cnt = (parseInt(r[2] || '') || 0) * multiple;
|
||||
if (id !== 0 && cnt !== 0) {
|
||||
switch (r[0]) {
|
||||
case '0':
|
||||
items.push({id, cnt, type});
|
||||
break;
|
||||
case '1':
|
||||
weapons.push({id, cnt, type});
|
||||
break;
|
||||
case '2':
|
||||
armors.push({id, cnt, type});
|
||||
break;
|
||||
case '3':
|
||||
souls.push({id, cnt, type});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 扣体力
|
||||
if(!warInfo.hasOwnProperty('cost')) {
|
||||
warInfo['cost'] = 0;
|
||||
}
|
||||
const now = Date.now(); // 当前时间戳
|
||||
let apJson = await setAp(now, roleId, -1 * warInfo.cost * count); // 扣除体力
|
||||
if(!apJson) {
|
||||
return { code: 202, data: '体力不足' }
|
||||
}
|
||||
|
||||
// 扫荡次数
|
||||
let dailyNum = {};
|
||||
if(warInfo.war_type == WAR_TYPE.DAILY) {
|
||||
let checkResult = await checkDailyAndIncrease(roleId, battleId, count, true);
|
||||
if(checkResult.status == -1) {
|
||||
return {code: 202, data: checkResult.msg}
|
||||
}
|
||||
dailyNum = { type: checkResult.type, count: checkResult.count, sum: checkResult.sum };
|
||||
}
|
||||
|
||||
// 发奖励
|
||||
let warReward = new WarReward(roleId, roleName, battleId, true);
|
||||
let result = await warReward.saveReward(count);
|
||||
|
||||
// 扫荡记录
|
||||
await BattleSweepRecordModel.saveBattleSweepRecordById(roleId, battleId, {
|
||||
$set: {
|
||||
roleName,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.war_type
|
||||
},
|
||||
$inc: { count }
|
||||
});
|
||||
return {weapons, armors, items, souls};
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
battleId, count,
|
||||
goods: result,
|
||||
apJson,
|
||||
dailyNum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user