试炼
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Application, BackendSession, pinus } from 'pinus';
|
||||
import { resResult, genCode } from '../../../pubUtils/util';
|
||||
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';
|
||||
@@ -10,6 +10,10 @@ import { lockData } from '../../../services/redLockService';
|
||||
import { GUILD_DATA_NAME } 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';
|
||||
|
||||
export default function (app: Application) {
|
||||
return new GuildTrainHandler(app);
|
||||
}
|
||||
@@ -20,13 +24,13 @@ export class GuildTrainHandler {
|
||||
}
|
||||
|
||||
//获得试炼的详情
|
||||
async getTrainScriptByTrainId(msg: { code: string}, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
async getTrainInstance(msg: {}, session: BackendSession) {
|
||||
const roleId = session.get('roleId');
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await getUserGuild(roleId, code);
|
||||
let userGuild = await getUserGuild(roleId);
|
||||
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) {
|
||||
@@ -44,65 +48,69 @@ export class GuildTrainHandler {
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async trainScriptStart(msg: { code: string, trainId: number, hid: number }, session: BackendSession) {
|
||||
const { code, trainId, hid } = msg;
|
||||
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');
|
||||
let userGuild = await getUserGuild(roleId, code);
|
||||
if (!userGuild)
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await getUserGuild(roleId);
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
if (userGuild.trainCount > 2) //TODO检查次数
|
||||
const { guildCode: code } = userGuild;
|
||||
if (userGuild.trainCount <= 0)
|
||||
return resResult(STATUS.GUILD_TRAIN_BATTLE_COUNT_NOT_ENOUGH);
|
||||
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId, 'trainId isComplete trainScripts');
|
||||
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 trainScript = findWhere(guildTrain.trainScripts, { hid });
|
||||
if (!trainScript)
|
||||
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);
|
||||
//TODO 判断是否压制成功
|
||||
if (trainScript.progress >= 1000) {
|
||||
if (trainInstance.progress >= instance.progress) {
|
||||
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,
|
||||
roleId, roleName, battleId: instance.warId,
|
||||
status: 0,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.warType,
|
||||
record: { heroes:[], trainId, hid},
|
||||
record: { heroes:[], trainId, hid, guildCode: code, difficulty},
|
||||
}
|
||||
}, true);
|
||||
return resResult(STATUS.SUCCESS, { battleCode });
|
||||
}
|
||||
|
||||
async trainScriptEnd(msg: { code: string, battleCode: string, isSuccess: boolean}, session: BackendSession) {
|
||||
const { code, battleCode, isSuccess} = msg;
|
||||
async trainBattleEnd(msg: { battleCode: string, isSuccess: boolean}, session: BackendSession) {
|
||||
const { 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);
|
||||
const roleName = session.get('roleName');
|
||||
const sid = session.get('sid');
|
||||
let userGuild = await getUserGuild(roleId);
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
let time = battleRecord.createdAt.getTime()/1000;
|
||||
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 > getTodayZeroPoint()) {
|
||||
if (userGuild.trainTime > getTodayZeroPoint()) {
|
||||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount: userGuild.trainCount + 1});
|
||||
} else {
|
||||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount: 1, trainTime: nowSeconds()});
|
||||
}
|
||||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount: userGuild.trainCount - 1});
|
||||
}
|
||||
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);//加锁
|
||||
@@ -113,29 +121,39 @@ export class GuildTrainHandler {
|
||||
res.releaseCallback();//解锁
|
||||
return resResult(STATUS.GUILD_TRAIN_SCRIPT_NOT_OPENED);
|
||||
}
|
||||
let trainScript = findWhere(guildTrain.trainScripts,{hid});
|
||||
if (!trainScript)
|
||||
let trainInstance = findWhere(guildTrain.trainInstances, { hid });
|
||||
if (!trainInstance)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
let addScore;
|
||||
//结算奖励 TODO
|
||||
|
||||
|
||||
let { trainInstances } = 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 = isSuccess?trainSoloReward.winScore:trainSoloReward.failScore;
|
||||
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 (trainScript.progress < 1000 ) {
|
||||
if (trainScript.progress + addScore >= 1000) {
|
||||
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.trainScripts.forEach(({hid: otherHid, progress})=>{
|
||||
if (hid != otherHid && progress == 1000) {
|
||||
guildTrain.trainInstances.forEach(({hid: otherHid, progress})=>{
|
||||
if (hid != otherHid && progress != instance.progress) {
|
||||
isComplete = false;
|
||||
}
|
||||
});
|
||||
@@ -143,7 +161,7 @@ export class GuildTrainHandler {
|
||||
needLockNext = true;
|
||||
}
|
||||
}
|
||||
let progress = 1000;
|
||||
let progress = instance.progress;
|
||||
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
if (needLockNext) { //
|
||||
let nextResGuildTrain = await lockTrain(code, trainId + 1);
|
||||
@@ -152,7 +170,7 @@ export class GuildTrainHandler {
|
||||
}
|
||||
res.releaseCallback();//解锁
|
||||
} else {
|
||||
let progress = trainScript.progress + addScore;
|
||||
let progress = trainInstance.progress + addScore;
|
||||
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
res.releaseCallback();//解锁
|
||||
}
|
||||
@@ -170,26 +188,35 @@ export class GuildTrainHandler {
|
||||
}
|
||||
guildTrains.push(resGuildTrain);
|
||||
let { trainCount, trainRewards } = userGuild;
|
||||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, trainIds);
|
||||
let result:any = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, trainIds);
|
||||
result.battleGoods = goods;
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async getTrainScriptBox(msg: { code: string, trainId: number , hid: number, index: number}, session: BackendSession) {
|
||||
let { code, trainId, hid, index } = msg;
|
||||
async getTrainInstanceBox(msg: { trainId: number , hid: number, index: number}, session: BackendSession) {
|
||||
let { trainId, hid, index } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await getUserGuild(roleId, code);
|
||||
if (!userGuild)
|
||||
const roleName = session.get('roleName');
|
||||
const sid = session.get('sid');
|
||||
let userGuild = await getUserGuild(roleId);
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
const { guildCode: code } = userGuild;
|
||||
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);
|
||||
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;
|
||||
|
||||
let good = getRandomByLen(heroRewards);
|
||||
let goods = await addItems(roleId, roleName, sid, [good]);
|
||||
|
||||
let resGuildTrain = await GuildTrainModel.receiveBoxByIndex(code, roleId, trainId, hid, index, good);
|
||||
res.releaseCallback();//解锁
|
||||
|
||||
@@ -199,21 +226,24 @@ export class GuildTrainHandler {
|
||||
}
|
||||
let result:any = getGuildTrainRewards(resGuildTrain);
|
||||
|
||||
result.goods = [good];
|
||||
result.goods = goods;
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async getTrainLvUpRewards(msg: {code: string, trainId: number }, session: BackendSession) {
|
||||
let { code, trainId } = msg;
|
||||
async getTrainLvUpRewards(msg: {trainId: number }, session: BackendSession) {
|
||||
let { trainId } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
//TODO奖励
|
||||
let good;
|
||||
const roleName = session.get('roleName');
|
||||
const sid = session.get('sid');
|
||||
|
||||
let { jinjieReward } = getTrainBaseByLv(trainId);
|
||||
let userGuild = await UserGuildModel.receiveTrainRewards(roleId, trainId);
|
||||
if (!userGuild) {
|
||||
return resResult(STATUS.GUILD_GET_TRAIN_REWARD_FAIL);
|
||||
}
|
||||
let goods = await addItems(roleId, roleName, sid, jinjieReward);
|
||||
let { trainRewards } = userGuild;
|
||||
return resResult(STATUS.SUCCESS, { trainRewards });
|
||||
return resResult(STATUS.SUCCESS, { trainRewards, goods });
|
||||
}
|
||||
//购买挑战次数
|
||||
async purchaseTrainCount(msg: {code: string, trainId: number }, session: BackendSession) {
|
||||
|
||||
Reference in New Issue
Block a user