288 lines
14 KiB
TypeScript
288 lines
14 KiB
TypeScript
import { Application, BackendSession, pinus } from 'pinus';
|
|
import { resResult, genCode, getRandomByLen } 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, getHourPoint, getCurHourPoint } from '../../../pubUtils/timeUtil';
|
|
import { getUserGuild, getGuildTrainInfo, unlockTrain, resetTrain, getGuildTrainRewards} from '../../../services/guildTrainService';
|
|
import { findIndex, findWhere, indexBy } from 'underscore'
|
|
import { lockData } from '../../../services/redLockService';
|
|
import { GUILD_DATA_NAME, REFRESH_HOUR } from '../../../consts/constModules/guildConst';
|
|
import { UserGuildModel } from '../../../db/UserGuild';
|
|
import { GuildModel } from '../../../db/Guild';
|
|
import { getArmyTrainJuDian, getTrainSoloReward, getTrainBaseByLv } from '../../../pubUtils/data';
|
|
import { CURRENCY_BY_TYPE, CURRENCY_TYPE } from '../../../consts/constModules/itemConst';
|
|
import { handleCost, addItems } from '../../../services/rewardService';
|
|
import { ARMY } from '../../../pubUtils/dicParam';
|
|
import { addActive } from '../../../services/guildService';
|
|
import { GUILD_STRUCTURE } from '../../../consts/constModules/guildConst';
|
|
export default function (app: Application) {
|
|
return new GuildTrainHandler(app);
|
|
}
|
|
|
|
export class GuildTrainHandler {
|
|
constructor(private app: Application) {
|
|
|
|
}
|
|
|
|
//获得试炼的详情
|
|
async getTrainInstance(msg: {}, session: BackendSession) {
|
|
const roleId: string = session.get('roleId');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
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 unlockTrain(code, trainId);
|
|
if (!guildTrains) {
|
|
guildTrains.push(guildTrain);
|
|
}
|
|
}
|
|
let { trainCount, trainRewards, buyTrainCount } = userGuild;
|
|
let result:any = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, [trainId]);
|
|
result.buyTrainCount = buyTrainCount;
|
|
return resResult(STATUS.SUCCESS, result);
|
|
}
|
|
|
|
async trainBattleStart(msg: { hid: number, trainId: number, difficulty: number }, session: BackendSession) {
|
|
const { hid, difficulty, trainId } = msg;
|
|
const roleId = session.get('roleId');
|
|
const roleName = session.get('roleName');
|
|
const serverId = parseInt(session.get('serverId'));
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
if (userGuild.trainCount <= 0)
|
|
return resResult(STATUS.GUILD_TRAIN_BATTLE_COUNT_NOT_ENOUGH);
|
|
let { trainId: curTeainId } = await GuildModel.findGuild(code, serverId, 'trainId');
|
|
if (curTeainId !== trainId)
|
|
return resResult(STATUS.GUILD_TRAIN_LEVEL_IS_COMPLETE);
|
|
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId, 'trainId isComplete trainInstances');
|
|
if (!guildTrain && guildTrain.isComplete)
|
|
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
|
let { trainInstances } = getArmyTrainJuDian(trainId);
|
|
let instance = findWhere(trainInstances, { hid });
|
|
if (!instance)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let trainInstance = findWhere(guildTrain.trainInstances, { hid });
|
|
if (!trainInstance)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (trainInstance.progress >= instance.progress) {
|
|
return resResult(STATUS.GUILD_TRAIN_IS_COMPLETE);
|
|
}
|
|
const battleCode = genCode(8); // 关卡唯一值
|
|
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
|
$set: {
|
|
roleId, roleName, battleId: instance.warId,
|
|
status: 0,
|
|
record: { heroes:[], trainId, hid, guildCode: code, difficulty},
|
|
}
|
|
}, true);
|
|
return resResult(STATUS.SUCCESS, { battleCode });
|
|
}
|
|
|
|
async trainBattleEnd(msg: { battleCode: string, isSuccess: boolean}, session: BackendSession) {
|
|
const { battleCode, isSuccess } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
const roleName: string = session.get('roleName');
|
|
const sid: string = session.get('sid');
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
const battleRecord = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
|
// if (!battleRecord || battleRecord.status != 0 || roleId != battleRecord.roleId || battleRecord.record.guildCode != code) {
|
|
// return resResult(STATUS.WRONG_PARMS);
|
|
// }
|
|
let time = Math.floor(battleRecord.createdAt.getTime()/1000);
|
|
// if (userGuild.trainCount - 1 < 0) {
|
|
// return resResult(STATUS.WRONG_PARMS);
|
|
// }
|
|
|
|
if (time < getCurHourPoint(REFRESH_HOUR) && nowSeconds() > getCurHourPoint(REFRESH_HOUR)) {
|
|
return resResult(STATUS.GUILD_TRAIN_IS_RESETED);//关卡已经重置
|
|
}
|
|
if (time > getHourPoint(REFRESH_HOUR)) {
|
|
userGuild = await UserGuildModel.updateInfo(roleId, {}, { trainCount: -1 });
|
|
}
|
|
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
|
$set: { status: isSuccess?1:2 }
|
|
}, true);
|
|
|
|
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 trainInstance = findWhere(guildTrain.trainInstances, { hid });
|
|
if (!trainInstance)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
|
|
let { trainInstances, soloRewardRatio } = getArmyTrainJuDian(trainId);
|
|
let instance = findWhere(trainInstances, { hid });
|
|
if (!instance)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let trainSoloReward = getTrainSoloReward(battleRecord.record.difficulty);
|
|
if (!trainSoloReward)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
let addScore = Math.floor((isSuccess?trainSoloReward.winScore:trainSoloReward.failScore) * (soloRewardRatio + 100 ) / 100);
|
|
let goods = await addItems(roleId, roleName, sid, [{id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.HONOUR), count: isSuccess?trainSoloReward.failHonour:trainSoloReward.failHonour}]);
|
|
let guildTrains = [];
|
|
let trainIds = [];
|
|
//是否压制
|
|
let { isComplete, reports, ranks } = guildTrain;
|
|
let index = findIndex(ranks, {roleId});
|
|
if (index !== -1) {
|
|
ranks[index].score += addScore;
|
|
} else {
|
|
ranks.push({score: addScore, roleId});
|
|
}
|
|
let needLockNext = false;
|
|
let resGuildTrain;
|
|
let report = {roleId, trainId, hid, score: addScore, time: nowSeconds(), isSuccessed: isSuccess, type: 1};//1表示普通战报, 2表示系统战报即:被成功压制
|
|
if (trainInstance.progress < instance.progress ) {
|
|
if (trainInstance.progress + addScore >= instance.progress) {
|
|
reports.push(...[{isSuccessed: true, type: 2, time:nowSeconds(), score: addScore, roleId, trainId, hid},report]);
|
|
if (!isComplete) {
|
|
isComplete = true;
|
|
guildTrain.trainInstances.forEach(({hid: otherHid, progress})=>{
|
|
if (hid != otherHid && progress != instance.progress) {
|
|
isComplete = false;
|
|
}
|
|
});
|
|
if (isComplete) { //解锁下一关
|
|
needLockNext = true;
|
|
}
|
|
}
|
|
let progress = instance.progress;
|
|
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
|
if (needLockNext) { //
|
|
let nextResGuildTrain = await unlockTrain(code, trainId + 1);
|
|
trainIds.push(nextResGuildTrain.trainId);
|
|
guildTrains.push(nextResGuildTrain);
|
|
}
|
|
res.releaseCallback();//解锁
|
|
} else {
|
|
let progress = trainInstance.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:any = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, trainIds);
|
|
result.battleGoods = goods;
|
|
await addActive(roleId, serverId, 2, 1);
|
|
return resResult(STATUS.SUCCESS, result);
|
|
}
|
|
|
|
async getTrainInstanceBox(msg: { trainId: number , hid: number, index: number}, session: BackendSession) {
|
|
let { trainId, hid, index } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
const roleName: string = session.get('roleName');
|
|
const sid = session.get('sid');
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
let { structure } = await GuildModel.findGuild(code, serverId, 'structure');
|
|
let stru = findWhere(structure, {id: 4});
|
|
let res:any = await lockData(serverId, GUILD_DATA_NAME.TRAIN_BOX, code + '_' + trainId);//加锁
|
|
if (!!res.err)
|
|
return resResult(STATUS.REDLOCK_ERR);
|
|
|
|
let { shilianRewardRatio } = getTrainBaseByLv(GUILD_STRUCTURE.TRAIN);
|
|
let { heroRewards, trainInstances } = getArmyTrainJuDian(trainId);
|
|
let { progress } = findWhere(trainInstances, {hid});
|
|
let guildTrain = await GuildTrainModel.findTrainInstanceBoxByIndex(code, roleId, trainId, hid, index, progress, nowSeconds());
|
|
if (!guildTrain) {
|
|
res.releaseCallback();//解锁
|
|
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
|
}
|
|
|
|
let good = getRandomByLen(heroRewards);
|
|
good.count = Math.floor((100 + shilianRewardRatio) * good.count/100);
|
|
let goods = await addItems(roleId, roleName, sid, [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 = goods;
|
|
return resResult(STATUS.SUCCESS, result);
|
|
}
|
|
|
|
async getTrainLvUpRewards(msg: {trainId: number }, session: BackendSession) {
|
|
let { trainId } = msg;
|
|
const roleId: string = session.get('roleId');
|
|
const roleName: string = session.get('roleName');
|
|
const sid: string = session.get('sid');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
const { guildCode: code } = userGuild;
|
|
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId);
|
|
if (!guildTrain||!guildTrain.isComplete)
|
|
return resResult(STATUS.GUILD_TRAIN_IS_NOT_COMPLETE);
|
|
let { jinjieReward } = getArmyTrainJuDian(trainId);
|
|
if (userGuild.trainRewards.indexOf(trainId) != -1)
|
|
return resResult(STATUS.GUILD_TRAIN_QUALITY_REWARD_IS_GOT);
|
|
userGuild = await UserGuildModel.receiveTrainRewards(roleId, trainId);
|
|
if (!userGuild) {
|
|
return resResult(STATUS.INTERNAL_ERR);
|
|
}
|
|
let goods = await addItems(roleId, roleName, sid, jinjieReward);
|
|
let { trainRewards } = userGuild;
|
|
return resResult(STATUS.SUCCESS, { trainRewards, goods });
|
|
}
|
|
|
|
//购买挑战次数
|
|
async purchaseTrainCount(msg: {count: number}, session: BackendSession) {
|
|
let { count } = msg;
|
|
const roleId:string = session.get('roleId');
|
|
const serverId: number = parseInt(session.get('serverId'));
|
|
const sid:string = session.get('sid');
|
|
let userGuild = await getUserGuild(roleId, serverId);
|
|
if (!userGuild)
|
|
return resResult(STATUS.WRONG_PARMS);
|
|
if (userGuild.buyTrainCount >= ARMY.ARMY_TRAIN_BUYTIMES)
|
|
return resResult(STATUS.GUILD_BUY_TRAIN_COUNT_REACH_MAX);
|
|
let result = await handleCost(roleId, sid, [{id: CURRENCY_BY_TYPE.get(CURRENCY_TYPE.GOLD), count: ARMY.ARMY_TRAIN_TIMESCOST}]);
|
|
if(!result)
|
|
return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);
|
|
let { trainCount, buyTrainCount } = await UserGuildModel.addTrainCount(roleId, count);
|
|
return resResult(STATUS.SUCCESS, { trainCount, buyTrainCount });
|
|
}
|
|
} |