From 756ae1be582e31f48a4676213b3d1ca3005d0bc6 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Fri, 12 Jan 2024 12:09:28 +0100 Subject: [PATCH] progress status and progress communication done --- main.js | 120 +++++++++++++++++++++++++++-------------- preload.js | 16 ++++++ src/App.svelte | 16 ++++-- src/app.pcss | 4 ++ src/pages/Login.svelte | 4 +- src/util/mathUtil.ts | 3 ++ tailwind.config.cjs | 3 ++ 7 files changed, 122 insertions(+), 44 deletions(-) create mode 100644 src/util/mathUtil.ts diff --git a/main.js b/main.js index da5d1b8..0536b6c 100644 --- a/main.js +++ b/main.js @@ -18,32 +18,44 @@ function isDev() { function registerIPCPipes() { ipcMain.handle("ezpplauncher:login", async (e, args) => { - const fetchResult = await fetch("https://ez-pp.farm/login/check", { - method: "POST", - body: JSON.stringify({ - username: args.username, - password: args.password, - }), - headers: { - "Content-Type": "application/json", - }, - }); + const timeout = new AbortController(); + const timeoutId = setTimeout(() => timeout.abort(), 8000); + try { + const fetchResult = await fetch("https://ez-pp.farm/login/check", { + signal: timeout.signal, + method: "POST", + body: JSON.stringify({ + username: args.username, + password: args.password, + }), + headers: { + "Content-Type": "application/json", + }, + }); - if (fetchResult.ok) { - const result = await fetchResult.json(); - if ("user" in result) { - if (args.saveCredentials) { - config.set("username", args.username); - config.set("password", args.password); + clearTimeout(timeoutId); + + if (fetchResult.ok) { + const result = await fetchResult.json(); + if ("user" in result) { + if (args.saveCredentials) { + config.set("username", args.username); + config.set("password", args.password); + } + config.remove("guest"); } - config.remove("guest"); + return result; } - return result; + return { + code: 500, + message: "Something went wrong while logging you in.", + }; + } catch (err) { + return { + code: 500, + message: "Something went wrong while logging you in.", + }; } - return { - code: 500, - message: "Something went wrong while logging you in.", - }; }); ipcMain.handle("ezpplauncher:autologin", async (e) => { @@ -54,25 +66,37 @@ function registerIPCPipes() { if (username == undefined || password == undefined) { return { code: 200, message: "No autologin" }; } - const fetchResult = await fetch("https://ez-pp.farm/login/check", { - method: "POST", - body: JSON.stringify({ - username: username, - password: password, - }), - headers: { - "Content-Type": "application/json", - }, - }); + const timeout = new AbortController(); + const timeoutId = setTimeout(() => timeout.abort(), 8000); + try { + const fetchResult = await fetch("https://ez-pp.farm/login/check", { + signal: timeout.signal, + method: "POST", + body: JSON.stringify({ + username: username, + password: password, + }), + headers: { + "Content-Type": "application/json", + }, + }); - if (fetchResult.ok) { - const result = await fetchResult.json(); - return result; + clearTimeout(timeoutId); + + if (fetchResult.ok) { + const result = await fetchResult.json(); + return result; + } + return { + code: 500, + message: "Something went wrong while logging you in.", + }; + } catch (err) { + return { + code: 500, + message: "Something went wrong while logging you in.", + }; } - return { - code: 500, - message: "Something went wrong while logging you in.", - }; }); ipcMain.handle("ezpplauncher:guestlogin", (e) => { @@ -87,6 +111,24 @@ function registerIPCPipes() { config.remove("guest"); return true; }); + + ipcMain.handle("ezpplauncher:launch", async (e) => { + mainWindow.webContents.send("ezpplauncher:launchstatus", { + status: "Checking osu! directory...", + }); + await new Promise((res) => setTimeout(res, 1500)); + mainWindow.webContents.send("ezpplauncher:launchstatus", { + status: "Checking for osu! updates...", + }); + mainWindow.webContents.send("ezpplauncher:launchprogress", { + progress: 0, + }); + await new Promise((res) => setTimeout(res, 1500)); + mainWindow.webContents.send("ezpplauncher:launchprogress", { + progress: 100, + }); + return true; + }); } function createWindow() { diff --git a/preload.js b/preload.js index 87bed75..0c862fb 100644 --- a/preload.js +++ b/preload.js @@ -36,3 +36,19 @@ window.addEventListener("logout", async (e) => { window.addEventListener("guest-login", async (e) => { await ipcRenderer.invoke("ezpplauncher:guestlogin"); }); + +window.addEventListener("launch", async (e) => { + await ipcRenderer.invoke("ezpplauncher:launch"); +}); + +ipcRenderer.addListener("ezpplauncher:launchstatus", (e, args) => { + window.dispatchEvent( + new CustomEvent("launchStatusUpdate", { detail: args }), + ); +}); + +ipcRenderer.addListener("ezpplauncher:launchprogress", (e, args) => { + window.dispatchEvent( + new CustomEvent("launchProgressUpdate", { detail: args }), + ); +}); diff --git a/src/App.svelte b/src/App.svelte index 62716a0..0ba0886 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -4,19 +4,20 @@ Dropdown, DropdownItem, DropdownHeader, - DropdownDivider + DropdownDivider, } from "flowbite-svelte"; import { ArrowRightFromBracketSolid, ArrowRightToBracketSolid, - UserSettingsSolid + UserSettingsSolid, } from "flowbite-svelte-icons"; import ezppLogo from "../public/favicon.png"; import { currentPage, currentUser, launching, - launchStatus + launchPercentage, + launchStatus, } from "./storage/localStore"; import { Page } from "./consts/pages"; import Login from "./pages/Login.svelte"; @@ -40,9 +41,16 @@ }; window.addEventListener("launchStatusUpdate", (e) => { - const status = (e as CustomEvent).detail; + console.log((e as CustomEvent).detail); + const status = (e as CustomEvent).detail.status; launchStatus.set(status); }); + + window.addEventListener("launchProgressUpdate", (e) => { + console.log((e as CustomEvent).detail); + const progress = (e as CustomEvent).detail.progress; + launchPercentage.set(progress); + }); diff --git a/src/app.pcss b/src/app.pcss index 9f37220..5e16355 100644 --- a/src/app.pcss +++ b/src/app.pcss @@ -54,6 +54,10 @@ html .cet-titlebar .cet-control-icon svg { } } +.animatedProgress div { + transition: width 0.35s cubic-bezier(0.65, -0.02, 0.31, 1.01); +} + @keyframes progress-loading { 50% { background-position-x: -115%; diff --git a/src/pages/Login.svelte b/src/pages/Login.svelte index 63e3131..47bcf62 100644 --- a/src/pages/Login.svelte +++ b/src/pages/Login.svelte @@ -142,7 +142,9 @@ {/if} - Save credentials + Save credentials