Fix PlayerEvent being completely broken

Fix the name being passed as the type when deserialising the event, the
stats being missing, and incorrect parameters passed to plugins.
This commit is contained in:
Hornwitser committed 2023-11-28 14:28:22 +01:00
1 parent b9c3458cc2
commit e3e5867d60
5 files changed
+30 -20

No files matched your search

+2 -1
View File
@@ -289,11 +289,12 @@ export default class HostConnection extends BaseConnection {
user.recalculatePlayerStats();
this._controller.userUpdated(user);
let instance = this._controller.instances!.get(instanceId);
let instance = this._controller.instances!.get(instanceId)!;
await lib.invokeHook(this._controller.plugins, "onPlayerEvent", instance, {
type: event.type,
name: event.name,
reason: event.reason,
stats: event.stats,
});
}
}
+7 -10
View File
@@ -303,13 +303,12 @@ export default class Instance extends lib.Link {
stats.lastJoinAt = new Date();
stats.joinCount += 1;
let event = {
instance_id: this.id,
let event: lib.PlayerEvent = {
type: "join",
name,
stats,
};
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("join", name, undefined, stats));
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("join", name, stats));
lib.invokeHook(this.plugins, "onPlayerEvent", event);
}
@@ -324,14 +323,13 @@ export default class Instance extends lib.Link {
stats.onlineTimeMs += stats.lastLeaveAt.getTime() - stats.lastJoinAt!.getTime();
this._hadPlayersOnline = true;
let event = {
instance_id: this.id,
let event: lib.PlayerEvent = {
type: "leave",
name,
reason,
stats,
};
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("leave", name, reason, stats));
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("leave", name, stats, reason));
lib.invokeHook(this.plugins, "onPlayerEvent", event);
}
@@ -353,13 +351,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
stats.onlineTimeMs = onlineTimeTicks * 1000 / 60;
let event = {
instance_id: this.id,
let event: lib.PlayerEvent = {
type: "import",
name,
stats: stats.toJSON(),
stats,
};
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("import", name, undefined, stats));
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("import", name, stats));
lib.invokeHook(this.plugins, "onPlayerEvent", event);
count += 1;
}
+3 -3
View File
@@ -853,10 +853,10 @@ export class InstancePlayerUpdateEvent {
static dst = "controller" as const;
constructor(
public type: string,
public type: "join" | "leave" | "import",
public name: string,
public stats: PlayerStats,
public reason?: string,
public stats?: PlayerStats,
) { }
static jsonSchema = Type.Object({
@@ -867,6 +867,6 @@ export class InstancePlayerUpdateEvent {
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.name, json.name, json.reason, PlayerStats.fromJSON(json.stats));
return new this(json.type, json.name, PlayerStats.fromJSON(json.stats), json.reason);
}
}
+6
View File
@@ -4,6 +4,7 @@
*/
import * as libHelpers from "./helpers";
import type { PluginConfigGroup } from "./config";
import type PlayerStats from "./PlayerStats";
// TODO Add proper typing for plugins
/* Used to define the export of info.ts from plugins */
@@ -54,6 +55,11 @@ export interface PlayerEvent {
* or "server_quit" if the server exits while the player is online.
*/
reason?: string;
/**
* Statistics recorded for this player on the instance this event
* originated from.
*/
stats: PlayerStats,
}
/**
+12 -6
View File
@@ -65,7 +65,8 @@ describe("class Instance", function() {
new lib.MessageEvent(
1, src, addr("controller"), "InstancePlayerUpdateEvent",
new lib.InstancePlayerUpdateEvent(
"join", "player", undefined,
"join",
"player",
new PlayerStats({
join_count: 1,
last_join_at: stats.lastJoinAt.getTime(),
@@ -110,14 +111,16 @@ describe("class Instance", function() {
new lib.MessageEvent(
2, src, addr("controller"), "InstancePlayerUpdateEvent",
new lib.InstancePlayerUpdateEvent(
"leave", "player", "quit",
"leave",
"player",
new PlayerStats({
join_count: 1,
online_time_ms: stats.onlineTimeMs,
last_join_at: stats.lastJoinAt.getTime(),
last_leave_at: stats.lastLeaveAt.getTime(),
last_leave_reason: "quit",
})
}),
"quit",
)
),
);
@@ -160,7 +163,8 @@ describe("class Instance", function() {
new lib.MessageEvent(
2, src, addr("controller"), "InstancePlayerUpdateEvent",
new lib.InstancePlayerUpdateEvent(
"join", "foo", undefined,
"join",
"foo",
new PlayerStats({
join_count: 1,
last_join_at: stats.lastJoinAt.getTime(),
@@ -181,14 +185,16 @@ describe("class Instance", function() {
new lib.MessageEvent(
2, src, addr("controller"), "InstancePlayerUpdateEvent",
new lib.InstancePlayerUpdateEvent(
"leave", "player", "quit",
"leave",
"player",
new PlayerStats({
join_count: 1,
online_time_ms: stats.onlineTimeMs,
last_join_at: stats.lastJoinAt.getTime(),
last_leave_at: stats.lastLeaveAt.getTime(),
last_leave_reason: "quit",
})
}),
"quit",
)
)
);