224 lines
10 KiB
TypeScript
224 lines
10 KiB
TypeScript
import { Application, BackendSession, pinus } from 'pinus';
|
||
import { resResult, genCode } from '../../../pubUtils/util';
|
||
import { STATUS, GUILD_OPERATE, GUILD_AUTH, GUILD_JOB } from '../../../consts';
|
||
import { GuildTrainModel } from '../../../db/GuildTrain';
|
||
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||
import { nowSeconds, getTodayZeroPoint } from '../../../pubUtils/timeUtil';
|
||
import { getUserGuild, getGuildTrainInfo, lockTrain, getGuildTrain, getGuildTrainRewards} from '../../../services/guildTrainService';
|
||
import { findIndex, findWhere, indexBy } from 'underscore'
|
||
import { lockData } from '../../../services/redLockService';
|
||
import { GUILD_DATA_NAME } from '../../../consts/constModules/guildConst';
|
||
import { UserGuildModel } from '../../../db/UserGuild';
|
||
import { GuildModel } from '../../../db/Guild';
|
||
export default function (app: Application) {
|
||
return new GuildTrainHandler(app);
|
||
}
|
||
|
||
export class GuildTrainHandler {
|
||
constructor(private app: Application) {
|
||
|
||
}
|
||
|
||
//获得试炼的详情
|
||
async getTrainScriptByTrainId(msg: { code: string}, session: BackendSession) {
|
||
const { code } = msg;
|
||
const roleId = session.get('roleId');
|
||
const serverId = session.get('serverId');
|
||
let userGuild = await getUserGuild(roleId, code);
|
||
if (!userGuild)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
let { trainId } = await GuildModel.findGuild(code, serverId, 'trainId');
|
||
let trainIds = [trainId];
|
||
if (trainId - 1 > 0) {
|
||
trainIds.push(trainId - 1);
|
||
}
|
||
let guildTrains = await GuildTrainModel.findGuildTrainByTrainIds(code, trainIds);
|
||
if (!guildTrains || !findWhere(guildTrains, {trainId})) {
|
||
let guildTrain = await lockTrain(code, trainId);
|
||
if (!guildTrains) {
|
||
guildTrains.push(guildTrain);
|
||
}
|
||
}
|
||
let { trainCount, trainRewards } = userGuild;
|
||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, [trainId]);
|
||
return resResult(STATUS.SUCCESS, result);
|
||
}
|
||
|
||
async trainScriptStart(msg: { code: string, trainId: number, hid: number }, session: BackendSession) {
|
||
const { code, trainId, hid } = msg;
|
||
const roleId = session.get('roleId');
|
||
const roleName = session.get('roleName');
|
||
let userGuild = await getUserGuild(roleId, code);
|
||
if (!userGuild)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
if (userGuild.trainCount > 2) //TODO检查次数
|
||
return resResult(STATUS.GUILD_TRAIN_BATTLE_COUNT_NOT_ENOUGH);
|
||
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId, 'trainId isComplete trainScripts');
|
||
if (!guildTrain && guildTrain.isComplete)
|
||
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
||
let trainScript = findWhere(guildTrain.trainScripts, { hid });
|
||
if (!trainScript)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
//TODO 判断是否压制成功
|
||
if (trainScript.progress >= 1000) {
|
||
return resResult(STATUS.GUILD_TRAIN_IS_COMPLETE);
|
||
}
|
||
const battleCode = genCode(8); // 关卡唯一值
|
||
//TODO查看地图字典
|
||
let warInfo;
|
||
let warId;
|
||
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||
$set: {
|
||
roleId, roleName, battleId: warId,
|
||
status: 0,
|
||
warName: warInfo.gk_name,
|
||
warType: warInfo.warType,
|
||
record: { heroes:[], trainId, hid},
|
||
}
|
||
}, true);
|
||
return resResult(STATUS.SUCCESS, { battleCode });
|
||
}
|
||
|
||
async trainScriptEnd(msg: { code: string, battleCode: string, isSuccess: boolean}, session: BackendSession) {
|
||
const { code, battleCode, isSuccess} = msg;
|
||
const roleId = session.get('roleId');
|
||
const serverId = session.get('serverId');
|
||
const battleRecord = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
||
if(!battleRecord || battleRecord.status != 0 || roleId != battleRecord.roleId) {
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
}
|
||
let userGuild = await getUserGuild(roleId, code);
|
||
if (!userGuild)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
let time = battleRecord.createdAt.getTime()/1000;
|
||
if (time > getTodayZeroPoint()) {
|
||
if (userGuild.trainTime > getTodayZeroPoint()) {
|
||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount: userGuild.trainCount + 1});
|
||
} else {
|
||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount: 1, trainTime: nowSeconds()});
|
||
}
|
||
}
|
||
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||
$set: { status: isSuccess?1:2 }
|
||
}, true);
|
||
let guildTrains = [];
|
||
let trainIds = [];
|
||
let trainId = battleRecord.record.trainId;
|
||
let hid = battleRecord.record.hid;
|
||
let res:any = await lockData(serverId, GUILD_DATA_NAME.TRAIN, code + '_' + trainId);//加锁
|
||
if (!!res.err)
|
||
return resResult(STATUS.REDLOCK_ERR);
|
||
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId);
|
||
if (!guildTrain) {
|
||
res.releaseCallback();//解锁
|
||
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
||
}
|
||
let trainScript = findWhere(guildTrain.trainScripts,{hid});
|
||
if (!trainScript)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
let addScore;
|
||
//结算奖励 TODO
|
||
|
||
|
||
//是否压制
|
||
let { isComplete, reports, ranks } = guildTrain;
|
||
let index = findIndex(ranks, {roleId});
|
||
if (index !== -1) {
|
||
ranks[index].score += addScore;
|
||
}
|
||
let needLockNext = false;
|
||
let resGuildTrain;
|
||
let report = {roleId, trainId, hid, score: addScore, time: nowSeconds(), isSuccessed: isSuccess, type: 1};//1表示普通战报, 2表示系统战报即:被成功压制
|
||
if (trainScript.progress < 1000 ) {
|
||
if (trainScript.progress + addScore >= 1000) {
|
||
reports.push(...[{isSuccessed: true, type: 2, time:nowSeconds(), score: addScore, roleId, trainId, hid},report]);
|
||
if (!isComplete) {
|
||
isComplete = true;
|
||
guildTrain.trainScripts.forEach(({hid: otherHid, progress})=>{
|
||
if (hid != otherHid && progress == 1000) {
|
||
isComplete = false;
|
||
}
|
||
});
|
||
if (isComplete) { //解锁下一关
|
||
needLockNext = true;
|
||
}
|
||
}
|
||
let progress = 1000;
|
||
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||
if (needLockNext) { //
|
||
let nextResGuildTrain = await lockTrain(code, trainId + 1);
|
||
trainIds.push(nextResGuildTrain.trainId);
|
||
guildTrains.push(nextResGuildTrain);
|
||
}
|
||
res.releaseCallback();//解锁
|
||
} else {
|
||
let progress = trainScript.progress + addScore;
|
||
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||
res.releaseCallback();//解锁
|
||
}
|
||
} else {
|
||
reports.push(report);
|
||
resGuildTrain = await GuildTrainModel.updateGuildTrain(code, trainId, {reports, ranks});
|
||
res.releaseCallback();//解锁
|
||
}
|
||
if (!needLockNext) {
|
||
if (trainId - 1 > 0 ) {
|
||
let lastGuildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId - 1);
|
||
guildTrains.push(lastGuildTrain);
|
||
}
|
||
trainIds.push(resGuildTrain.trainId);
|
||
}
|
||
guildTrains.push(resGuildTrain);
|
||
let { trainCount, trainRewards } = userGuild;
|
||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, trainIds);
|
||
return resResult(STATUS.SUCCESS, result);
|
||
}
|
||
|
||
async getTrainScriptBox(msg: { code: string, trainId: number , hid: number, index: number}, session: BackendSession) {
|
||
let { code, trainId, hid, index } = msg;
|
||
const roleId = session.get('roleId');
|
||
const serverId = session.get('serverId');
|
||
let userGuild = await getUserGuild(roleId, code);
|
||
if (!userGuild)
|
||
return resResult(STATUS.WRONG_PARMS);
|
||
let res:any = await lockData(serverId, GUILD_DATA_NAME.TRAIN_BOX, code + '_' + trainId);//加锁
|
||
if (!!res.err)
|
||
return resResult(STATUS.REDLOCK_ERR);
|
||
let guildTrain = await GuildTrainModel.findTrainScriptBoxByIndex(code, roleId, trainId, hid, index, 1000, nowSeconds() - 24*60*60);
|
||
if (!guildTrain) {
|
||
res.releaseCallback();//解锁
|
||
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
||
}
|
||
let good;
|
||
let resGuildTrain = await GuildTrainModel.receiveBoxByIndex(code, roleId, trainId, hid, index, good);
|
||
res.releaseCallback();//解锁
|
||
|
||
if (!resGuildTrain) {
|
||
let result:any = getGuildTrainRewards(guildTrain);
|
||
return resResult(STATUS.GUILD_GET_TRAIN_BOX_FAIL, result);
|
||
}
|
||
let result:any = getGuildTrainRewards(resGuildTrain);
|
||
|
||
result.goods = [good];
|
||
return resResult(STATUS.SUCCESS, result);
|
||
}
|
||
|
||
async getTrainLvUpRewards(msg: {code: string, trainId: number }, session: BackendSession) {
|
||
let { code, trainId } = msg;
|
||
const roleId = session.get('roleId');
|
||
//TODO奖励
|
||
let good;
|
||
let userGuild = await UserGuildModel.receiveTrainRewards(roleId, trainId);
|
||
if (!userGuild) {
|
||
return resResult(STATUS.GUILD_GET_TRAIN_REWARD_FAIL);
|
||
}
|
||
let { trainRewards } = userGuild;
|
||
return resResult(STATUS.SUCCESS, { trainRewards });
|
||
}
|
||
//购买挑战次数
|
||
async purchaseTrainCount(msg: {code: string, trainId: number }, session: BackendSession) {
|
||
let { code, trainId } = msg;
|
||
const roleId = session.get('roleId');
|
||
|
||
}
|
||
} |