From 392dc2cf86237b280a371b606899d5ed905092d5 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:51:29 +0700 Subject: [PATCH] Categorize servers by continents --- src/modules/ui/dialog/settings-dialog.ts | 58 +++++++++++++++++++----- src/types/index.d.ts | 16 +++++-- src/utils/translation.ts | 5 ++ src/utils/xcloud-interceptor.ts | 55 +++++++++++++--------- 4 files changed, 99 insertions(+), 35 deletions(-) diff --git a/src/modules/ui/dialog/settings-dialog.ts b/src/modules/ui/dialog/settings-dialog.ts index e407fcf..f9473ce 100644 --- a/src/modules/ui/dialog/settings-dialog.ts +++ b/src/modules/ui/dialog/settings-dialog.ts @@ -1044,13 +1044,37 @@ export class SettingsNavigationDialog extends NavigationDialog { } private renderServerSetting(setting: SettingTabContentItem): HTMLElement { - let selectedValue; + let selectedValue =getPref(PrefKey.SERVER_REGION); + + const continents: Record = { + 'america-north': { + label: t('continent-north-america'), + }, + 'america-south': { + label: t('continent-south-america'), + }, + 'asia': { + label: t('continent-asia'), + }, + 'australia': { + label: t('continent-australia'), + }, + 'europe': { + label: t('continent-europe'), + }, + 'other': { + label: t('other'), + }, + }; const $control = CE('select', { - id: `bx_setting_${setting.pref}`, - title: setting.label, - tabindex: 0, - }); + id: `bx_setting_${setting.pref}`, + title: setting.label, + tabindex: 0, + }); $control.name = $control.id; $control.addEventListener('input', (e: Event) => { @@ -1058,8 +1082,6 @@ export class SettingsNavigationDialog extends NavigationDialog { this.onGlobalSettingChanged(e); }); - selectedValue = getPref(PrefKey.SERVER_REGION); - setting.options = {}; for (const regionName in STATES.serverRegions) { const region = STATES.serverRegions[regionName]; @@ -1076,15 +1098,29 @@ export class SettingsNavigationDialog extends NavigationDialog { } setting.options[value] = label; + + const $option = CE('option', {value: value}, label); + const continent = continents[region.contintent]; + if (!continent.children) { + continent.children = []; + } + continent.children.push($option); } - for (const value in setting.options) { - const label = setting.options[value]; + const fragment = document.createDocumentFragment(); + let key: keyof typeof continents; + for (key in continents) { + const continent = continents[key]; + if (!continent.children) { + continue; + } - const $option = CE('option', {value: value}, label); - $control.appendChild($option); + fragment.appendChild(CE('optgroup', { + label: continent.label, + }, ...continent.children)); } + $control.appendChild(fragment); $control.disabled = Object.keys(STATES.serverRegions).length === 0; // Select preferred region diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 02d5a88..9e0d117 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -21,14 +21,24 @@ interface Window { interface NavigatorBattery extends Navigator { getBattery: () => Promise<{ - charging: boolean, - level: float, + charging: boolean; + level: float; }>, } +type ServerContinent = 'america-north' | 'america-south' | 'asia' | 'australia' | 'europe' | 'other'; +type ServerRegion = { + baseUri: string; + isDefault: boolean; + name: string; + shortName: string; + + contintent: ServerContinent; +}; + type BxStates = { supportedRegion: boolean; - serverRegions: any; + serverRegions: Record; selectedRegion: any; gsToken: string; isSignedIn: boolean; diff --git a/src/utils/translation.ts b/src/utils/translation.ts index 9479f0c..47947c4 100644 --- a/src/utils/translation.ts +++ b/src/utils/translation.ts @@ -67,6 +67,11 @@ const Texts = { "confirm-reload-stream": "Do you want to refresh the stream?", "connected": "Connected", "console-connect": "Connect", + "continent-asia": "Asia", + "continent-australia": "Australia", + "continent-europe": "Europe", + "continent-north-america": "North America", + "continent-south-america": "South America", "contrast": "Contrast", "controller": "Controller", "controller-friendly-ui": "Controller-friendly UI", diff --git a/src/utils/xcloud-interceptor.ts b/src/utils/xcloud-interceptor.ts index 7ec88c5..782eeaf 100644 --- a/src/utils/xcloud-interceptor.ts +++ b/src/utils/xcloud-interceptor.ts @@ -14,22 +14,31 @@ import { PrefKey } from "@/enums/pref-keys"; import { getPref, StreamResolution, StreamTouchController } from "./settings-storages/global-settings-storage"; export class XcloudInterceptor { - private static readonly SERVER_EMOJIS = { - AustraliaEast: 'πŸ‡¦πŸ‡Ί', - AustraliaSouthEast: 'πŸ‡¦πŸ‡Ί', - BrazilSouth: 'πŸ‡§πŸ‡·', - EastUS: 'πŸ‡ΊπŸ‡Έ', - EastUS2: 'πŸ‡ΊπŸ‡Έ', - JapanEast: 'πŸ‡―πŸ‡΅', - KoreaCentral: 'πŸ‡°πŸ‡·', - MexicoCentral: 'πŸ‡²πŸ‡½', - NorthCentralUs: 'πŸ‡ΊπŸ‡Έ', - SouthCentralUS: 'πŸ‡ΊπŸ‡Έ', - SwedenCentral: 'πŸ‡ΈπŸ‡ͺ', - UKSouth: 'πŸ‡¬πŸ‡§', - WestEurope: 'πŸ‡ͺπŸ‡Ί', - WestUS: 'πŸ‡ΊπŸ‡Έ', - WestUS2: 'πŸ‡ΊπŸ‡Έ', + private static readonly SERVER_EXTRA_INFO: Record = { + // North America + EastUS: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + EastUS2: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + NorthCentralUs: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + SouthCentralUS: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + WestUS: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + WestUS2: ['πŸ‡ΊπŸ‡Έ', 'america-north'], + MexicoCentral: ['πŸ‡²πŸ‡½', 'america-north'], + + // South America + BrazilSouth: ['πŸ‡§πŸ‡·', 'america-south'], + + // Asia + JapanEast: ['πŸ‡―πŸ‡΅', 'asia'], + KoreaCentral: ['πŸ‡°πŸ‡·', 'asia'], + + // Australia + AustraliaEast: ['πŸ‡¦πŸ‡Ί', 'australia'], + AustraliaSouthEast: ['πŸ‡¦πŸ‡Ί', 'australia'], + + // Europe + SwedenCentral: ['πŸ‡ΈπŸ‡ͺ', 'europe'], + UKSouth: ['πŸ‡¬πŸ‡§', 'europe'], + WestEurope: ['πŸ‡ͺπŸ‡Ί', 'europe'], }; private static async handleLogin(request: RequestInfo | URL, init?: RequestInit) { @@ -53,10 +62,11 @@ export class XcloudInterceptor { // Get server list const serverRegex = /\/\/(\w+)\./; - const serverEmojis = XcloudInterceptor.SERVER_EMOJIS; + const serverExtra = XcloudInterceptor.SERVER_EXTRA_INFO; - for (let region of obj.offeringSettings.regions) { - const regionName = region.name as keyof typeof serverEmojis; + let region: ServerRegion; + for (region of obj.offeringSettings.regions) { + const regionName = region.name as keyof typeof serverExtra; let shortName = region.name; if (region.isDefault) { @@ -66,8 +76,11 @@ export class XcloudInterceptor { let match = serverRegex.exec(region.baseUri); if (match) { shortName = match[1]; - if (serverEmojis[regionName]) { - shortName = serverEmojis[regionName] + ' ' + shortName; + if (serverExtra[regionName]) { + shortName = serverExtra[regionName][0] + ' ' + shortName; + region.contintent = serverExtra[regionName][1]; + } else { + region.contintent = 'other'; } }