后台:添加在线人数

This commit is contained in:
luying
2022-05-26 15:32:39 +08:00
parent 70617c3871
commit 1024f4d34d
3 changed files with 34 additions and 1 deletions
+27 -1
View File
@@ -1,5 +1,5 @@
import { Service } from 'egg';
import { STATUS } from '@consts';
import { REDIS_KEY, STATUS } from '@consts';
import { ServerlistModel } from '@db/Serverlist';
import { gameData } from '@pubUtils/data';
import { DicHero } from '@pubUtils/dictionary/DicHero';
@@ -19,6 +19,7 @@ import { DicServerName } from '@pubUtils/dictionary/DicServerName';
import { CreateRegionParam } from '@domain/backEndField/params';
import { UserOrderModel } from '@db/UserOrder';
import { SurveyModel } from '@db/Survery';
import { RedisClient } from 'redis';
/**
* Test Service
@@ -60,6 +61,31 @@ export default class Game extends Service {
});
}
public async getOnlineUsersByServer() {
const { ctx } = this;
let redisClient: RedisClient = ctx.app.context.redisClient;
let allRoles = await redisClient.hgetallAsync(REDIS_KEY.ONLINE_USERS);
let result: { serverId: number, count: number }[] = [];
for(let roleId in allRoles) {
try{
let param = allRoles[roleId].split('|');
let [,,,, _serverId] = param;
let serverId = parseInt(_serverId);
let index = result.findIndex(cur => cur.serverId == serverId);
if(index == -1) {
result.push({ serverId, count: 1 });
} else {
result[index].count++;
}
} catch(e) {
continue;
}
}
return ctx.service.utils.resResult(STATUS.SUCCESS, {
result
});
}
public async stopServerRegister(id: number) {
const { ctx } = this;
const server = await ServerlistModel.updateByServerId(id, { stopRegisterTime: nowSeconds() - 1 });