feat(gvg): 定时器

This commit is contained in:
luying
2023-02-16 16:54:53 +08:00
parent 7543baa8cb
commit 605ee869a9
12 changed files with 210 additions and 56 deletions

View File

@@ -15,7 +15,7 @@ class GVGBattleData {
public serverType: number; // 单服还是跨服
private teams: Map<string, GVGTeamMem> = new Map(); // 队伍, teamCode => team
private rolePoints: Map<string, Set<number>> = new Map(); // roleId => pointId[],用于更新玩家的积分
private rolePoints: Map<string, Map<number, string>> = new Map(); // roleId => pointId[],用于更新玩家的积分
private roleToTeam: Map<string, string[]> = new Map(); // roleId => teamCode
private areaToTeams: Map<number, Set<string>> = new Map(); // areaId => teamCode set用于定时下发地图玩家数据
@@ -63,9 +63,9 @@ class GVGBattleData {
this.teams.set(team.teamCode, new GVGTeamMem(team))
}
this.teams.get(team.teamCode).setCity(team.cityId, team.areaId, team.pointId);
if(team.pointId > 0) {
if(!this.rolePoints.has(team.roleId)) this.rolePoints.set(team.roleId, new Set());
this.rolePoints.get(team.roleId).add(team.pointId);
if(team.pointId > 0 && !team.isRobot) {
if(!this.rolePoints.has(team.roleId)) this.rolePoints.set(team.roleId, new Map());
this.rolePoints.get(team.roleId).set(team.pointId, team.teamCode);
}
let teamCodesOfRole = this.roleToTeam.get(team.roleId)||[];
if(teamCodesOfRole.indexOf(team.teamCode) == -1) teamCodesOfRole.push(team.teamCode);
@@ -87,14 +87,14 @@ class GVGBattleData {
}
}
private setRolePoints(roleId: string, fromPointId: number, pointId: number) {
private setRolePoints(roleId: string, fromPointId: number, pointId: number, teamCode: string) {
if(fromPointId > 0 && pointId == 0) { // 被打得撤离积分点
if(this.rolePoints.has(roleId) && this.rolePoints.get(roleId).has(pointId)) {
this.rolePoints.get(roleId).delete(pointId);
}
} else if (fromPointId == 0 && pointId > 0) {
if(!this.rolePoints.has(roleId)) this.rolePoints.set(roleId, new Set());
this.rolePoints.get(roleId).add(pointId);
if(!this.rolePoints.has(roleId)) this.rolePoints.set(roleId, new Map());
this.rolePoints.get(roleId).set(pointId, teamCode);
}
}
@@ -117,7 +117,7 @@ class GVGBattleData {
let teamMem = this.teams.get(teamCode);
if(!teamMem) return;
let { pointId: fromPointId } = teamMem;
this.setRolePoints(roleId, fromPointId, pointId);
this.setRolePoints(roleId, fromPointId, pointId, teamCode);
teamMem.pointId = pointId;
}
@@ -130,7 +130,7 @@ class GVGBattleData {
}
let { areaId: fromAreaId, pointId: fromPointId } = teamMem;
teamMem.updateTeam(team);
this.setRolePoints(team.roleId, fromPointId, team.pointId);
this.setRolePoints(team.roleId, fromPointId, team.pointId, team.teamCode);
this.setAreaMap(team.teamCode, fromAreaId, team.areaId);
}
}
@@ -140,6 +140,18 @@ class GVGBattleData {
let teamCodes = this.roleToTeam.get(GVG_CATAPULT)||[];
return teamCodes.map(teamCode => this.teams.get(teamCode));
}
// 积分点情况
public findSettledPoint() {
let teams: GVGTeamMem[] = [];
for(let [_roleId, pointIds] of this.rolePoints) {
for(let [pointId, teamCode] of pointIds) {
let team = this.teams.get(teamCode);
if(pointId > 0 && team) teams.push(team);
}
}
return teams;
}
}
export function getGVGBattleData(groupId: number, serverType: number) {
@@ -156,6 +168,7 @@ export function getGVGBattleMap() {
export async function initTeamToMem() {
let sid = pinus.app.getServerId();
console.log('##### initTeamToMem', sid)
let servers = pinus.app.getServersByType('guild');
let { configId, period } = getGVGPeriodData();
// if(period != GVG_PERIOD.BATTLE) return;

View File

@@ -3,7 +3,7 @@ import { GVGLeagueType } from "../../db/GVGLeague";
import { GVGTeamModel, GVGTeamType, GVGTeamUpdate } from "../../db/GVGTeam";
import { GVGCityModel, GVGCityType } from "../../db/GVGCity";
import { gameData } from "../../pubUtils/data";
import { GVG_AREA_TYPE, GVG_TECH_TYPE, STATUS } from "../../consts";
import { GVG_AREA_TYPE, GVG_TECH_TYPE, REDIS_KEY, STATUS } from "../../consts";
import { nowSeconds } from "../../pubUtils/timeUtil";
import { DicGVGAreaPoint } from "../../pubUtils/dictionary/DicGVGAreaPoint";
import { getGVGBattleData, getGVGBattleMap } from "./gvgBattleMemory";
@@ -15,6 +15,9 @@ import { getGVGConfig } from "./gvgService";
import { GVGLeaguePrepareModel } from "../../db/GVGLeaguePrepare";
import { pinus } from "pinus";
import { dispatch } from "../../pubUtils/dispatcher";
import { Rank } from "../rankService";
import { LeagueRankInfo } from "../../domain/rank";
import { findKeys } from "../redisService";
/**
@@ -79,19 +82,19 @@ export function checkMoveStatus(team: GVGTeamType, cityId: number, areaId: numbe
return STATUS.SUCCESS;
}
export async function initRobots(confidId: number, groupId: number, serverType: number, cityId: number) {
export async function initRobots(configId: number, groupId: number, serverType: number, cityId: number) {
let robotTeams = await GVGTeamModel.findRobotTeams(groupId, serverType, cityId);
let updateDicPoints: DicGVGAreaPoint[] = [];
let { areaIds = []} = gameData.gvgCity.get(cityId);
for(let [_, point] of gameData.gvgAreaPoint) {
if(areaIds.indexOf(point.areaId) == -1) continue;
let robotTeam = robotTeams.find(team => team.pointId == point.pointId);
if(!robotTeam || (!robotTeam.isBroken && robotTeam.configId != confidId) ) {
if(!robotTeam || (!robotTeam.isBroken && robotTeam.configId != configId) ) {
updateDicPoints.push(point);
}
}
if(updateDicPoints.length > 0) {
robotTeams = await GVGTeamModel.initRobots(confidId, groupId, serverType, cityId, updateDicPoints);
robotTeams = await GVGTeamModel.initRobots(configId, groupId, serverType, cityId, updateDicPoints);
// 存入内存
let teamObj = getGVGBattleData(groupId, serverType);
teamObj.enterCity(...robotTeams);
@@ -169,6 +172,7 @@ export function checkGVGBattleStart(roleId: string, attackTeam: GVGTeamType, def
return STATUS.SUCCESS;
}
// —————————— 定时器相关 —————————— //
// gvg激战期开始定时器
export async function gvgBattleStart() {
let servers = pinus.app.getServersByType('guild');
@@ -209,7 +213,7 @@ export async function initCatapult(cityId: number, groupId: number, serverType:
// 投石车投伤害
export async function catapultHurt() {
for(let [_, teamObj] of getGVGBattleMap()) {
for(let [_key, teamObj] of getGVGBattleMap()) {
let teams = teamObj.findCatapult();
for(let catapult of teams) {
if(catapult.isBroken) continue;
@@ -221,4 +225,79 @@ export async function catapultHurt() {
teamObj.battleEnd(teams);
}
}
}
// 排行榜积分更新
export async function addBattleRankScore(gvgTeam: GVGTeamType, incScore: number) {
let { configId, groupId, serverType, cityId, roleId, leagueCode, isRobot } = gvgTeam;
if(isRobot) return;
let r1 = new Rank(REDIS_KEY.GVG_BATTLE_RANK, { configId, groupId, serverType, cityId });
r1.setRankWithRoleInfo(roleId, incScore, Date.now(), null, true);
let r2 = new Rank(REDIS_KEY.GVG_BATTLE_LEAGUE_RANK, { configId, groupId, serverType, cityId });
r2.setRankWithLeagueInfo(leagueCode, incScore, Date.now(), null, true);
}
// 获取排行榜
export async function getBattleRanks(configId: number, groupId: number, serverType: number, cityId: number, myLeague: GVGLeagueType) {
let r = new Rank(REDIS_KEY.GVG_BATTLE_LEAGUE_RANK, { configId, groupId, serverType, cityId });
r.setGenerFieldsFun((obj => {
if(obj instanceof LeagueRankInfo) {
return { rank: obj.rank, leagueCode: obj.code, leagueName: obj.name, score: obj.num }
}
return obj
}));
let { ranks, myRank } = await r.getRankListWithMyRank({ leagueCode: myLeague.leagueCode });
if (!myRank) {
myRank = await r.generMyRankWithLeague(myLeague.leagueCode, 0, 0, myLeague);
}
return { ranks, myRank }
}
// 每5秒一次结算
export async function gvgBattleSeconds() {
// 每5秒给据点上的人加积分
for(let [_key, teamObj] of getGVGBattleMap()) {
let teams = teamObj.findSettledPoint();
for(let teamMem of teams) {
if(teamMem.isBroken || teamMem.durability <= 0) continue;
let addScore = gameData.gvgAreaPoint.get(teamMem.pointId)?.score||0;
let team = await GVGTeamModel.addScore(teamMem.teamCode, addScore);
await addBattleRankScore(team, addScore);
}
}
// 向下推送区域数据
}
export async function gvgBattleEnd() {
let { configId } = getGVGConfig();
let guardLeagueCnt = new Map<string, number>();
// 城池占领情况
let keys = await findKeys(`${REDIS_KEY.GVG_BATTLE_LEAGUE_RANK}:${configId}:`);
let rankKeys = keys.map(key => {
let [,, _groupId, _serverType, _cityId] = key.split(':');
return { groupId: parseInt(_groupId), serverType: parseInt(_serverType), cityId: parseInt(_cityId) };
}).sort((a, b) => b.cityId - a.cityId);
for(let { groupId, serverType, cityId } of rankKeys) {
let r = new Rank(REDIS_KEY.GVG_BATTLE_LEAGUE_RANK, { configId, groupId, serverType, cityId });
let ranks = await r.getRankByRange();
for(let obj of ranks) { // 排名最高
let rank = <LeagueRankInfo>obj;
let cnt = guardLeagueCnt.get(rank.code)||0;
if(cnt < GVG.GVG_CITY_OCCUPIED_NUMBER) {
await GVGCityModel.guardCity(configId, groupId, serverType, cityId, rank);
guardLeagueCnt.set(rank.code, cnt + 1);
break;
}
}
}
// 排行榜发放奖励
}