Skip to content

Commit

Permalink
server ping
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCat committed Nov 26, 2023
1 parent 366a48c commit f56fd7c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/renderer/runtime/components/ServerButton/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<button className={classes.button} onClick={onClick}>
<span className={classes.title}>{server.title}</span>
<span className={classes.online}>
{server.online?.current || 10} / {server.online?.maximum || 100}
{players.online || 0} / {players.max || 10}
</span>
<div className={classes.next}>
<svg
Expand All @@ -27,8 +31,8 @@ export function ServerButton({ onClick, server }: ServerButtonProps) {
</div>
<progress
className={classes.progress}
value={server.online?.current || 10}
max={server.online?.maximum || 100}
value={players.online || 0}
max={players.max || 10}
></progress>
</button>
);
Expand Down
24 changes: 24 additions & 0 deletions src/renderer/runtime/hooks/pingServer.ts
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 6 additions & 2 deletions src/renderer/runtime/scenes/ServerPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -75,8 +77,10 @@ export default function ServerPanel() {
</div>
<div className={classes.line}></div>
<div className={classes.count}>
10
<div className={classes.total}>из 100</div>
{players.online || 0}
<div className={classes.total}>
из {players.max || 10}
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit f56fd7c

Please sign in to comment.