feat(稷下学宫): ff931afe4到7421822f6

This commit is contained in:
luying
2023-08-30 11:02:52 +08:00
parent 59a334d673
commit 05cbe318e9
102 changed files with 147876 additions and 715 deletions
+130 -76
View File
@@ -80,11 +80,11 @@ export function genCode(len) {
return code;
}
/**
* 生成 len 长度的随机字符串
* @param len 长度
* @param radix 基数
*/
/**
* 生成 len 长度的随机字符串
* @param len 长度
* @param radix 基数
*/
export function generateStr(len: number, radix = 36) {
return `${csprng(len, radix)}`;
}
@@ -140,8 +140,8 @@ export function decodeIdCntArrayStr(str: string, multi: number) {
* @param proTime 之后的时间
*/
export function deltaDays(preTime: Date, proTime: Date, useNaturalZero = false): number {
let beginZeroPoint = getZeroPointOfTimeD(preTime, SHOP_REFRESH_TYPE.DAILY, useNaturalZero? 0: REFRESH_TIME);
let endZeroPoint = getZeroPointOfTimeD(proTime, SHOP_REFRESH_TYPE.DAILY, useNaturalZero? 0: REFRESH_TIME);
let beginZeroPoint = getZeroPointOfTimeD(preTime, SHOP_REFRESH_TYPE.DAILY, useNaturalZero ? 0 : REFRESH_TIME);
let endZeroPoint = getZeroPointOfTimeD(proTime, SHOP_REFRESH_TYPE.DAILY, useNaturalZero ? 0 : REFRESH_TIME);
return moment(endZeroPoint).diff(moment(beginZeroPoint), "days");
}
@@ -227,7 +227,7 @@ export function getRandSingleIndex(len: number) {
*/
export function getRandEelmWithWeight<T extends { weight: number }>(randomList: T[]): { dic: T, index: number } {
let len = randomList.reduce((pre, cur) => {
return pre + (cur.weight||0);
return pre + (cur.weight || 0);
}, 0);
let index = Math.floor(Math.random() * len);
let result = { dic: null, index: -1 };
@@ -243,6 +243,45 @@ export function getRandEelmWithWeight<T extends { weight: number }>(randomList:
return result
}
export function getRandEelmWithWeightAndNum<T extends { weight: number }>(randomList: T[], num: number): { dic: T, index: number }[] {
let result: { dic: T, index: number }[] = [];
let remainingList = randomList.slice(); // Make a copy of randomList to work with
for (let n = 0; n < Math.min(num, randomList.length); n++) {
if (remainingList.length === 0) {
// If there are no more elements to choose from, exit the loop
break;
}
let len = remainingList.reduce((pre, cur) => {
return pre + (cur.weight || 0);
}, 0);
let index = Math.floor(Math.random() * len);
let found = false;
for (let i = 0; i < remainingList.length; i++) {
let { weight = 0 } = remainingList[i];
if (index < weight) {
result.push({ dic: remainingList[i], index: randomList.indexOf(remainingList[i]) });
remainingList.splice(i, 1); // Remove the selected element from the list
found = true;
break;
}
index -= weight;
}
if (!found) {
// In case the index exceeds the weights, add a default value
result.push({ dic: null, index: -1 });
}
}
return result;
}
/**
* 不改变原数组长度,将内部元素打乱
* @param source
@@ -271,10 +310,12 @@ export function getRandValue(base: number, ratio: number, decimal = 2): number {
*/
export function getRandValueByMinMax(min: number, max: number, decimal = 2): number {
let pow = Math.pow(10, decimal);
return Math.floor((min + (max - min) * Math.random()) * pow)/pow;
return Math.floor((min + (max - min) * Math.random()) * pow) / pow;
}
export function resResult<T>(status: { code: number, simStr: string }, data: T = <T>{}, customMsg = ''): { code: number, msg: string, data: T } {
const { code, simStr } = status;
if (code !== STATUS.SUCCESS.code) {
@@ -349,7 +390,7 @@ export const cal = {
export function getDecimalCnt(num: number) {
let str = num.toString();
return str.split('.')[1]? str.split('.')[1].length: 0;
return str.split('.')[1] ? str.split('.')[1].length : 0;
}
//计算公式
@@ -371,9 +412,9 @@ export function getDecimalCnt(num: number) {
// }
// }
export function ratioReward(reward: {id: number, count: number}[], ratio: number): {id: number, count: number}[] {
export function ratioReward(reward: { id: number, count: number }[], ratio: number): { id: number, count: number }[] {
return reward.map(cur => {
return {id: cur.id, count: cur.count * ratio}
return { id: cur.id, count: cur.count * ratio }
});
}
@@ -426,7 +467,7 @@ export function readFileAndParseJson(path: string) {
try {
let readResult = readFile(path);
return JSON.parse(readResult);
} catch(e) {
} catch (e) {
throw new Error(`connectors.json 格式错误:${(<Error>e).message}`);
}
}
@@ -462,9 +503,9 @@ export function readServerNameFileList() {
export function readWordTxt(fileName: string) {
try {
let file = fs.readFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`)).toString('utf8').replace(/^\uFEFF/, '');
let file = fs.readFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`)).toString('utf8').replace(/^\uFEFF/, '');
return file;
} catch(e) {
} catch (e) {
console.log(e)
return null
}
@@ -472,18 +513,18 @@ export function readWordTxt(fileName: string) {
export function writeWordTxt(fileName: string, words: string) {
try {
let file = fs.writeFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`), words);
let file = fs.writeFileSync(path.resolve(__dirname, `../resource/${fileName}.txt`), words);
return file;
} catch(e) {
} catch (e) {
return null
}
}
export function readTsFile(fileName: string) {
try {
let file = fs.readFileSync(path.resolve(__dirname, `../pubUtils/${fileName}.js`)).toString('utf8').replace(/^\uFEFF/, '');
let file = fs.readFileSync(path.resolve(__dirname, `../pubUtils/${fileName}.js`)).toString('utf8').replace(/^\uFEFF/, '');
return file;
} catch(e) {
} catch (e) {
return null
}
}
@@ -492,7 +533,7 @@ export function readFileAndParse(fileName: string) {
try {
let readResult = readJsonFile(fileName);
return JSON.parse(readResult);
} catch(e) {
} catch (e) {
throw new Error(`${fileName}格式错误:${(<Error>e).message}`);
}
}
@@ -500,11 +541,11 @@ export function readFileAndParse(fileName: string) {
export function readWarJsonFileAndParse() {
let warJsons = readWarJsonFileList();
let result = [];
for(let { name, str } of warJsons) {
for (let { name, str } of warJsons) {
try {
let json = JSON.parse(str);
result.push(json);
} catch(e) {
} catch (e) {
throw new Error(`${name}格式错误:${(<Error>e).message}`);
}
}
@@ -622,7 +663,7 @@ export function checkRoleIsRobot(roleId: string) {
// 将一般的roleId转为带_r的
export function makeRobotId(roleId: string, sysType?: ROBOT_SYS_TYPE) {
if(sysType) {
if (sysType) {
return `${sysType}|${roleId}_r`;
} else {
return `${roleId}_r`;
@@ -631,7 +672,7 @@ export function makeRobotId(roleId: string, sysType?: ROBOT_SYS_TYPE) {
// 获取来源系统
export function getRobotSysType(roleId: string) {
let type = roleId.split('|')[0];
if(isNaN(parseInt(type))) {
if (isNaN(parseInt(type))) {
return 0
} else {
return parseInt(type);
@@ -658,13 +699,13 @@ export function splitString(dataString: string, key: string) {
export function isTimestamp(time: number, len = 10) {
if(!isNumber(time)) return false;
if(time.toString().length != len) return false;
if (!isNumber(time)) return false;
if (time.toString().length != len) return false;
return true;
}
export function getReasonByWarType(warType: number) {
switch(warType) {
switch (warType) {
case WAR_TYPE.NORMAL:
return ITEM_CHANGE_REASON.NORMAL_BATTLE_END;
case WAR_TYPE.VESTIGE:
@@ -708,55 +749,55 @@ export function getReasonByWarType(warType: number) {
return ITEM_CHANGE_REASON.BOSS_BATTLE_END;
case WAR_TYPE.LADDER:
return ITEM_CHANGE_REASON.LADDER_BATTLE_REWARD;
default:
default:
return ITEM_CHANGE_REASON.NORMAL_BATTLE_END;
}
}
export function getWarTypeName(warType: number) {
switch(warType) {
case WAR_TYPE.NORMAL: return '主线';
case WAR_TYPE.VESTIGE: return '支线';
case WAR_TYPE.EVENT: return '事件';
case WAR_TYPE.DAILY: return '每日';
case WAR_TYPE.EXPEDITION: return '远征';
case WAR_TYPE.MYSTERY: return '秘境';
case WAR_TYPE.COM_BATTLE: return '寻宝';
case WAR_TYPE.TOWER: return '镇念塔';
case WAR_TYPE.PVP: return '竞技';
case WAR_TYPE.GUILD_ACTIVITY: return '军团活动';
case WAR_TYPE.GUILD_TRAIN: return '训练场';
case WAR_TYPE.MAIN_ELITE: return '主线精英';
case WAR_TYPE.BRANCH_ELITE: return '梦魇支线';
case WAR_TYPE.BRANCH: return '支线';
case WAR_TYPE.ACT_TREASURE_HUNT: return '运营活动-神州探秘';
case WAR_TYPE.ACT_SELF_SHOP: return '运营活动-糜家商队';
case WAR_TYPE.ACT_DAILY_GK: return '运营活动-节日活动';
case WAR_TYPE.ACT_NEW_HERO_GK: return '新武将活动关卡';
case WAR_TYPE.TRY: return '试用关卡';
case WAR_TYPE.BOSS: return '演武台';
switch (warType) {
case WAR_TYPE.NORMAL: return '主线';
case WAR_TYPE.VESTIGE: return '支线';
case WAR_TYPE.EVENT: return '事件';
case WAR_TYPE.DAILY: return '每日';
case WAR_TYPE.EXPEDITION: return '远征';
case WAR_TYPE.MYSTERY: return '秘境';
case WAR_TYPE.COM_BATTLE: return '寻宝';
case WAR_TYPE.TOWER: return '镇念塔';
case WAR_TYPE.PVP: return '竞技';
case WAR_TYPE.GUILD_ACTIVITY: return '军团活动';
case WAR_TYPE.GUILD_TRAIN: return '训练场';
case WAR_TYPE.MAIN_ELITE: return '主线精英';
case WAR_TYPE.BRANCH_ELITE: return '梦魇支线';
case WAR_TYPE.BRANCH: return '支线';
case WAR_TYPE.ACT_TREASURE_HUNT: return '运营活动-神州探秘';
case WAR_TYPE.ACT_SELF_SHOP: return '运营活动-糜家商队';
case WAR_TYPE.ACT_DAILY_GK: return '运营活动-节日活动';
case WAR_TYPE.ACT_NEW_HERO_GK: return '新武将活动关卡';
case WAR_TYPE.TRY: return '试用关卡';
case WAR_TYPE.BOSS: return '演武台';
}
}
}
/**
* 一群人分总数固定的东西
* @param total 总数
* @param max 每个人能拿到的最大数量
* @param memberCnt 人数
*/
export function getRandResultByMember(total: number, max: number, memberCnt: number) {
let arr: number[] = [];
for(let i = 1; i <= memberCnt; i++) {
let randMax = total > max? max: total;
let randMin = total - (memberCnt - i) * max;
if(randMin < 0) randMin = 0;
if(randMin > max) randMin = max;
let rand = getRandValueByMinMax(randMin, randMax + 1, 0);
arr.push(rand);
total -= rand;
}
return { arr, remain: total }
}
/**
* 一群人分总数固定的东西
* @param total 总数
* @param max 每个人能拿到的最大数量
* @param memberCnt 人数
*/
export function getRandResultByMember(total: number, max: number, memberCnt: number) {
let arr: number[] = [];
for (let i = 1; i <= memberCnt; i++) {
let randMax = total > max ? max : total;
let randMin = total - (memberCnt - i) * max;
if (randMin < 0) randMin = 0;
if (randMin > max) randMin = max;
let rand = getRandValueByMinMax(randMin, randMax + 1, 0);
arr.push(rand);
total -= rand;
}
return { arr, remain: total }
}
//数据格式转换'id&数量|id&数量|' ->> Array<RewardInter> 老资源格式
export function stringToRewardInter(rewardStr: string): Array<RewardInter> {
@@ -773,16 +814,16 @@ export function stringToRewardInter(rewardStr: string): Array<RewardInter> {
}
export function addToMap<T>(map: Map<T, number>, id: T, value: number) {
if(!map.has(id)) {
if (!map.has(id)) {
map.set(id, value);
} else {
map.set(id, map.get(id) + value);
}
}
export function arrToMap<T>(arr: T[], getKey: (obj: T) => number|string): Map<number, T> {
export function arrToMap<T>(arr: T[], getKey: (obj: T) => number | string): Map<number, T> {
let map = new Map();
for(let obj of arr) {
for (let obj of arr) {
let key = getKey(obj);
map.set(key, obj);
}
@@ -841,12 +882,12 @@ export function getGachaRemainFloor(gachaId: number, userFloor: Floor[]) {
let dicGacha = gameData.gacha.get(gachaId);
if(dicGacha.gachaType != GACHA_TYPE.NORMAL && dicGacha.gachaType != GACHA_TYPE.ACTIVITY && dicGacha.gachaType != GACHA_TYPE.TAUTOR) return 0;
for(let floorId of dicGacha.floor) {
for (let floorId of dicGacha.floor) {
let dicGachaFloor = gameData.gachaFloor.get(floorId);
if(dicGachaFloor && dicGachaFloor.floorType == GACHA_FLOOR_TYPE.MAIN_FLOOR) {
if (dicGachaFloor && dicGachaFloor.floorType == GACHA_FLOOR_TYPE.MAIN_FLOOR) {
let myFloor = userFloor.find(cur => cur.id == floorId);
return dicGachaFloor.param - (myFloor?.count||0);
return dicGachaFloor.param - (myFloor?.count || 0);
}
}
return 0
@@ -859,7 +900,7 @@ export function isDevelopEnv(env: string) {
export function getArrayOfNumber(len: number) {
let arr: number[] = [];
for(let i = 1; i <= len; i++) arr.push(i);
for (let i = 1; i <= len; i++) arr.push(i);
return arr;
}
@@ -890,4 +931,17 @@ export function swapFields(originObj: object, targetObj: object, fieldsToSwap: s
originObj[field] = targetObj[field];
targetObj[field] = temp;
}
}
export function compareNumberArray(arrA: number[], arrB: number[]) {
if(arrA.length != arrB.length) return false;
let sortArrA: number[] = [], sortArrB: number[] = [];
for(let a of arrA) sortArrA.push(a);
for(let b of arrB) sortArrB.push(b);
sortArrA.sort();
sortArrB.sort();
for(let i = 0; i < sortArrA.length; i++) {
if(sortArrA[i] != sortArrB[i]) return false;
}
return true;
}