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

@@ -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;
}
}
}
// 排行榜发放奖励
}