mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +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
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user