Files
ZYZ/game-server/app/services/roleService.ts
2021-02-24 18:34:54 +08:00

82 lines
3.1 KiB
TypeScript

import { ChannelUser } from './../domain/ChannelUser';
import { Channel } from 'pinus';
import { getRandNum, getRandomArr } from '../pubUtils/util';
import { TERAPH_RANDOM } from "../consts/consts";
import { indexOf } from 'underscore';
const TERAPH_STRENGTHEN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
/**
* 计算强化次数和消耗
* @param count 强化次数
* @param attrNames 可以强化的属性名字
* @param teraphInfo 字典表对应的神像信息
* @param teraph
*/
export function checkTeraphMaterialEnough(count: number, attrNames:Array<any>, teraphInfo: any, teraph: any) {
let res = {};
let consumes = [];
let times = 0;
if (count < 10) {
for (let i = 0; i < count; i++) {
if (!attrNames.length) {
break;
}
let num = getRandNum(TERAPH_RANDOM.MIN, TERAPH_RANDOM.MAX);
let arr = getRandomArr(attrNames, num)
let critical = getRandNum(0, 100);//属性暴击率
let criEffect = 1;
if (critical <= teraphInfo.criRate)
criEffect = teraphInfo.criEffect;
for (let attrName of arr) {
let attrNameMax = attrName+'Max';
res[attrName] = teraphInfo[attrName] * criEffect;
if (teraph[attrName] + res[attrName] > teraphInfo[attrNameMax]) {
res[attrName] = teraphInfo[attrNameMax] - teraph[attrName];
let attrIndex = indexOf(attrNames, attrName);
attrNames.splice(attrIndex, 1);
}
}
times++;
}
} else if (count == 10){
let criticalArrTimes = getRandomArr(TERAPH_STRENGTHEN, 2);//10次中随机2次发生暴击
for (let item of TERAPH_STRENGTHEN) {
if (!attrNames.length) {
break;
}
let criEffect = 1;
let index = indexOf(criticalArrTimes, {item});
if (index > 0) {
criEffect = teraphInfo.criEffect;//本次是暴击 item 在 criticalArrTimes 中
}
let num = getRandNum(TERAPH_RANDOM.MIN, TERAPH_RANDOM.MAX);
let arr = getRandomArr(attrNames, num);
for (let attrName of arr) {
res[attrName] = (res[attrName]||0) + teraphInfo[attrName] * criEffect;
let attrNameMax = attrName+'Max';
if (teraph[attrName] + res[attrName] > teraphInfo[attrNameMax]) {
res[attrName] = teraphInfo[attrNameMax] - teraph[attrName];
let attrIndex = indexOf(attrNames, attrName);
attrNames.splice(attrIndex, 1);
}
}
times++;
}
}
//计算消耗
for (let {id, count} of teraphInfo.upMaterial) {
consumes.push({ id, count: count*times});
}
return {attr: res, consumes};
}
/**
*
* @param channel
* @param user
*/
export function addUserToChannel(channel: Channel, user: ChannelUser) {
const users = channel.getMembers();
const { uid, sid } = user;
if (users.indexOf(uid) === -1) {
channel.add(uid, sid);
}
}