活动存入内存中

This commit is contained in:
luying
2021-09-11 14:02:23 +08:00
parent 7a5722aca4
commit aef511b1aa
35 changed files with 189 additions and 59 deletions

View File

@@ -1,5 +1,8 @@
import { Application, ChannelService, HandlerService, } from 'pinus';
import { ActivityModel, ActivityModelType } from '../../../db/Activity';
import { ServerlistModel } from '../../../db/Serverlist';
import { reloadResources } from '../../../pubUtils/data';
import { _getActivitiesByType, _getActivityById } from '../../../services/activity/activityService';
export default function (app: Application) {
new HandlerService(app, {});
@@ -11,9 +14,13 @@ export class ActivityRemote {
constructor(private app: Application) {
this.app = app;
this.channelService = app.get('channelService');
this.loadActivities();
}
private channelService: ChannelService;
private activityByServer: Map<number, number[]> = new Map(); // serverId => activityId[];
private activities: Map<number, ActivityModelType> = new Map(); // activityId => activity
private activityByType: Map<number, Map<number, number[]>> = new Map(); // serverId => type => activityId[]
/**
* 重载json资源
@@ -21,4 +28,49 @@ export class ActivityRemote {
public async reloadResources() {
reloadResources();
}
private async loadActivities() {
let servers = await ServerlistModel.getAllServerList();
let groupToServer = new Map<number, number[]>();
for(let { serverId, activityGroupId } of servers) {
for(let groupId of activityGroupId) {
if(!groupToServer.has(groupId)) {
groupToServer.set(groupId, []);
}
groupToServer.get(groupId).push(serverId);
}
}
let activities = await ActivityModel.findOpenAndComingActivityes();
for(let activity of activities) {
this.activities.set(activity.activityId, activity);
let servers = groupToServer.get(activity.activityId)||[];
for(let serverId of servers) {
if(!this.activityByServer.has(serverId)) {
this.activityByServer.set(serverId, []);
}
this.activityByServer.get(serverId).push(activity.activityId);
if(!this.activityByType.has(serverId)) {
this.activityByType.set(serverId, new Map());
}
if(!this.activityByType.get(serverId).has(activity.type)) {
this.activityByType.get(serverId).set(activity.type, []);
}
this.activityByType.get(serverId).get(activity.type).push(activity.activityId);
}
}
this.app.set('activityByServer', this.activityByServer);
this.app.set('activityByType', this.activityByType);
this.app.set('activities', this.activities);
}
public getActivityById(activityId: number) {
return _getActivityById(activityId);
}
public getActivitiesByType(serverId: number, type: number) {
return _getActivitiesByType(serverId, type);
}
}