feat(gvgBattle): 添加部分保存阵容、进出城池等接口的逻辑

This commit is contained in:
liangtongchuan
2023-02-05 22:53:45 +08:00
committed by luying
parent 3fc6b91a51
commit 596e6e18cf
8 changed files with 197 additions and 29 deletions

View File

@@ -1,13 +1,16 @@
import { GVGCityType } from './../../../db/GVGCity';
import { LeagueCityPoint, GVGTeamMem } from './../../../../../shared/domain/battleField/gvgBattle';
import { GVGRecModel } from '../../../db/GVGRec';
import { LeagueGood } from '../../../domain/gvgField/returnData';
import { GVGTeamModel } from '../../../db/GVGTeam';
import { GVGUserDataModel } from '../../../db/GVGUserData';
import { GVGCityModel } from '../../../db/GVGCity';
import { Application, BackendSession, ChannelService, HandlerService } from "pinus";
import { DEBUG_MAGIC_WORD, STATUS } from "../../../consts";
import { DEBUG_MAGIC_WORD, GVG_PERIOD, STATUS } from "../../../consts";
import { LineupHero } from "../../../domain/roleField/hero";
import { resResult, genCode } from "../../../pubUtils/util";
import { GVGConfigModel } from '../../../db/GVGConfig';
import { GVGLeagueModel } from '../../../db/GVGLeague';
import { checkGVGPeriod, getGVGPeriodData } from '../../../services/gvg/gvgService';
export default function (app: Application) {
new HandlerService(app, {});
@@ -16,6 +19,12 @@ export default function (app: Application) {
export class GVGBattleHandler {
channelService: ChannelService;
// 积分点占领情况cityId -> LeagueCode -> LeagueCityPoint
private pointOccupy: Map<number, Map<string, LeagueCityPoint>> = new Map();
// 城池队伍状态cityId -> areaId -> GVGTeamMem
private cityTeamStatus: Map<number, Map<number, GVGTeamMem>> = new Map();
constructor(private app: Application) {
this.channelService = app.get('channelService');
}
@@ -27,41 +36,116 @@ export class GVGBattleHandler {
// spine: 形象
// lineup: 阵容
async saveTeam(msg: { index: number, head: number, frame: number, spine: number, lineup: [ LineupHero ] }, session: BackendSession) {
if(checkGVGPeriod(GVG_PERIOD.BATTLE)) return resResult(STATUS.GVG_NOT_BATTLE_PERIOD);
const roleId = session.get('roleId');
const guildCode = session.get('guildCode');
let myLeague = await GVGLeagueModel.findLeagueByGuild(guildCode);
if(!myLeague) return resResult(STATUS.GVG_LEAGUE_NOT_EXIST);
const { index, head, frame, spine, lineup } = msg;
return resResult(STATUS.SUCCESS, { teams: [ 'teamCode' ] });
const team = await GVGTeamModel.saveTeam(roleId, myLeague.leagueCode, index, head, frame, spine, lineup);
if (!team) {
return resResult(STATUS.GVG_SAVE_TEAM_FAILED);
}
return resResult(STATUS.SUCCESS, { teams: [ team.teamCode ] });
}
// 获取城池信息
async getCity(msg: { cityId: number }, session: BackendSession) {
const { cityId } = msg;
if(checkGVGPeriod(GVG_PERIOD.BATTLE)) return resResult(STATUS.GVG_NOT_BATTLE_PERIOD);
const city = await GVGCityModel.findOne({ cityId }).lean();
const { cityId } = msg;
let { configId } = getGVGPeriodData();
const city = await GVGCityModel.findOne({ configId, cityId }).lean();
return resResult(STATUS.SUCCESS, { city });
}
// 进入城池
async enterCity(msg: { cityId: number }, session: BackendSession) {
if(checkGVGPeriod(GVG_PERIOD.BATTLE)) return resResult(STATUS.GVG_NOT_BATTLE_PERIOD);
const roleId = session.get('roleId')
const guildCode = session.get('guildCode')
const { cityId } = msg;
let { configId } = getGVGPeriodData();
let city = await GVGCityModel.findOne({ cityId }).lean();
let city: GVGCityType = await GVGCityModel.findOne({ cityId, configId }).lean();
if (!city) {
return resResult(STATUS.GVG_CITY_NOT_FOUND);
}
const { teamCnt, userCnt } = city;
// 检测是否已经在城池中,如果在城池中,直接返回城池信息
let myLeague = await GVGLeagueModel.findLeagueByGuild(guildCode);
if(!myLeague) return resResult(STATUS.GVG_LEAGUE_NOT_EXIST);
let gvgUserData = await GVGUserDataModel.findByRole(configId, myLeague.leagueCode, roleId);
if (!gvgUserData) {
return resResult(STATUS.GVG_USER_NOT_FOUND);
}
if (gvgUserData.cityId === cityId) {
return resResult(STATUS.SUCCESS, { city });
}
// 不在城池则检测是否满员
const { userCnt } = city;
// 检测是否满员
if (userCnt >= 200) {
return resResult(STATUS.GVG_BATTLE_CITY_FULL);
}
// ! 更新城池人数和队伍数,更新用户城池信息
// 检测玩家是否已经在其他城池中,由 checkMyTeam 接口检测
// if (gvgUserData.cityId) {
// }
const roleTeamCnt = await GVGTeamModel.getTeamCnt(roleId);
city = await GVGCityModel.updateCityUser(configId, cityId, 1, roleTeamCnt);
gvgUserData = await GVGUserDataModel.changeCity(configId, myLeague.leagueCode, roleId, cityId);
// ! 队伍默认进入的据点暂时设为 0更新内存队伍信息
const res = await GVGTeamModel.resetTeamsLoc(roleId, cityId, 0);
if (!res) {
return resResult(STATUS.GVG_RESET_TEAM_LOC_FAILED);
}
return resResult(STATUS.SUCCESS, { city });
}
// 离开城池
async leaveCity(msg: { cityId: number }, session: BackendSession) {
if(checkGVGPeriod(GVG_PERIOD.BATTLE)) return resResult(STATUS.GVG_NOT_BATTLE_PERIOD);
const roleId = session.get('roleId')
const guildCode = session.get('guildCode')
const { cityId } = msg;
let { configId } = getGVGPeriodData();
// 检测是否已经在城池中
let myLeague = await GVGLeagueModel.findLeagueByGuild(guildCode);
if(!myLeague) return resResult(STATUS.GVG_LEAGUE_NOT_EXIST);
let gvgUserData = await GVGUserDataModel.findByRole(configId, myLeague.leagueCode, roleId);
if (!gvgUserData) {
return resResult(STATUS.GVG_USER_NOT_FOUND);
}
if (gvgUserData.cityId != cityId) {
return resResult(STATUS.GVG_USER_NOT_IN_CITY);
}
const roleTeamCnt = await GVGTeamModel.getTeamCnt(roleId);
const city = await GVGCityModel.updateCityUser(configId, cityId, -1, -roleTeamCnt);
if (!city) {
return resResult(STATUS.GVG_CITY_NOT_FOUND);
}
// 更新玩家城池和队伍城池
gvgUserData = await GVGUserDataModel.changeCity(configId, myLeague.leagueCode, roleId, 0);
const res = await GVGTeamModel.resetTeamsLoc(roleId, 0, 0);
if (!res) {
return resResult(STATUS.GVG_RESET_TEAM_LOC_FAILED);
}
// ! 还需处理内存数据
return resResult(STATUS.SUCCESS);
}
@@ -192,20 +276,15 @@ export class GVGBattleHandler {
if (magicWord !== DEBUG_MAGIC_WORD) {
return resResult(STATUS.INTERNAL_ERR);
}
let { configId } = getGVGPeriodData();
// 检查城市是否存在
let city = await GVGCityModel.getCityByCityId(cityId);
let city = await GVGCityModel.getCityByCityId(configId, cityId);
if (city) {
return resResult(STATUS.SUCCESS, { city });
}
// 创建城市
const config = await GVGConfigModel.findConfig();
if (!config || !config.configId) {
return resResult(STATUS.INTERNAL_ERR);
}
const { configId } = config;
city = await GVGCityModel.createCity(cityId, configId);
city = await GVGCityModel.createCity(configId, cityId);
return resResult(STATUS.SUCCESS, { city });
}
}