Files
ZYZ/game-server/app/services/serverService.ts
2023-03-18 18:05:09 +08:00

103 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { pinus } from "pinus";
import { uniq } from "underscore";
import { GVG_SERVER_TYPE, 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 } from "./redisService";
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 => {
let openTime = parseInt(serverCreateTimes[obj[0]]);
return obj[1] == groupId && getServerTypeByTime(openTime) == GVG_SERVER_TYPE.MULTI;
}).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 pvpServerGroup = await ServerGroupModel.findByTime(now, SERVER_GROUP_FUN_TYPE.PVP);
let map = new Map<number, number[]>();
for(let { serverId, groupId } of pvpServerGroup) {
if(!map.has(groupId)) map.set(groupId, []);
map.get(groupId).push(serverId);
}
return map
}