活动:修改活动的时候改变内存

This commit is contained in:
luying
2021-09-15 20:05:19 +08:00
parent 1b4cdd7ab6
commit d8ea05ecd9
4 changed files with 115 additions and 54 deletions

View File

@@ -42,8 +42,23 @@ export class ActivityRemote {
}
let activities = await ActivityModel.findOpenAndComingActivityes();
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);
}
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)) {
@@ -60,66 +75,34 @@ export class ActivityRemote {
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 async updateActivities(activities: ActivityModelType[]) {
let activityIds: number[] = [];
for(let activity of activities) {
let { activityId, groupId, type } = activity;
// 'activities'
this.activities.set(activityId, activity);
let serverIds = this.groupToServer.get(groupId)||[]; // 现在这个group对应的serverIds
// 'activityByServer' & 'activityByType'
// 将原来的都删掉,再塞入
for(let [serverId, activityIds] of this.activityByServer) {
let index = activityIds.indexOf(activityId);
if(index != -1) this.activityByServer.get(serverId).splice(index);
}
for(let [serverId, typeToActivities] of this.activityByType) {
for(let [type, activityIds] of typeToActivities) {
let index = activityIds.indexOf(activityId);
if(index != -1) this.activityByType.get(serverId).get(type).splice(index);
}
}
// 塞入
for(let serverId of serverIds) {
if(!this.activityByServer.has(serverId)) {
this.activityByServer.set(serverId, []);
}
this.activityByServer.get(serverId).push(activityId);
if(!this.activityByType.has(serverId)) {
this.activityByType.set(serverId, new Map());
}
if(!this.activityByType.get(serverId).has(type)) {
this.activityByType.get(serverId).set(type, []);
}
this.activityByType.get(serverId).get(type).push(activityId);
}
this.activities.set(activity.activityId, activity);
activityIds.push(activity.activityId);
}
this.setActivityTypeAndServer();
}
public async deleteActivities(activityIds: number[]) {
for(let activityId of activityIds) {
// 'activities'
this.activities.delete(activityId);
// 'activityByServer' & 'activityByType'
// 将原来的都删掉
for(let [serverId, activityIds] of this.activityByServer) {
let index = activityIds.indexOf(activityId);
if(index != -1) this.activityByServer.get(serverId).splice(index);
}
for(let [serverId, typeToActivities] of this.activityByType) {
for(let [type, activityIds] of typeToActivities) {
let index = activityIds.indexOf(activityId);
if(index != -1) this.activityByType.get(serverId).get(type).splice(index);
}
}
}
this.setActivityTypeAndServer();
}
public async saveGroupToServer(groupId: number, serverIds: number[]) {
this.groupToServer.set(groupId, serverIds);
this.setActivityTypeAndServer();
}
public async saveActivitiesToGroup(groupId: number, activities: number[]) {
for(let activityId of activities) {
this.activities.get(activityId).groupId = groupId;
}
this.setActivityTypeAndServer();
}
public getActivityById(activityId: number) {