add: server status
This commit is contained in:
@ -9,6 +9,19 @@ import LaunchPage from './pages/LaunchPage';
|
||||
import { ReactNode, useEffect, useState } from 'react';
|
||||
import './App.css';
|
||||
|
||||
// Переместите launchOptions сюда, вне компонентов
|
||||
const launchOptions = {
|
||||
downloadUrl:
|
||||
'https://github.com/DIKER0K/Comfort/releases/latest/download/Comfort.zip',
|
||||
apiReleaseUrl: 'https://api.github.com/repos/DIKER0K/Comfort/releases/latest',
|
||||
versionFileName: 'comfort_version.txt',
|
||||
packName: 'Comfort',
|
||||
memory: 4096,
|
||||
baseVersion: '1.21.4',
|
||||
serverIp: 'popa-popa.ru',
|
||||
fabricVersion: 'fabric0.16.14',
|
||||
};
|
||||
|
||||
const AuthCheck = ({ children }: { children: ReactNode }) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean | null>(null);
|
||||
|
||||
@ -66,7 +79,7 @@ const App = () => {
|
||||
path="/"
|
||||
element={
|
||||
<AuthCheck>
|
||||
<LaunchPage />
|
||||
<LaunchPage launchOptions={launchOptions} />
|
||||
</AuthCheck>
|
||||
}
|
||||
/>
|
||||
|
121
src/renderer/components/ServerStatus/ServerStatus.tsx
Normal file
121
src/renderer/components/ServerStatus/ServerStatus.tsx
Normal file
@ -0,0 +1,121 @@
|
||||
import { Box, Typography, CircularProgress, Avatar } from '@mui/material';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
interface ServerStatusProps {
|
||||
serverIp: string;
|
||||
serverPort?: number;
|
||||
refreshInterval?: number; // Интервал обновления в миллисекундах
|
||||
}
|
||||
|
||||
const ServerStatus = ({
|
||||
serverIp,
|
||||
serverPort,
|
||||
refreshInterval = 60000, // По умолчанию обновление раз в минуту
|
||||
}: ServerStatusProps) => {
|
||||
const [serverStatus, setServerStatus] = useState<{
|
||||
online: number;
|
||||
max: number;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
icon: string | null;
|
||||
motd: string;
|
||||
}>({
|
||||
online: 0,
|
||||
max: 0,
|
||||
loading: true,
|
||||
error: null,
|
||||
icon: null,
|
||||
motd: '',
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
// Функция для получения статуса сервера
|
||||
const fetchServerStatus = async () => {
|
||||
try {
|
||||
setServerStatus((prev) => ({ ...prev, loading: true, error: null }));
|
||||
console.log('Отправляем запрос на сервер с параметрами:', {
|
||||
host: serverIp,
|
||||
port: serverPort || 25565,
|
||||
});
|
||||
|
||||
// Проверяем, что serverIp имеет значение
|
||||
if (!serverIp) {
|
||||
throw new Error('Адрес сервера не указан');
|
||||
}
|
||||
|
||||
const result = await window.electron.ipcRenderer.invoke(
|
||||
'get-server-status',
|
||||
{
|
||||
host: serverIp,
|
||||
port: serverPort || 25565,
|
||||
},
|
||||
);
|
||||
|
||||
if (result.success) {
|
||||
setServerStatus({
|
||||
online: result.online,
|
||||
max: result.max,
|
||||
loading: false,
|
||||
error: null,
|
||||
icon: result.icon,
|
||||
motd: result.motd || serverIp,
|
||||
});
|
||||
} else {
|
||||
setServerStatus({
|
||||
online: 0,
|
||||
max: 0,
|
||||
loading: false,
|
||||
error: result.error || 'Неизвестная ошибка',
|
||||
icon: null,
|
||||
motd: '',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Ошибка при получении статуса сервера:', error);
|
||||
setServerStatus((prev) => ({
|
||||
...prev,
|
||||
loading: false,
|
||||
error: 'Ошибка при получении статуса сервера',
|
||||
icon: null,
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// Загрузка при первом рендере
|
||||
fetchServerStatus();
|
||||
|
||||
// Периодическое обновление
|
||||
const interval = setInterval(fetchServerStatus, refreshInterval);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [serverIp, serverPort, refreshInterval]);
|
||||
|
||||
return (
|
||||
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
|
||||
{/* Отображаем иконку сервера или иконку по умолчанию */}
|
||||
{serverStatus.icon ? (
|
||||
<Avatar
|
||||
src={serverStatus.icon}
|
||||
alt={serverStatus.motd || 'Minecraft сервер'}
|
||||
sx={{ width: '2em', height: '2em' }}
|
||||
/>
|
||||
) : (
|
||||
<Avatar sx={{ width: '2em', height: '2em', bgcolor: 'primary.main' }}>
|
||||
?
|
||||
</Avatar>
|
||||
)}
|
||||
|
||||
{serverStatus.loading ? (
|
||||
<CircularProgress size={20} />
|
||||
) : serverStatus.error ? (
|
||||
<Typography color="error">Ошибка загрузки</Typography>
|
||||
) : (
|
||||
<Typography sx={{ fontWeight: 'bold' }}>
|
||||
{serverStatus.online} / {serverStatus.max} игроков
|
||||
</Typography>
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServerStatus;
|
@ -8,6 +8,7 @@ import {
|
||||
} from '@mui/material';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import ServerStatus from '../components/ServerStatus/ServerStatus';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@ -21,19 +22,21 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
const launchOptions = {
|
||||
downloadUrl:
|
||||
'https://github.com/DIKER0K/Comfort/releases/latest/download/Comfort.zip',
|
||||
apiReleaseUrl: 'https://api.github.com/repos/DIKER0K/Comfort/releases/latest',
|
||||
versionFileName: 'comfort_version.txt',
|
||||
packName: 'Comfort',
|
||||
memory: 4096,
|
||||
baseVersion: '1.21.4',
|
||||
serverIp: 'popa-popa.ru',
|
||||
fabricVersion: 'fabric0.16.14',
|
||||
};
|
||||
// Определяем тип для props
|
||||
interface LaunchPageProps {
|
||||
launchOptions: {
|
||||
downloadUrl: string;
|
||||
apiReleaseUrl: string;
|
||||
versionFileName: string;
|
||||
packName: string;
|
||||
memory: number;
|
||||
baseVersion: string;
|
||||
serverIp: string;
|
||||
fabricVersion: string;
|
||||
};
|
||||
}
|
||||
|
||||
const LaunchPage = (launchOptions: any) => {
|
||||
const LaunchPage = ({ launchOptions }: LaunchPageProps) => {
|
||||
const navigate = useNavigate();
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const [downloadProgress, setDownloadProgress] = useState(0);
|
||||
@ -181,11 +184,15 @@ const LaunchPage = (launchOptions: any) => {
|
||||
Добро пожаловать в лаунчер
|
||||
</Typography>
|
||||
|
||||
<Box sx={{ mb: 3 }}>
|
||||
<ServerStatus
|
||||
serverIp={launchOptions.serverIp}
|
||||
refreshInterval={30000}
|
||||
/>
|
||||
</Box>
|
||||
|
||||
{isDownloading ? (
|
||||
<Box sx={{ mb: 3, width: '100%' }}>
|
||||
{/* <Typography sx={{ mb: 1 }}>
|
||||
Загрузка и установка: {downloadProgress}%
|
||||
</Typography> */}
|
||||
<Box sx={{ display: 'flex', alignItems: 'center' }}>
|
||||
<Box sx={{ width: '100%', mr: 1 }}>
|
||||
<LinearProgress
|
||||
@ -201,16 +208,6 @@ const LaunchPage = (launchOptions: any) => {
|
||||
>{`${Math.round(downloadProgress)}%`}</Typography>
|
||||
</Box>
|
||||
</Box>
|
||||
{/* {installMessage && (
|
||||
<Typography variant="body1" sx={{ mt: 1, color: 'white' }}>
|
||||
{installMessage}
|
||||
</Typography>
|
||||
)}
|
||||
{installStep && (
|
||||
<Typography variant="body2" sx={{ color: 'white' }}>
|
||||
Шаг: {installStep}
|
||||
</Typography>
|
||||
)} */}
|
||||
</Box>
|
||||
) : (
|
||||
<Button
|
||||
|
Reference in New Issue
Block a user