Notify on connection events

Implement notification to plugins for disconnect request, connect, drop,
and close.  This allows plugins to properly handle slaves and the master
server shutting down without sending messages that will get lost.
This commit is contained in:
Hornwitser committed 2020-05-08 23:49:28 +02:00
1 parent b8b8c44083
commit c35b6de70e
13 files changed
+278 -31

No files matched your search

+21 -8
View File
@@ -13,12 +13,17 @@ function removeTags(content) {
}
class InstancePlugin extends plugin.BaseInstancePlugin {
constructor(...args) {
super(...args);
async init() {
this.messageQueue = [];
}
async init() {
// XXX Nothing?
onMasterConnectionEvent(event) {
if (event === "connect") {
for (let message of this.messageQueue) {
this.sendChat(message);
}
this.messageQueue = [];
}
}
async chatEventHandler(message) {
@@ -27,12 +32,20 @@ class InstancePlugin extends plugin.BaseInstancePlugin {
await this.instance.server.sendRcon(`/sc game.print('${luaTools.escapeString(content)}')`, true);
}
sendChat(message) {
this.info.messages.chat.send(this.instance, {
instance_name: this.instance.name,
content: message,
});
}
async onOutput(output) {
if (output.type === "action" && output.action === "CHAT") {
this.info.messages.chat.send(this.instance, {
instance_name: this.instance.name,
content: output.message,
});
if (this.slave.connector.connected) {
this.sendChat(output.message);
} else {
this.messageQueue.push(output.message);
}
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ describe("global_chat plugin", function() {
let instancePlugin;
before(async function() {
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance());
instancePlugin = new instance.InstancePlugin(info, new mock.MockInstance(), new mock.MockSlave());
await instancePlugin.init();
});
+4 -15
View File
@@ -49,9 +49,7 @@ class MasterPlugin extends plugin.BaseMasterPlugin {
this.lastProgressBroadcast = Date.now();
if (techs.length) {
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.progress.send(slaveConnection, { technologies: techs });
}
this.broadcastEventToSlaves(this.info.messages.progress, { technologies: techs });
}
}
@@ -89,12 +87,7 @@ class MasterPlugin extends plugin.BaseMasterPlugin {
tech.progress = null;
this.progressToBroadcast.delete(name);
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, {
name,
level: tech.level
});
}
this.broadcastEventToSlaves(this.info.messages.finished, { name, level: tech.level });
}
}
@@ -125,9 +118,7 @@ class MasterPlugin extends plugin.BaseMasterPlugin {
if (progress) {
this.progressToBroadcast.add(name);
} else if (researched || baseLevel(name) != level) {
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, { name, level });
}
this.broadcastEventToSlaves(this.info.messages.finished, { name, level });
}
} else {
@@ -138,9 +129,7 @@ class MasterPlugin extends plugin.BaseMasterPlugin {
if (tech.level < level || researched) {
// Send update if the unlocked level is greater
if (level - !researched > tech.level - !tech.researched) {
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.finished.send(slaveConnection, { name, level: level - !researched });
}
this.broadcastEventToSlaves(this.info.messages.finished, { name, level: level - !researched });
}
tech.level = level;
tech.progress = progress;
+1 -3
View File
@@ -77,9 +77,7 @@ class MasterPlugin extends plugin.BaseMasterPlugin {
}
itemsToUpdate = [...itemsToUpdate.entries()];
for (let slaveConnection of this.master.slaveConnections.values()) {
this.info.messages.updateStorage.send(slaveConnection, { items: itemsToUpdate });
}
this.broadcastEventToSlaves(this.info.messages.updateStorage, { items: itemsToUpdate });
this.itemsLastUpdate = new Map(this.items._items.entries());
}