试炼
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) {
|
||||
|
||||
@@ -79,6 +79,7 @@ export async function getBossInstanceWhenEnd(bossInstance: BossInstanceType, rol
|
||||
});
|
||||
result.myRank = myRank;
|
||||
result.ranks = pushRanks;
|
||||
result.isBattled = isBattled;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { UserGuildModel } from '../db/UserGuild';
|
||||
import { getJobInfoById } from '../pubUtils/gamedata';
|
||||
import { getArmyTrainJuDian } from '../pubUtils/data';
|
||||
import { getTodayZeroPoint, nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { GUILD_REPORT_NUM } from '../consts/constModules/guildConst';
|
||||
import { GuildTrainType, GuildTrainModel } from '../db/GuildTrain';
|
||||
import { GuildTrainType, GuildTrainModel, TrainInstance } from '../db/GuildTrain';
|
||||
import { GuildModel } from '../db/Guild';
|
||||
import { findWhere } from 'underscore';
|
||||
export async function getUserGuild(roleId: string, code: string) {
|
||||
export async function getUserGuild(roleId: string) {
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId,'trainCount trainTime trainRewards');
|
||||
if (!userGuild||userGuild.guildCode != code)
|
||||
if (!userGuild)
|
||||
return;
|
||||
let { trainCount, trainTime} = userGuild;
|
||||
if (trainTime < getTodayZeroPoint()) {
|
||||
trainCount = 0;
|
||||
trainCount = 2;
|
||||
}
|
||||
userGuild = await UserGuildModel.updateInfo(roleId, {trainCount, trainTime: nowSeconds()});
|
||||
return userGuild;
|
||||
@@ -37,16 +37,21 @@ export function getGuildTrain (guildTrains, trainCount:number, trainRewards: Arr
|
||||
return { list, trainCount, trainRewards};
|
||||
}
|
||||
|
||||
export function getGuildTrainRewards (guildTrain) {
|
||||
let trainBoxs = guildTrain.trainScripts.map(({hid, trainBoxs})=>{
|
||||
return {hid, recordBoxs: trainBoxs};
|
||||
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, trainScripts, ranks, reports})=>{
|
||||
guildTrains.map(({trainId, isComplete, trainInstances, ranks, reports})=>{
|
||||
if (trainIds.indexOf(trainId) == -1) {
|
||||
return;
|
||||
}
|
||||
@@ -59,10 +64,15 @@ export function getGuildTrainInfo (guildTrains: Array<GuildTrainType>, roleId:
|
||||
myRank = {roleId: rankRoleId, score, rankLv: index+1};
|
||||
return {roleId: rankRoleId, score, rankLv: index+1};
|
||||
});
|
||||
let { trainInstances: instances } = getArmyTrainJuDian(trainId);
|
||||
let resTrainBoxs = [];
|
||||
let resTrainScripts = trainScripts.map(({hid, progress, time, trainBoxs})=>{
|
||||
resTrainBoxs.push({hid, recordBoxs: trainBoxs});
|
||||
return {hid, progress, time};
|
||||
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) {
|
||||
@@ -74,17 +84,17 @@ export function getGuildTrainInfo (guildTrains: Array<GuildTrainType>, roleId:
|
||||
reports = [...trainReports, ...reports];
|
||||
}
|
||||
let flag = false;
|
||||
let lastTrainBoxs = lastGuildTrain.trainScripts.map(({hid, time, trainBoxs}) => {
|
||||
if (time + 24 * 60 * 60 < nowSeconds()) {
|
||||
let lastTrainBoxs = lastGuildTrain.trainInstances.map(({hid, endTime, trainBoxs}) => {
|
||||
if (endTime < nowSeconds()) {
|
||||
flag = true;
|
||||
}
|
||||
return {};
|
||||
return {hid, recordBoxs: trainBoxs, trainId: lastGuildTrain.trainId, endTime, isComplete: true};
|
||||
});
|
||||
if (flag) {
|
||||
resTrainBoxs.push(...lastTrainBoxs);
|
||||
}
|
||||
}
|
||||
guildTrain = {trainId, isComplete, trainScripts: resTrainScripts, trainBoxs: resTrainBoxs, reports, myRank, ranks: resRanks};
|
||||
guildTrain = {trainId, isComplete, trainInstances: resTrainInstances, trainBoxs: resTrainBoxs, reports, myRank, ranks: resRanks};
|
||||
});
|
||||
return { guildTrain, trainCount, trainRewards};
|
||||
}
|
||||
@@ -94,8 +104,19 @@ export async function lockTrain(code: string, trainId: number) {
|
||||
if (!!guildTrain) {
|
||||
return;
|
||||
}
|
||||
let trainScripts;
|
||||
guildTrain = await GuildTrainModel.openGuildTrain(code, trainId, trainScripts);
|
||||
await GuildModel.updateInfo(code, {trainId});
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user