192 lines
7.9 KiB
TypeScript
192 lines
7.9 KiB
TypeScript
import { UserGuildModel } from '../db/UserGuild';
|
|
import { getArmyTrainJuDian } from '../pubUtils/data';
|
|
import { nowSeconds, getHourPoint } from '../pubUtils/timeUtil';
|
|
import { GUILD_REPORT_NUM, GUILD_DATA_NAME, REFRESH_HOUR } from '../consts/constModules/guildConst';
|
|
import { GuildTrainType, GuildTrainModel, TrainInstance } from '../db/GuildTrain';
|
|
import { GuildModel } from '../db/Guild';
|
|
import { findWhere } from 'underscore';
|
|
import { ARMY } from '../pubUtils/dicParam';
|
|
import { lockData } from './redLockService';
|
|
import { pinus } from 'pinus';
|
|
import { MailModel, MailType } from '../db/Mail';
|
|
import { getRandomByLen, resResult } from '../pubUtils/util';
|
|
import { getRedis } from './redisService';
|
|
import { STATUS } from '../consts/statusCode';
|
|
export async function getUserGuild(roleId: string, serverId: number) {
|
|
let userGuild = await UserGuildModel.getMyGuild(roleId,'trainCount trainTime trainRewards');
|
|
if (!userGuild)
|
|
return;
|
|
let { trainCount, trainTime, buyTrainCount, guildCode} = userGuild;
|
|
await resetTrain(guildCode, serverId);
|
|
if (trainTime < getHourPoint(REFRESH_HOUR)) {
|
|
trainCount = ARMY.ARMY_TRAIN_BUYTIMES;
|
|
buyTrainCount = 0;
|
|
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount, trainTime: nowSeconds(), buyTrainCount}, {});
|
|
}
|
|
|
|
return userGuild;
|
|
}
|
|
|
|
export async function recordUserGuild(roleId: string, code: string) {
|
|
let userGuild = await UserGuildModel.getMyGuild(roleId,'trainCount trainTime trainRewards');
|
|
if (!userGuild||userGuild.guildCode != code)
|
|
return;
|
|
let { trainCount, trainTime} = userGuild;
|
|
if (trainTime < getHourPoint(REFRESH_HOUR)) {
|
|
trainCount = 0;
|
|
}
|
|
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount, trainTime: nowSeconds()}, {});
|
|
return userGuild;
|
|
}
|
|
|
|
export function getGuildTrain (guildTrains, trainCount:number, trainRewards: Array<number>) {
|
|
let list = [];
|
|
list = guildTrains.map(({trainId, isComplete})=>{
|
|
return ({trainId, isComplete, });
|
|
});
|
|
return { list, trainCount, trainRewards};
|
|
}
|
|
|
|
export function getGuildTrainRewards (guildTrain) {
|
|
let { trainInstances: instances } = getArmyTrainJuDian(guildTrain.trainId);
|
|
let trainBoxs = guildTrain.trainInstances.map(({hid, trainBoxs, endTime})=>{
|
|
let instance = findWhere(instances, { hid });
|
|
let isComplete = false;
|
|
if ( guildTrain.progress >= instance.progress)
|
|
isComplete = true;
|
|
return {hid, recordBoxs: trainBoxs, trainId: guildTrain.trainId, endTime, isComplete};
|
|
})
|
|
return { trainBoxs};
|
|
}
|
|
|
|
export function getGuildTrainInfo (guildTrains: Array<GuildTrainType>, roleId: string, trainCount:number, trainRewards: Array<number>, trainIds:Array<number>) {
|
|
let guildTrain;
|
|
guildTrains.map(({trainId, isComplete, trainInstances, ranks, reports})=>{
|
|
if (trainIds.indexOf(trainId) == -1) {
|
|
return;
|
|
}
|
|
ranks.sort(function(a, b) {
|
|
return b.score - a.score;
|
|
});
|
|
let myRank = {};
|
|
let resRanks = ranks.map(({roleId: rankRoleId, score}, index)=>{
|
|
if (roleId == rankRoleId)
|
|
myRank = {roleId: rankRoleId, score, rankLv: index+1};
|
|
return {roleId: rankRoleId, score, rankLv: index+1};
|
|
});
|
|
let { trainInstances: instances } = getArmyTrainJuDian(trainId);
|
|
let resTrainBoxs = [];
|
|
let resTrainInstances = trainInstances.map(({hid, progress, endTime, trainBoxs})=>{
|
|
let instance = findWhere(instances, { hid });
|
|
let isComplete = false;
|
|
if ( progress >= instance.progress)
|
|
isComplete = true;
|
|
resTrainBoxs.push({hid, recordBoxs: trainBoxs, trainId, endTime, isComplete});
|
|
return {hid, progress, endTime, isComplete};
|
|
})
|
|
let lastGuildTrain = findWhere(guildTrains, { trainId: trainId - 1});
|
|
if (!!lastGuildTrain) {
|
|
let lenNum = lastGuildTrain.reports.length;
|
|
if (lenNum < GUILD_REPORT_NUM)
|
|
reports = [...lastGuildTrain.reports, ...reports];
|
|
else {
|
|
let trainReports = lastGuildTrain.reports.splice(lenNum - GUILD_REPORT_NUM - 1, GUILD_REPORT_NUM);
|
|
reports = [...trainReports, ...reports];
|
|
}
|
|
let flag = false;
|
|
let lastTrainBoxs = lastGuildTrain.trainInstances.map(({hid, endTime, trainBoxs}) => {
|
|
if (endTime < nowSeconds()) {
|
|
flag = true;
|
|
}
|
|
return {hid, recordBoxs: trainBoxs, trainId: lastGuildTrain.trainId, endTime, isComplete: true};
|
|
});
|
|
if (flag) {
|
|
resTrainBoxs.push(...lastTrainBoxs);
|
|
}
|
|
}
|
|
guildTrain = {trainId, isComplete, trainInstances: resTrainInstances, trainBoxs: resTrainBoxs, reports, myRank, ranks: resRanks};
|
|
});
|
|
return { guildTrain, trainCount, trainRewards};
|
|
}
|
|
|
|
export async function unlockTrain(code: string, trainId: number) {
|
|
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId);
|
|
if (!!guildTrain) {
|
|
return;
|
|
}
|
|
let { trainInstances } = getArmyTrainJuDian(trainId);
|
|
// 初始化
|
|
|
|
let instances:Array<TrainInstance> = trainInstances.map(trainInstance => {
|
|
let t = new TrainInstance();
|
|
t.hid = trainInstance.hid;
|
|
t.progress = 0;
|
|
t.endTime = 0;
|
|
t.trainBoxs = [];
|
|
return t;
|
|
});
|
|
guildTrain = await GuildTrainModel.openGuildTrain(code, trainId, instances);
|
|
await GuildModel.updateInfo(code, {trainId}, {});
|
|
|
|
return guildTrain;
|
|
}
|
|
|
|
export async function resetTrain(code: string, serverId: number) {
|
|
let res:any = await lockData(serverId, GUILD_DATA_NAME.GUILD, code);//加锁
|
|
if (!!res.err)
|
|
return;
|
|
let guild = await GuildModel.resetGuildTrain(code, serverId);
|
|
if (!guild) {
|
|
res.releaseCallback();//解锁
|
|
return;
|
|
}
|
|
res.releaseCallback();//解锁
|
|
const userGuildList = await UserGuildModel.getListByGuild(code, 'trainRewards', {});
|
|
const guildTrains = await GuildTrainModel.getGuildTrainBoxs(code);
|
|
let mails = new Array<MailType>();
|
|
let pushMessage = [];
|
|
let uids = [];
|
|
userGuildList.forEach(async function ({roleId, trainRewards}) {
|
|
let goods = [];
|
|
guildTrains.forEach(guildTrain=>{
|
|
guildTrain.trainInstances.forEach(({ trainBoxs })=>{
|
|
if (!findWhere(trainBoxs, {roleId})) {
|
|
let { heroRewards } = getArmyTrainJuDian(guildTrain.trainId);
|
|
let good = getRandomByLen(heroRewards);
|
|
goods.push(good);
|
|
}
|
|
})
|
|
});
|
|
for (let trainId = 1; trainId < guild.trainId; trainId++) {
|
|
let { jinjieReward } = getArmyTrainJuDian(trainId);
|
|
if (trainRewards.indexOf(trainId)) {
|
|
goods.push(...jinjieReward);
|
|
}
|
|
}
|
|
let key = 'login_roleId_' + roleId;
|
|
let sid = await getRedis(key);
|
|
if (!!goods.length) {
|
|
const doc = new MailModel();
|
|
const mail = Object.assign(doc.toJSON(), {roleId, goods, sendName: '系统', mailId: 1, sendTime: nowSeconds(), content:'练兵场未领取宝箱奖励和进阶奖励:'});
|
|
mails.push(mail);
|
|
if (!!sid) {
|
|
pushMessage.push({route: 'onMailsAdd', data:[mail], uid: roleId, sid});
|
|
}
|
|
}
|
|
if (!!sid) {
|
|
uids.push({ uid: roleId, sid});
|
|
}
|
|
});
|
|
pinus.app.channelService.pushMessageByUids('onGuildTainReset', resResult(STATUS.SUCCESS, {}), uids);
|
|
await MailModel.addMails(mails);
|
|
await GuildTrainModel.resetGuildTrain(code);
|
|
await unlockTrain(code, 1);
|
|
await UserGuildModel.resetTrainUserGuild(code);
|
|
}
|
|
|
|
export async function checkesetTrain(roleId: string, serverId: number) {
|
|
let userGuild = await UserGuildModel.getMyGuild(roleId,'guildCode');
|
|
if (!userGuild)
|
|
return;
|
|
await resetTrain(userGuild.guildCode, serverId);
|
|
} |