修改util为公共地址
This commit is contained in:
@@ -1,153 +0,0 @@
|
||||
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', 'dic_zyz_gk_expedition']; // 关卡相关的表
|
||||
const allWarInfos = new Map<number, any>();
|
||||
const towerInfos = new Map<number, any>();
|
||||
const towerTaskInfos = new Map<number, any>();
|
||||
const towerTasksByQuality = new Map<number, Array<number>>();
|
||||
const heroInfos = new Map<number, any>();
|
||||
const jobInfos = new Map<number, any>();
|
||||
|
||||
function parseWarData() {
|
||||
let result = null;
|
||||
for (let filename of wars) {
|
||||
let warInfo = gamedata['jsons'][filename]||[];
|
||||
for(let war of warInfo) {
|
||||
if(war.war_id) {
|
||||
allWarInfos.set(war.war_id, war);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function parseTowerData() {
|
||||
const towerFile = 'dic_zyz_tower';
|
||||
const towerData = gamedata['jsons'][towerFile] || [];
|
||||
towerData.forEach(elem => {
|
||||
if (elem.lv) {
|
||||
towerInfos.set(elem.lv, elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function parseTowerTaskData() {
|
||||
const towerTaskFile = 'dic_zyz_tower_tasks';
|
||||
const towerTaskData = gamedata['jsons'][towerTaskFile] || [];
|
||||
towerTaskData.forEach(elem => {
|
||||
if (elem && elem.taskId) {
|
||||
towerTaskInfos.set(elem.taskId, elem);
|
||||
let tasks = towerTasksByQuality.get(elem.quality) || [];
|
||||
tasks.push(elem.taskId);
|
||||
towerTasksByQuality.set(elem.quality, tasks);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function parseHeroData() {
|
||||
const heroFile = 'dic_zyz_hero';
|
||||
const heroesData = gamedata['jsons'][heroFile] || [];
|
||||
heroesData.forEach(elem => {
|
||||
if (elem && elem.heroId) {
|
||||
heroInfos.set(elem.heroId, elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function parseJobData() {
|
||||
const jobFile = 'dic_zyz_job';
|
||||
const jobsData = gamedata['jsons'][jobFile] || [];
|
||||
jobsData.forEach(elem => {
|
||||
if (elem && elem.jobid) {
|
||||
heroInfos.set(elem.jobid, elem);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function initData (folder) {
|
||||
if(!gamedata.hasOwnProperty(folder)) {
|
||||
gamedata[folder] = {};
|
||||
}
|
||||
fs.readdirSync(__dirname + '/../resource/' + folder)
|
||||
.filter(function(file) {
|
||||
return (file.indexOf(".") !== 0) && (file !== "index.js");
|
||||
})
|
||||
//筛选有文件名且不是index进行遍历
|
||||
.forEach(function(file) {
|
||||
var name = file.split('.')[0];
|
||||
try {
|
||||
gamedata[folder][name] = JSON.parse(
|
||||
fs.readFileSync(path.resolve(__dirname, "../resource/" + folder + "/" + file))
|
||||
);
|
||||
} catch(e) {
|
||||
console.error('【文件缺少】:' + file);
|
||||
gamedata[folder][name] = [];
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function parseData() {
|
||||
parseWarData();
|
||||
parseTowerData();
|
||||
parseTowerTaskData();
|
||||
parseHeroData();
|
||||
parseJobData();
|
||||
}
|
||||
|
||||
initData('jsons'); // 加载一般json
|
||||
initData('warJsons'); // 加载出兵表
|
||||
parseData();
|
||||
|
||||
export function getGamedata(key) {
|
||||
return gamedata['jsons'][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) {
|
||||
return allWarInfos.get(warid);
|
||||
}
|
||||
|
||||
export function getTowerDataByLv(lv: number) {
|
||||
return towerInfos.get(lv);
|
||||
}
|
||||
|
||||
export function getGoodById(gid) {
|
||||
let goodsInfo = gamedata['jsons']['dic_goods']||[];
|
||||
return goodsInfo.find(cur => {
|
||||
return cur.good_id == gid
|
||||
});
|
||||
}
|
||||
|
||||
export function getTaskById(tid: number) {
|
||||
const taskInfo = towerTaskInfos.get(tid);
|
||||
return taskInfo;
|
||||
}
|
||||
|
||||
export function getTaskIdByQuality(quality: number) {
|
||||
const taskIds = towerTasksByQuality.get(quality);
|
||||
return taskIds;
|
||||
}
|
||||
|
||||
export function getHeroInfoById(hid: number) {
|
||||
const heroInfo = heroInfos.get(hid);
|
||||
return heroInfo;
|
||||
}
|
||||
|
||||
export function getJobInfoById(jid: number) {
|
||||
const jobInfo = jobInfos.get(jid);
|
||||
return jobInfo;
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
|
||||
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';
|
||||
const moment = require('moment');
|
||||
|
||||
export function genCode(len) {
|
||||
const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
const charArr = chars.split('');
|
||||
let code = '';
|
||||
for (let i = 0; i < len; i++) {
|
||||
code += charArr[Math.floor(Math.random() * charArr.length)];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
export function decodeStr(type, str) {
|
||||
if(str == '&'|| !str) {
|
||||
return []
|
||||
} else {
|
||||
return str.split('|').map(cur => {
|
||||
return decodeStrSingle(type,cur);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function decodeStrSingle(type, str) { //单层,返回对象而不是数组
|
||||
if(str == '&'|| !str) {
|
||||
return {}
|
||||
} else {
|
||||
let arr = str.split('&');
|
||||
let result = {};
|
||||
switch (type) {
|
||||
case 'fixReward': {
|
||||
let [gid, count] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count)};
|
||||
break;
|
||||
}
|
||||
case 'conditionReward': {
|
||||
let [gid, count, condition] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count), condition: parseInt(condition) };
|
||||
break;
|
||||
}
|
||||
case 'randomReward': {
|
||||
let [gid, count, frequency] = arr;
|
||||
if(isNaN(gid) || isNaN(count)) throw new Error('格式错误');
|
||||
result = { gid: parseInt(gid), count: parseInt(count), frequency: parseInt(frequency) };
|
||||
break;
|
||||
}
|
||||
case 'eventSuitLevel': {
|
||||
let [min, max] = arr;
|
||||
if(isNaN(min) || isNaN(max)) throw new Error('格式错误');
|
||||
result = { min: parseInt(min), max: parseInt(max) };
|
||||
break;
|
||||
}
|
||||
case 'attribute': {
|
||||
let [id, value] = arr;
|
||||
if(isNaN(id) || isNaN(value)) throw new Error('格式错误');
|
||||
result = { id: parseInt(id), value: parseInt(value) };
|
||||
break;
|
||||
}
|
||||
case 'position': {
|
||||
let [x, y] = arr;
|
||||
if(isNaN(x) || isNaN(y)) throw new Error('格式错误');
|
||||
result = { x: parseInt(x), y: parseInt(y) };
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 | 分隔的字符串解析为数组,如:a|b|c 解析为[a, b, c]
|
||||
* @param str 要解析的字符串
|
||||
*/
|
||||
export function decodeArrayStr(str: string) {
|
||||
if(str == '&') str = '';
|
||||
return str.split('|');
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 | 和 & 分隔的字符串解析为 Map,,如:a&b|c&d|e&f 解析为 Map {a=>b, c=>d, e=>f}
|
||||
* @param str 要解析的字符串
|
||||
*/
|
||||
export function decodeIdCntArrayStr(str: string, multi: number) {
|
||||
const strArr = decodeArrayStr(str);
|
||||
const strMap = new Map();
|
||||
strArr.forEach(item => {
|
||||
const kv = item.split('&');
|
||||
strMap.set(kv[0], multi? parseInt(kv[1]) * multi: kv[1]);
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
export function getRandomWithWeight(randomList: any) {
|
||||
let len = randomList.reduce((pre, cur) => {
|
||||
return pre + cur.weight||1;
|
||||
}, 0);
|
||||
let index = Math.floor(Math.random() * len);
|
||||
let result = { dic: null, index: -1 };
|
||||
for(let i = 0; i < randomList.length; i++) {
|
||||
let {weight = 0} = randomList[i];
|
||||
if(index < weight) {
|
||||
result.dic = randomList[i];
|
||||
result.index = i;
|
||||
break;
|
||||
}
|
||||
index -= weight;
|
||||
}
|
||||
return result
|
||||
}
|
||||
/**
|
||||
* 传入两个时间,返回按照时间差计算,第二个时间比第一个晚几天
|
||||
* @param preTime 之前的时间
|
||||
* @param proTime 之后的时间
|
||||
*/
|
||||
export function deltaDays(preTime: Date, proTime: Date): number {
|
||||
return moment(proTime).diff(moment(preTime), "days");
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算按照每 x 天 y 点刷新一次,是否应该刷新
|
||||
* @param preTime 基准时间
|
||||
* @param curTime 当前时间
|
||||
* @param hour 几点刷新
|
||||
* @param deltaDay 间隔几天刷新,默认每天刷新(deltaDay = 1)
|
||||
*/
|
||||
export function shouldRefresh(preTime: Date, curTime: Date, hour: number, deltaDay = 1): boolean {
|
||||
let refeshTime = curTime.setHours(hour, 0, 0, 0);
|
||||
if (refeshTime - preTime.getTime() > deltaDay * 24 * 60 * 60 * 1000 && curTime.getTime() >= refeshTime) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getRandEelm(source: Array<any> = [], cnt = 1): Array<any> {
|
||||
if (cnt > source.length) return null;
|
||||
if (cnt === source.length) return source;
|
||||
let idxs = new Set();
|
||||
for (let i = 0; i < cnt; ++i) {
|
||||
let rand = Math.floor(Math.random() * source.length);
|
||||
idxs.add(rand);
|
||||
if (idxs.size == cnt) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return source.filter((item, idx) => idxs.has(idx));
|
||||
}
|
||||
Reference in New Issue
Block a user