185 lines
8.6 KiB
TypeScript
185 lines
8.6 KiB
TypeScript
import { pinus } from "pinus";
|
||
import { uniq } from "underscore";
|
||
import { ACTIVITY_TYPE, GVG_SERVER_TYPE, REDIS_KEY, SERVER_GROUP_FUN_TYPE } from "../consts";
|
||
import { ServerGroupModel } from "../db/ServerGroup";
|
||
import { ServerlistModel } from "../db/Serverlist";
|
||
import { nowSeconds } from "../pubUtils/timeUtil";
|
||
import { getServerTypeByTime } from "./gvg/gvgService";
|
||
import { getAllServerCreateTime, redisClient } from "./redisService";
|
||
import moment = require("moment");
|
||
import { RegionModel, RegionType } from "../db/Region";
|
||
import { errlogger } from "../util/logger";
|
||
import { CreateServerParam } from "../domain/backEndField/params";
|
||
import { sendOpenServerMail } from "./gmService";
|
||
import { ActivityGroupModel } from "../db/ActivityGroup";
|
||
import { ActivityModel } from "../db/Activity";
|
||
|
||
export async function setServerGroup() {
|
||
const servers = await ServerlistModel.findByEnv(pinus.app.get('env'));
|
||
let now = nowSeconds();
|
||
const gvgServerGroup = await ServerGroupModel.findByTime(now, SERVER_GROUP_FUN_TYPE.GVG);
|
||
const pvpServerGroup = await ServerGroupModel.findByTime(now, SERVER_GROUP_FUN_TYPE.PVP);
|
||
// const arenaServerGroup = await ServerGroupModel.findByTime(now, SERVER_GROUP_FUN_TYPE.ARENA);
|
||
pinus.app.set('gvgServerGroup', servers.map(obj => {
|
||
let server = gvgServerGroup.find(cur => cur.serverId == obj.id);
|
||
return [obj.id, server? server.groupId: obj.groupId]
|
||
}));
|
||
pinus.app.set('pvpServerGroup', servers.map(obj => {
|
||
let server = pvpServerGroup.find(cur => cur.serverId == obj.id);
|
||
return [obj.id, server? server.groupId: obj.groupId]
|
||
}));
|
||
// pinus.app.set('arenaServerGroup', servers.map(obj => {
|
||
// let server = arenaServerGroup.find(cur => cur.serverId == obj.id);
|
||
// return [obj.id, server? server.groupId: obj.groupId]
|
||
// }));
|
||
|
||
}
|
||
|
||
export async function getAllGroupOfServer(serverId: number) {
|
||
let gvgGroupId = await getGVGGroupIdOfServer(serverId);
|
||
let pvpGroupId = await getPVPGroupIdOfServer(serverId);
|
||
return uniq([gvgGroupId, pvpGroupId]);
|
||
}
|
||
|
||
// GVG相关:查询本小区所在战区
|
||
export async function getGVGGroupIdOfServer(serverId: number) {
|
||
return await getGroupIdOfServer(serverId, SERVER_GROUP_FUN_TYPE.GVG);
|
||
}
|
||
|
||
// PVP相关:查询本小区所在战区
|
||
export async function getPVPGroupIdOfServer(serverId: number) {
|
||
return await getGroupIdOfServer(serverId, SERVER_GROUP_FUN_TYPE.PVP);
|
||
}
|
||
|
||
// 查询本小区所在的战区
|
||
export async function getGroupIdOfServer(serverId: number, fun: SERVER_GROUP_FUN_TYPE) {
|
||
let arr: number[][] = pinus.app.get(getPinusKeyOfFun(fun))||[];
|
||
let obj = arr.find(cur => cur[0] == serverId)||[];
|
||
return obj[1]||0;
|
||
}
|
||
|
||
// 查询当前和本小区同一个战区的其他小区
|
||
export async function getGVGServersOfSameGroup(type: GVG_SERVER_TYPE, id: number) {
|
||
if(type == GVG_SERVER_TYPE.SINGLE) return [id];
|
||
|
||
let groupId = await getGroupIdOfServer(id, SERVER_GROUP_FUN_TYPE.GVG);
|
||
return await getServersByGroupId(groupId, SERVER_GROUP_FUN_TYPE.GVG);
|
||
}
|
||
|
||
export async function getPVPServersOfSameGroup(serverId: number) {
|
||
let groupId = await getGroupIdOfServer(serverId, SERVER_GROUP_FUN_TYPE.PVP);
|
||
return await getServersByGroupId(groupId, SERVER_GROUP_FUN_TYPE.PVP);
|
||
}
|
||
|
||
export async function getGVGServersByGroupId(groupId: number) {
|
||
return getServersByGroupId(groupId, SERVER_GROUP_FUN_TYPE.GVG);
|
||
}
|
||
|
||
export async function getPvpServersByGroupId(groupId: number) {
|
||
return getServersByGroupId(groupId, SERVER_GROUP_FUN_TYPE.PVP);
|
||
}
|
||
|
||
export async function getServersByGroupId(groupId: number, fun: SERVER_GROUP_FUN_TYPE) {
|
||
let arr: number[][] = pinus.app.get(getPinusKeyOfFun(fun))||[];
|
||
let serverCreateTimes = await getAllServerCreateTime();
|
||
|
||
return arr.filter(obj => {
|
||
if(fun == SERVER_GROUP_FUN_TYPE.GVG) {
|
||
let openTime = parseInt(serverCreateTimes[obj[0]]);
|
||
return obj[1] == groupId && getServerTypeByTime(openTime) == GVG_SERVER_TYPE.MULTI;
|
||
} else {
|
||
return obj[1] == groupId
|
||
}
|
||
}).map(obj => obj[0]);
|
||
}
|
||
|
||
function getPinusKeyOfFun(fun: SERVER_GROUP_FUN_TYPE) {
|
||
switch(fun) {
|
||
case SERVER_GROUP_FUN_TYPE.GVG: return 'gvgServerGroup';
|
||
case SERVER_GROUP_FUN_TYPE.PVP: return 'pvpServerGroup';
|
||
case SERVER_GROUP_FUN_TYPE.ARENA: return 'arenaServerGroup';
|
||
default: return '';
|
||
}
|
||
}
|
||
|
||
export async function getPVPServerGroup() {
|
||
let now = nowSeconds();
|
||
const servers = await ServerlistModel.findByEnv(pinus.app.get('env'));
|
||
const pvpServerGroup = await ServerGroupModel.findByTime(now, SERVER_GROUP_FUN_TYPE.PVP);
|
||
let map = new Map<number, number[]>();
|
||
for(let server of servers) {
|
||
let serverGroup = pvpServerGroup.find(cur => cur.serverId == server.id);
|
||
let groupId = serverGroup? serverGroup.groupId: server.groupId;
|
||
if(!map.has(groupId)) map.set(groupId, []);
|
||
map.get(groupId).push(server.id);
|
||
}
|
||
return map
|
||
}
|
||
|
||
export async function autoCreateServerSchedule() {
|
||
console.log('******* autoCreateServerSchedule *******')
|
||
let region = await RegionModel.findRegionByEnv(pinus.app.get('env'));
|
||
if(!region) {
|
||
return errlogger.error('create new server region not found');
|
||
}
|
||
let time = moment().format('HH:mm');
|
||
let latestServerOpenTime = region.latestServerOpenTime||0;
|
||
if(region.stategy && region.stategy.type == 1 && region.stategy.timers.indexOf(time) != -1 && latestServerOpenTime < nowSeconds()) {
|
||
let latestServer = await ServerlistModel.findByServerId(region.latestServerUniqId);
|
||
if(!latestServer || latestServer.playerCnt >= region.stategy.maxPlayerCnt) {
|
||
let params = new CreateServerParam();
|
||
params.setByRegionStategy(region, nowSeconds())
|
||
await createNewServer(region, (latestServer?.serverId||0) + 1, params);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
export async function autoCreateServerWhenRoleInit(serverId: number) {
|
||
console.log('******* autoCreateServerWhenRoleInit *******')
|
||
let server = await ServerlistModel.incRoleCnt(serverId);
|
||
if(!server) {
|
||
return errlogger.error('server not found');
|
||
}
|
||
let region = await RegionModel.findRegionByEnv(pinus.app.get('env'));
|
||
if(!region) {
|
||
return errlogger.error('create new server region not found');
|
||
}
|
||
|
||
let latestServerOpenTime = region.latestServerOpenTime||0;
|
||
if(region.stategy && region.stategy.isOpen && region.stategy.type == 2 && region.latestServerUniqId == server.id && server.playerCnt >= region.stategy.maxPlayerCnt && latestServerOpenTime <= nowSeconds()) {
|
||
let params = new CreateServerParam();
|
||
params.setByRegionStategy(region, nowSeconds())
|
||
await createNewServer(region, server.serverId + 1, params);
|
||
}
|
||
}
|
||
|
||
export async function createNewServer(region: RegionType ,serverId: number, params: CreateServerParam, uid?: number) {
|
||
console.log('******* createNewServer *******')
|
||
let newServer = await ServerlistModel.newServer(params, region, serverId, uid);
|
||
if(!newServer) return;
|
||
if(params.openMail) await sendOpenServerMail('openMail', params.openMail, newServer, uid);
|
||
// if(params.circleMail) await sendOpenServerMail('circleMail', params.circleMail, newServer, uid);
|
||
if(params.activityGroupId && params.activityGroupId.length > 0) {
|
||
await ActivityGroupModel.addServerToGroupData(newServer.id, params.activityGroupId);
|
||
let activityGroups = await ActivityGroupModel.findByServerId(newServer.id);
|
||
await pinus.app.rpc.activity.activityRemote.addServerToGroup.broadcast(params.activityGroupId, [newServer.id]);
|
||
|
||
let aids = activityGroups.reduce((pre, cur) => [...pre, ...cur.activities], []);
|
||
let activities = await ActivityModel.findActivityByIds(aids);
|
||
let timeLimitRanks = activities.filter(cur => cur.type == ACTIVITY_TYPE.TIME_LIMIT_RANK);
|
||
if(timeLimitRanks.length > 0) {
|
||
pinus.app.rpc.systimer.systimerRemote.updateTimeLimitRank.broadcast(timeLimitRanks);
|
||
}
|
||
}
|
||
await RegionModel.newServer(region.id, newServer);
|
||
await redisClient().hsetAsync(REDIS_KEY.SERVER, `${newServer.id}`, `${newServer.name}`);
|
||
await redisClient().hsetAsync(REDIS_KEY.SERVER_OPEN_TIME, `${newServer.id}`, `${newServer.openTime}`);
|
||
await pinus.app.rpc.guild.guildRemote.setServerGroup.broadcast();
|
||
await pinus.app.rpc.chat.chatRemote.setServerGroup.broadcast();
|
||
await pinus.app.rpc.battle.battleRemote.setServerGroup.broadcast();
|
||
await pinus.app.rpc.connector.connectorRemote.setServerGroup.broadcast();
|
||
await pinus.app.rpc.gm.gmRemote.setServerGroup.broadcast();
|
||
await pinus.app.rpc.systimer.systimerRemote.setServerGroup.broadcast();
|
||
|
||
} |