远征结算和宝箱领取

This commit is contained in:
luying
2020-10-14 20:16:41 +08:00
parent 57d60be3a2
commit 2b1575516f
11 changed files with 1819 additions and 116 deletions

View File

@@ -1,8 +1,10 @@
import { ActionPointModel } from '../../../db/ActionPoint';
import { BattleDropModel } from '../../../db/BattleDrop';
import { ExpeditionPointModel } from '../../../db/ExpeditionPoint';
import { RoleModel } from '../../../db/Role';
import { getWarById, getWarJsons } from '../../../util/gamedata';
import { getWarById, getWarJsons, getGamedata } from '../../../util/gamedata';
import { decodeStr, Reward } from '../../../util/util';
import { ACTION_POIN, BATTLE_REWARD_TYPE, WAR_JSON_ATTRIBUTE_TYPE } from '../../../consts/consts';
@@ -121,6 +123,8 @@ export class WarReward {
public async saveReward(num: number) {
this.rewards = new Array();
let warType = this.warInfo.warType;
await this.handleFixReward(num);
await this.handleConditionReward(num);
await this.handleRandomReward(num);
@@ -141,7 +145,7 @@ export async function matchPlayers(scale: number, range: number, myCe: number ,e
return false
}
export async function matchRobots(scale: number, myCe: number, robotCe: number, warJsonIndex:any, role, enemyObj: {enemyFrom: number, enemyId: string, enemies: Array<any> }) {
export async function matchRobots(scale: number, myCe: number, robotCe: number, warJsonIndex:any, lv: number, enemyObj: {enemyFrom: number, enemyId: string, enemies: Array<any> }) {
let {json: dicWarJson, fileName } = getWarJsons(warJsonIndex);
if(dicWarJson) {
enemyObj.enemyFrom = 2;
@@ -152,8 +156,9 @@ export async function matchRobots(scale: number, myCe: number, robotCe: number,
let attribute = decodeWarJsonAttribute(enemy.attribute); // 格式:{'hp':1000, ...}
for(let value in attribute) {
attribute[value] *= ratio;
attribute[value] = Math.round(attribute[value]);
}
enemyObj.enemies.push({...enemy, attribute, lv: role.lv});
enemyObj.enemies.push({...enemy, attribute, lv});
}
return true
@@ -172,4 +177,37 @@ export function decodeWarJsonAttribute(attribute) {
}
}
return obj
}
export async function getPointRewardStatus(roleId: string) {
let role = await RoleModel.findByRoleId(roleId);
let {expeditionPoint} = role;
let dicExpeditionPoint = getGamedata('dic_expedition_point');
let pointRewards = {
expeditionPoint,
rewards: dicExpeditionPoint.map(cur => {
return { point: cur.point, received: false }
})
};
let pointStatusInDatabase = await ExpeditionPointModel.getExpeditionPoint(roleId);
if(pointStatusInDatabase) {
let { rewards = [] } = pointStatusInDatabase;
pointRewards.rewards.forEach(cur => {
let obj = rewards.find(ccur => ccur.point == cur.point);
if(obj) cur.received = obj.received;
});
}
return pointRewards
}
export async function getCEScaleAndRange(roleId: string, curDicExpedition: any) {
// 匹配,判断是不是新手期
const role = await RoleModel.findByRoleId(roleId);
let now = new Date();
let today = now.setHours(0,0,0,0);
let isNew = today - role.createdAt.getTime() <= 3*24*60*60*1000;
let scale = isNew?curDicExpedition.CEScaleNew:curDicExpedition.CEScale;
let range = isNew?curDicExpedition.CERangeNew:curDicExpedition.CERange;
return {scale, range, lv: role.lv}
}

View File

@@ -2,8 +2,8 @@ import { Application, BackendSession, FrontendOrBackendSession } from 'pinus';
import { getGamedata } from '../../../util/gamedata';
import { EventRecordModel } from '../../../db/EventRecord';
import { RoleModel } from '../../../db/Role';
import { genCode, decodeStr, decodeStrSingle, Reward, getRandomWithWeight } from '../../../util/util';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE, EVENT_RANDOM_TYPE_ONE_OPEN } from '../../../consts/consts';
import { genCode, decodeStr, decodeStrSingle, Reward } from '../../../util/util';
import { EVENT_STATUS, EVENT_RECORD_STATUS, EVENT_TYPE } from '../../../consts/consts';
export default function(app: Application) {
return new EventBattleHandler(app);
@@ -177,55 +177,36 @@ async function refreshEvent(num: number, roleId: string, roleName: string, t) {
suitLevel = decodeStrSingle('eventSuitLevel', suitLevel);
return suitLevel.min <= role.lv && suitLevel.max >= role.lv
});
if(EVENT_RANDOM_TYPE_ONE_OPEN) { // 每轮进行记录,保证每次随机出的不重复
let historyRecord = await EventRecordModel.getHostoryEventRecord(roleId);
let {history, turn} = historyRecord;
let randomList = dicEvent.filter(cur => {
return history.find(ccur => {
return ccur.eventId != cur.eventID;
});
let historyRecord = await EventRecordModel.getHostoryEventRecord(roleId);
let {history, turn} = historyRecord;
let randomList = dicEvent.filter(cur => {
return history.find(ccur => {
return ccur.eventId != cur.eventID;
});
console.log(JSON.stringify(randomList));
for(let i = 0; i < num; i++) {
if(randomList.length == 0) { // 刷新的3个事件不重复
turn ++;
randomList = [...dicEvent];
}
let {dic, index} = getRandomWithWeight(randomList);
if(!dic) break;
let eventCode = genCode(8);
let data = await EventRecordModel.saveEventRecord(eventCode, {
roleId, refTime: t, eventId: dic.eventID,
roleName, turn, type: dic.eventType, battleId: dic.warId||0, quality: dic.quality,
status: EVENT_RECORD_STATUS.WAITING
});
event.push(data)
randomList.splice(index, 1);
}
} else {
let randomList = dicEvent;
for(let i = 0; i < num; i++) {
let {dic} = getRandomWithWeight(randomList);
console.log('****', JSON.stringify(randomList))
if(!dic) break;
let eventCode = genCode(8);
let data = await EventRecordModel.saveEventRecord(eventCode, {
roleId, refTime: t, eventId: dic.eventID,
roleName, type: dic.eventType, battleId: dic.warId||0, quality: dic.quality,
status: EVENT_RECORD_STATUS.WAITING
});
event.push(data)
});
console.log(JSON.stringify(randomList));
for(let i = 0; i < num; i++) {
if(randomList.length == 0) { // 一轮刷新过,开始新的一轮,保证所有事件都能刷新一遍
turn ++;
randomList = [...dicEvent];
}
if(randomList.length == 0) break; // 如果还是为0pass
let index = Math.floor(Math.random() * randomList.length);
let dic = randomList[index];
let eventCode = genCode(8);
let data = await EventRecordModel.saveEventRecord(eventCode, {
roleId, refTime: t, eventId: dic.eventID,
roleName, turn, type: dic.eventType, battleId: dic.warId||0, quality: dic.quality,
status: EVENT_RECORD_STATUS.WAITING
});
event.push(data)
randomList.splice(index, 1);
}
return event;
}
function pushEventMsg(app, roleId, channelName, msg ) {
console.log('***pushEventMsg', channelName)
let channelService = app.get('channelService');

View File

@@ -1,11 +1,13 @@
import { Application, BackendSession } from 'pinus';
import { getGamedata } from '../../../util/gamedata';
import { getGamedata, getWarById } from '../../../util/gamedata';
import { BattleRecordModel } from '../../../db/BattleRecord';
import { ExpeditionRecordModel } from '../../../db/ExpeditionRecord';
import { ExpeditionWarRecordModel } from '../../../db/ExpeditionWarRecord';
import { ExpeditionPointModel } from '../../../db/ExpeditionPoint';
import { RoleModel } from '../../../db/Role';
import { calculateSumCE, genCode } from '../../../util/util';
import { matchPlayers, matchRobots } from './battleUtils';
import { calculateSumCE, genCode, Reward, decodeStr } from '../../../util/util';
import { matchPlayers, matchRobots, getAp, setAp, WarReward, getPointRewardStatus, getCEScaleAndRange } from './battleUtils';
import { EXPEDITION_INCREASE_POINT } from '../../../consts/consts';
export default function(app: Application) {
return new ExpeditionBattleHandler(app);
@@ -39,26 +41,12 @@ export class ExpeditionBattleHandler {
let expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCode(expeditionCode);
let curLv = 0;
if(expeditionWarRecord.length > 0) {
curLv = expeditionRecord[expeditionWarRecord.length - 1].expeditionId;
curLv = expeditionWarRecord[expeditionWarRecord.length - 1].expeditionId;
}
// 点数,和宝箱领取状态
let role = await RoleModel.findByRoleId(roleId);
let {expeditionPoint} = role;
let dicExpeditionPoint = getGamedata('dic_expedition_point');
let pointRewards = {
expeditionPoint,
rewards: dicExpeditionPoint.map(cur => {
return { point: cur.point, received: false }
})
};
let pointStatusInDatabase = await ExpeditionPointModel.getExpeditionPoint(roleId);
if(pointStatusInDatabase) {
let { rewards = [] } = pointStatusInDatabase;
pointRewards.rewards = rewards.map(cur => {
return { point: cur.point, received: cur.received }
})
}
let pointRewards = await getPointRewardStatus(roleId);
return {
code: 200,
data: {
@@ -86,7 +74,6 @@ export class ExpeditionBattleHandler {
// 计算我方战斗力(最高五人)
let myCe = await calculateSumCE(roleId, 1, { num: 5 });
console.log('****myCe', myCe);
let enemyObj = {
enemyFrom: 0,
enemyId: '',
@@ -95,24 +82,14 @@ export class ExpeditionBattleHandler {
let dicExpedition = getGamedata('dic_expedition');
let curDicExpedition = dicExpedition.find(cur => cur.id == expeditionId);
// 匹配,判断是不是新手期
const role = await RoleModel.findByRoleId(roleId);
let now = new Date();
let today = now.setHours(0,0,0,0);
let isNew = today - role.createdAt.getTime() <= 3*24*60*60*1000;
let scale = isNew?curDicExpedition.CEScaleNew:curDicExpedition.CEScale;
let range = isNew?curDicExpedition.CERangeNew:curDicExpedition.CERange;
// 获取系数和步长
let {scale, range, lv} = await getCEScaleAndRange(roleId, curDicExpedition);
// 优先匹配其他玩家
let flag = await matchPlayers(scale, range, myCe, enemyObj);
// 当数量不够时使用机器人匹配
if(!flag) {
flag = await matchRobots(scale, myCe, curDicExpedition.ce, curDicExpedition.json, role, enemyObj);
flag = await matchRobots(scale, myCe, curDicExpedition.ce, curDicExpedition.json, lv, enemyObj);
}
console.log('**********flag', flag)
if(!flag) {
return {code: 202, data:'无法匹配对手'};
}
@@ -139,23 +116,177 @@ export class ExpeditionBattleHandler {
* 进入战斗
* 记录我军数据,生成战斗唯一表示,记录状态
*/
async checkBattle(msg: { }, session: BackendSession) {
async checkBattle(msg: { expeditionCode: string, expeditionId: number, battleId: number, heroes: Array<number> }, session: BackendSession) {
const { expeditionCode, expeditionId, battleId, heroes } = msg;
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let warInfo = getWarById(battleId);
if(!warInfo) {
return { code: 202, data: "缺少关卡信息" }
}
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 expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCodeAndId(expeditionCode, expeditionId);
if(!expeditionWarRecord ) {
return {code: 202, data: '需要先匹配好对手'}
}
if(expeditionWarRecord.battleStatus == 1) {
return {code: 202, data: '已挑战过这一关'}
}
const battleCode = genCode(8);
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
$set: {
roleId, roleName, battleId,
status: 0,
warName: warInfo.gk_name,
warType: warInfo.warType,
record: { heroes }
}
}, true);
let result = await ExpeditionWarRecordModel.updateStatus(expeditionCode, expeditionId, 0, battleCode);
return {
code: 200,
data: {
expeditionCode,
expeditionId,
battleId,
battleCode,
battleStatus: result.battleStatus,
apJson
}
}
}
/**
* 战斗结算
* 结算战斗奖励,更新远征状态
*/
async battleEnd(msg: { }, session: BackendSession) {
async battleEnd(msg: { expeditionCode: string, expeditionId: number, battleCode: string, battleId: number, isSuccess: boolean, heroes: Array<{actorId: number, hp: number, ap: number}>, enemies: Array<{dataId: number, hp: number, ap: number}>, star: number }, session: BackendSession) {
const { expeditionCode, battleCode, battleId, expeditionId, isSuccess, heroes, star, enemies } = 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);
if(!BattleRecord || BattleRecord.status != 0) {
return { code: 202, data: '关卡状态错误' }
}
let flag = 1; // 对比hero信息
let { record: { heroes: dbHeroes } } = BattleRecord;
for(let {actorId} of heroes) {
if(dbHeroes.indexOf(actorId) == -1) flag = 0;
}
if(!flag) {
return { code: 202, data: '关卡信息不同' }
}
const now = Date.now(); // 当前时间戳
let apJson = await setAp(now, roleId, -1 * warInfo.cost); // 扣除体力
if(!apJson) {
return { code: 202, data: '体力不足' }
}
// 检查record
let expeditionRecord = await ExpeditionRecordModel.getExpeditionRecordByCode(expeditionCode);
let expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCodeAndId(expeditionCode, expeditionId);
if(!expeditionRecord|| !expeditionWarRecord) {
return { code: 202, data:'未找到该记录' }
}
// 更新我方剩余血量
await ExpeditionRecordModel.updateHeroStatus(expeditionCode, expeditionRecord.heroes, heroes);
// 更新敌人剩余状态及战斗状态
expeditionWarRecord = await ExpeditionWarRecordModel.updateEnemiesStatus(expeditionCode, expeditionId, isSuccess?1:2, enemies);
// 更新battleRecord状态
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
$set: { status: isSuccess?1: 2, star }
}, true);
// 更新点数
let role = await RoleModel.increaseExpeditionPoint(roleId, isSuccess?EXPEDITION_INCREASE_POINT:0);
let { expeditionPoint = 0 } = role;
// 关卡奖励
let warReward = new WarReward(roleId, roleName, battleId, isSuccess);
let reward = await warReward.saveReward(1);
return {
code: 200,
data: {
expeditionCode, expeditionId,
battleCode, battleId,
battleStatus: expeditionWarRecord.battleStatus,
goods: reward,
apJson,
expeditionPoint
}
}
}
/**
* 领取战斗宝箱
* 领取宝箱,更新远征状态
*/
async battleReward(msg: { }, session: BackendSession) {
async battleReward(msg: { expeditionCode: string, expeditionId: number }, session: BackendSession) {
const { expeditionCode, expeditionId } = msg;
let roleId = session.get('roleId');
let roleName = session.get('roleName');
// 检查expeditionWarRecord的battleStatus
let expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCodeAndId(expeditionCode, expeditionId);
if(!expeditionWarRecord) {
return {code: 202, data: '未找到该关卡'}
}
if(expeditionWarRecord.battleStatus != 1) {
return {code: 202, data: '需要战斗胜利后才可以领取'}
}
if(expeditionWarRecord.received) {
return {code: 202, data: '已经领取过了'}
}
// 设置expeditionWarRecord的received和rewards
let dicExpedition = getGamedata('dic_expedition');
let curDicExpedition = dicExpedition.find(cur => cur.id == expeditionId);
if(!curDicExpedition) {
return {code: 202, data: '未找到该配置'}
}
let result = await ExpeditionWarRecordModel.updateBoxStatus(expeditionCode, expeditionId, true, curDicExpedition.reward);
let { battleId, battleCode, battleStatus, received } = result;
// 获取东西
let rewards = decodeStr('fixReward', curDicExpedition.reward);
let rewardObject = new Reward(roleId, roleName, rewards);
let goods = await rewardObject.saveReward();
return {
code: 200,
data: {
expeditionCode, expeditionId,
battleId, battleCode, battleStatus, received,
goods
}
}
}
@@ -163,8 +294,47 @@ export class ExpeditionBattleHandler {
* 领取点数宝箱
* 领取点数宝箱,不扣除点数,那么就需要记录领取状态并且有返回
*/
async pointReward(msg: { }, session: BackendSession) {
async pointReward(msg: { point }, session: BackendSession) {
const { point } = msg;
let roleId = session.get('roleId');
let roleName = session.get('roleName');
let role = await RoleModel.findByRoleId(roleId);
let {expeditionPoint} = role;
let dicExpeditionPoint = getGamedata('dic_expedition_point');
let curDicExpeditionPoint = dicExpeditionPoint.find(cur => cur.point == point);
if(!curDicExpeditionPoint) {
return {code: 202, data: '该点数不能领取奖励'}
}
if(point > expeditionPoint) {
return {code: 202, data: '点数不足'};
}
let pointStatusInDatabase = await ExpeditionPointModel.getExpeditionPoint(roleId);
if(pointStatusInDatabase) {
let {rewards} = pointStatusInDatabase;
let curReward = rewards.find(cur => cur.point == point);
if(curReward && curReward.received) {
return {code: 202, data: '已领取过'}
}
}
// 标记状态
await ExpeditionPointModel.updatePointStatus(roleId, point, curDicExpeditionPoint.reward);
let pointRewards = await getPointRewardStatus(roleId);
let rewards = decodeStr('fixReward', curDicExpeditionPoint.reward);
let rewardObject = new Reward(roleId, roleName, rewards);
let goods = await rewardObject.saveReward();
return {
code: 202,
data: {
pointRewards,
goods
}
}
}
/**
@@ -173,5 +343,38 @@ export class ExpeditionBattleHandler {
*/
async resetPointReward(msg: { }, session: BackendSession) {
let roleId = session.get('roleId');
// let roleName = session.get('roleName');
let pointStatusInDatabase = await ExpeditionPointModel.getExpeditionPoint(roleId);
if(!pointStatusInDatabase) {
return {code: 202, data: '已刷新'}
}
let { rewards } = pointStatusInDatabase;
let dicExpeditionPoint = getGamedata('dic_expedition_point');
let flag: boolean, maxPoint = 0;
for(let dic of dicExpeditionPoint) {
let curReward = rewards.find(cur => cur.point == dic.point);
if(curReward && curReward.received) {
flag = true;
}
if(dic.point > maxPoint) maxPoint = dic.point;
}
if(!flag) {
return {code: 202, data: '宝箱需要领取完才可以刷新'}
}
// 刷新
await ExpeditionPointModel.completeStatus(roleId);
await RoleModel.increaseExpeditionPoint(roleId, maxPoint * -1);
let pointRewards = await getPointRewardStatus(roleId);
return {
code: 200,
data: {
pointRewards
}
}
}
}

View File

@@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
let gamedata = {};
const wars = ['dic_zyz_gk_main', 'dic_zyz_gk_mainElite', 'dic_zyz_gk_daily', 'dic_zyz_gk_event', 'dic_zyz_gk_tower']; // 关卡相关的表
const wars = ['dic_zyz_gk_main', 'dic_zyz_gk_mainElite', 'dic_zyz_gk_daily', 'dic_zyz_gk_event', 'dic_zyz_gk_tower', 'dic_zyz_gk_expedition']; // 关卡相关的表
const allWarInfos = new Map<number, any>();
const towerInfos = new Map<number, any>();