Files
ZYZ/game-server/app/util/util.ts
2020-10-14 17:19:09 +08:00

192 lines
5.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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) {
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;
}
}
return result;
};
}
// 获取物品
export class Reward {
roleId: string;
roleName: string;
rewards: Array<{type?: number, gid: number, count: number}>;
constructor(roleId: string, roleName: string, rewards: Array<{type?: number, gid: number, count: number}>) {
this.roleId = roleId;
this.roleName = roleName;
this.rewards = rewards;
}
public async saveReward() {
let returnGoods = new Array();
for(let goods of this.rewards) {
let goodInfo = getGoodById(goods.gid);
if(goodInfo.goodType == GOOD_TYPE.EQUIP) { // 装备
let result = await this.rewardWeapons(goodInfo, {id: goods.gid, cnt: goods.count });
for(let obj of result) {
returnGoods.push({dropType: goods.type, ...obj})
}
}
}
return returnGoods;
}
private async rewardWeapons (dicGood: any, weapon: {id:number,cnt:number }) {
let weaponsData = [];
let cnt = weapon.cnt;
while (cnt > 0) {
const seqId = await CounterModel.getNewCounter('eid');
const equipInfo = {
roleId: this.roleId,
roleName: this.roleName,
eid: weapon.id,
eName: dicGood.name,
seqId,
quality: dicGood.lv,
type: dicGood.goodType
}
const equip = await EquipModel.createEquip(equipInfo);
cnt -= 1;
weaponsData.push(equip);
}
return weaponsData;
}
}
/**
* 将 | 分隔的字符串解析为数组a|b|c 解析为[a, b, c]
* @param str 要解析的字符串
*/
export function decodeArrayStr(str: string) {
if(str == '&') str = '';
return str.split('|');
}
/**
* 将 | 和 & 分隔的字符串解析为 Mapa&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
}