mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Share player stats with master server
Send the per player stats with the player event updates to the master server and expose it to the Web UI and ctl interfaces.
This commit is contained in:
1 parent
deb6842a4d
commit
0245ccd16c
12 files changed
+400
-34
No files matched your search
@@ -1,16 +1,24 @@
|
||||
import React, { useEffect, useContext, useState } from "react";
|
||||
import { useHistory, useParams } from "react-router-dom";
|
||||
import { Button, Col, Form, Input, PageHeader, Popconfirm, Popover, Row, Tag, Select, Space, Spin, Switch } from "antd";
|
||||
import {
|
||||
Button, Col, Descriptions, Form, Input, PageHeader, Popconfirm,
|
||||
Popover, Row, Table, Tag, Select, Space, Spin, Switch,
|
||||
} from "antd";
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useInstanceList } from "../model/instance";
|
||||
import ControlContext from "./ControlContext";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import { useUser } from "../model/user";
|
||||
import { formatDuration } from "../util/time_format";
|
||||
import { formatLastSeen, sortLastSeen, useUser } from "../model/user";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numerice: "true", sensitivity: "base" }).compare;
|
||||
|
||||
|
||||
export default function UserViewPage() {
|
||||
@@ -21,6 +29,7 @@ export default function UserViewPage() {
|
||||
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [instanceList] = useInstanceList();
|
||||
let [user, updateUser] = useUser(userName);
|
||||
let [roles, setRoles] = useState(null);
|
||||
let [form] = Form.useForm();
|
||||
@@ -83,10 +92,22 @@ export default function UserViewPage() {
|
||||
</Select>
|
||||
</Form.Item>;
|
||||
|
||||
function instanceName(id) {
|
||||
let instance = instanceList.find(i => i.id === id);
|
||||
return instance ? instance["name"] : id;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader
|
||||
className="site-page-header"
|
||||
title={userName}
|
||||
title={<Space>
|
||||
{userName}
|
||||
<span>
|
||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
||||
</span>
|
||||
</Space>}
|
||||
extra={
|
||||
account.hasPermission("core.user.delete") && <Popconfirm
|
||||
title={<>
|
||||
@@ -110,7 +131,23 @@ export default function UserViewPage() {
|
||||
</Popconfirm>
|
||||
}
|
||||
/>
|
||||
<Form form={form}>
|
||||
<Form
|
||||
form={form}
|
||||
labelCol={{
|
||||
sm: { span: 6 },
|
||||
md: { span: 6 },
|
||||
lg: { span: 4 },
|
||||
xl: { span: 3 },
|
||||
xxl: { span: 2 },
|
||||
}}
|
||||
wrapperCol={{
|
||||
sm: { span: 18 },
|
||||
md: { span: 18 },
|
||||
lg: { span: 20 },
|
||||
xl: { span: 21 },
|
||||
xxl: { span: 22 },
|
||||
}}
|
||||
>
|
||||
<Form.Item
|
||||
label="Roles"
|
||||
>
|
||||
@@ -237,6 +274,58 @@ export default function UserViewPage() {
|
||||
</Space>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
<SectionHeader title="Player stats" />
|
||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, lg: 3 }}>
|
||||
<Descriptions.Item label="Total play time">
|
||||
{user["player_stats"]["online_time_ms"] && formatDuration(user["player_stats"]["online_time_ms"])}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Total join count">
|
||||
{user["player_stats"]["join_count"]}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Last seen">
|
||||
{formatLastSeen(user)}
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
<SectionHeader title="Instance stats" />
|
||||
<Table
|
||||
size="small"
|
||||
columns={[
|
||||
{
|
||||
title: "Instance",
|
||||
key: "instance",
|
||||
render: ([id]) => instanceName(id),
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a[0], b[0]),
|
||||
},
|
||||
{
|
||||
title: "Play time",
|
||||
key: "playTime",
|
||||
render: ([, stats]) => formatDuration(stats["online_time_ms"] || 0),
|
||||
sorter: (a, b) => (a[1]["online_time_ms"] || 0) - (b[1]["online_time_ms"] || 0),
|
||||
},
|
||||
{
|
||||
title: "Join count",
|
||||
key: "joinCoint",
|
||||
render: ([, stats]) => stats["join_count"] || 0,
|
||||
sorter: (a, b) => (a[1]["join_count"] || 0) - (b[1]["join_count"] || 0),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
title: "Last seen",
|
||||
key: "lastSeen",
|
||||
render: ([id]) => formatLastSeen(user, id),
|
||||
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
||||
},
|
||||
]}
|
||||
dataSource={user["instance_stats"]}
|
||||
pagination={false}
|
||||
rowKey={([id]) => id}
|
||||
onRow={([id], rowIndex) => ({
|
||||
onClick: event => {
|
||||
history.push(`/instances/${id}/view`);
|
||||
},
|
||||
})}
|
||||
/>
|
||||
<PluginExtra component="UserViewPage" user={user} />
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
import React, { useEffect, useContext, useState } from "react";
|
||||
import { useHistory } from "react-router-dom";
|
||||
import { Button, Form, Input, Modal, PageHeader, Table, Tag } from "antd";
|
||||
import { Button, Form, Input, Modal, PageHeader, Space, Table, Tag } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import { formatDuration } from "../util/time_format";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PluginExtra from "./PluginExtra";
|
||||
import { useUserList } from "../model/user";
|
||||
import { formatLastSeen, sortLastSeen, useUserList } from "../model/user";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numerice: "true", sensitivity: "base" }).compare;
|
||||
|
||||
@@ -80,7 +81,15 @@ export default function UsersPage() {
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
key: "name",
|
||||
render: user => <Space>
|
||||
{user["name"]}
|
||||
<span>
|
||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
||||
</span>
|
||||
</Space>,
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
},
|
||||
@@ -90,24 +99,19 @@ export default function UsersPage() {
|
||||
render: user => user.roles.map(id => <Tag key={id}>{(roles.get(id) || { name: id })["name"]}</Tag>),
|
||||
},
|
||||
{
|
||||
title: "Admin",
|
||||
key: "admin",
|
||||
render: user => user["is_admin"] && "yes",
|
||||
sorter: (a, b) => a["is_admin"] - b["is_admin"],
|
||||
title: "Play time",
|
||||
key: "playTime",
|
||||
render: user => user["player_stats"]["online_time_ms"]
|
||||
&& formatDuration(user["player_stats"]["online_time_ms"]),
|
||||
sorter: (a, b) => (a["player_stats"]["online_time_ms"] || 0) -
|
||||
(b["player_stats"]["online_time_ms"] || 0),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
title: "Whitelisted",
|
||||
key: "whitelisted",
|
||||
render: user => user["is_whitelisted"] && "yes",
|
||||
sorter: (a, b) => a["is_whitelisted"] - b["is_whitelisted"],
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
title: "Banned",
|
||||
key: "banned",
|
||||
render: user => user["is_banned"] && "yes",
|
||||
sorter: (a, b) => a["is_banned"] - b["is_banned"],
|
||||
title: "Last seen",
|
||||
key: "lastSeen",
|
||||
render: user => formatLastSeen(user),
|
||||
sorter: sortLastSeen,
|
||||
responsive: ["lg"],
|
||||
},
|
||||
]}
|
||||
|
||||
@@ -1,9 +1,56 @@
|
||||
import { useEffect, useContext, useState } from "react";
|
||||
import React, { useEffect, useContext, useState } from "react";
|
||||
import { Tag } from "antd";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
import { libLink, libLogging } from "@clusterio/lib";
|
||||
const { logger } = libLogging;
|
||||
|
||||
function calculateLastSeen(user, instanceId) {
|
||||
let stats;
|
||||
if (instanceId === undefined) {
|
||||
stats = user["player_stats"];
|
||||
} else {
|
||||
let entry = (user["instance_stats"] || []).find(([id]) => id === instanceId);
|
||||
if (!entry) {
|
||||
return undefined;
|
||||
}
|
||||
stats = entry[1];
|
||||
}
|
||||
if (stats["last_leave_at"] > stats["last_join_at"]) {
|
||||
return stats["last_leave_at"];
|
||||
}
|
||||
if (stats["last_join_at"]) {
|
||||
return stats["last_leave_at"];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function formatLastSeen(user, instanceId = undefined) {
|
||||
if (user["instances"].some(id => instanceId === undefined || id === instanceId)) {
|
||||
return <Tag color="green">Online</Tag>;
|
||||
}
|
||||
let lastSeen = calculateLastSeen(user, instanceId);
|
||||
if (lastSeen === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return new Date(lastSeen).toLocaleTimeString();
|
||||
}
|
||||
|
||||
export function sortLastSeen(userA, userB, instanceIdA = undefined, instanceIdB = undefined) {
|
||||
function epoch(user, instanceId) {
|
||||
return user["instances"].some(id => instanceId === undefined || id === instanceId);
|
||||
}
|
||||
|
||||
let epochA = epoch(userA, instanceIdA);
|
||||
let epochB = epoch(userB, instanceIdB);
|
||||
if (epochA !== epochB) {
|
||||
return epochA - epochB;
|
||||
}
|
||||
|
||||
let lastSeenA = calculateLastSeen(userA, instanceIdA) || 0;
|
||||
let lastSeenB = calculateLastSeen(userB, instanceIdB) || 0;
|
||||
return lastSeenA - lastSeenB;
|
||||
}
|
||||
|
||||
export function useUser(name) {
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
const formatSecond = new Intl.NumberFormat(undefined, { style: "unit", unit: "second", unitDisplay: "narrow" }).format;
|
||||
const formatMinute = new Intl.NumberFormat(undefined, { style: "unit", unit: "minute", unitDisplay: "narrow" }).format;
|
||||
const formatHour = new Intl.NumberFormat(undefined, { style: "unit", unit: "hour", unitDisplay: "narrow" }).format;
|
||||
const formatDay = new Intl.NumberFormat(undefined, { style: "unit", unit: "day", unitDisplay: "narrow" }).format;
|
||||
|
||||
/**
|
||||
* Formats a duration of time in milliseconds to something human readable.
|
||||
* @param {number} ms - Duration to formatDuration
|
||||
* @param {object=} options - Options to control the formatting.
|
||||
* @returns {string} human readable representation of the duration.
|
||||
*/
|
||||
export function formatDuration(ms, options = {}) {
|
||||
let result = "";
|
||||
if (ms < 0) {
|
||||
result += "-";
|
||||
ms = -ms;
|
||||
}
|
||||
|
||||
if (ms > 86400e3) {
|
||||
result += formatDay(Math.floor(ms / 86400e3));
|
||||
ms %= 86400e3;
|
||||
}
|
||||
if (ms > 3600e3) {
|
||||
result += formatHour(Math.floor(ms / 3600e3));
|
||||
ms %= 3600e3;
|
||||
}
|
||||
if (ms > 60e3) {
|
||||
result += formatMinute(Math.floor(ms / 60e3));
|
||||
ms %= 60e3;
|
||||
}
|
||||
result += formatSecond(Math.round(ms / 1e3));
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user