演武场和练兵场
This commit is contained in:
118
game-server/app/servers/battle/handler/guildBossHandler.ts
Normal file
118
game-server/app/servers/battle/handler/guildBossHandler.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Application, BackendSession, pinus } from 'pinus';
|
||||
import { resResult, genCode } from '../../../pubUtils/util';
|
||||
import { STATUS } from '../../../consts';
|
||||
import { BossScriptModel } from '../../../db/BossScript';
|
||||
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';
|
||||
export default function (app: Application) {
|
||||
return new GuildHandler(app);
|
||||
}
|
||||
|
||||
export class GuildHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
// 获得boss关卡
|
||||
async getBossScript(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
//TODO校验
|
||||
const roleId = session.get('roleId');
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!bossScript) {
|
||||
return resResult(STATUS.SUCCESS, {type: 1});//1:等待团长开启,2:今日已开启,且boss通关,3:开启中
|
||||
}
|
||||
let result = await getBossScriptInfo(bossScript, roleId);
|
||||
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
//开启演武场
|
||||
async openBossScript(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
//TODO检查权限
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!!bossScript && ( bossScript.bossHp > 0 || bossScript.time >= 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};
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async bossScriptStart(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
const roleName = session.get('roleName');
|
||||
//TODO校验
|
||||
let bossScript = await BossScriptModel.findBossScript(code);
|
||||
if (!bossScript)
|
||||
return resResult(STATUS.GUILD_SCRIPT_NOT_OPENED);
|
||||
if (bossScript.bossHp <= 0)
|
||||
return resResult(STATUS.GUILD_SCRIPT_IS_COMPLETE);
|
||||
let myRank = findWhere(bossScript.ranks, {roleId});
|
||||
if (!!myRank && myRank.time > getTodayZeroPoint())
|
||||
return resResult(STATUS.GUILD_SCRIPT_IS_BATTLED);
|
||||
let { warId, ranks } = bossScript;
|
||||
const battleCode = genCode(8); // 关卡唯一值
|
||||
//TODO查看地图字典
|
||||
let warInfo;
|
||||
await BattleRecordModel.updateBattleRecordByCode(battleCode, {
|
||||
$set: {
|
||||
roleId, roleName, battleId: warId,
|
||||
status: 0,
|
||||
warName: warInfo.gk_name,
|
||||
warType: warInfo.warType,
|
||||
record: { heroes:[],recordNum: bossScript.num, bossHp: bossScript.bossHp},
|
||||
}
|
||||
}, true);
|
||||
const serverId = session.get('serverId');
|
||||
await addBossScript(code, serverId, roleId);
|
||||
if (!findWhere(ranks, {roleId})) {
|
||||
await BossScriptModel.pushRanks(code, {roleId, score: 0, time:nowSeconds()});
|
||||
}
|
||||
return resResult(STATUS.SUCCESS, { battleCode });
|
||||
}
|
||||
|
||||
async action (msg: { code: string, damage: number, battleCode: string }, session: BackendSession ) {
|
||||
const { code, battleCode, damage } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
const serverId = session.get('serverId');
|
||||
let flag = await checkMemberExists(code, serverId, roleId, battleCode);
|
||||
if (!flag) {
|
||||
return;
|
||||
}
|
||||
//记录伤害
|
||||
let bossScript = await BossScriptModel.updateBossHp(code, damage, roleId);
|
||||
if (!bossScript) {//进入结算
|
||||
let flag = await bossResult(code, serverId, GUILD_DATA_NAME.BOSS_SCRIPT, roleId, damage);
|
||||
if (!flag) {
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
}
|
||||
} else {
|
||||
pushBossHpMessage(code, serverId, bossScript.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;
|
||||
const roleId = session.get('roleId');
|
||||
const battleRecord = await BattleRecordModel.getBattleRecordByCode(battleCode, true);
|
||||
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);
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
}
|
||||
164
game-server/app/servers/battle/handler/guildTrainHandler.ts
Normal file
164
game-server/app/servers/battle/handler/guildTrainHandler.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
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 } 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';
|
||||
export default function (app: Application) {
|
||||
return new GuildTrainHandler(app);
|
||||
}
|
||||
|
||||
export class GuildTrainHandler {
|
||||
constructor(private app: Application) {
|
||||
|
||||
}
|
||||
|
||||
async getTrainScript(msg: { code: string }, session: BackendSession) {
|
||||
const { code } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
let guildTrains = await GuildTrainModel.findGuildTrain(code);
|
||||
let userGuild = await getUserGuild(roleId, code);
|
||||
if (!userGuild)
|
||||
return resResult(STATUS.WRONG_PARMS);
|
||||
if (!guildTrains)
|
||||
guildTrains = [];
|
||||
let { trainCount, trainRewards } = userGuild;
|
||||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards);
|
||||
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);
|
||||
if (!guildTrain)
|
||||
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 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;
|
||||
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 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})=>{
|
||||
if (hid != otherHid && progress == 1000) {
|
||||
isComplete = false;
|
||||
}
|
||||
});
|
||||
if (isComplete) {//解锁下一关
|
||||
needLockNext = true;
|
||||
}
|
||||
}
|
||||
let progress = 1000;
|
||||
GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
if (needLockNext) {
|
||||
|
||||
}
|
||||
res.releaseCallback();//解锁
|
||||
} else {
|
||||
let progress = trainScript.progress + addScore;
|
||||
GuildTrainModel.updateGuildTrainProgress(code, trainId, hid, progress, ranks, reports, isComplete);
|
||||
res.releaseCallback();//解锁
|
||||
}
|
||||
} else {
|
||||
reports.push(report);
|
||||
GuildTrainModel.updateGuildTrain(code, trainId, {reports, ranks});
|
||||
res.releaseCallback();//解锁
|
||||
}
|
||||
let guildTrains = await GuildTrainModel.findGuildTrain(code);
|
||||
if (!guildTrains)
|
||||
guildTrains = [];
|
||||
let { trainCount, trainRewards } = userGuild;
|
||||
let result = getGuildTrainInfo(guildTrains, roleId, trainCount, trainRewards);
|
||||
return resResult(STATUS.SUCCESS, result);
|
||||
}
|
||||
|
||||
async getTrainScriptBox(msg: { code: string }, session: BackendSession) {
|
||||
|
||||
}
|
||||
|
||||
async getTrainLvUpRewards(msg: { trainId: number }, session: BackendSession) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user