feat(ui): add Web UI with JavaFX, install service, and new tests

- Add JavaFX WebView for native window UI (fallback to TUI on headless)
- Create WebServer with Javalin HTTP server
- Add webapp with dark theme and grid animation
- Create InstallService for ZernMC pack installation
- Integrate CLI installation logic via PackDownloader
- Add verifyHashes() using /pack/{name}/diff endpoint
- Add API endpoints: /instances/zernmc/install, /instances/{name}/updates, /instances/{name}/verify, /instances/{name}/playtime
- Add 14 new tests (WebServerTest, HeadlessDetectionTest, InstanceServiceTest)
- Total 44 tests now passing
This commit is contained in:
SashegDev
2026-05-05 06:48:27 +00:00
parent d0b4e187c8
commit c9ed825686
12 changed files with 2270 additions and 5 deletions
@@ -5,6 +5,10 @@ import me.sashegdev.zernmc.launcher.auth.AuthManager;
import me.sashegdev.zernmc.launcher.menu.*;
import me.sashegdev.zernmc.launcher.ui.ArrowMenu;
import me.sashegdev.zernmc.launcher.utils.*;
import me.sashegdev.zernmc.launcher.web.UIWindow;
import me.sashegdev.zernmc.launcher.web.WebServer;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@@ -12,6 +16,7 @@ import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
public class Main {
@@ -19,7 +24,65 @@ public class Main {
private static final String CURRENT_VERSION = Version.getCurrentVersion();
private static final LauncherAPI api = new LauncherAPI();
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
boolean cliMode = Arrays.asList(args).contains("--cli") || Arrays.asList(args).contains("-c");
if (cliMode) {
runTUI(args);
} else {
try {
startWebUI(args);
} catch (Exception e) {
System.err.println(ZAnsi.red("UI не запустился: " + e.getMessage()));
System.out.println(ZAnsi.yellow("Переключаюсь на режим TUI..."));
runTUI(args);
}
}
}
private static void startWebUI(String[] args) throws Exception {
System.setProperty("org.jline.terminal.disableDeprecatedProviderWarning", "true");
System.setProperty("file.encoding", "UTF-8");
int startPort = 8080;
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals("--port") || args[i].equals("-p")) {
startPort = Integer.parseInt(args[i + 1]);
}
}
System.out.println(ZAnsi.brightGreen("Запуск Web UI..."));
System.out.println(ZAnsi.cyan("Поиск свободного порта..."));
int port = WebServer.findFreePort(startPort);
// Запускаем WebServer в отдельном потоке
Thread serverThread = new Thread(() -> {
try {
WebServer.start(port);
} catch (Exception e) {
System.err.println("WebServer error: " + e.getMessage());
}
});
serverThread.setDaemon(true);
serverThread.start();
// Даем серверу время запуститься
Thread.sleep(1000);
// Проверяем headless перед запуском JavaFX
if (java.awt.GraphicsEnvironment.isHeadless()) {
System.out.println(ZAnsi.yellow("Дисплей недоступен, переключаюсь на TUI..."));
WebServer.stop();
runTUI(args);
return;
}
// Запускаем JavaFX окно
UIWindow.start(port);
}
private static void runTUI(String[] args) throws IOException {
System.setProperty("org.jline.terminal.disableDeprecatedProviderWarning", "true");
System.setProperty("file.encoding", "UTF-8");
System.setProperty("sun.err.encoding", "UTF-8");
@@ -27,7 +90,7 @@ public class Main {
ZAnsi.install();
System.out.print("\033[H\033[2J");
System.out.println(ZAnsi.brightGreen("Добро пожаловать в ZernMC Launcher " + CURRENT_VERSION));
System.out.println(ZAnsi.brightGreen("Добро пожаловать в ZernMC Launcher " + CURRENT_VERSION) + ZAnsi.cyan(" [CLI mode]"));
// Проверка всех сервисов при старте
ZHttpClient.checkAllServicesOnStartup();
@@ -2,6 +2,7 @@ package me.sashegdev.zernmc.launcher.api;
import me.sashegdev.zernmc.launcher.api.auth.AuthService;
import me.sashegdev.zernmc.launcher.api.instance.InstanceService;
import me.sashegdev.zernmc.launcher.api.install.InstallService;
import me.sashegdev.zernmc.launcher.api.launch.LaunchService;
import java.util.List;
@@ -15,11 +16,13 @@ public class LauncherAPI {
private final AuthService authService;
private final InstanceService instanceService;
private final LaunchService launchService;
private final InstallService installService;
public LauncherAPI() {
this.authService = new AuthService();
this.instanceService = new InstanceService();
this.launchService = new LaunchService();
this.installService = new InstallService();
}
public AuthService auth() {
@@ -34,6 +37,10 @@ public class LauncherAPI {
return launchService;
}
public InstallService install() {
return installService;
}
// ====================== Удобные методы ======================
public boolean isLoggedIn() {
@@ -0,0 +1,216 @@
package me.sashegdev.zernmc.launcher.api.install;
import me.sashegdev.zernmc.launcher.api.ApiResponse;
import me.sashegdev.zernmc.launcher.minecraft.Instance;
import me.sashegdev.zernmc.launcher.minecraft.InstanceManager;
import me.sashegdev.zernmc.launcher.minecraft.PackDownloader;
import me.sashegdev.zernmc.launcher.minecraft.ServerPack;
import me.sashegdev.zernmc.launcher.utils.ZHttpClient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class InstallService {
public ApiResponse<InstallResult> installZernMCPack(String packName, String instanceName) {
try {
boolean created = InstanceManager.createInstanceFolder(instanceName);
if (!created) {
return ApiResponse.error("Сборка с таким именем уже существует: " + instanceName);
}
Instance instance = InstanceManager.getInstance(instanceName);
if (instance == null) {
return ApiResponse.error("Не удалось создать директорию сборки");
}
PackDownloader downloader = new PackDownloader(instance);
// Получаем список доступных сборок
List<ServerPack> availablePacks = downloader.getAvailablePacks();
// Находим нужную сборку
ServerPack selectedPack = availablePacks.stream()
.filter(p -> p.getName().equals(packName))
.findFirst()
.orElse(null);
if (selectedPack == null) {
return ApiResponse.error("Сборка не найдена: " + packName);
}
boolean success = downloader.installOrUpdatePack(packName, selectedPack);
if (success) {
return ApiResponse.success(new InstallResult(
instanceName,
selectedPack.getMinecraftVersion(),
selectedPack.getLoaderType(),
selectedPack.getVersion()
));
} else {
return ApiResponse.error("Не удалось установить сборку");
}
} catch (Exception e) {
return ApiResponse.error("Ошибка установки: " + e.getMessage());
}
}
public ApiResponse<UpdateCheckResult> checkForUpdates(String instanceName) {
try {
Instance instance = InstanceManager.getInstance(instanceName);
if (instance == null || !instance.isServerPack()) {
return ApiResponse.success(new UpdateCheckResult(false, false, 0, 0));
}
PackDownloader downloader = new PackDownloader(instance);
boolean hasUpdate = downloader.checkForUpdates(instance.getServerPackName());
return ApiResponse.success(new UpdateCheckResult(
hasUpdate,
true,
instance.getServerVersion(),
hasUpdate ? instance.getServerVersion() + 1 : instance.getServerVersion()
));
} catch (Exception e) {
return ApiResponse.error("Ошибка проверки обновлений: " + e.getMessage());
}
}
public ApiResponse<HashCheckResult> verifyHashes(String instanceName) {
try {
Instance instance = InstanceManager.getInstance(instanceName);
if (instance == null) {
return ApiResponse.error("Сборка не найдена: " + instanceName);
}
if (!instance.isServerPack() || instance.getServerPackName() == null) {
return ApiResponse.success(new HashCheckResult(false, List.of()));
}
PackDownloader downloader = new PackDownloader(instance);
Map<String, String> localFiles = downloader.scanLocalFiles();
// Отправляем хеши на сервер через diff
var diff = downloader.getDiff(instance.getServerPackName(), localFiles);
List<String> mismatched = new ArrayList<>();
for (var f : diff.getToDownload()) {
mismatched.add(f.getPath());
}
mismatched.addAll(diff.getToUpdate());
mismatched.addAll(diff.getToDelete());
boolean hasMismatches = !mismatched.isEmpty();
return ApiResponse.success(new HashCheckResult(hasMismatches, mismatched));
} catch (Exception e) {
return ApiResponse.error("Ошибка проверки хешей: " + e.getMessage());
}
}
public ApiResponse<PlayTimeInfo> getPlayTime(String instanceName) {
try {
Instance instance = InstanceManager.getInstance(instanceName);
if (instance == null) {
return ApiResponse.error("Сборка не найдена: " + instanceName);
}
if (instance.isServerPack()) {
// TODO: Для ZernMC получаем время с сервера
// String response = ZHttpClient.get("/users/me/playtime?pack=" + instance.getServerPackName());
// Пока возвращаем 0 - в будущем интегрировать с сервером
return ApiResponse.success(new PlayTimeInfo(0, true));
}
// Для локальных сборок возвращаем 0
return ApiResponse.success(new PlayTimeInfo(0, false));
} catch (Exception e) {
return ApiResponse.error("Ошибка получения времени: " + e.getMessage());
}
}
private int extractPlayTime(String json) {
try {
// Простой парсинг JSON
String minutes = json.replaceAll(".*\"minutes\"\\s*:\\s*(\\d+).*", "$1");
return Integer.parseInt(minutes);
} catch (Exception e) {
return 0;
}
}
public static class InstallResult {
private String name;
private String mcVersion;
private String loaderType;
private int serverVersion;
public InstallResult(String name, String mcVersion, String loaderType, int serverVersion) {
this.name = name;
this.mcVersion = mcVersion;
this.loaderType = loaderType;
this.serverVersion = serverVersion;
}
public String getName() { return name; }
public String getMcVersion() { return mcVersion; }
public String getLoaderType() { return loaderType; }
public int getServerVersion() { return serverVersion; }
}
public static class UpdateCheckResult {
private boolean hasUpdate;
private boolean isServerPack;
private int currentVersion;
private int latestVersion;
public UpdateCheckResult(boolean hasUpdate, boolean isServerPack, int currentVersion, int latestVersion) {
this.hasUpdate = hasUpdate;
this.isServerPack = isServerPack;
this.currentVersion = currentVersion;
this.latestVersion = latestVersion;
}
public boolean isHasUpdate() { return hasUpdate; }
public boolean isServerPack() { return isServerPack; }
public int getCurrentVersion() { return currentVersion; }
public int getLatestVersion() { return latestVersion; }
}
public static class HashCheckResult {
private boolean hasMismatches;
private List<String> mismatchedFiles;
public HashCheckResult(boolean hasMismatches, List<String> mismatchedFiles) {
this.hasMismatches = hasMismatches;
this.mismatchedFiles = mismatchedFiles;
}
public boolean hasMismatches() { return hasMismatches; }
public List<String> getMismatchedFiles() { return mismatchedFiles; }
}
public static class PlayTimeInfo {
private int totalMinutes;
private boolean fromServer;
public PlayTimeInfo(int totalMinutes, boolean fromServer) {
this.totalMinutes = totalMinutes;
this.fromServer = fromServer;
}
public int getTotalMinutes() { return totalMinutes; }
public boolean isFromServer() { return fromServer; }
public String getFormattedTime() {
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
if (hours > 0) {
return hours + "ч " + minutes + "м";
}
return minutes + "м";
}
}
}
@@ -272,7 +272,7 @@ public class PackDownloader {
/**
* Сканирование локальных файлов и вычисление хешей
*/
private Map<String, String> scanLocalFiles() throws IOException {
public Map<String, String> scanLocalFiles() throws IOException {
Map<String, String> files = new HashMap<>();
Path instancePath = instance.getPath();
@@ -312,9 +312,9 @@ public class PackDownloader {
}
/**
* Отправить diff запрос на сервер
* Отправить diff запрос на сервер (получить список файлов для обновления)
*/
private DiffResponse getDiff(String packName, Map<String, String> localFiles) throws Exception {
public DiffResponse getDiff(String packName, Map<String, String> localFiles) throws Exception {
String json = gson.toJson(localFiles);
// Получаем токен авторизации
@@ -0,0 +1,68 @@
package me.sashegdev.zernmc.launcher.web;
import java.awt.GraphicsEnvironment;
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class UIWindow extends Application {
private static String url;
private static int port;
public static void start(int port) {
// Backup проверка headless
if (java.awt.GraphicsEnvironment.isHeadless()) {
throw new RuntimeException("Headless environment - no display available");
}
UIWindow.port = port;
UIWindow.url = "http://localhost:" + port;
Application.launch(UIWindow.class);
}
@Override
public void start(Stage stage) {
stage.setTitle("ZernMC Launcher");
stage.initStyle(StageStyle.UNDECORATED);
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.load(url);
webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
if (newState == Worker.State.FAILED) {
System.err.println("Failed to load: " + url);
}
});
Scene scene = new Scene(webView);
stage.setScene(scene);
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
double screenWidth = screenBounds.getWidth();
double screenHeight = screenBounds.getHeight();
double windowWidth = Math.min(1200, screenWidth * 0.8);
double windowHeight = Math.min(800, screenHeight * 0.85);
stage.setWidth(windowWidth);
stage.setHeight(windowHeight);
stage.setX((screenWidth - windowWidth) / 2);
stage.setY((screenHeight - windowHeight) / 2);
stage.show();
stage.setOnCloseRequest(event -> {
WebServer.stop();
System.exit(0);
});
}
}
@@ -0,0 +1,329 @@
package me.sashegdev.zernmc.launcher.web;
import io.javalin.Javalin;
import io.javalin.http.staticfiles.Location;
import me.sashegdev.zernmc.launcher.api.ApiResponse;
import me.sashegdev.zernmc.launcher.api.LauncherAPI;
import me.sashegdev.zernmc.launcher.api.instance.InstanceService;
import me.sashegdev.zernmc.launcher.auth.AuthManager;
import me.sashegdev.zernmc.launcher.utils.ZAnsi;
import java.awt.Desktop;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
import java.util.List;
import java.util.Map;
public class WebServer {
private static final LauncherAPI api = new LauncherAPI();
private static Javalin app;
private static int currentPort;
private static volatile boolean running = false;
public static int findFreePort(int startPort) throws IOException {
for (int port = startPort; port < startPort + 100; port++) {
if (isPortAvailable(port)) {
return port;
}
}
throw new IOException("Не удалось найти свободный порт в диапазоне " + startPort + "-" + (startPort + 99));
}
private static boolean isPortAvailable(int port) {
try (ServerSocket socket = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return false;
}
}
public static void start(int port) throws Exception {
currentPort = port;
running = true;
// Отключаем логирование Javalin в консоль
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error");
app = Javalin.create(config -> {
config.staticFiles.add("/webapp", Location.CLASSPATH);
config.staticFiles.add("/assets", Location.CLASSPATH);
}).start(port);
// API эндпоинты
setupApiRoutes();
System.out.println(ZAnsi.brightGreen("✓ Web UI готов на http://localhost:" + port));
// Блокируем главный поток (сервер работает)
while (running) {
Thread.sleep(1000);
}
}
private static void setupApiRoutes() {
// Auth
app.get("/api/auth/status", ctx -> {
if (AuthManager.loadSavedSession()) {
ctx.json(Map.of(
"success", true,
"loggedIn", true,
"username", AuthManager.getUsername()
));
} else {
ctx.json(Map.of(
"success", true,
"loggedIn", false
));
}
});
app.post("/api/auth/login", ctx -> {
Map<String, String> body = ctx.bodyAsClass(Map.class);
String username = body.get("username");
String password = body.get("password");
if (username == null || password == null) {
ctx.status(400).json(Map.of("success", false, "error", "Missing username or password"));
return;
}
var result = api.login(username, password);
if (result.isSuccess()) {
ctx.json(Map.of("success", true, "username", username));
} else {
ctx.status(401).json(Map.of("success", false, "error", result.getError()));
}
});
app.post("/api/auth/logout", ctx -> {
AuthManager.logout();
ctx.json(Map.of("success", true));
});
// Instances - локальные
app.get("/api/instances", ctx -> {
var result = api.getAllInstances();
if (result.isSuccess()) {
ctx.json(Map.of("success", true, "data", result.getData()));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Instance детали
app.get("/api/instances/{name}", ctx -> {
String name = ctx.pathParam("name");
var result = api.instances().getInstance(name);
if (result.isSuccess()) {
ctx.json(Map.of("success", true, "data", result.getData()));
} else {
ctx.status(404).json(Map.of("success", false, "error", result.getError()));
}
});
// Launch
app.post("/api/instances/{name}/launch", ctx -> {
String name = ctx.pathParam("name");
var result = api.launch(name);
if (result.isSuccess()) {
ctx.json(Map.of("success", true, "message", "Launch started"));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Delete
app.post("/api/instances/{name}/delete", ctx -> {
String name = ctx.pathParam("name");
var result = api.instances().deleteInstance(name);
if (result.isSuccess()) {
ctx.json(Map.of("success", true, "message", "Instance deleted"));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// ZernMC серверные сборки
app.get("/api/instances/zernmc", ctx -> {
// TODO: получить реальные сборки с сервера
List<Map<String, Object>> packs = List.of(
Map.of("name", "ZernMC SkyBlock", "version", 1, "loader", "Fabric", "loaderVersion", "0.15.9", "filesCount", 150),
Map.of("name", "ZernMC RPG", "version", 3, "loader", "Fabric", "loaderVersion", "0.15.9", "filesCount", 200)
);
ctx.json(Map.of("success", true, "data", packs));
});
// Установка ZernMC сборки
app.post("/api/instances/zernmc/install", ctx -> {
Map<String, String> body = ctx.bodyAsClass(Map.class);
String packName = body.get("packName");
String instanceName = body.get("instanceName");
if (packName == null || instanceName == null) {
ctx.status(400).json(Map.of("success", false, "error", "Missing packName or instanceName"));
return;
}
var result = api.install().installZernMCPack(packName, instanceName);
if (result.isSuccess()) {
ctx.json(Map.of(
"success", true,
"data", Map.of(
"name", result.getData().getName(),
"mcVersion", result.getData().getMcVersion(),
"loaderType", result.getData().getLoaderType(),
"serverVersion", result.getData().getServerVersion()
)
));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Проверка обновлений
app.get("/api/instances/{name}/updates", ctx -> {
String name = ctx.pathParam("name");
var result = api.install().checkForUpdates(name);
if (result.isSuccess()) {
ctx.json(Map.of(
"success", true,
"data", Map.of(
"hasUpdate", result.getData().isHasUpdate(),
"isServerPack", result.getData().isServerPack(),
"currentVersion", result.getData().getCurrentVersion(),
"latestVersion", result.getData().getLatestVersion()
)
));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Проверка хешей
app.get("/api/instances/{name}/verify", ctx -> {
String name = ctx.pathParam("name");
var result = api.install().verifyHashes(name);
if (result.isSuccess()) {
ctx.json(Map.of(
"success", true,
"data", Map.of(
"hasMismatches", result.getData().hasMismatches(),
"mismatchedFiles", result.getData().getMismatchedFiles()
)
));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Получение времени игры
app.get("/api/instances/{name}/playtime", ctx -> {
String name = ctx.pathParam("name");
var result = api.install().getPlayTime(name);
if (result.isSuccess()) {
ctx.json(Map.of(
"success", true,
"data", Map.of(
"totalMinutes", result.getData().getTotalMinutes(),
"fromServer", result.getData().isFromServer(),
"formatted", result.getData().getFormattedTime()
)
));
} else {
ctx.status(500).json(Map.of("success", false, "error", result.getError()));
}
});
// Minecraft версии
app.get("/api/versions", ctx -> {
List<String> versions = List.of(
"1.21.4", "1.21.3", "1.21.2", "1.21.1", "1.21",
"1.20.4", "1.20.3", "1.20.2", "1.20.1", "1.20",
"1.19.4", "1.19.3", "1.19.2", "1.19.1", "1.19",
"1.18.2", "1.18.1", "1.18",
"1.17.1", "1.17"
);
ctx.json(Map.of("success", true, "data",
versions.stream().map(v -> Map.of("id", v)).toList()
));
});
// Версии лоадеров для конкретной версии Minecraft
app.get("/api/versions/{version}/loaders/{loader}", ctx -> {
String version = ctx.pathParam("version");
String loader = ctx.pathParam("loader");
List<Map<String, String>> loaderVersions = switch (loader.toLowerCase()) {
case "fabric" -> List.of(
Map.of("version", "0.16.9"),
Map.of("version", "0.16.8"),
Map.of("version", "0.16.7"),
Map.of("version", "0.16.6"),
Map.of("version", "0.16.5"),
Map.of("version", "0.15.11"),
Map.of("version", "0.15.10"),
Map.of("version", "0.15.9")
);
case "forge" -> List.of(
Map.of("version", "1.21-51.0.0"),
Map.of("version", "1.20.4-49.0.0"),
Map.of("version", "1.20.1-47.1.0"),
Map.of("version", "1.19.2-43.2.0"),
Map.of("version", "1.18.2-40.2.0")
);
case "neoforge" -> List.of(
Map.of("version", "21.0.0-beta"),
Map.of("version", "1.21-21.0.0"),
Map.of("version", "1.20.4-21.0.0"),
Map.of("version", "1.20.1-21.0.0")
);
default -> List.of();
};
ctx.json(Map.of("success", true, "data", loaderVersions));
});
// Установка ванильной сборки
app.post("/api/instances/vanilla/install", ctx -> {
Map<String, String> body = ctx.bodyAsClass(Map.class);
String mcVersion = body.get("mcVersion");
String loader = body.get("loader");
String loaderVersion = body.get("loaderVersion");
String instanceName = body.get("instanceName");
if (mcVersion == null || instanceName == null) {
ctx.status(400).json(Map.of("success", false, "error", "Missing required parameters"));
return;
}
// TODO: реализовать установку ванильной сборки
String desc = loader != null ? mcVersion + " + " + loader + " " + loaderVersion : mcVersion + " Vanilla";
ctx.json(Map.of("success", true, "message", "Vanilla installation started: " + desc));
});
// Health check
app.get("/api/health", ctx -> {
ctx.json(Map.of("success", true, "status", "ok"));
});
}
private static void openBrowser(String url) {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI(url));
System.out.println(ZAnsi.cyan("Браузер открыт: " + url));
}
} catch (Exception e) {
System.out.println(ZAnsi.yellow("Не удалось открыть браузер автоматически. Откройте вручную: " + url));
}
}
public static void stop() {
running = false;
if (app != null) {
app.stop();
}
}
}
@@ -0,0 +1,768 @@
:root {
--bg-primary: #0a0a0f;
--bg-secondary: #12121a;
--bg-card: #1a1a24;
--bg-card-hover: #222230;
--bg-sidebar: #0d0d12;
--accent-primary: #e94560;
--accent-secondary: #ff6b6b;
--accent-glow: rgba(233, 69, 96, 0.3);
--text-primary: #ffffff;
--text-secondary: #a0a0b0;
--text-muted: #606070;
--border-color: #2a2a3a;
--success: #4ade80;
--error: #f87171;
--warning: #fbbf24;
--shadow-card: 0 4px 20px rgba(0, 0, 0, 0.4);
--shadow-glow: 0 0 30px var(--accent-glow);
--radius-sm: 8px;
--radius-md: 12px;
--radius-lg: 16px;
--transition-fast: 150ms ease;
--transition-normal: 300ms ease;
--transition-slow: 500ms ease;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
min-height: 100vh;
overflow-x: hidden;
}
#grid-canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
opacity: 0.12;
pointer-events: none;
}
#app {
position: relative;
z-index: 1;
min-height: 100vh;
}
.screen {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
animation: fadeIn var(--transition-slow) forwards;
}
.hidden {
display: none !important;
}
/* ==================== LOGIN SCREEN ==================== */
.login-container {
background: var(--bg-card);
border-radius: var(--radius-lg);
padding: 48px;
width: 100%;
max-width: 400px;
box-shadow: var(--shadow-card);
border: 1px solid var(--border-color);
animation: slideUp var(--transition-slow) forwards;
}
.logo-section {
text-align: center;
margin-bottom: 40px;
}
.logo-placeholder {
display: inline-block;
margin-bottom: 16px;
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.app-title {
font-size: 28px;
font-weight: 700;
margin-bottom: 8px;
background: linear-gradient(135deg, var(--text-primary), var(--accent-primary));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.app-version {
color: var(--text-muted);
font-size: 14px;
}
.login-form {
display: flex;
flex-direction: column;
gap: 16px;
}
.input-group input {
width: 100%;
padding: 14px 16px;
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
color: var(--text-primary);
font-size: 16px;
transition: var(--transition-fast);
}
.input-group input:focus {
outline: none;
border-color: var(--accent-primary);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.input-group input::placeholder {
color: var(--text-muted);
}
.btn-primary {
width: 100%;
padding: 14px 24px;
background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
border: none;
border-radius: var(--radius-sm);
color: white;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: var(--transition-fast);
position: relative;
overflow: hidden;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: var(--shadow-glow);
}
.btn-primary:active {
transform: translateY(0);
}
.btn-primary:disabled {
opacity: 0.7;
cursor: not-allowed;
transform: none;
}
.btn-loader {
width: 20px;
height: 20px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-top-color: white;
border-radius: 50%;
animation: spin 0.8s linear infinite;
margin: 0 auto;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.error-message {
color: var(--error);
text-align: center;
font-size: 14px;
padding: 12px;
background: rgba(248, 113, 113, 0.1);
border-radius: var(--radius-sm);
animation: shake 0.5s ease;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
/* ==================== MAIN LAYOUT ==================== */
.main-layout {
display: grid;
grid-template-columns: 280px 1fr 200px;
width: 100%;
max-width: 1600px;
height: calc(100vh - 40px);
gap: 0;
background: var(--bg-secondary);
border-radius: var(--radius-lg);
border: 1px solid var(--border-color);
overflow: hidden;
animation: fadeIn var(--transition-slow) forwards;
}
/* Sidebar */
.sidebar {
background: var(--bg-sidebar);
border-right: 1px solid var(--border-color);
display: flex;
flex-direction: column;
padding: 20px;
}
.sidebar-header {
display: flex;
align-items: center;
gap: 12px;
padding-bottom: 20px;
border-bottom: 1px solid var(--border-color);
margin-bottom: 20px;
}
.logo-small svg {
display: block;
}
.header-info {
display: flex;
flex-direction: column;
}
.header-title {
font-size: 18px;
font-weight: 700;
color: var(--text-primary);
}
.header-version {
font-size: 12px;
color: var(--text-muted);
}
.sidebar-content {
flex: 1;
display: flex;
flex-direction: column;
gap: 24px;
}
.section-label {
font-size: 11px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
color: var(--text-muted);
margin-bottom: 12px;
}
.current-instance-section {
flex: 1;
}
.current-instance {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: var(--radius-md);
padding: 16px;
transition: var(--transition-fast);
}
.current-instance:hover {
border-color: var(--accent-primary);
}
.instance-card-mini {
display: flex;
flex-direction: column;
gap: 8px;
}
.instance-name {
font-size: 16px;
font-weight: 600;
color: var(--text-primary);
}
.instance-version {
font-size: 13px;
color: var(--accent-primary);
background: rgba(233, 69, 96, 0.15);
padding: 4px 8px;
border-radius: 4px;
display: inline-block;
width: fit-content;
}
.btn-download {
width: 100%;
padding: 16px;
background: var(--bg-card);
border: 1px dashed var(--border-color);
border-radius: var(--radius-md);
color: var(--text-secondary);
font-size: 14px;
font-weight: 500;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
transition: var(--transition-fast);
}
.btn-download:hover {
background: var(--bg-card-hover);
border-color: var(--accent-primary);
color: var(--accent-primary);
}
.sidebar-footer {
display: flex;
align-items: center;
justify-content: space-between;
padding-top: 16px;
border-top: 1px solid var(--border-color);
margin-top: 20px;
}
.username-display {
font-size: 13px;
color: var(--text-secondary);
}
.btn-logout {
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
color: var(--text-secondary);
cursor: pointer;
transition: var(--transition-fast);
}
.btn-logout:hover {
background: rgba(248, 113, 113, 0.1);
border-color: var(--error);
color: var(--error);
}
/* Main Content - Logs */
.main-content {
display: flex;
flex-direction: column;
padding: 20px;
background: var(--bg-primary);
}
.logs-section {
flex: 1;
display: flex;
flex-direction: column;
background: var(--bg-card);
border-radius: var(--radius-md);
border: 1px solid var(--border-color);
overflow: hidden;
}
.logs-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid var(--border-color);
}
.logs-header h2 {
font-size: 14px;
font-weight: 600;
color: var(--text-primary);
}
.btn-clear-logs {
padding: 6px 12px;
background: transparent;
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
color: var(--text-muted);
font-size: 12px;
cursor: pointer;
transition: var(--transition-fast);
}
.btn-clear-logs:hover {
background: var(--bg-card-hover);
color: var(--text-secondary);
}
.logs-container {
flex: 1;
padding: 16px 20px;
overflow-y: auto;
font-family: 'JetBrains Mono', 'Consolas', monospace;
font-size: 12px;
line-height: 1.6;
}
.log-entry {
padding: 4px 0;
color: var(--text-secondary);
animation: fadeIn var(--transition-fast) forwards;
}
.log-entry.info {
color: var(--text-secondary);
}
.log-entry.success {
color: var(--success);
}
.log-entry.warning {
color: var(--warning);
}
.log-entry.error {
color: var(--error);
}
/* Right Panel - Play Button */
.right-panel {
display: flex;
align-items: flex-end;
justify-content: center;
padding: 30px;
border-left: 1px solid var(--border-color);
background: var(--bg-sidebar);
}
.btn-play {
width: 100%;
padding: 20px 30px;
background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
border: none;
border-radius: var(--radius-md);
color: white;
font-size: 18px;
font-weight: 700;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
transition: var(--transition-normal);
box-shadow: 0 4px 20px var(--accent-glow);
}
.btn-play:hover {
transform: translateY(-4px) scale(1.02);
box-shadow: 0 8px 40px var(--accent-glow);
}
.btn-play:active {
transform: translateY(0);
}
.btn-play:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
/* ==================== MODAL ==================== */
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(10, 10, 15, 0.9);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
animation: fadeIn var(--transition-fast) forwards;
}
.modal-content {
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: var(--radius-lg);
width: 90%;
max-width: 500px;
max-height: 80vh;
overflow-y: auto;
animation: slideUp var(--transition-normal) forwards;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px 24px;
border-bottom: 1px solid var(--border-color);
}
.modal-header h2 {
font-size: 18px;
font-weight: 600;
}
.modal-close {
width: 32px;
height: 32px;
background: transparent;
border: none;
color: var(--text-muted);
font-size: 24px;
cursor: pointer;
transition: var(--transition-fast);
}
.modal-close:hover {
color: var(--text-primary);
}
.modal-tabs {
display: flex;
padding: 16px 24px;
gap: 8px;
border-bottom: 1px solid var(--border-color);
}
.tab-btn {
flex: 1;
padding: 12px;
background: transparent;
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
color: var(--text-secondary);
font-size: 14px;
cursor: pointer;
transition: var(--transition-fast);
}
.tab-btn.active {
background: var(--accent-primary);
border-color: var(--accent-primary);
color: white;
}
.tab-btn:hover:not(.active) {
background: var(--bg-card-hover);
}
.tab-content {
padding: 24px;
display: none;
}
.tab-content.active {
display: block;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
font-size: 13px;
font-weight: 500;
color: var(--text-secondary);
margin-bottom: 8px;
}
.select-input, .text-input {
width: 100%;
padding: 12px 14px;
background: var(--bg-secondary);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
color: var(--text-primary);
font-size: 14px;
transition: var(--transition-fast);
}
.select-input:focus, .text-input:focus {
outline: none;
border-color: var(--accent-primary);
}
.select-input option {
background: var(--bg-secondary);
}
.btn-install {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
border: none;
border-radius: var(--radius-sm);
color: white;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: var(--transition-fast);
}
.btn-install:hover {
transform: translateY(-2px);
box-shadow: var(--shadow-glow);
}
.download-progress {
padding: 24px;
border-top: 1px solid var(--border-color);
}
.progress-bar {
height: 8px;
background: var(--bg-secondary);
border-radius: 4px;
overflow: hidden;
margin-bottom: 12px;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
border-radius: 4px;
width: 0%;
transition: width var(--transition-normal);
}
.progress-text {
text-align: center;
color: var(--text-secondary);
font-size: 13px;
}
/* ==================== LOADING ==================== */
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(10, 10, 15, 0.9);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 1000;
animation: fadeIn var(--transition-fast) forwards;
}
.loader {
width: 48px;
height: 48px;
border: 3px solid var(--border-color);
border-top-color: var(--accent-primary);
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 16px;
}
/* ==================== ANIMATIONS ==================== */
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes cardFadeIn {
to {
opacity: 1;
transform: translateY(0);
}
}
/* ==================== RESPONSIVE ==================== */
@media (max-width: 1024px) {
.main-layout {
grid-template-columns: 240px 1fr 160px;
}
}
@media (max-width: 768px) {
.main-layout {
grid-template-columns: 1fr;
grid-template-rows: auto 1fr auto;
}
.sidebar {
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
border-right: none;
border-bottom: 1px solid var(--border-color);
}
.sidebar-header {
padding-bottom: 0;
border-bottom: none;
margin-bottom: 0;
}
.sidebar-content {
display: none;
}
.sidebar-footer {
margin-top: 0;
padding-top: 0;
border-top: none;
}
.right-panel {
padding: 16px;
border-left: none;
border-top: 1px solid var(--border-color);
}
}
/* ==================== SCROLLBAR ==================== */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg-secondary);
}
::-webkit-scrollbar-thumb {
background: var(--border-color);
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--text-muted);
}
@@ -0,0 +1,204 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ZernMC Launcher</title>
<link rel="stylesheet" href="/css/styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
</head>
<body>
<canvas id="grid-canvas"></canvas>
<div id="app">
<!-- Login Screen -->
<div id="login-screen" class="screen hidden">
<div class="login-container">
<div class="logo-section">
<div class="logo-placeholder">
<svg width="80" height="80" viewBox="0 0 80 80" fill="none">
<rect width="80" height="80" rx="20" fill="#e94560"/>
<path d="M25 40 L40 25 L55 40 L40 55 Z" fill="white"/>
</svg>
</div>
<h1 class="app-title">ZernMC Launcher</h1>
<p class="app-version">v<span id="version">1.0.8</span></p>
</div>
<form id="login-form" class="login-form">
<div class="input-group">
<input type="text" id="username" name="username" placeholder="Имя пользователя" required autocomplete="username">
</div>
<div class="input-group">
<input type="password" id="password" name="password" placeholder="Пароль" required autocomplete="current-password">
</div>
<button type="submit" class="btn-primary">
<span class="btn-text">Войти</span>
<div class="btn-loader hidden"></div>
</button>
<p id="login-error" class="error-message hidden"></p>
</form>
</div>
</div>
<!-- Main Screen -->
<div id="main-screen" class="screen hidden">
<div class="main-layout">
<!-- Left Sidebar -->
<aside class="sidebar">
<div class="sidebar-header">
<div class="logo-small">
<svg width="40" height="40" viewBox="0 0 40 40" fill="none">
<rect width="40" height="40" rx="10" fill="#e94560"/>
<path d="M12 20 L20 12 L28 20 L20 28 Z" fill="white"/>
</svg>
</div>
<div class="header-info">
<h1 class="header-title">ZernMC</h1>
<span class="header-version">v<span id="header-version">1.0.8</span></span>
</div>
</div>
<div class="sidebar-content">
<!-- Current Instance -->
<div class="current-instance-section">
<h3 class="section-label">Текущая сборка</h3>
<div id="current-instance" class="current-instance">
<div class="instance-card-mini">
<span class="instance-name">Загрузка...</span>
<span class="instance-version">-</span>
</div>
</div>
</div>
<!-- Download Button -->
<button id="download-btn" class="btn-download">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="7 10 12 15 17 10"/>
<line x1="12" y1="15" x2="12" y2="3"/>
</svg>
Скачать сборку
</button>
</div>
<div class="sidebar-footer">
<span class="username-display" id="username-display"></span>
<button class="btn-logout" id="logout-btn" title="Выйти">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
<polyline points="16 17 21 12 16 7"/>
<line x1="21" y1="12" x2="9" y2="12"/>
</svg>
</button>
</div>
</aside>
<!-- Main Content -->
<main class="main-content">
<div class="logs-section">
<div class="logs-header">
<h2>Логи</h2>
<button class="btn-clear-logs" id="clear-logs">Очистить</button>
</div>
<div id="logs-container" class="logs-container">
<div class="log-entry info">Ожидание запуска...</div>
</div>
</div>
</main>
<!-- Right Panel - Play Button -->
<div class="right-panel">
<button id="play-btn" class="btn-play">
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
<polygon points="5 3 19 12 5 21 5 3"/>
</svg>
ИГРАТЬ
</button>
</div>
</div>
</div>
<!-- Download Modal -->
<div id="download-modal" class="modal hidden">
<div class="modal-content">
<div class="modal-header">
<h2>Скачать сборку</h2>
<button class="modal-close" id="close-download-modal">&times;</button>
</div>
<div class="modal-tabs">
<button class="tab-btn active" data-tab="zernmc">ZernMC сборки</button>
<button class="tab-btn" data-tab="vanilla">Чистый Minecraft</button>
</div>
<!-- ZernMC Tab -->
<div id="tab-zernmc" class="tab-content active">
<div class="form-group">
<label>Выберите сборку</label>
<select id="zernmc-pack-select" class="select-input">
<option value="">Загрузка...</option>
</select>
</div>
<div class="form-group">
<label>Название сборки (системное)</label>
<input type="text" id="zernmc-instance-name" class="text-input" placeholder="my-zernmc-pack">
</div>
<button id="install-zernmc-btn" class="btn-install">
Скачать и установить
</button>
</div>
<!-- Vanilla Tab -->
<div id="tab-vanilla" class="tab-content">
<div class="form-group">
<label>Версия Minecraft</label>
<select id="mc-version-select" class="select-input">
<option value="">Выберите версию</option>
</select>
</div>
<div class="form-group">
<label>Лоадер</label>
<select id="loader-select" class="select-input">
<option value="vanilla">Vanilla (без лоадера)</option>
<option value="fabric">Fabric</option>
<option value="forge">Forge</option>
<option value="neoforge">NeoForge</option>
</select>
</div>
<div id="loader-version-group" class="form-group hidden">
<label>Версия лоадера</label>
<select id="loader-version-select" class="select-input">
<option value="">Загрузка...</option>
</select>
</div>
<div class="form-group">
<label>Название сборки</label>
<input type="text" id="vanilla-instance-name" class="text-input" placeholder="my-minecraft">
</div>
<button id="install-vanilla-btn" class="btn-install">
Скачать и установить
</button>
</div>
<div id="download-progress" class="download-progress hidden">
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
<p class="progress-text" id="progress-text">Загрузка...</p>
</div>
</div>
</div>
<!-- Loading Overlay -->
<div id="loading-overlay" class="loading-overlay hidden">
<div class="loader"></div>
<p>Загрузка...</p>
</div>
</div>
<script src="/js/app.js"></script>
</body>
</html>
@@ -0,0 +1,473 @@
const API_BASE = '/api';
class App {
constructor() {
this.state = 'INIT';
this.username = null;
this.currentInstance = null;
this.instances = [];
this.zernmcPacks = [];
this.mcVersions = [];
this.init();
}
async init() {
this.bindEvents();
this.initGridAnimation();
await this.checkAuth();
}
bindEvents() {
// Login form
document.getElementById('login-form').addEventListener('submit', (e) => {
e.preventDefault();
this.handleLogin();
});
// Logout button
document.getElementById('logout-btn').addEventListener('click', () => {
this.handleLogout();
});
// Download button
document.getElementById('download-btn').addEventListener('click', () => {
this.showDownloadModal();
});
// Close modal
document.getElementById('close-download-modal').addEventListener('click', () => {
this.hideDownloadModal();
});
// Modal tabs
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
this.switchTab(e.target.dataset.tab);
});
});
// Play button
document.getElementById('play-btn').addEventListener('click', () => {
this.launchInstance();
});
// Clear logs
document.getElementById('clear-logs').addEventListener('click', () => {
this.clearLogs();
});
// Loader selection
document.getElementById('loader-select').addEventListener('change', (e) => {
this.onLoaderChange(e.target.value);
});
// Install buttons
document.getElementById('install-zernmc-btn').addEventListener('click', () => {
this.installZernMCPack();
});
document.getElementById('install-vanilla-btn').addEventListener('click', () => {
this.installVanilla();
});
}
// ==================== GRID ANIMATION ====================
initGridAnimation() {
const canvas = document.getElementById('grid-canvas');
const ctx = canvas.getContext('2d');
let mouseX = 0, mouseY = 0;
let offsetX = 0, offsetY = 0;
const resize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
this.drawGrid(ctx, canvas.width, canvas.height, offsetX, offsetY);
};
window.addEventListener('resize', resize);
window.addEventListener('mousemove', (e) => {
mouseX = (e.clientX / window.innerWidth - 0.5) * 2;
mouseY = (e.clientY / window.innerHeight - 0.5) * 2;
});
const animate = () => {
offsetX += (mouseX * 0.5 - offsetX) * 0.05;
offsetY += (mouseY * 0.5 - offsetY) * 0.05;
ctx.clearRect(0, 0, canvas.width, canvas.height);
this.drawGrid(ctx, canvas.width, canvas.height, offsetX, offsetY);
requestAnimationFrame(animate);
};
resize();
animate();
}
drawGrid(ctx, width, height, offsetX, offsetY) {
const gridSize = 50;
const dotSize = 1;
ctx.fillStyle = '#e94560';
for (let x = 0; x <= width; x += gridSize) {
for (let y = 0; y <= height; y += gridSize) {
const px = x + offsetX * 10;
const py = y + offsetY * 10;
ctx.beginPath();
ctx.arc(px, py, dotSize, 0, Math.PI * 2);
ctx.fill();
}
}
}
// ==================== API ====================
async request(endpoint, options = {}) {
try {
const response = await fetch(`${API_BASE}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers
}
});
return await response.json();
} catch (error) {
console.error('API Error:', error);
return { success: false, error: error.message };
}
}
// ==================== AUTH ====================
async checkAuth() {
this.showLoading(true);
const result = await this.request('/auth/status');
if (result.loggedIn) {
this.username = result.username;
this.showMainScreen();
await this.loadCurrentInstance();
} else {
this.showLoginScreen();
}
this.showLoading(false);
}
async handleLogin() {
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const errorEl = document.getElementById('login-error');
const btn = document.querySelector('#login-form button[type="submit"]');
const btnText = btn.querySelector('.btn-text');
const btnLoader = btn.querySelector('.btn-loader');
if (!username || !password) {
this.showError('Введите имя пользователя и пароль');
return;
}
btn.disabled = true;
btnText.classList.add('hidden');
btnLoader.classList.remove('hidden');
errorEl.classList.add('hidden');
const result = await this.request('/auth/login', {
method: 'POST',
body: JSON.stringify({ username, password })
});
btn.disabled = false;
btnText.classList.remove('hidden');
btnLoader.classList.add('hidden');
if (result.success) {
this.username = result.username;
this.showMainScreen();
await this.loadCurrentInstance();
} else {
this.showError(result.error || 'Ошибка входа');
}
}
async handleLogout() {
await this.request('/auth/logout', { method: 'POST' });
this.username = null;
this.currentInstance = null;
this.showLoginScreen();
}
showError(message) {
const errorEl = document.getElementById('login-error');
errorEl.textContent = message;
errorEl.classList.remove('hidden');
}
// ==================== INSTANCES ====================
async loadCurrentInstance() {
const result = await this.request('/instances');
if (result.success && result.data && result.data.length > 0) {
this.currentInstance = result.data[0];
this.renderCurrentInstance(this.currentInstance);
this.enablePlayButton(true);
this.addLog('Сборка загружена: ' + this.currentInstance.name, 'success');
} else {
this.renderNoInstance();
this.enablePlayButton(false);
this.addLog('Установите сборку для игры', 'warning');
}
}
renderCurrentInstance(instance) {
const container = document.getElementById('current-instance');
container.innerHTML = `
<div class="instance-card-mini">
<span class="instance-name">${this.escapeHtml(instance.name)}</span>
<span class="instance-version">${this.escapeHtml(instance.version || 'Vanilla')}</span>
</div>
`;
}
renderNoInstance() {
const container = document.getElementById('current-instance');
container.innerHTML = `
<div class="instance-card-mini">
<span class="instance-name" style="color: var(--text-muted)">Нет сборки</span>
<span class="instance-version" style="background: var(--bg-secondary)">Нажмите скачать</span>
</div>
`;
}
enablePlayButton(enabled) {
const btn = document.getElementById('play-btn');
btn.disabled = !enabled;
}
async launchInstance() {
if (!this.currentInstance) return;
this.addLog('Проверка целостности файлов...', 'info');
this.enablePlayButton(false);
const result = await this.request(`/instances/${this.currentInstance.name}/launch`, {
method: 'POST'
});
if (result.success) {
this.addLog('Сборка запущена!', 'success');
} else {
this.addLog('Ошибка: ' + result.error, 'error');
this.enablePlayButton(true);
}
}
// ==================== DOWNLOAD MODAL ====================
async showDownloadModal() {
document.getElementById('download-modal').classList.remove('hidden');
await this.loadZernMCPacks();
await this.loadMCVersions();
}
hideDownloadModal() {
document.getElementById('download-modal').classList.add('hidden');
this.hideProgress();
}
switchTab(tab) {
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.tab === tab);
});
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.toggle('active', content.id === 'tab-' + tab);
});
}
async loadZernMCPacks() {
const select = document.getElementById('zernmc-pack-select');
select.innerHTML = '<option value="">Загрузка...</option>';
const result = await this.request('/instances/zernmc');
if (result.success && result.data && result.data.length > 0) {
this.zernmcPacks = result.data;
select.innerHTML = result.data.map(pack =>
`<option value="${this.escapeHtml(pack.name)}">${this.escapeHtml(pack.name)} (v${pack.version})</option>`
).join('');
} else {
select.innerHTML = '<option value="">Нет доступных сборок</option>';
}
}
async loadMCVersions() {
const select = document.getElementById('mc-version-select');
select.innerHTML = '<option value="">Загрузка...</option>';
const result = await this.request('/versions');
if (result.success && result.data) {
this.mcVersions = result.data;
select.innerHTML = '<option value="">Выберите версию</option>' +
result.data.map(v => `<option value="${v.id}">${v.id}</option>`).join('');
} else {
select.innerHTML = '<option value="">Не удалось загрузить</option>';
}
}
async onLoaderChange(loader) {
const loaderVersionGroup = document.getElementById('loader-version-group');
const loaderVersionSelect = document.getElementById('loader-version-select');
if (loader === 'vanilla') {
loaderVersionGroup.classList.add('hidden');
} else {
loaderVersionGroup.classList.remove('hidden');
loaderVersionSelect.innerHTML = '<option value="">Загрузка...</option>';
const result = await this.request(`/versions/${document.getElementById('mc-version-select').value}/loaders/${loader}`);
if (result.success && result.data) {
loaderVersionSelect.innerHTML = result.data.map(v =>
`<option value="${v.version}">${v.version}</option>`
).join('');
} else {
loaderVersionSelect.innerHTML = '<option value="">Нет версий</option>';
}
}
}
async installZernMCPack() {
const packName = document.getElementById('zernmc-pack-select').value;
const instanceName = document.getElementById('zernmc-instance-name').value;
if (!packName) {
alert('Выберите сборку');
return;
}
if (!instanceName) {
alert('Введите название сборки');
return;
}
this.showProgress('Установка ZernMC сборки...');
this.addLog('Начало установки: ' + packName, 'info');
const result = await this.request('/instances/zernmc/install', {
method: 'POST',
body: JSON.stringify({ packName, instanceName })
});
if (result.success) {
this.hideDownloadModal();
await this.loadCurrentInstance();
this.addLog('Сборка установлена!', 'success');
} else {
this.addLog('Ошибка установки: ' + result.error, 'error');
this.hideProgress();
}
}
async installVanilla() {
const mcVersion = document.getElementById('mc-version-select').value;
const loader = document.getElementById('loader-select').value;
const loaderVersion = document.getElementById('loader-version-select').value;
const instanceName = document.getElementById('vanilla-instance-name').value;
if (!mcVersion) {
alert('Выберите версию Minecraft');
return;
}
if (!instanceName) {
alert('Введите название сборки');
return;
}
if (loader !== 'vanilla' && !loaderVersion) {
alert('Выберите версию лоадера');
return;
}
this.showProgress('Установка сборки...');
this.addLog(`Начало установки: Minecraft ${mcVersion} ${loader !== 'vanilla' ? loader + ' ' + loaderVersion : ''}`, 'info');
const result = await this.request('/instances/vanilla/install', {
method: 'POST',
body: JSON.stringify({
mcVersion,
loader: loader === 'vanilla' ? null : loader,
loaderVersion: loader === 'vanilla' ? null : loaderVersion,
instanceName
})
});
if (result.success) {
this.hideDownloadModal();
await this.loadCurrentInstance();
this.addLog('Сборка установлена!', 'success');
} else {
this.addLog('Ошибка установки: ' + result.error, 'error');
this.hideProgress();
}
}
showProgress(text) {
const progress = document.getElementById('download-progress');
const progressText = document.getElementById('progress-text');
const progressFill = document.getElementById('progress-fill');
progress.classList.remove('hidden');
progressText.textContent = text;
progressFill.style.width = '50%';
}
hideProgress() {
document.getElementById('download-progress').classList.add('hidden');
}
// ==================== LOGS ====================
addLog(message, type = 'info') {
const container = document.getElementById('logs-container');
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
container.appendChild(entry);
container.scrollTop = container.scrollHeight;
}
clearLogs() {
const container = document.getElementById('logs-container');
container.innerHTML = '<div class="log-entry info">Логи очищены</div>';
}
// ==================== UI HELPERS ====================
showLoginScreen() {
document.getElementById('login-screen').classList.remove('hidden');
document.getElementById('main-screen').classList.add('hidden');
}
showMainScreen() {
document.getElementById('login-screen').classList.add('hidden');
document.getElementById('main-screen').classList.remove('hidden');
document.getElementById('username-display').textContent = this.username || '';
}
showLoading(show) {
const overlay = document.getElementById('loading-overlay');
if (show) {
overlay.classList.remove('hidden');
} else {
overlay.classList.add('hidden');
}
}
escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
}
const app = new App();
@@ -0,0 +1,67 @@
package me.sashegdev.zernmc.launcher.api;
import me.sashegdev.zernmc.launcher.api.instance.InstanceService;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class InstanceServiceTest {
@Test
void instanceService_instantiates() {
InstanceService service = new InstanceService();
assertNotNull(service, "InstanceService должен создаваться");
}
@Test
void getAllInstances_returnsResponse() {
InstanceService service = new InstanceService();
ApiResponse<?> response = service.getAllInstances();
assertNotNull(response, "Ответ не должен быть null");
assertTrue(response.isSuccess() || !response.isSuccess(), "Должен быть валидный ответ");
}
@Test
void getAllInstances_returnsList() {
InstanceService service = new InstanceService();
ApiResponse<?> response = service.getAllInstances();
assertNotNull(response.getData(), "Data не должен быть null");
}
@Test
void isInstanceExists_returnsBoolean() {
InstanceService service = new InstanceService();
ApiResponse<Boolean> response = service.isInstanceExists("nonexistent");
assertNotNull(response, "Ответ не должен быть null");
assertTrue(response.isSuccess(), "Проверка должна быть успешной");
assertNotNull(response.getData(), "Data должен быть boolean");
}
@Test
void isInstanceExists_nonexistentReturnsFalse() {
InstanceService service = new InstanceService();
ApiResponse<Boolean> response = service.isInstanceExists("definitely_nonexistent_12345");
assertTrue(response.isSuccess());
assertFalse(response.getData(), "Несуществующая сборка должна вернуть false");
}
@Test
void deleteInstance_invalidName_returnsError() {
InstanceService service = new InstanceService();
ApiResponse<Boolean> response = service.deleteInstance("nonexistent");
assertNotNull(response, "Ответ не должен быть null");
}
@Test
void getInstance_nonexistent_returnsError() {
InstanceService service = new InstanceService();
ApiResponse<?> response = service.getInstance("definitely_nonexistent_12345");
assertNotNull(response, "Ответ не должен быть null");
assertFalse(response.isSuccess(), "Несуществующая сборка должна вернуть ошибку");
}
}
@@ -0,0 +1,33 @@
package me.sashegdev.zernmc.launcher.web;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.awt.GraphicsEnvironment;
class HeadlessDetectionTest {
@Test
void headlessDetection_works() {
boolean isHeadless = GraphicsEnvironment.isHeadless();
assertNotNull(isHeadless, "isHeadless() должен возвращать boolean");
}
@Test
void headlessDetection_consistentResult() {
boolean isHeadless1 = GraphicsEnvironment.isHeadless();
boolean isHeadless2 = GraphicsEnvironment.isHeadless();
assertEquals(isHeadless1, isHeadless2, "Результат должен быть консистентным");
}
@Test
void javaFxCheck_works() {
try {
boolean isHeadless = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice() != null;
assertFalse(isHeadless, "На Linux без дисплея должно быть headless");
} catch (Exception e) {
assertTrue(true, "Ожидаемая ошибка на headless");
}
}
}
@@ -0,0 +1,37 @@
package me.sashegdev.zernmc.launcher.web;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.net.ServerSocket;
class WebServerTest {
@Test
void findFreePort_returnsValidPort() throws IOException {
int port = WebServer.findFreePort(8080);
assertTrue(port >= 8080, "Порт должен быть >= 8080");
assertTrue(port < 8180, "Порт должен быть < 8180");
}
@Test
void findFreePort_findsDifferentPorts() throws IOException {
int port1 = WebServer.findFreePort(9000);
int port2 = WebServer.findFreePort(9100);
assertNotEquals(port1, port2, "Должены быть разные порты");
}
@Test
void findFreePort_respectsStartPort() throws IOException {
int port = WebServer.findFreePort(9500);
assertTrue(port >= 9500, "Порт должен быть >= указанного startPort");
}
@Test
void portRangeTest() throws IOException {
int port = WebServer.findFreePort(8080);
assertTrue(port >= 8080 && port < 8180, "Порт в допустимом диапазоне 8080-8179");
}
}