战力计算,远征战力筛选

This commit is contained in:
luying
2020-11-12 18:21:05 +08:00
parent 40c16587b3
commit b0a0bbc71b
34 changed files with 22812 additions and 15964 deletions
+47
View File
@@ -2,6 +2,9 @@ import { STATUS } from './../consts/statusCode';
import { getGamedata } from './gamedata';
import { HeroModel } from '../db/Hero';
import { RoleModel } from '../db/Role';
import { PvpDefenseModel } from '../db/PvpDefense';
import Actor from './actor';
const moment = require('moment');
@@ -26,6 +29,8 @@ const moment = require('moment');
}
export function decodeStrSingle(type, str) { //单层,返回对象而不是数组
let last = str.substr(str.length-1, 1);
if(last == '&') str = str.substr(0, str.length - 1);
if(str == '&'|| !str) {
return {}
} else {
@@ -320,4 +325,46 @@ export function setLocalHours(hour: number, curTime = new Date()) {
let localTime = new Date(curTime.getTime() + offset * 60 * 1000 + 8 * 60 * 60 * 1000); // 中国时间
localTime.setUTCHours(hour, 0, 0, 0); // 中国的几点
return localTime.getTime() - offset * 60 * 1000 - 8 * 60 * 60 * 1000; // 回到格林威治时间
}
export async function updateCe(roleId: string, hero: any ) {
let {hid, ce: oldCe, historyCe} = hero;
let actor = new Actor(hero);
let ce = actor.calculateCE();
if(ce != oldCe) {
// 更新总战力&最强五人战力
let role = await RoleModel.findByRoleId(roleId);
let distance = ce - oldCe;
let historyDistance = ce > historyCe ?ce - historyCe: 0;
let topFive = role?.topFive||new Array();
topFive.sort((a, b) => {return b.ce - a.ce}); // 0-5,最大-最小
let index = topFive.findIndex(cur => cur.hid == hid);
if(index == -1) { // 不在最强列表
if(topFive.length < 5) { // 不满5人
topFive.push({hid, ce});
} else if(topFive.length == 5){
if(ce > topFive[topFive.length - 1].ce) { // 跻身最强6人
topFive.pop();
topFive.push({hid, ce});
} else {
topFive[index].ce = ce;
}
} else {
topFive.splice(5, topFive.length - 5);
}
} else { // 原来就是最强5人
if(ce < topFive[topFive.length - 1].ce) { // 滑出最强
let heroes = await HeroModel.getTopHero(roleId, 5);
topFive = heroes.map(cur => {return {hid: cur.hid, ce: cur.ce}});
} else {
topFive[index].ce = ce;
}
}
let topFiveCe = topFive.reduce((pre, cur) => {
return pre + cur.ce
}, 0);
await RoleModel.findOneAndUpdate({roleId}, { topFive, topFiveCe, $inc: {ce: distance} });
await PvpDefenseModel.findOneAndUpdate({roleId, 'heroes.actorId': hid}, {$inc: {ce: distance}});
await HeroModel.findOneAndUpdate({roleId, hid}, {$inc:{ce: distance, historyCe: historyDistance}})
}
}