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, _getActivities } from '../../../services/activity/activityService'; import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService'; import { taflush } from '../../../services/sdkService'; import { ActivityInRemote } from '../../../domain/activityField/activityField'; export default function (app: Application) { new HandlerService(app, {}); return new ActivityRemote(app); } export class ActivityRemote { constructor(private app: Application) { this.app = app; this.channelService = app.get('channelService'); } private channelService: ChannelService; private activityByServer: Map = new Map(); // serverId => activityId[]; private activities: Map = new Map(); // activityId => activity private activityByType: Map> = new Map(); // serverId => type => activityId[]; private groupToServer: Map = new Map(); // group => serverId[]; /** * 重载json资源 */ public async reloadResources() { reloadResources(); } public async loadActivities() { let servers = await ServerlistModel.findByEnv(this.app.get('env')); for(let { id: serverId, activityGroupId } of servers) { for(let groupId of activityGroupId) { if(!this.groupToServer.has(groupId)) { this.groupToServer.set(groupId, []); } this.groupToServer.get(groupId).push(serverId); } } let activities = await ActivityModel.findOpenAndComingActivityes(); let activityIds: number[] = []; for(let activity of activities) { this.activities.set(activity.activityId, new ActivityInRemote(activity)); activityIds.push(activity.activityId); } this.setActivityTypeAndServer(); this.app.set('activityByServer', this.activityByServer); this.app.set('activityByType', this.activityByType); this.app.set('activities', this.activities); this.app.set('groupToServer', this.groupToServer); console.log('****** loadActivities') } private setActivityTypeAndServer() { this.activityByServer.clear(); this.activityByType.clear(); for(let [_, activity] of this.activities) { let servers = this.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); this.app.set('groupToServer', this.groupToServer); } public async updateActivities(activities: ActivityInRemote[]) { console.log('******* activities', activities) let activityIds: number[] = []; for(let activity of activities) { this.activities.set(activity.activityId, activity); activityIds.push(activity.activityId); } this.setActivityTypeAndServer(); this.app.set('activityByServer', this.activityByServer); this.app.set('activityByType', this.activityByType); this.app.set('activities', this.activities); this.app.set('groupToServer', this.groupToServer); } public async deleteActivities(activityIds: number[]) { for(let activityId of activityIds) { this.activities.delete(activityId); } this.setActivityTypeAndServer(); this.app.set('activityByServer', this.activityByServer); this.app.set('activityByType', this.activityByType); this.app.set('activities', this.activities); this.app.set('groupToServer', this.groupToServer); } public async saveGroupToServer(groupId: number, serverIds: number[]) { this.groupToServer.set(groupId, serverIds); this.setActivityTypeAndServer(); this.app.set('activityByServer', this.activityByServer); this.app.set('activityByType', this.activityByType); this.app.set('activities', this.activities); this.app.set('groupToServer', this.groupToServer); } public async saveActivitiesToGroup(groupId: number, activities: number[]) { for(let activityId of activities) { if(this.activities.get(activityId)) { this.activities.get(activityId).groupId = groupId; } } this.setActivityTypeAndServer(); } public getActivityById(activityId: number) { return _getActivityById(activityId); } public getActivitiesByType(serverId: number, type: number) { return _getActivitiesByType(serverId, type); } public getActivities() { return _getActivities(); } public setServerMainten(serverIds: number[], startTime: number, endTime: number) { setServerMainten(serverIds, startTime, endTime); } public stopServerMainten(serverIds: number[]) { stopServerMainten(serverIds); } public getServerMainten(serverId: number) { return getServerMainten(serverId); } public taflush() { return taflush(); } }