Provide reason for player leaving

Expose the reason for why a player is leaving a server in playerEvent.
This commit is contained in:
Hornwitser committed 2022-07-08 01:00:54 +02:00
1 parent 8017497bdd
commit db562ce6a0
5 files changed
+37 -10

No files matched your search

+2
View File
@@ -1284,10 +1284,12 @@ messages.playerEvent = new Event({
type: "player_event", type: "player_event",
links: ["instance-slave", "slave-master"], links: ["instance-slave", "slave-master"],
forwardTo: "master", forwardTo: "master",
eventRequired: ["instance_id", "type", "name"],
eventProperties: { eventProperties: {
"instance_id": { type: "integer" }, "instance_id": { type: "integer" },
"type": { type: "string", enum: ["join", "leave"] }, "type": { type: "string", enum: ["join", "leave"] },
"name": { type: "string" }, "name": { type: "string" },
"reason": { type: "string" },
}, },
}); });
+6
View File
@@ -208,6 +208,9 @@ class BaseInstancePlugin {
* @param {Object} event - Information about the event. * @param {Object} event - Information about the event.
* @param {string} event.type - Either "join" or "leave". * @param {string} event.type - Either "join" or "leave".
* @param {string} event.name - Name of the player that joined/left. * @param {string} event.name - Name of the player that joined/left.
* @param {string=} event.reason -
* Only present for type "leave". Reason for player leaving the
* game, one of the possible reasons in defines.disconnect_reason.
*/ */
async onPlayerEvent(event) { } async onPlayerEvent(event) { }
@@ -500,6 +503,9 @@ class BaseMasterPlugin {
* @param {Object} event - Information about the event. * @param {Object} event - Information about the event.
* @param {string} event.type - Either "join" or "leave". * @param {string} event.type - Either "join" or "leave".
* @param {string} event.name - Name of the player that joined/left. * @param {string} event.name - Name of the player that joined/left.
* @param {string=} event.reason -
* Only present for type "leave". Reason for player leaving the
* game, one of the possible reasons in defines.disconnect_reason.
*/ */
async onPlayerEvent(instance, event) { } async onPlayerEvent(instance, event) { }
+10 -1
View File
@@ -28,9 +28,18 @@ impl.events[defines.events.on_player_joined_game] = function(event)
api.send_json("player_event", { type = "join", name = player.name }) api.send_json("player_event", { type = "join", name = player.name })
end end
local disconnect_reason_name = {}
for name, i in pairs(defines.disconnect_reason) do
disconnect_reason_name[i] = name
end
impl.events[defines.events.on_player_left_game] = function(event) impl.events[defines.events.on_player_left_game] = function(event)
local player = game.players[event.player_index] local player = game.players[event.player_index]
api.send_json("player_event", { type = "leave", name = player.name }) api.send_json("player_event", {
type = "leave",
name = player.name,
reason = disconnect_reason_name[event.reason],
})
end end
-- Internal API -- Internal API
+14 -4
View File
@@ -250,7 +250,7 @@ class Instance extends libLink.Link {
if (event.type === "join") { if (event.type === "join") {
this._recordPlayerJoin(event.name); this._recordPlayerJoin(event.name);
} else if (event.type === "leave") { } else if (event.type === "leave") {
this._recordPlayerLeave(event.name); this._recordPlayerLeave(event.name, event.reason);
} }
}); });
@@ -264,6 +264,9 @@ class Instance extends libLink.Link {
* Unix timestamp in ms the player was last seen leaving this * Unix timestamp in ms the player was last seen leaving this
* instance. Not present if player has yet to leave after first * instance. Not present if player has yet to leave after first
* time joining. * time joining.
* @property {string=} lastLeaveReason -
* Reason the player was last seen leaving with. Not present if
* player has yet to leave after first time joining.
* @property {number} joinCount - * @property {number} joinCount -
* Count of the number of times this player has been seen * Count of the number of times this player has been seen
* joining this server. * joining this server.
@@ -297,7 +300,12 @@ class Instance extends libLink.Link {
if (parsed.action === "JOIN") { if (parsed.action === "JOIN") {
this._recordPlayerJoin(name); this._recordPlayerJoin(name);
} else if (["LEAVE", "KICK", "BAN"].includes(parsed.action)) { } else if (["LEAVE", "KICK", "BAN"].includes(parsed.action)) {
this._recordPlayerLeave(name); let reason = {
"LEAVE": "quit",
"KICK": "kicked",
"BAN": "banned",
}[parsed.action];
this._recordPlayerLeave(name, reason);
} }
}); });
@@ -322,7 +330,7 @@ class Instance extends libLink.Link {
this.playersOnline.forEach(player => joined.delete(player)); this.playersOnline.forEach(player => joined.delete(player));
for (let player of left) { for (let player of left) {
this._recordPlayerLeave(player); this._recordPlayerLeave(player, "quit");
} }
// Missing join messages is not supposed to happen. // Missing join messages is not supposed to happen.
@@ -358,13 +366,14 @@ class Instance extends libLink.Link {
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
} }
_recordPlayerLeave(name) { _recordPlayerLeave(name, reason) {
if (!this.playersOnline.delete(name)) { if (!this.playersOnline.delete(name)) {
return; return;
} }
let stats = this.playerStats.get(name); let stats = this.playerStats.get(name);
stats.lastLeaveAt = Date.now(); stats.lastLeaveAt = Date.now();
stats.lastLeaveReason = reason;
stats.onlineTimeMs += stats.lastLeaveAt - stats.lastJoinAt; stats.onlineTimeMs += stats.lastLeaveAt - stats.lastJoinAt;
this._hadPlayersOnline = true; this._hadPlayersOnline = true;
@@ -372,6 +381,7 @@ class Instance extends libLink.Link {
instance_id: this.id, instance_id: this.id,
type: "leave", type: "leave",
name, name,
reason,
}; };
libLink.messages.playerEvent.send(this, event); libLink.messages.playerEvent.send(this, event);
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
+5 -5
View File
@@ -89,21 +89,21 @@ describe("class Instance", function() {
it("should send player_event", function() { it("should send player_event", function() {
instance._recordPlayerJoin("player"); instance._recordPlayerJoin("player");
instance._recordPlayerLeave("player"); instance._recordPlayerLeave("player", "quit");
assert.deepEqual( assert.deepEqual(
connector.sentMessages[1], connector.sentMessages[1],
{ {
seq: 2, seq: 2,
type: "player_event_event", type: "player_event_event",
data: { type: "leave", instance_id: instance.id, name: "player" }, data: { type: "leave", instance_id: instance.id, name: "player", reason: "quit" },
}, },
); );
}); });
it("should be idempotent", function() { it("should be idempotent", function() {
instance._recordPlayerJoin("player"); instance._recordPlayerJoin("player");
instance._recordPlayerLeave("player"); instance._recordPlayerLeave("player", "quit");
instance._recordPlayerLeave("player"); instance._recordPlayerLeave("player", "quit");
assert(!instance.playersOnline.has("player"), "player was not removed"); assert(!instance.playersOnline.has("player"), "player was not removed");
assert.equal(connector.sentMessages.length, 2); assert.equal(connector.sentMessages.length, 2);
}); });
@@ -148,7 +148,7 @@ describe("class Instance", function() {
{ {
seq: 2, seq: 2,
type: "player_event_event", type: "player_event_event",
data: { type: "leave", instance_id: instance.id, name: "player" }, data: { type: "leave", instance_id: instance.id, name: "player", reason: "quit" },
}, },
); );
}); });