军团表逻辑
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
import { Application, BackendSession, pinus } from 'pinus';
|
||||
import { resResult, genCode } from '../../../pubUtils/util';
|
||||
import { resResult, genCode, getRandomByLen } from '../../../pubUtils/util';
|
||||
import { STATUS } from '../../../consts';
|
||||
import { BossScriptModel } from '../../../db/BossScript';
|
||||
import { BossInstanceModel } from '../../../db/BossInstance';
|
||||
import { BattleRecordModel } from '../../../db/BattleRecord';
|
||||
import { nowSeconds, getTodayZeroPoint } from '../../../pubUtils/timeUtil';
|
||||
import { getBossScriptInfo, bossResult, checkMemberExists, pushBossHpMessage, getBossScriptWhenEnd, addBossScript } from '../../../services/guildBossService';
|
||||
import { findWhere } from 'underscore'
|
||||
import { GUILD_DATA_NAME } from '../../../consts/constModules/guildConst';
|
||||
import { getBossInstanceInfo, bossResult, checkMemberExists, pushBossHpMessage, getBossInstanceWhenEnd, addBossInstance } from '../../../services/guildBossService';
|
||||
import { findWhere, random } from 'underscore'
|
||||
import { GUILD_DATA_NAME, GUILD_STRUCTURE } from '../../../consts/constModules/guildConst';
|
||||
import { UserGuildModel } from '../../../db/UserGuild';
|
||||
import { GUILD_OPERATE } from '../../../consts';
|
||||
import { checkAuth } from '../../../services/guildService';
|
||||
import { GuildModel } from '../../../db/Guild';
|
||||
import { getBossByLv } from '../../../pubUtils/data';
|
||||
import { lockData } from '../../../services/redLockService';
|
||||
export default function (app: Application) {
|
||||
return new GuildHandler(app);
|
||||
}
|
||||
@@ -16,50 +22,73 @@ export class GuildHandler {
|
||||
}
|
||||
|
||||
// 获得boss关卡
|
||||
async getBossScript(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
//TODO校验
|
||||
async getBossInstance(msg: {}, session: BackendSession) {
|
||||
const roleId = session.get('roleId');
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!bossScript) {
|
||||
return resResult(STATUS.SUCCESS, {type: 1});//1:等待团长开启,2:今日已开启,且boss通关,3:开启中
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
const { guildCode: code } = userGuild;
|
||||
let bossInstance = await BossInstanceModel.findBossInstance(code);
|
||||
if (!bossInstance) {
|
||||
return resResult(STATUS.SUCCESS, {status: 1});//1:等待团长开启,2:今日已开启,且boss通关,3:开启中
|
||||
}
|
||||
let result = await getBossScriptInfo(bossScript, roleId);
|
||||
let result = await getBossInstanceInfo(bossInstance, roleId);
|
||||
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
//开启演武场
|
||||
async openBossScript(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
async openBossInstance(msg: {}, session: BackendSession) {
|
||||
const roleId = session.get('roleId');
|
||||
//TODO检查权限
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!!bossScript && ( bossScript.bossHp > 0 || bossScript.time >= getTodayZeroPoint() )) {
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId, 'auth guildCode');
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
const { guildCode: code } = userGuild;
|
||||
const checkMyResult = await checkAuth(GUILD_OPERATE.OPEN_BOSS, roleId, null, userGuild);
|
||||
if(!checkMyResult) return resResult(STATUS.GUILD_AUTH_NOT_ENOUGH);
|
||||
let res:any = await lockData(serverId, GUILD_DATA_NAME.BOSS_SCRIPT, code);//加锁
|
||||
if (!!res.err)
|
||||
return resResult(STATUS.REDLOCK_ERR);
|
||||
let bossInstance = await BossInstanceModel.findBossInstance(code);
|
||||
if (!!bossInstance && ( bossInstance.bossHp > 0 || bossInstance.startTime >= getTodayZeroPoint() )) {
|
||||
return resResult(STATUS.GUILD_SCRIPT_IS_OPENED_TODAY);
|
||||
}
|
||||
//TODO随机地图
|
||||
let bossHp = 1000;
|
||||
let warId = 1;
|
||||
await BossScriptModel.openBossScript(code, bossHp, warId);
|
||||
let result = {warId, ranks: [], myRank: {}, bossHp, type: 3};
|
||||
const guild = await GuildModel.findByCode(code, serverId, 'lv structure');
|
||||
if(!guild) {
|
||||
return resResult(STATUS.GUILD_NOT_FOUND);
|
||||
}
|
||||
const { structure } = guild;
|
||||
const curStructure = structure.find(cur => cur.id == GUILD_STRUCTURE.BOSS);
|
||||
if(!curStructure) {
|
||||
return resResult(STATUS.GUILD_STRUCTURE_NOT_FOUND);
|
||||
}
|
||||
let bossBase = getBossByLv(curStructure.lv);
|
||||
let {bossHp, warId } = getRandomByLen(bossBase.wars);
|
||||
let resGuild = await GuildModel.costFund(code, bossBase.consume)
|
||||
if (!resGuild)
|
||||
return resResult(STATUS.GUILD_FUND_NOT_ENOUGH);
|
||||
await BossInstanceModel.openBossInstance(code, bossHp, warId);
|
||||
let result = {warId, ranks: [], myRank: {}, bossHp, type: 3, isBattled: false};
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async bossScriptStart(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
async battleBoss(msg: {}, session: BackendSession) {
|
||||
const roleId = session.get('roleId');
|
||||
const roleName = session.get('roleName');
|
||||
//TODO校验
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!bossScript)
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
const { guildCode: code } = userGuild;
|
||||
let bossInstance = await BossInstanceModel.findBossInstance(code);
|
||||
if (!bossInstance)
|
||||
return resResult(STATUS.GUILD_SCRIPT_NOT_OPENED);
|
||||
if (bossScript.bossHp <= 0)
|
||||
if (bossInstance.bossHp <= 0)
|
||||
return resResult(STATUS.GUILD_SCRIPT_IS_COMPLETE);
|
||||
let myRank = findWhere(bossScript.ranks, {roleId});
|
||||
let myRank = findWhere(bossInstance.ranks, {roleId});
|
||||
if (!!myRank && myRank.time > getTodayZeroPoint())
|
||||
return resResult(STATUS.GUILD_SCRIPT_IS_BATTLED);
|
||||
let { warId, ranks } = bossScript;
|
||||
let { warId, ranks } = bossInstance;
|
||||
const battleCode = genCode(8); // 关卡唯一值
|
||||
//TODO查看地图字典
|
||||
let warInfo;
|
||||
@@ -67,52 +96,66 @@ export class GuildHandler {
|
||||
$set: {
|
||||
roleId, roleName, battleId: warId,
|
||||
status: 0,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.warType,
|
||||
record: { heroes:[],recordNum: bossScript.num, bossHp: bossScript.bossHp},
|
||||
// warName: warInfo.gk_name,
|
||||
// warType: warInfo.warType,
|
||||
record: { heroes:[],recordNum: bossInstance.num, bossHp: bossInstance.bossHp},
|
||||
}
|
||||
}, true);
|
||||
const serverId = session.get('serverId');
|
||||
await addBossScript(code, serverId, roleId);
|
||||
await addBossInstance(code, serverId, roleId);
|
||||
if (!findWhere(ranks, {roleId})) {
|
||||
await BossScriptModel.pushRanks(code, {roleId, score: 0, time:nowSeconds()});
|
||||
await BossInstanceModel.pushRanks(code, {roleId, score: 0, time: nowSeconds()});
|
||||
} else {
|
||||
await BossInstanceModel.updateRank(code, roleId);
|
||||
}
|
||||
return resResult(STATUS.SUCCESS, { battleCode });
|
||||
}
|
||||
|
||||
async action (msg: { code: string, damage: number, battleCode: string }, session: BackendSession ) {
|
||||
const { code, battleCode, damage } = msg;
|
||||
async action (msg: { damage: number, battleCode: string }, session: BackendSession ) {
|
||||
const { battleCode, damage } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
const { guildCode: code } = userGuild;
|
||||
let flag = await checkMemberExists(code, serverId, roleId, battleCode);
|
||||
if (!flag) {
|
||||
return;
|
||||
return resResult(STATUS.REDLOCK_ERR);
|
||||
}
|
||||
//记录伤害
|
||||
let bossScript = await BossScriptModel.updateBossHp(code, damage, roleId);
|
||||
if (!bossScript) {//进入结算
|
||||
let bossInstance = await BossInstanceModel.updateBossHp(code, damage, roleId);
|
||||
if (!bossInstance) {//进入结算
|
||||
let flag = await bossResult(code, serverId, GUILD_DATA_NAME.BOSS_SCRIPT, roleId, damage);
|
||||
if (!flag) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
return resResult(STATUS.SUCCESS, { bossHp: 0 });
|
||||
} else {
|
||||
pushBossHpMessage(code, serverId, bossScript.bossHp);
|
||||
pushBossHpMessage(code, serverId, bossInstance.bossHp);
|
||||
return resResult(STATUS.SUCCESS, { bossHp: bossInstance.bossHp });
|
||||
}
|
||||
return resResult(STATUS.SUCCESS, { bossHp: bossScript.bossHp>0?bossScript.bossHp:0 });
|
||||
}
|
||||
|
||||
async bossScriptEnd(msg: { code: string, battleCode: string }, session: BackendSession) {
|
||||
const { code, battleCode } = msg;
|
||||
async battleBossEnd(msg: { battleCode: string }, session: BackendSession) {
|
||||
const { battleCode } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
let userGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
|
||||
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) {
|
||||
if (!battleRecord || battleRecord.status != 0 || roleId != battleRecord.roleId) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||||
$set: { status: 1 }//战斗结束统一设置成1
|
||||
}, true);
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
let result = await getBossScriptWhenEnd(bossScript, roleId, battleRecord.record.recordNum);
|
||||
let bossInstance = await BossInstanceModel.findBossInstance(code);
|
||||
let result = await getBossInstanceWhenEnd(bossInstance, roleId, battleRecord.record.recordNum);
|
||||
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -4,11 +4,12 @@ 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 } from '../../../services/guildTrainService';
|
||||
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);
|
||||
}
|
||||
@@ -18,17 +19,28 @@ export class GuildTrainHandler {
|
||||
|
||||
}
|
||||
|
||||
async getTrainScript(msg: { code: string }, session: BackendSession) {
|
||||
//获得试炼的详情
|
||||
async getTrainScriptByTrainId(msg: { code: string}, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
let guildTrains = await GuildTrainModel.findGuildTrain(code);
|
||||
const serverId = session.get('serverId');
|
||||
let userGuild = await getUserGuild(roleId, code);
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
if (!guildTrains)
|
||||
guildTrains = [];
|
||||
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);
|
||||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, [trainId]);
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
@@ -41,8 +53,8 @@ export class GuildTrainHandler {
|
||||
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);
|
||||
if (!guildTrain)
|
||||
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)
|
||||
@@ -89,12 +101,13 @@ export class GuildTrainHandler {
|
||||
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 true;
|
||||
return resResult(STATUS.REDLOCK_ERR);
|
||||
let guildTrain = await GuildTrainModel.findTrainByTrainIdNotLock(code, trainId);
|
||||
if (!guildTrain) {
|
||||
res.releaseCallback();//解锁
|
||||
@@ -111,14 +124,14 @@ export class GuildTrainHandler {
|
||||
let { isComplete, reports, ranks } = guildTrain;
|
||||
let index = findIndex(ranks, {roleId});
|
||||
if (index !== -1) {
|
||||
ranks[index].score += addScore;
|
||||
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]);
|
||||
await GuildTrainModel.updateGuildTrain(code, trainId, {reports, ranks});
|
||||
let needLockNext = false;
|
||||
if (!isComplete) {
|
||||
isComplete = true;
|
||||
guildTrain.trainScripts.forEach(({hid: otherHid, progress})=>{
|
||||
@@ -126,39 +139,86 @@ export class GuildTrainHandler {
|
||||
isComplete = false;
|
||||
}
|
||||
});
|
||||
if (isComplete) {//解锁下一关
|
||||
if (isComplete) { //解锁下一关
|
||||
needLockNext = true;
|
||||
}
|
||||
}
|
||||
let progress = 1000;
|
||||
GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
if (needLockNext) {
|
||||
|
||||
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;
|
||||
GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
resGuildTrain = await GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
res.releaseCallback();//解锁
|
||||
}
|
||||
} else {
|
||||
reports.push(report);
|
||||
GuildTrainModel.updateGuildTrain(code, trainId, {reports, ranks});
|
||||
resGuildTrain = await GuildTrainModel.updateGuildTrain(code, trainId, {reports, ranks});
|
||||
res.releaseCallback();//解锁
|
||||
}
|
||||
let guildTrains = await GuildTrainModel.findGuildTrain(code);
|
||||
if (!guildTrains)
|
||||
guildTrains = [];
|
||||
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);
|
||||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards, trainIds);
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async getTrainScriptBox(msg: { code: string }, session: BackendSession) {
|
||||
|
||||
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: { trainId: number }, session: BackendSession) {
|
||||
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');
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { BossScriptType, BossScriptModel } from '../db/BossScript';
|
||||
import { BossInstanceType, BossInstanceModel } from '../db/BossInstance';
|
||||
import { getTodayZeroPoint, nowSeconds } from '../pubUtils/timeUtil';
|
||||
import { lockData } from '../services/redLockService';
|
||||
import { findIndex, indexBy, values } from 'underscore';
|
||||
@@ -10,22 +10,22 @@ import { deepCopy, resResult } from '../pubUtils/util';
|
||||
import { BattleRecordModel } from '../db/BattleRecord';
|
||||
/**
|
||||
*
|
||||
* @param bossScript
|
||||
* @param bossInstance
|
||||
* @param roleId
|
||||
*/
|
||||
export async function getBossScriptInfo(bossScript: BossScriptType, roleId:string) {
|
||||
let { warId, ranks, bossHp, winWarId, winTime, roleIdRecords, guildCode } = bossScript;
|
||||
export async function getBossInstanceInfo(bossInstance: BossInstanceType, roleId:string) {
|
||||
let { warId, ranks, bossHp, winWarId, winTime, roleIdRecords, guildCode } = bossInstance;
|
||||
ranks.sort(function(a, b) {
|
||||
return b.score - a.score + a.time - b.time;
|
||||
});
|
||||
let result:any = {};
|
||||
//1:等待团长开启,2:今日已开启,且boss通关,3:开启中
|
||||
if ( bossScript.bossHp <= 0 ) {
|
||||
if ( bossInstance.bossHp <= 0 ) {
|
||||
if (!!winWarId && winTime > getTodayZeroPoint() && roleIdRecords.indexOf(roleId) == -1) {
|
||||
result = { winWarId };
|
||||
await BossScriptModel.recordRoleIdWhenCheck(guildCode, roleId);
|
||||
await BossInstanceModel.recordRoleIdWhenCheck(guildCode, roleId);
|
||||
}
|
||||
if (bossScript.time < getTodayZeroPoint()) {
|
||||
if (bossInstance.startTime < getTodayZeroPoint()) {
|
||||
result.type = 1;
|
||||
return result;
|
||||
} else {
|
||||
@@ -36,7 +36,7 @@ export async function getBossScriptInfo(bossScript: BossScriptType, roleId:strin
|
||||
let isBattled = false;
|
||||
let myRank = {};
|
||||
|
||||
ranks.forEach(({roleId: battleRoleId, score, time}, index) => {
|
||||
let lastRanks = ranks.map(({roleId: battleRoleId, score, time}, index) => {
|
||||
if (roleId == battleRoleId) {
|
||||
myRank = {roleId, score, rankLv: index + 1};
|
||||
if (time >= getTodayZeroPoint())
|
||||
@@ -44,22 +44,22 @@ export async function getBossScriptInfo(bossScript: BossScriptType, roleId:strin
|
||||
}
|
||||
return {roleId, score, rankLv: index + 1};
|
||||
});
|
||||
return {warId, ranks, myRank, bossHp, type: 3, isBattled};
|
||||
return {warId, ranks: lastRanks, myRank, bossHp, type: 3, isBattled};
|
||||
}
|
||||
/**
|
||||
* 战斗结束返回
|
||||
* @param bossScript
|
||||
* @param bossInstance
|
||||
* @param roleId
|
||||
* @param battleNum
|
||||
*/
|
||||
export async function getBossScriptWhenEnd(bossScript: BossScriptType, roleId:string, battleNum:number) {
|
||||
let { warId, ranks, bossHp, winWarId, guildCode, recordRanks, winNum } = bossScript;
|
||||
export async function getBossInstanceWhenEnd(bossInstance: BossInstanceType, roleId:string, battleNum:number) {
|
||||
let { warId, ranks, bossHp, winWarId, guildCode, recordRanks, winNum } = bossInstance;
|
||||
let pushRanks;
|
||||
let result:any = {};
|
||||
if (battleNum == winNum) {
|
||||
pushRanks = deepCopy(recordRanks);
|
||||
result = {warId: winWarId, bossHp: 0, type: 3};
|
||||
await BossScriptModel.recordRoleIdWhenCheck(guildCode, roleId);
|
||||
await BossInstanceModel.recordRoleIdWhenCheck(guildCode, roleId);
|
||||
} else {
|
||||
pushRanks = deepCopy(ranks);
|
||||
result = {warId, bossHp, type: 3};
|
||||
@@ -94,10 +94,10 @@ export async function bossResult(code: string, serverId: string, dataName: strin
|
||||
let res:any = await lockData(serverId, dataName, code);//加锁
|
||||
if (!!res.err)
|
||||
return true;
|
||||
let {winSettled, ranks, num, warId, bossHp} = await BossScriptModel.findBossScript(code);//锁定关卡信息
|
||||
let {winSettled, ranks, num, warId, bossHp} = await BossInstanceModel.findBossInstance(code);//锁定关卡信息
|
||||
if (winSettled) {
|
||||
res.releaseCallback();//解锁
|
||||
await BossScriptModel.recordRoleIdWhenCheck(code, roleId);
|
||||
await BossInstanceModel.recordRoleIdWhenCheck(code, roleId);
|
||||
return true;
|
||||
}
|
||||
let index = findIndex(ranks, {roleId});
|
||||
@@ -110,7 +110,7 @@ export async function bossResult(code: string, serverId: string, dataName: strin
|
||||
res.releaseCallback();//解锁
|
||||
return true;
|
||||
}
|
||||
let { recordRanks } = await BossScriptModel.updateBossScript(code, { bossHp: 0, winSettled: true, ranks, recordRanks: ranks, winNum: num, winWarId: warId, roleIdRecords:[roleId]}, );
|
||||
let { recordRanks } = await BossInstanceModel.updateBossInstance(code, { bossHp: 0, winSettled: true, ranks, recordRanks: ranks, winNum: num, winWarId: warId, roleIdRecords:[roleId]}, );
|
||||
await pushBossHpMessage(code, serverId, 0, true);
|
||||
res.releaseCallback();//数据修改解锁
|
||||
recordRanks.sort(function(a, b) {
|
||||
@@ -137,7 +137,7 @@ export async function bossResult(code: string, serverId: string, dataName: strin
|
||||
return true;
|
||||
}
|
||||
|
||||
export async function addBossScript(code: string, serverId:string, roleId: string) {
|
||||
export async function addBossInstance(code: string, serverId:string, roleId: string) {
|
||||
let loginKey = 'login_roleId_' + roleId;
|
||||
let sid = await getRedis(loginKey);
|
||||
let key = 'serverId_' + serverId + 'guildCode_' + code;
|
||||
@@ -148,12 +148,13 @@ export async function addBossScript(code: string, serverId:string, roleId: strin
|
||||
export async function pushBossHpMessage(code: string, serverId:string, bossHp:number, isDelKey?: boolean ) {
|
||||
let key = 'serverId_' + serverId + 'guildCode_' + code;
|
||||
let members = await smembersAsync(key);
|
||||
members.forEach(member=>{
|
||||
let uids = members.map(member=>{
|
||||
let arr = member.split('|');
|
||||
let uid = arr[0];
|
||||
let sid = arr[1];
|
||||
pinus.app.channelService.pushMessageByUids('onBossHpUpdate', resResult(STATUS.SUCCESS, {bossHp}), [{uid, sid}]);
|
||||
return {uid, sid};
|
||||
});
|
||||
pinus.app.channelService.pushMessageByUids('onBossHpUpdate', resResult(STATUS.SUCCESS, {bossHp}), uids);
|
||||
if (isDelKey) {
|
||||
delAsync(key);
|
||||
}
|
||||
@@ -170,7 +171,7 @@ export async function checkMemberExists(code: string, serverId:string, roleId: s
|
||||
if(!battleRecord || battleRecord.status != 0 || roleId != battleRecord.roleId) {
|
||||
return false;
|
||||
}
|
||||
addBossScript(code, serverId, roleId);
|
||||
addBossInstance(code, serverId, roleId);
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
|
||||
@@ -3,6 +3,7 @@ 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');
|
||||
@@ -28,31 +29,64 @@ export async function recordUserGuild(roleId: string, code: string) {
|
||||
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 getGuildTrainInfo (guildTrains: Array<GuildTrainType>, roleId: string, trainCount:number, trainRewards: Array<number>) {
|
||||
guildTrains.forEach(({trainId, isComplete, trainScripts, ranks, reports})=>{
|
||||
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 = {};
|
||||
ranks.forEach(({roleId: rankRoleId, score}, index)=>{
|
||||
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 guildTrain = findWhere(guildTrains, { trainId: trainId - 1});
|
||||
if (!!guildTrain) {
|
||||
let lenNum = guildTrain.reports.length;
|
||||
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 = [...guildTrain.reports, ...reports]
|
||||
reports = [...lastGuildTrain.reports, ...reports];
|
||||
else {
|
||||
let trainReports = guildTrain.reports.splice(lenNum - GUILD_REPORT_NUM - 1, GUILD_REPORT_NUM);
|
||||
reports = [...trainReports, ...reports]
|
||||
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);
|
||||
}
|
||||
}
|
||||
return {trainId, isComplete, trainScripts, reports, myRank, ranks};
|
||||
guildTrain = {trainId, isComplete, trainScripts: resTrainScripts, trainBoxs: resTrainBoxs, reports, myRank, ranks: resRanks};
|
||||
});
|
||||
return { list: guildTrains, trainCount, trainRewards};
|
||||
return { guildTrain, trainCount, trainRewards};
|
||||
}
|
||||
|
||||
export async function lockTrain(code: string, trainId: number) {
|
||||
@@ -60,5 +94,8 @@ 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});
|
||||
return guildTrain;
|
||||
}
|
||||
@@ -49,7 +49,10 @@ export async function lockData(serverId: string, dataName: string, id: string )
|
||||
let key = 'serverId_'+serverId+'_'+dataName+'_'+id;
|
||||
let lockKey = 'locks:' + key;
|
||||
console.log(' lockKey = '+ lockKey);
|
||||
_redlockCache.redlock.lock(lockKey, _redlockCache.ttl).then(function(lock) {
|
||||
return await lock(lockKey);
|
||||
}
|
||||
function lock(lockKey: string) {
|
||||
return _redlockCache.redlock.lock(lockKey, _redlockCache.ttl).then(function(lock) {
|
||||
setLock(lockKey, lock);
|
||||
return {err: null, releaseCallback: releaseCallback.bind(null, lockKey)};
|
||||
}).catch(function(err) {
|
||||
@@ -57,7 +60,6 @@ export async function lockData(serverId: string, dataName: string, id: string )
|
||||
return { err };
|
||||
});
|
||||
}
|
||||
|
||||
export async function releaseCallback(lockKey: string) {
|
||||
releaseLock(lockKey);
|
||||
}
|
||||
Reference in New Issue
Block a user