From 6e76f660b791df8c8a2cffa7605be14735792fcd Mon Sep 17 00:00:00 2001 From: Ipmake Date: Mon, 9 Oct 2023 18:27:36 +0200 Subject: [PATCH] Update 2 Final Stage --- src/main/index.ts | 200 +++++++++--------- src/renderer/src/components/LaunchSection.tsx | 2 +- src/renderer/src/components/SettingsMenu.tsx | 69 ++++-- src/renderer/src/components/popUp/index.tsx | 4 +- .../popUp/views/AccountSettings.tsx | 4 +- src/renderer/src/states/gameState.ts | 4 +- 6 files changed, 158 insertions(+), 125 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 798a380..93ac3b4 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -119,11 +119,15 @@ function createWindow(): void { }) async function updateServerList(): Promise { - serverList = ( - await axios.get(`http://localhost:23501/stats/servers`, { - timeout: 5000 - }) - ).data + try { + serverList = ( + await axios.get(`https://api.kocity.xyz/stats/servers`, { + timeout: 5000 + }) + ).data + } catch (error) { + console.log(error) + } } setInterval(updateServerList, 1000 * 60 * 3) updateServerList() @@ -378,9 +382,11 @@ function createWindow(): void { // Make the directory using sudoer and edit the permissions of the directory to allow everyone to write to it windows only await new Promise((resolve, reject) => { sudo.exec( - `mkdir "${arg.path}" && icacls "${arg.path}" /grant "${ - os.userInfo().username - }":(OI)(CI)F /T`, + os.platform() === 'win32' + ? `mkdir "${arg.path}" && icacls "${arg.path}" /grant "${ + os.userInfo().username + }":(OI)(CI)F /T` + : `mkdir "${arg.path}" && chown -R ${os.userInfo().username} "${arg.path}"`, { name: 'Knockout City Launcher' }, (error) => { if (error) reject(new Error(error.message)), console.log(error) @@ -400,7 +406,9 @@ function createWindow(): void { // Make the directory using sudoer and edit the permissions of the directory to allow everyone to write to it windows only await new Promise((resolve, reject) => { sudo.exec( - `icacls "${arg.path}" /grant "${os.userInfo().username}":(OI)(CI)F /T`, + os.platform() === 'win32' + ? `icacls "${arg.path}" /grant "${os.userInfo().username}":(OI)(CI)F /T` + : `chown -R ${os.userInfo().username} "${arg.path}"`, { name: 'Knockout City Launcher' }, (error) => { if (error) reject(new Error(error.message)), console.log(error) @@ -612,7 +620,7 @@ function createWindow(): void { if (!accToken) return axios .post( - `http://localhost:23501/stats/user/username/${arg.username}/playtime`, + `https://api.kocity.xyz/stats/user/username/${arg.username}/playtime`, {}, { headers: { @@ -632,16 +640,12 @@ function createWindow(): void { `-username=${arg.authkey ? arg.authkey : arg.username}`, `-backend=${arg.server}` ] - const game = spawn( - `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity/KnockoutCity.exe`, - args, - { - cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity`, - detached: true, - stdio: 'ignore', - env: {} - } - ) + const game = spawn(`${os.platform() === 'linux' ? 'wine ' : ''}KnockoutCity.exe`, args, { + cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCity`, + detached: true, + stdio: 'ignore', + env: {} + }) console.log(game.spawnargs) event.returnValue = 'launched' game.on('error', (err) => { @@ -748,6 +752,12 @@ function createWindow(): void { console.log(arg) console.log('Starting server') + if (os.platform() === 'linux') { + win.webContents.executeJavaScript(`window.postMessage({type: "server-closed"})`) + event.returnValue = 'stopping' + return dialog.showErrorBox('Error', 'Hosting a server is currently not supported on Linux.') + } + const args: string[] = [] if (arg.port != 0) args.push(`-backend_port=${arg.port}`) if (arg.maxUsers && arg.maxUsers != 0) @@ -755,9 +765,7 @@ function createWindow(): void { if (arg.secret.trim() != '') args.push(`-secret=${arg.secret}`) const server = spawn( - `${arg.path}/${ - arg.version == 1 ? 'highRes' : 'lowRes' - }/KnockoutCityServer/KnockoutCityServer.exe`, + `${os.platform() === 'linux' ? 'wine ' : ''}KnockoutCityServer.exe`, args, { cwd: `${arg.path}/${arg.version == 1 ? 'highRes' : 'lowRes'}/KnockoutCityServer` @@ -865,95 +873,79 @@ if (gotTheLock) { app.quit() } - if (!fs.existsSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/preyprank`) && is.dev) { - fs.mkdirSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/`, { recursive: true }) - fs.writeFileSync(`${os.homedir}/AppData/LocalLow/IPGG/kocitylauncher/preyprank`, 'true') - - const window = new BrowserWindow({ - fullscreen: true, - frame: false, - webPreferences: { - nodeIntegration: true - } - }) - - if (is.dev && process.env['ELECTRON_RENDERER_URL']) { - window.loadFile( - path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/joke.html') - ) - } else { - window.loadFile(path.join(__dirname, '../../resources/joke.html')) - } - - window.once('close', () => { - app.quit() + axios + .get('http://cdn.ipgg.net/kocity/version', { + timeout: 5000 }) - } else - axios - .get('http://cdn.ipgg.net/kocity/version', { - timeout: 5000 - }) - .then(async (res) => { - console.log('Checking for update') - // get the version of the app from electron - const version = app.getVersion().trim() - console.log(`${version} => ${res.data}`) - if (`${res.data}`.trim() == `${version}`.trim()) { + .then(async (res) => { + console.log('Checking for update') + // get the version of the app from electron + const version = app.getVersion().trim() + console.log(`${version} => ${res.data}`) + if (`${res.data}`.trim() == `${version}`.trim()) { + createWindow() + } else { + if (!(os.platform() === 'win32')) { + dialog.showErrorBox( + 'Update Available', + 'There is an update available but auto updating is currently only supported on Windows. Please download the latest version from https://kocity.xyz' + ) createWindow() - } else { - const { response } = await dialog.showMessageBox({ - type: 'info', - title: 'Update Available', - message: `An update is available for the Knockout City Launcher! Would you like to download it?`, - buttons: ['Yes', 'No'] - }) - if (response === 0) { - // open a small update window - const updateWindow = new BrowserWindow({ - width: 500, - height: 250, - frame: false, - resizable: false, - icon: './www/logo.png', - webPreferences: { - nodeIntegration: true - } - }) - console.log('Update window opened!') - - if (is.dev && process.env['ELECTRON_RENDERER_URL']) { - updateWindow.loadFile( - path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/update.html') - ) - } else { - updateWindow.loadFile(path.join(__dirname, '../../resources/update.html')) + return + } + const { response } = await dialog.showMessageBox({ + type: 'info', + title: 'Update Available', + message: `An update is available for the Knockout City Launcher! Would you like to download it?`, + buttons: ['Yes', 'No'] + }) + if (response === 0) { + // open a small update window + const updateWindow = new BrowserWindow({ + width: 500, + height: 250, + frame: false, + resizable: false, + icon: './www/logo.png', + webPreferences: { + nodeIntegration: true } + }) + console.log('Update window opened!') - // download the exe file using axios - console.log('Downloading update...') - axios - .get('http://cdn.ipgg.net/kocity/kocitylauncher.exe', { - responseType: 'arraybuffer' - }) - .then(async (res) => { - console.log('Update complete!') + if (is.dev && process.env['ELECTRON_RENDERER_URL']) { + updateWindow.loadFile( + path.join(process.env['ELECTRON_RENDERER_URL'], '../../resources/update.html') + ) + } else { + updateWindow.loadFile(path.join(__dirname, '../../resources/update.html')) + } - // write it to the download folder - fs.writeFileSync(`${os.tmpdir()}/kocity-update.exe`, res.data) + // download the exe file using axios + console.log('Downloading update...') + axios + .get('http://cdn.ipgg.net/kocity/kocitylauncher.exe', { + responseType: 'arraybuffer' + }) + .then(async (res) => { + console.log('Update complete!') - // open the file - spawn(`${os.tmpdir()}/kocity-update.exe`, { detached: true, stdio: 'ignore' }) + // write it to the download folder + fs.writeFileSync(`${os.tmpdir()}/kocity-update.exe`, res.data) - setTimeout(() => { - app.quit() - process.exit(0) - }, 1000) - }) - } else { - createWindow() - } + // open the file + spawn(`${os.tmpdir()}/kocity-update.exe`, { detached: true, stdio: 'ignore' }) + + setTimeout(() => { + app.quit() + process.exit(0) + }, 1000) + }) + } else { + createWindow() } - }) + } + }) }) } else { app.quit() diff --git a/src/renderer/src/components/LaunchSection.tsx b/src/renderer/src/components/LaunchSection.tsx index 54c5bae..7bbdc13 100644 --- a/src/renderer/src/components/LaunchSection.tsx +++ b/src/renderer/src/components/LaunchSection.tsx @@ -61,7 +61,7 @@ function LaunchSection(): JSX.Element { } const res = await axios - .post(`http://localhost:23501/auth/getkey`, { + .post(`https://api.kocity.xyz/auth/getkey`, { username: localStorage.getItem('username'), authToken: localStorage.getItem('authToken'), server: localStorage.getItem('currServer') diff --git a/src/renderer/src/components/SettingsMenu.tsx b/src/renderer/src/components/SettingsMenu.tsx index 902ab5d..b4e1f1f 100644 --- a/src/renderer/src/components/SettingsMenu.tsx +++ b/src/renderer/src/components/SettingsMenu.tsx @@ -1,4 +1,4 @@ -import { ExitToApp, Login, Settings } from '@mui/icons-material' +import { ExitToApp, Login, Settings, Star } from '@mui/icons-material' import { Box, Button, @@ -8,14 +8,16 @@ import { Stack, Switch, TextField, + Tooltip, Typography } from '@mui/material' -import React from 'react' +import React, { useEffect, useState } from 'react' // states import { useGameState } from '../states/gameState' import { useUIState } from '../states/uiState' import { useAuthState } from '../states/authState' +import axios from 'axios' function SettingsMenu(): JSX.Element { const [gameDirectory, setGameDirectory] = React.useState(localStorage.getItem('gameDirectory')) @@ -24,6 +26,20 @@ function SettingsMenu(): JSX.Element { const { setPopUpState } = useUIState() const { authState, username, setUsername } = useAuthState() + const [premium, setPremium] = useState(parseInt(localStorage.getItem('premium') ?? '0')) + + useEffect(() => { + axios + .get(`https://api.kocity.xyz/stats/user/username/${localStorage.getItem('username')}`) + .then((res) => { + if (!res.data?.user) return + + setPremium(res.data.user.premium) + localStorage.setItem('premium', `${res.data.user.premium}`) + localStorage.setItem('username', res.data.user.username) + }) + }, []) + return ( - { - setUsername(e.target.value) - localStorage.setItem('username', e.target.value) - }} - /> + + { + setUsername(e.target.value) + localStorage.setItem('username', e.target.value) + }} + /> + 0 ? 'inital' : 'none' + }} + > + 0 ? 'inital' : 'none', + color: '#FFFF00', + + transition: 'all 0.2s ease-in-out', + '&:hover': { + color: '#FFAA00', + transform: 'scale(1.1)' + } + }} + /> + + + S diff --git a/src/renderer/src/components/popUp/index.tsx b/src/renderer/src/components/popUp/index.tsx index 0be0bbf..47bdfb4 100644 --- a/src/renderer/src/components/popUp/index.tsx +++ b/src/renderer/src/components/popUp/index.tsx @@ -79,7 +79,7 @@ function PopUp(): JSX.Element { if (value.length === 6) { setPopUpLoading(true) const data = await axios - .post(`http://localhost:23501/auth/login/`, { + .post(`https://api.kocity.xyz/auth/login/`, { code: value }) .catch((err) => { @@ -212,7 +212,7 @@ function PopUp(): JSX.Element { console.log('SAVE') setPopUpLoading(true) const data = await axios - .post(`http://localhost:23501/auth/register/`, { + .post(`https://api.kocity.xyz/auth/register/`, { username: ( document.getElementById('username') as HTMLInputElement ).value.trim(), diff --git a/src/renderer/src/components/popUp/views/AccountSettings.tsx b/src/renderer/src/components/popUp/views/AccountSettings.tsx index af11273..1034d7f 100644 --- a/src/renderer/src/components/popUp/views/AccountSettings.tsx +++ b/src/renderer/src/components/popUp/views/AccountSettings.tsx @@ -48,7 +48,7 @@ export default function AccountSettings(): JSX.Element { useEffect(() => { setTimeout(() => { axios - .get(`http://localhost:23501/stats/user/username/${localStorage.getItem('username')}`) + .get(`https://api.kocity.xyz/stats/user/username/${localStorage.getItem('username')}`) .then((res) => { console.log(res.data.user.color) setSelectedColor(colors.findIndex((color) => color === res.data.user.color)) @@ -294,7 +294,7 @@ export default function AccountSettings(): JSX.Element { setLoaderPopUpActive(true) axios .post( - `http://localhost:23501/stats/user/username/${localStorage.getItem( + `https://api.kocity.xyz/stats/user/username/${localStorage.getItem( 'username' )}/setColor`, { diff --git a/src/renderer/src/states/gameState.ts b/src/renderer/src/states/gameState.ts index a4114d9..a800b98 100644 --- a/src/renderer/src/states/gameState.ts +++ b/src/renderer/src/states/gameState.ts @@ -39,7 +39,7 @@ export const useGameState = create((set) => ({ set(() => ({ publicServers: publicServers })) }, fetchPublicServers: async (): Promise => { - const response = await axios.get(`http://localhost:23501/stats/servers`).catch((err) => { + const response = await axios.get(`https://api.kocity.xyz/stats/servers`).catch((err) => { console.log(err) return { data: [ @@ -60,7 +60,7 @@ export const useGameState = create((set) => ({ playtime: null, fetchPlaytime: async (): Promise => { const response = await axios - .get(`http://localhost:23501/stats/user/username/${localStorage.getItem('username')}`) + .get(`https://api.kocity.xyz/stats/user/username/${localStorage.getItem('username')}`) .catch((err) => { console.log(err) return {