远征本主界面及匹配机器人
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
import { ActionPointModel } from '../../../db/ActionPoint';
|
||||
import { BattleDropModel } from '../../../db/BattleDrop';
|
||||
|
||||
import { getWarById, getGoodById } from '../../../util/gamedata';
|
||||
import { getWarById, getWarJsons } from '../../../util/gamedata';
|
||||
import { decodeStr, Reward } from '../../../util/util';
|
||||
import { ACTION_POIN, BATTLE_REWARD_TYPE, GOOD_TYPE } from '../../../consts/consts';
|
||||
import { ACTION_POIN, BATTLE_REWARD_TYPE, WAR_JSON_ATTRIBUTE_TYPE } from '../../../consts/consts';
|
||||
|
||||
export async function getAp(now: number, roleId: string) {
|
||||
let dataAp = await ActionPointModel.getAp(roleId);
|
||||
@@ -130,4 +130,46 @@ export class WarReward {
|
||||
return returnGoods;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export async function matchPlayers(scale: number, range: number, myCe: number ,enemyObj: {enemyFrom: number, enemyId: string, enemies: Array<any> }) {
|
||||
|
||||
let min = myCe * scale * (1 - range/100);
|
||||
let max = myCe * scale * (1 + range/100);
|
||||
console.log(min, max, enemyObj);
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export async function matchRobots(scale: number, myCe: number, robotCe: number, warJsonIndex:any, role, enemyObj: {enemyFrom: number, enemyId: string, enemies: Array<any> }) {
|
||||
let {json: dicWarJson, fileName } = getWarJsons(warJsonIndex);
|
||||
if(dicWarJson) {
|
||||
enemyObj.enemyFrom = 2;
|
||||
enemyObj.enemyId = fileName;
|
||||
|
||||
let ratio = myCe / robotCe * scale; // 玩家战力/机器人初始战力*系数
|
||||
for(let enemy of dicWarJson) {
|
||||
let attribute = decodeWarJsonAttribute(enemy.attribute); // 格式:{'hp':1000, ...}
|
||||
for(let value in attribute) {
|
||||
attribute[value] *= ratio;
|
||||
}
|
||||
enemyObj.enemies.push({...enemy, attribute, lv: role.lv});
|
||||
}
|
||||
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function decodeWarJsonAttribute(attribute) {
|
||||
let arr = decodeStr('attribute', attribute);
|
||||
let obj = {};
|
||||
for(let {id, value} of arr) {
|
||||
let field = WAR_JSON_ATTRIBUTE_TYPE[id];
|
||||
if(field) {
|
||||
obj[field] = value;
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import { Application, BackendSession } from 'pinus';
|
||||
import { getGamedata } from '../../../util/gamedata';
|
||||
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';
|
||||
|
||||
export default function(app: Application) {
|
||||
return new ExpeditionBattleHandler(app);
|
||||
}
|
||||
|
||||
export class ExpeditionBattleHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取初始数据
|
||||
* 获取当前远征挑战情况,远征点数,点数宝箱领取情况 */
|
||||
async getStatus(msg: { }, session: BackendSession) {
|
||||
|
||||
let roleId = session.get('roleId');
|
||||
let roleName = session.get('roleName');
|
||||
|
||||
// 每日刷新,刷新关卡进度,宝箱进度,武将状态
|
||||
let now = new Date();
|
||||
let today = now.setHours(0, 0, 0, 0);
|
||||
let expeditionRecord = await ExpeditionRecordModel.getTodayRecord(roleId, today);
|
||||
if(!expeditionRecord) { // 每天新建一条记录
|
||||
let code = genCode(8);
|
||||
expeditionRecord = await ExpeditionRecordModel.createTodayRecord(code, {
|
||||
roleId, roleName, time: today, heroes: []
|
||||
});
|
||||
}
|
||||
|
||||
// 每一关的挑战状态
|
||||
let { expeditionCode, heroes } = expeditionRecord;
|
||||
let expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCode(expeditionCode);
|
||||
let curLv = 0;
|
||||
if(expeditionWarRecord.length > 0) {
|
||||
curLv = expeditionRecord[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 }
|
||||
})
|
||||
}
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
expeditionCode,
|
||||
curLv,
|
||||
expeditionWarRecord,
|
||||
pointRewards,
|
||||
heroes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取敌军数据
|
||||
* 匹配其他玩家,或机器人数据
|
||||
*/
|
||||
async getEnemies(msg: { expeditionCode: string, expeditionId: number }, session: BackendSession) {
|
||||
|
||||
const roleId = session.get('roleId');
|
||||
// const roleName = session.get('roleName');
|
||||
const { expeditionCode, expeditionId } = msg;
|
||||
|
||||
let expeditionWarRecord = await ExpeditionWarRecordModel.getRecordByCodeAndId(expeditionCode, expeditionId);
|
||||
if(!expeditionWarRecord) { // 如果没有信息
|
||||
|
||||
// 计算我方战斗力(最高五人)
|
||||
let myCe = await calculateSumCE(roleId, 1, { num: 5 });
|
||||
console.log('****myCe', myCe);
|
||||
let enemyObj = {
|
||||
enemyFrom: 0,
|
||||
enemyId: '',
|
||||
enemies: new Array()
|
||||
};
|
||||
|
||||
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 flag = await matchPlayers(scale, range, myCe, enemyObj);
|
||||
|
||||
// 当数量不够时使用机器人匹配
|
||||
if(!flag) {
|
||||
flag = await matchRobots(scale, myCe, curDicExpedition.ce, curDicExpedition.json, role, enemyObj);
|
||||
}
|
||||
|
||||
console.log('**********flag', flag)
|
||||
if(!flag) {
|
||||
return {code: 202, data:'无法匹配对手'};
|
||||
}
|
||||
// 保存
|
||||
let {warId} = curDicExpedition;
|
||||
expeditionWarRecord = await ExpeditionWarRecordModel.saveRecord(expeditionCode, expeditionId, {
|
||||
roleId, battleId: warId, ...enemyObj
|
||||
});
|
||||
}
|
||||
|
||||
let { battleId, enemyFrom, enemies, battleStatus } = expeditionWarRecord;
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
expeditionCode, expeditionId, battleId,
|
||||
battleStatus,
|
||||
enemyFrom,
|
||||
enemies
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 进入战斗
|
||||
* 记录我军数据,生成战斗唯一表示,记录状态
|
||||
*/
|
||||
async checkBattle(msg: { }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 战斗结算
|
||||
* 结算战斗奖励,更新远征状态
|
||||
*/
|
||||
async battleEnd(msg: { }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 领取战斗宝箱
|
||||
* 领取宝箱,更新远征状态
|
||||
*/
|
||||
async battleReward(msg: { }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 领取点数宝箱
|
||||
* 领取点数宝箱,不扣除点数,那么就需要记录领取状态并且有返回
|
||||
*/
|
||||
async pointReward(msg: { }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新点数宝箱
|
||||
* 扣除远征点数,并刷新箱子状态
|
||||
*/
|
||||
async resetPointReward(msg: { }, session: BackendSession) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { CounterModel } from '../../../db/Counter';
|
||||
import { HeroModel } from '../../../db/Hero';
|
||||
import { EquipModel } from '../../../db/Equip';
|
||||
import { calculateCE } from '../../../util/util';
|
||||
import {Application, BackendSession, createTcpMailBox} from 'pinus';
|
||||
|
||||
export default function(app: Application) {
|
||||
@@ -27,12 +28,14 @@ export class RoleHandler {
|
||||
|
||||
async initHeros(roleId: string, roleName: string) {
|
||||
const seqId = await CounterModel.getNewCounter('hid');
|
||||
let ce = calculateCE({hid: 1, lv: 1});
|
||||
const heroInfo = {
|
||||
roleId,
|
||||
roleName,
|
||||
hid: 1,
|
||||
hName: '曹操',
|
||||
seqId
|
||||
seqId,
|
||||
ce
|
||||
}
|
||||
await HeroModel.createHero(heroInfo);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ function parseTowerData() {
|
||||
}
|
||||
|
||||
function initData (folder) {
|
||||
console.log('加载文件夹:', folder);
|
||||
if(!gamedata.hasOwnProperty(folder)) {
|
||||
gamedata[folder] = {};
|
||||
}
|
||||
@@ -67,8 +66,15 @@ export function getGamedata(key) {
|
||||
}
|
||||
|
||||
// 获取出兵表
|
||||
export function getWarJsons(key: string) {
|
||||
return gamedata['warJsons'][key];
|
||||
export function getWarJsons(key: any) {
|
||||
key = key + '';
|
||||
let arr = key.split('&');
|
||||
let index = Math.floor(Math.random() * arr.length);
|
||||
key = arr[index];
|
||||
return {
|
||||
json: gamedata['warJsons'][key],
|
||||
fileName: key
|
||||
}
|
||||
}
|
||||
|
||||
export function getWarById(warid: number) {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
|
||||
import { getWarById, getGoodById } from './gamedata';
|
||||
import { getWarById, getGoodById, getGamedata } from './gamedata';
|
||||
import { CounterModel } from '../db/Counter';
|
||||
import { EquipModel } from '../db/Equip';
|
||||
import { HeroModel } from '../db/Hero';
|
||||
import { GOOD_TYPE } from '../consts/consts';
|
||||
|
||||
export function genCode(len) {
|
||||
@@ -54,11 +55,17 @@ import { GOOD_TYPE } from '../consts/consts';
|
||||
result = { min: parseInt(min), max: parseInt(max) };
|
||||
|
||||
}
|
||||
case 'attribute': {
|
||||
let [id, value] = arr;
|
||||
if(isNaN(id) || isNaN(value)) throw new Error('格式错误');
|
||||
result = { id: parseInt(id), value: parseInt(value) };
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 获取物品
|
||||
export class Reward {
|
||||
roleId: string;
|
||||
@@ -108,6 +115,7 @@ import { GOOD_TYPE } from '../consts/consts';
|
||||
return weaponsData;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 | 分隔的字符串解析为数组,如:a|b|c 解析为[a, b, c]
|
||||
* @param str 要解析的字符串
|
||||
@@ -130,3 +138,35 @@ export function decodeIdCntArrayStr(str: string, multi: number) {
|
||||
});
|
||||
return strMap;
|
||||
}
|
||||
|
||||
// 计算当前武将战力
|
||||
export async function calculateSumCE(roleId: string, type: number, param: { num?: number, heroes?: Array<number> }) {
|
||||
let sum;
|
||||
if(type == 1) { // 最高num人战力和
|
||||
sum = await HeroModel.sumTopHeroCe(roleId, param.num);
|
||||
} else if(type == 2) { // 所有人战力和
|
||||
sum = await HeroModel.sumHeroCe(roleId);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
// 计算当前武将战力
|
||||
export function calculateCE(heroInfo: {hid: number, lv: number }) {
|
||||
let { hid, lv } = heroInfo;
|
||||
// 假设所有属性和等级的关系是简单的线性关系
|
||||
let dicHero = getGamedata('dic_zyz_hero');
|
||||
let curDicHero = dicHero.find(cur => cur.heroId == hid);
|
||||
|
||||
let { atk, matk, def, mdef, agi, luk, atk_up, matk_up, def_up, mdef_up, agi_up, luk_up } = curDicHero;
|
||||
atk += lv * atk_up;
|
||||
matk += lv * matk_up;
|
||||
def += lv * def_up;
|
||||
mdef += lv * mdef_up;
|
||||
agi += lv * agi_up;
|
||||
luk += lv * luk_up;
|
||||
|
||||
// 假设战力为所有属性的简单加法
|
||||
|
||||
let ce = atk + matk + def + mdef + agi + luk;
|
||||
return ce;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user