mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add first join at player statistic (#551)
Record the time a player was first seen joining an instance in PlayerStats. For existing stats this value is set to the last joined time when it is first loaded.
This commit is contained in:
1 parent
863d90c7fb
commit
527c98d378
7 files changed
+78
-14
No files matched your search
@@ -301,6 +301,9 @@ export default class Instance extends lib.Link {
|
||||
this.playerStats.set(name, stats);
|
||||
}
|
||||
stats.lastJoinAt = new Date();
|
||||
if (!stats.firstJoinAt) {
|
||||
stats.firstJoinAt = stats.lastJoinAt;
|
||||
}
|
||||
stats.joinCount += 1;
|
||||
|
||||
let event: lib.PlayerEvent = {
|
||||
|
||||
@@ -14,6 +14,11 @@ export default class PlayerStats {
|
||||
*/
|
||||
onlineTimeMs = 0;
|
||||
|
||||
/**
|
||||
* Timestapm the player was first seen joining.
|
||||
*/
|
||||
firstJoinAt?: Date;
|
||||
|
||||
/**
|
||||
* Timestamp the player was last seen joining.
|
||||
*/
|
||||
@@ -32,6 +37,7 @@ export default class PlayerStats {
|
||||
static jsonSchema = Type.Object({
|
||||
"join_count": Type.Optional(Type.Integer()),
|
||||
"online_time_ms": Type.Optional(Type.Number()),
|
||||
"first_join_at": Type.Optional(Type.Number()),
|
||||
"last_join_at": Type.Optional(Type.Number()),
|
||||
"last_leave_at": Type.Optional(Type.Number()),
|
||||
"last_leave_reason": Type.Optional(Type.String()),
|
||||
@@ -41,11 +47,17 @@ export default class PlayerStats {
|
||||
if (json["join_count"]) {
|
||||
this.joinCount = json["join_count"];
|
||||
}
|
||||
if (json["first_join_at"]) {
|
||||
this.firstJoinAt = new Date(json["first_join_at"]);
|
||||
}
|
||||
if (json["online_time_ms"]) {
|
||||
this.onlineTimeMs = json["online_time_ms"];
|
||||
}
|
||||
if (json["last_join_at"]) {
|
||||
this.lastJoinAt = new Date(json["last_join_at"]);
|
||||
if (!this.firstJoinAt) { // migrate: pre-alpha 14 did not have this field.
|
||||
this.firstJoinAt = this.lastJoinAt;
|
||||
}
|
||||
}
|
||||
if (json["last_leave_at"]) {
|
||||
this.lastLeaveAt = new Date(json["last_leave_at"]);
|
||||
@@ -65,6 +77,9 @@ export default class PlayerStats {
|
||||
if (this.onlineTimeMs) {
|
||||
json["online_time_ms"] = this.onlineTimeMs;
|
||||
}
|
||||
if (this.firstJoinAt) {
|
||||
json["first_join_at"] = this.firstJoinAt.getTime();
|
||||
}
|
||||
if (this.lastJoinAt) {
|
||||
json["last_join_at"] = this.lastJoinAt.getTime();
|
||||
}
|
||||
|
||||
@@ -125,6 +125,12 @@ export default class User {
|
||||
static _calculatePlayerStats(instanceStatsMap: Map<number, PlayerStats>) {
|
||||
let playerStats = new PlayerStats();
|
||||
for (let instanceStats of instanceStatsMap.values()) {
|
||||
if (
|
||||
instanceStats.firstJoinAt
|
||||
&& (!playerStats.firstJoinAt || instanceStats.firstJoinAt < playerStats.firstJoinAt)
|
||||
) {
|
||||
playerStats.firstJoinAt = instanceStats.firstJoinAt;
|
||||
}
|
||||
if (
|
||||
instanceStats.lastJoinAt
|
||||
&& (!playerStats.lastJoinAt || instanceStats.lastJoinAt > playerStats.lastJoinAt)
|
||||
|
||||
@@ -17,7 +17,7 @@ import PluginExtra from "./PluginExtra";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import { formatDuration } from "../util/time_format";
|
||||
import { formatLastSeen, sortLastSeen, useUser } from "../model/user";
|
||||
import { formatFirstSeen, formatLastSeen, sortFirstSeen, sortLastSeen, useUser } from "../model/user";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -286,13 +286,16 @@ export default function UserViewPage() {
|
||||
}
|
||||
</Form>
|
||||
<SectionHeader title="Player stats" />
|
||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, lg: 3 }}>
|
||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, md: 2, lg: 2, xl: 2, xxl: 2 }}>
|
||||
<Descriptions.Item label="Total online time">
|
||||
{formatDuration(user.playerStats?.onlineTimeMs ?? 0)}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Total join count">
|
||||
{user.playerStats?.joinCount ?? 0}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="First seen">
|
||||
{formatFirstSeen(user) || " "}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Last seen">
|
||||
{formatLastSeen(user) || " "}
|
||||
</Descriptions.Item>
|
||||
@@ -321,6 +324,12 @@ export default function UserViewPage() {
|
||||
sorter: (a, b) => (a[1].joinCount || 0) - (b[1].joinCount || 0),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
title: "First seen",
|
||||
key: "firstSeen",
|
||||
render: (_, [id]) => formatFirstSeen(user, id),
|
||||
sorter: (a, b) => sortFirstSeen(user, user, a[0], b[0]),
|
||||
},
|
||||
{
|
||||
title: "Last seen",
|
||||
key: "lastSeen",
|
||||
|
||||
@@ -11,7 +11,7 @@ import { formatDuration } from "../util/time_format";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import { formatLastSeen, sortLastSeen, useUsers } from "../model/user";
|
||||
import { formatFirstSeen, formatLastSeen, sortFirstSeen, sortLastSeen, useUsers } from "../model/user";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
@@ -109,6 +109,12 @@ export default function UsersPage() {
|
||||
(b.playerStats?.onlineTimeMs ?? 0),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
title: "First seen",
|
||||
key: "firstSeen",
|
||||
render: (_, user) => formatFirstSeen(user),
|
||||
sorter: (a, b) => sortFirstSeen(a, b),
|
||||
},
|
||||
{
|
||||
title: "Last seen",
|
||||
key: "lastSeen",
|
||||
|
||||
@@ -4,25 +4,40 @@ import ControlContext from "../components/ControlContext";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
function calculateLastSeen(user: lib.User, instanceId?: number) {
|
||||
let stats;
|
||||
function getInstanceStats(user: lib.User, instanceId?: number) {
|
||||
if (instanceId === undefined) {
|
||||
stats = user.playerStats;
|
||||
} else {
|
||||
stats = user.instanceStats.get(instanceId);
|
||||
if (!stats) {
|
||||
return undefined;
|
||||
}
|
||||
return user.playerStats;
|
||||
}
|
||||
if ((stats.lastLeaveAt?.getTime() ?? 0) > (stats.lastJoinAt?.getTime() ?? 0)) {
|
||||
return stats.lastLeaveAt?.getTime();
|
||||
return user.instanceStats.get(instanceId);
|
||||
}
|
||||
|
||||
function calculateFirstSeen(user: lib.User, instanceId?: number) {
|
||||
const stats = getInstanceStats(user, instanceId);
|
||||
return stats?.firstJoinAt?.getTime();
|
||||
}
|
||||
|
||||
function calculateLastSeen(user: lib.User, instanceId?: number) {
|
||||
const stats = getInstanceStats(user, instanceId);
|
||||
if (!stats) {
|
||||
return undefined;
|
||||
}
|
||||
if (stats.lastLeaveAt && stats.lastLeaveAt.getTime() > (stats.lastJoinAt?.getTime() ?? 0)) {
|
||||
return stats.lastLeaveAt.getTime();
|
||||
}
|
||||
if (stats.lastJoinAt) {
|
||||
return stats.lastLeaveAt?.getTime();
|
||||
return stats.lastJoinAt.getTime();
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function formatFirstSeen(user: lib.User, instanceId?: number) {
|
||||
const firstSeen = calculateFirstSeen(user, instanceId);
|
||||
if (firstSeen === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return new Date(firstSeen).toLocaleString();
|
||||
}
|
||||
|
||||
export function formatLastSeen(user: lib.User, instanceId?: number) {
|
||||
if (user.instances && [...user.instances].some(id => instanceId === undefined || id === instanceId)) {
|
||||
return <Tag color="green">Online</Tag>;
|
||||
@@ -34,6 +49,12 @@ export function formatLastSeen(user: lib.User, instanceId?: number) {
|
||||
return new Date(lastSeen).toLocaleString();
|
||||
}
|
||||
|
||||
export function sortFirstSeen(userA: lib.User, userB: lib.User, instanceIdA?: number, instanceIdB?: number) {
|
||||
const firstSeenA = calculateFirstSeen(userA, instanceIdA) || 0;
|
||||
const firstSeenB = calculateFirstSeen(userB, instanceIdB) || 0;
|
||||
return firstSeenA - firstSeenB;
|
||||
}
|
||||
|
||||
export function sortLastSeen(userA: lib.User, userB: lib.User, instanceIdA?: number, instanceIdB?: number) {
|
||||
function epoch(user: lib.User, instanceId?: number) {
|
||||
return user.instances && [...user.instances].some(id => instanceId === undefined || id === instanceId);
|
||||
|
||||
@@ -35,12 +35,14 @@ describe("lib/data/User", function() {
|
||||
[2, {
|
||||
join_count: 1,
|
||||
online_time_ms: 0,
|
||||
first_join_at: new Date("2020-05T12:00Z").getTime(),
|
||||
last_join_at: new Date("2020-05T12:02Z").getTime(),
|
||||
}],
|
||||
]});
|
||||
|
||||
assert.equal(user.playerStats.onlineTimeMs, 60e3);
|
||||
assert.equal(user.playerStats.joinCount, 2);
|
||||
assert.deepEqual(user.playerStats.firstJoinAt, new Date("2020-05T12:00Z"));
|
||||
assert.deepEqual(user.playerStats.lastJoinAt, new Date("2020-05T12:02Z"));
|
||||
assert.deepEqual(user.playerStats.lastLeaveAt, new Date("2020-05T12:01Z"));
|
||||
assert.equal(user.playerStats.lastLeaveReason, "quit");
|
||||
@@ -50,6 +52,7 @@ describe("lib/data/User", function() {
|
||||
lib.PlayerStats.fromJSON({
|
||||
join_count: 2,
|
||||
online_time_ms: 120e3,
|
||||
first_join_at: new Date("2020-05T11:00Z").getTime(),
|
||||
last_join_at: new Date("2020-05T13:00Z").getTime(),
|
||||
last_leave_at: new Date("2020-05T13:02Z").getTime(),
|
||||
last_leave_reason: "afk",
|
||||
@@ -59,6 +62,7 @@ describe("lib/data/User", function() {
|
||||
user.recalculatePlayerStats();
|
||||
assert.equal(user.playerStats.onlineTimeMs, 180e3);
|
||||
assert.equal(user.playerStats.joinCount, 4);
|
||||
assert.deepEqual(user.playerStats.firstJoinAt, new Date("2020-05T11:00Z"));
|
||||
assert.deepEqual(user.playerStats.lastJoinAt, new Date("2020-05T13:00Z"));
|
||||
assert.deepEqual(user.playerStats.lastLeaveAt, new Date("2020-05T13:02Z"));
|
||||
assert.equal(user.playerStats.lastLeaveReason, "afk");
|
||||
|
||||
Reference in new issue
Block a user