From f56fd7c0c4c99d9b8b310a49324cdcd138728063 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Mon, 27 Nov 2023 02:57:28 +0300 Subject: [PATCH] server ping --- .../runtime/components/ServerButton/index.tsx | 12 ++++++---- src/renderer/runtime/hooks/pingServer.ts | 24 +++++++++++++++++++ .../runtime/scenes/ServerPanel/index.tsx | 8 +++++-- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/renderer/runtime/hooks/pingServer.ts diff --git a/src/renderer/runtime/components/ServerButton/index.tsx b/src/renderer/runtime/components/ServerButton/index.tsx index 0a78cf6..030342c 100644 --- a/src/renderer/runtime/components/ServerButton/index.tsx +++ b/src/renderer/runtime/components/ServerButton/index.tsx @@ -1,16 +1,20 @@ +import { Server } from '@aurora-launcher/core'; import classes from './index.module.sass'; +import { usePingServer } from '../../hooks/pingServer'; interface ServerButtonProps { onClick: () => void; - server: any; + server: Server; } export function ServerButton({ onClick, server }: ServerButtonProps) { + const players = usePingServer(server); + return ( ); diff --git a/src/renderer/runtime/hooks/pingServer.ts b/src/renderer/runtime/hooks/pingServer.ts new file mode 100644 index 0000000..0cc0b74 --- /dev/null +++ b/src/renderer/runtime/hooks/pingServer.ts @@ -0,0 +1,24 @@ +import { Server } from '@aurora-launcher/core'; +import { useState, useEffect } from 'react'; + +export function usePingServer(server: Server) { + const [players, setPlayers] = useState({ online: 0, max: 10 }); + + useEffect(() => { + if (!server.ip) { + return; + } + + fetch( + `https://api.mcstatus.io/v2/status/java/${server.ip}:${server.port}`, + ).then((response) => { + response.json().then((data) => { + if (data.online) { + setPlayers(data.players); + } + }); + }); + }, [server]); + + return players; +} diff --git a/src/renderer/runtime/scenes/ServerPanel/index.tsx b/src/renderer/runtime/scenes/ServerPanel/index.tsx index 2922462..c064a7c 100644 --- a/src/renderer/runtime/scenes/ServerPanel/index.tsx +++ b/src/renderer/runtime/scenes/ServerPanel/index.tsx @@ -5,10 +5,12 @@ import If from '../../components/If'; import { useTitlebar } from '../../components/TitleBar/hooks'; import classes from './index.module.sass'; import { LoadProgress } from '../../../../common/types'; +import { usePingServer } from '../../hooks/pingServer'; export default function ServerPanel() { const [selectedProfile, setSelectedProfile] = useState({} as Profile); const [selectedServer, setSelectedServer] = useState({} as Server); + const players = usePingServer(selectedServer); const [console, setConsole] = useState(''); const [showProgress, setShowProgress] = useState(false); @@ -75,8 +77,10 @@ export default function ServerPanel() {
- 10 -
из 100
+ {players.online || 0} +
+ из {players.max || 10} +