101 lines
4.1 KiB
TypeScript
101 lines
4.1 KiB
TypeScript
import { UserGuildModel } from '../db/UserGuild';
|
|
import { getJobInfoById } from '../pubUtils/gamedata';
|
|
import { getTodayZeroPoint, nowSeconds } from '../pubUtils/timeUtil';
|
|
import { GUILD_REPORT_NUM } from '../consts/constModules/guildConst';
|
|
import { GuildTrainType, GuildTrainModel } from '../db/GuildTrain';
|
|
import { GuildModel } from '../db/Guild';
|
|
import { findWhere } from 'underscore';
|
|
export async function getUserGuild(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 < getTodayZeroPoint()) {
|
|
trainCount = 0;
|
|
}
|
|
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount, trainTime: nowSeconds()});
|
|
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 < getTodayZeroPoint()) {
|
|
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 trainBoxs = guildTrain.trainScripts.map(({hid, trainBoxs})=>{
|
|
return {hid, recordBoxs: trainBoxs};
|
|
})
|
|
return { trainBoxs};
|
|
}
|
|
|
|
export function getGuildTrainInfo (guildTrains: Array<GuildTrainType>, roleId: string, trainCount:number, trainRewards: Array<number>, trainIds:Array<number>) {
|
|
let guildTrain;
|
|
guildTrains.map(({trainId, isComplete, trainScripts, 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 resTrainBoxs = [];
|
|
let resTrainScripts = trainScripts.map(({hid, progress, time, trainBoxs})=>{
|
|
resTrainBoxs.push({hid, recordBoxs: trainBoxs});
|
|
return {hid, progress, time};
|
|
})
|
|
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.trainScripts.map(({hid, time, trainBoxs}) => {
|
|
if (time + 24 * 60 * 60 < nowSeconds()) {
|
|
flag = true;
|
|
}
|
|
return {};
|
|
});
|
|
if (flag) {
|
|
resTrainBoxs.push(...lastTrainBoxs);
|
|
}
|
|
}
|
|
guildTrain = {trainId, isComplete, trainScripts: resTrainScripts, trainBoxs: resTrainBoxs, reports, myRank, ranks: resRanks};
|
|
});
|
|
return { guildTrain, trainCount, trainRewards};
|
|
}
|
|
|
|
export async function lockTrain(code: string, trainId: number) {
|
|
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId);
|
|
if (!!guildTrain) {
|
|
return;
|
|
}
|
|
let trainScripts;
|
|
guildTrain = await GuildTrainModel.openGuildTrain(code, trainId, trainScripts);
|
|
await GuildModel.updateInfo(code, {trainId});
|
|
return guildTrain;
|
|
} |