From e8277fb4cefd181767d80cb406837c6297fc833c Mon Sep 17 00:00:00 2001 From: Vitaly Date: Thu, 23 Nov 2023 00:40:54 +0300 Subject: [PATCH] feat: now defaultProxy and defaultHost serve as default values when nothing is entered (behave as fallback values) use new config params with save at the end for previous behavior (but still deprecated) --- config.json | 3 +- src/globalState.ts | 7 ++++- src/menus/components/edit_box.js | 5 +++ src/menus/play_screen.js | 54 ++++++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/config.json b/config.json index 123d8f3f..7c0ce254 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,6 @@ { - "defaultHost": "pjs.deptofcraft.com", + "version": 1, + "defaultHost": "", "defaultProxy": "zardoy.site:2344", "defaultVersion": "1.18.2", "mapsProvider": "zardoy.site/maps" diff --git a/src/globalState.ts b/src/globalState.ts index 9815ca69..92bbb5c1 100644 --- a/src/globalState.ts +++ b/src/globalState.ts @@ -120,7 +120,12 @@ export const showContextmenu = (items: ContextMenuItem[], { clientX, clientY }) // --- -type AppConfig = { +export type AppConfig = { + defaultHost?: string + defaultHostSave?: string + defaultProxy?: string + defaultProxySave?: string + defaultVersion?: string mapsProvider?: string } diff --git a/src/menus/components/edit_box.js b/src/menus/components/edit_box.js index 0f42527a..c7210d43 100644 --- a/src/menus/components/edit_box.js +++ b/src/menus/components/edit_box.js @@ -114,6 +114,10 @@ class EditBox extends LitElement { type: Boolean, attribute: 'pmui-required' }, + placeholder: { + type: String, + attribute: 'pmui-placeholder' + }, state: { type: String, attribute: true @@ -144,6 +148,7 @@ class EditBox extends LitElement { autocomplete="off" autocapitalize="off" value="${this.value}" + placeholder=${ifDefined(this.placeholder || undefined)} list=${ifDefined(this.autocompleteValues ? `${this.id}-list` : undefined)} inputmode=${ifDefined(this.inputMode || undefined)} @input=${({ target: { value } }) => { this.value = this.inputMode === 'decimal' ? value.replaceAll(',', '.') : value }} diff --git a/src/menus/play_screen.js b/src/menus/play_screen.js index 7533ddb7..1deb7dc6 100644 --- a/src/menus/play_screen.js +++ b/src/menus/play_screen.js @@ -70,8 +70,10 @@ class PlayScreen extends LitElement { static get properties () { return { server: { type: String }, + serverImplicit: { type: String }, serverport: { type: Number }, proxy: { type: String }, + proxyImplicit: { type: String }, proxyport: { type: Number }, username: { type: String }, password: { type: String }, @@ -84,11 +86,17 @@ class PlayScreen extends LitElement { this.version = '' this.serverport = '' this.proxyport = '' + this.server = '' + this.proxy = '' + this.username = '' + this.password = '' + this.serverImplicit = '' + this.proxyImplicit = '' // todo set them sooner add indicator void window.fetch('config.json').then(async res => res.json()).then(c => c, (error) => { - console.error('Failed to load config.json', error) + console.warn('Failed to load optional config.json', error) return {} - }).then(config => { + }).then(async (/** @type {import('../globalState').AppConfig} */config) => { miscUiState.appConfig = config const params = new URLSearchParams(window.location.search) @@ -100,9 +108,34 @@ class PlayScreen extends LitElement { return qsValue || window.localStorage.getItem(localStorageKey) } - this.server = getParam('server', 'ip') ?? config.defaultHost - this.proxy = getParam('proxy') ?? config.defaultProxy - this.version = getParam('version') || (window.localStorage.getItem('version') ?? config.defaultVersion) + if (config.defaultHost === '' || config.defaultHostSave === '') { + let proxy = config.defaultProxy || config.defaultProxySave || params.get('proxy') + const cleanUrl = url => url.replaceAll(/(https?:\/\/|\/$)/g, '') + if (proxy && cleanUrl(proxy) !== cleanUrl(location.origin + location.pathname)) { + if (!proxy.startsWith('http')) proxy = 'https://' + proxy + const proxyConfig = await fetch(proxy + '/config.json').then(async res => res.json()).then(c => c, (error) => { + console.warn(`Failed to load config.json from proxy ${proxy}`, error) + return {} + }) + if (config.defaultHost === '' && proxyConfig.defaultHost) { + config.defaultHost = proxyConfig.defaultHost + } else { + config.defaultHost = '' + } + if (config.defaultHostSave === '' && proxyConfig.defaultHostSave) { + config.defaultHostSave = proxyConfig.defaultHostSave + } else { + config.defaultHostSave = '' + } + } + this.server = this.serverImplicit + } + + this.serverImplicit = config.defaultHost ?? '' + this.proxyImplicit = config.defaultProxy ?? '' + this.server = getParam('server', 'ip') ?? config.defaultHostSave ?? '' + this.proxy = getParam('proxy') ?? config.defaultProxySave ?? '' + this.version = getParam('version') || (window.localStorage.getItem('version') ?? config.defaultVersion ?? '') this.username = getParam('username') || 'pviewer' + (Math.floor(Math.random() * 1000)) this.password = getParam('password') || '' if (process.env.NODE_ENV === 'development' && params.get('reconnect') && this.server && this.username) { @@ -125,7 +158,8 @@ class PlayScreen extends LitElement { pmui-id="serverip" pmui-value="${this.server}" pmui-type="url" - pmui-required="true" + pmui-required="${this.serverImplicit === ''}}" + pmui-placeholder="${this.serverImplicit}" .autocompleteValues=${JSON.parse(localStorage.getItem('serverHistory') || '[]')} @input=${e => { this.server = e.target.value }} > @@ -135,6 +169,7 @@ class PlayScreen extends LitElement { pmui-id="port" pmui-value="${this.serverport}" pmui-type="number" + pmui-placeholder="25565" @input=${e => { this.serverport = e.target.value }} > @@ -144,7 +179,8 @@ class PlayScreen extends LitElement { pmui-label="Proxy IP" pmui-id="proxy" pmui-value="${this.proxy}" - pmui-required=${/* TODO derive from config? */false} + pmui-required="${this.proxyImplicit === ''}}" + pmui-placeholder="${this.proxyImplicit}" pmui-type="url" @input=${e => { this.proxy = e.target.value }} > @@ -190,8 +226,8 @@ class PlayScreen extends LitElement { } onConnectPress () { - const server = `${this.server}${this.serverport && `:${this.serverport}`}` - const proxy = this.proxy && `${this.proxy}${this.proxyport && `:${this.proxyport}`}` + const server = this.server ? `${this.server}${this.serverport && `:${this.serverport}`}` : this.serverImplicit + const proxy = this.proxy ? `${this.proxy}${this.proxyport && `:${this.proxyport}`}` : this.proxyImplicit window.localStorage.setItem('username', this.username) window.localStorage.setItem('password', this.password)