From 637c17923de2d3d23b3972024d31ae3b73e3d7b4 Mon Sep 17 00:00:00 2001 From: mentalisit <64618228+mentalisit@users.noreply.github.com> Date: Thu, 16 May 2024 12:45:32 -0500 Subject: [PATCH] version for two clients (#108) * version for two clients * change client * change client upd * client select * clear debug * fix client context * remove discord url * revert * selected alt * update import * update client * reactive client * fix selectAlt --------- Co-authored-by: Xinos --- package-lock.json | 13 +++- package.json | 1 + src/components/CompendiumPage.vue | 91 +++++++++++++++++++++------ src/composables/compendiumTechList.ts | 12 ++-- src/pages/CompendiumCorp.vue | 13 ++-- src/utils/compendium.ts | 25 ++++++-- src/utils/getDiscordUrl.ts | 15 ----- 7 files changed, 118 insertions(+), 52 deletions(-) delete mode 100644 src/utils/getDiscordUrl.ts diff --git a/package-lock.json b/package-lock.json index 60da1496..c2d6906b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hades-space", - "version": "4.2.0", + "version": "4.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hades-space", - "version": "4.2.0", + "version": "4.3.0", "hasInstallScript": true, "license": "GNU AFFERO GENERAL PUBLIC LICENSE V3", "dependencies": { @@ -15,6 +15,7 @@ "@vitejs/plugin-vue": "^5.0.4", "@vueuse/head": "^2.0.0", "bot_client": "github:mcgoldric/compendium-client", + "bot_client2": "github:mentalisit/compendium-client-new", "jsoncrush": "^1.1.8", "lodash.debounce": "^4.0.8", "parse-markdown-table": "^0.0.6", @@ -4124,6 +4125,14 @@ "events": "^3.3.0" } }, + "node_modules/bot_client2": { + "version": "1.0.0", + "resolved": "git+ssh://git@github.com/mentalisit/compendium-client-new.git#ea7f4d2ae6ce66a8d42d63fe9e678b744fc2ac41", + "license": "MIT", + "dependencies": { + "events": "^3.3.0" + } + }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", diff --git a/package.json b/package.json index b614b740..54a0b2b0 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@vitejs/plugin-vue": "^5.0.4", "@vueuse/head": "^2.0.0", "bot_client": "github:mcgoldric/compendium-client", + "bot_client2": "github:mentalisit/compendium-client-new", "jsoncrush": "^1.1.8", "lodash.debounce": "^4.0.8", "parse-markdown-table": "^0.0.6", diff --git a/src/components/CompendiumPage.vue b/src/components/CompendiumPage.vue index de1b54dd..5ad243d2 100644 --- a/src/components/CompendiumPage.vue +++ b/src/components/CompendiumPage.vue @@ -3,6 +3,26 @@
+
+
+ +
+
@@ -85,6 +105,17 @@ target="_blank" >HS Compendium +
+ +
+ +
(); -const guild = ref(); +const user = ref(); +const guild = ref(); +const defaultSwitchClient = ref(0); onMounted(async () => { isFetching.value = true; await clientInit(); - const u = client.getUser(); + const u = client.value.getUser(); isFetching.value = false; + console.log(client.value, u); + if (!u) { openCodeReqModal.value = true; if ('c' in router.currentRoute.value.query) { reqCode.value = router.currentRoute.value.query.c as string; - // noinspection ES6MissingAwait - applyReqCode(); + } + if ('client' in router.currentRoute.value.query) { + defaultSwitchclient.value.value = parseInt(router.currentRoute.value.query.client as string, 10); } } else { user.value = u; - guild.value = client.getGuild(); + guild.value = client.value.getGuild(); } }); async function applyReqCode() { - let ident: Identity; + let ident: Identity|Identity2; if (!reqCode.value) { error.value = t('CANNOT_BE_EMPTY'); @@ -162,7 +197,7 @@ async function applyReqCode() { isFetching.value = true; try { - ident = await client.checkConnectCode(reqCode.value); + ident = await client.value.checkConnectCode(reqCode.value); } catch (e: unknown) { error.value = (e as Error).toString(); console.error(e); @@ -181,16 +216,26 @@ async function applyReqCode() { if (!drop) { isFetching.value = true; - await client.connect(ident); - user.value = client.getUser(); - guild.value = client.getGuild(); + await client.value.connect(ident); + user.value = client.value.getUser(); + guild.value = client.value.getGuild(); isFetching.value = false; } } +function selectUserAlt(value: string) { + (client.value as Client2).switchAlt(value); +} + +function selectClient(value: number) { + isFetching.value = true; + switchInstance(value).then(() => { + isFetching.value = false; + }); +} function userProfileClick() { store.dispatch(types.OPEN_CONFIRM, t('TID_SETTINGS_DLG_SIGN_OUT')) .then(() => { - client.logout(); + client.value.logout(); user.value = undefined; guild.value = undefined; }) @@ -212,7 +257,13 @@ function userProfileClick() { .content { margin: 0 5%; display: flex; - justify-content: end; + justify-content: space-between; + align-items: center; + + .alt-switch { + width: 110%; + } + @media screen and (max-width: 1000px){ margin: 0 1%; @@ -319,6 +370,10 @@ function userProfileClick() { margin-left: 10px; font-style: italic; } +.switch-client { + padding-top: 4%; + font-size: 80%; +} @keyframes bg-pos-move { to { background-position: 1000% 0; } diff --git a/src/composables/compendiumTechList.ts b/src/composables/compendiumTechList.ts index 3fc558b6..c7a9ac83 100644 --- a/src/composables/compendiumTechList.ts +++ b/src/composables/compendiumTechList.ts @@ -13,13 +13,13 @@ export default function compendiumTechList() { const levelMap = ref>({}); const setTechLevel = debounce( - (tId: number, l: number) => client.setTechLevel(tId, l), + (tId: number, l: number) => client.value.setTechLevel(tId, l), 500, ) as SetTechLevel; onMounted(() => { - if (client.getUser()) { - data.value = client.getTechLevels(); + if (client.value.getUser()) { + data.value = client.value.getTechLevels(); } }); watch(data, (value) => { @@ -30,9 +30,9 @@ export default function compendiumTechList() { } }, { deep: true }); - client.on('sync', (tl: TechLevels) => data.value = tl); - client.on('connected', () => data.value = client.getTechLevels()); - client.on('disconnected', () => { + client.value.on('sync', (tl: TechLevels) => data.value = tl); + client.value.on('connected', () => data.value = client.value.getTechLevels()); + client.value.on('disconnected', () => { data.value = undefined; levelMap.value = undefined; }); diff --git a/src/pages/CompendiumCorp.vue b/src/pages/CompendiumCorp.vue index e29e7249..03bd1713 100644 --- a/src/pages/CompendiumCorp.vue +++ b/src/pages/CompendiumCorp.vue @@ -42,7 +42,7 @@
@@ -143,7 +143,6 @@ import { onMounted, reactive, Ref, ref, watch } from 'vue'; import { useStore } from 'vuex'; import { useI18n } from 'vue-i18n'; -import { getDiscordAvatarUrl } from '@Utils/getDiscordUrl'; import client from '../utils/compendium'; import types from '@/store/modules/userSettings/types'; @@ -221,8 +220,8 @@ let filteredByRoleCache: CorpMember[] = []; filteredMembers.value = Array.from({ length: 10 }, (i) => ({ userId: i, name: '', avatarUrl: '' }) as CorpMember); -client.on('connected', () => fetchCorp()); -client.on('disconnected', () => { +client.value.on('connected', () => fetchCorp()); +client.value.on('disconnected', () => { filteredByRoleCache = []; filteredMembers.value = Array.from({ length: 10 }, (i) => ({ userId: i, name: '', avatarUrl: '' }) as CorpMember); filterRoleId.value = ''; @@ -230,7 +229,7 @@ client.on('disconnected', () => { store.commit(types.SET_COMPENDIUM_CORP_LAST_ROLE_ID); }); onMounted(() => { - if (client.getUser()) { + if (client.value.getUser()) { fetchCorp(); } }); @@ -238,7 +237,7 @@ onMounted(() => { watch(filterRoleId, async (value) => { isFetching.value = true; store.commit(types.SET_COMPENDIUM_CORP_LAST_ROLE_ID, value); - filteredByRoleCache = await client.corpdata(value).then((r) => r.members); + filteredByRoleCache = await client.value.corpdata(value).then((r) => r.members); filteredMembers.value = filteredByRoleCache; isFetching.value = false; filterByTech(filterTech); @@ -248,7 +247,7 @@ watch(() => filterTech, filterByTech, { deep: true }); async function fetchCorp() { isFetching.value = true; - const resp = await client.corpdata(store.state.userSettings.compendiumCorpLastRoleId); + const resp = await client.value.corpdata(store.state.userSettings.compendiumCorpLastRoleId); data.value = resp; filteredByRoleCache = resp.members; diff --git a/src/utils/compendium.ts b/src/utils/compendium.ts index 3ddc1014..779af57e 100644 --- a/src/utils/compendium.ts +++ b/src/utils/compendium.ts @@ -1,17 +1,34 @@ -import { Compendium } from 'bot_client'; +import { Compendium as Client1 } from 'bot_client'; +import { Compendium as Client2 } from 'bot_client2'; +import { ref } from 'vue'; + +const clients = [Client1, Client2]; -const client = new Compendium(); let initialized = false; +const client = ref(new Client1()); + +const compendiumClient = localStorage.getItem('compendium_client'); +if (compendiumClient) { + switchInstance(parseInt(compendiumClient)); +} export default client; export async function init() { if (!initialized) { - await client.initialize(); + await client.value.initialize(); initialized = true; } } export function stop() { - client.shutdown(); + client.value.shutdown(); + initialized = false; +} + +export async function switchInstance(clientNum: number) { + stop(); + client.value = new clients[clientNum](); + localStorage.setItem('compendium_client', String(clientNum)); + await init(); } diff --git a/src/utils/getDiscordUrl.ts b/src/utils/getDiscordUrl.ts deleted file mode 100644 index 79ae7104..00000000 --- a/src/utils/getDiscordUrl.ts +++ /dev/null @@ -1,15 +0,0 @@ -const ENDPOINT = 'https://cdn.discordapp.com/'; - - -export function getDiscordAvatarUrl(userId: string, avatarHash: string, size = 80): URL { - const url = new URL(`avatars/${userId}/${avatarHash}.webp`, ENDPOINT); - - url.searchParams.set('size', String(size)); - return url; -} -export function getDiscordIconUrl(guildId: string, iconHash: string, size = 96): URL { - const url = new URL(`icons/${guildId}/${iconHash}.webp`, ENDPOINT); - - url.searchParams.set('size', String(size)); - return url; -}