From 1a2aacbba26a01a19790a50ecbd31bf4f80b6c5f Mon Sep 17 00:00:00 2001 From: Natsumi Date: Thu, 18 May 2023 10:32:42 +1200 Subject: [PATCH] Fix auto updater toggle, load friendsList tab much faster --- .vscode/settings.json | 8 +--- html/src/app.js | 67 ++++++++++++++++++++++++++++--- html/src/mixins/tabs/settings.pug | 2 +- html/src/repository/database.js | 47 ++++++++++++++++++++++ 4 files changed, 111 insertions(+), 13 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f84c3a3..5bbe8c61 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,11 +3,5 @@ "i18n-ally.keystyle": "nested", "i18n-ally.sourceLanguage": "en", "editor.defaultFormatter": "esbenp.prettier-vscode", - "editor.formatOnSave": true, - "[javascript]": { - "editor.defaultFormatter": "vscode.typescript-language-features" - }, - "[jade]": { - "editor.defaultFormatter": "svipas.prettier-plus" - } + "editor.formatOnSave": true } diff --git a/html/src/app.js b/html/src/app.js index bf2e96c6..2a1ed258 100644 --- a/html/src/app.js +++ b/html/src/app.js @@ -19363,17 +19363,74 @@ speechSynthesis.getVoices(); continue; } } - var inCurrentWorld = false; - if (this.lastLocation.playerList.has(ctx.ref.displayName)) { - inCurrentWorld = true; - } - this.getUserStats(ctx.ref, inCurrentWorld); ctx.ref.$friendNum = ctx.no; results.push(ctx.ref); } + this.getAllUserStats(); this.friendsListTable.data = results; }; + $app.methods.getAllUserStats = function () { + var userIds = []; + var displayNames = []; + for (var ctx of this.friends.values()) { + userIds.push(ctx.id); + if (ctx.ref?.displayName) { + displayNames.push(ctx.ref.displayName); + } + } + + database.getAllUserStats(userIds, displayNames).then((data) => { + var friendListMap = new Map(); + for (var item of data) { + if (!item.userId) { + // find userId from previous data with matching displayName + for (var ref of data) { + if ( + ref.displayName === item.displayName && + ref.userId + ) { + item.userId = ref.userId; + } + } + // if still no userId, find userId from friends list + if (!item.userId) { + for (var ref of this.friends.values()) { + if ( + ref?.ref?.id && + ref.ref.displayName === item.displayName + ) { + item.userId = ref.id; + } + } + } + // if still no userId, skip + if (!item.userId) { + continue; + } + } + + var friend = friendListMap.get(item.userId); + if (!friend) { + friendListMap.set(item.userId, item); + continue; + } + friend.timeSpent += item.timeSpent; + friend.joinCount += item.joinCount; + friend.displayName = item.displayName; + friendListMap.set(item.userId, friend); + } + for (var item of friendListMap.values()) { + var ref = this.friends.get(item.userId); + if (ref?.ref) { + ref.ref.$joinCount = item.joinCount; + ref.ref.$lastSeen = item.created_at; + ref.ref.$timeSpent = item.timeSpent; + } + } + }); + }; + $app.methods.getUserStats = async function (ctx) { var ref = await database.getUserStats(ctx); /* eslint-disable require-atomic-updates */ diff --git a/html/src/mixins/tabs/settings.pug b/html/src/mixins/tabs/settings.pug index dcec1f7a..29521572 100644 --- a/html/src/mixins/tabs/settings.pug +++ b/html/src/mixins/tabs/settings.pug @@ -25,7 +25,7 @@ mixin simpleRadioGroup(nameTrKey, model, options, onChange="") br el-radio-group(v-model=model @change=onChange size="mini") each option in options - el-radio-button(label="#{option.label}") {{ $t('#{option.translationKey}') }} + el-radio-button(label=option.label) {{ $t('#{option.translationKey}') }} mixin settingsTab() .x-container(v-show="$refs.menu && $refs.menu.activeIndex === 'settings'") diff --git a/html/src/repository/database.js b/html/src/repository/database.js index 029d212b..8b436724 100644 --- a/html/src/repository/database.js +++ b/html/src/repository/database.js @@ -1042,6 +1042,53 @@ class Database { return ref; } + async getAllUserStats(userIds, displayNames) { + var data = []; + // this makes me most sad + var userIdsString = ''; + for (var userId of userIds) { + userIdsString += `'${userId}', `; + } + userIdsString = userIdsString.slice(0, -2); + var displayNamesString = ''; + for (var displayName of displayNames) { + displayNamesString += `'${displayName.replaceAll("'", "''")}', `; + } + displayNamesString = displayNamesString.slice(0, -2); + + await sqliteService.execute( + (dbRow) => { + var row = { + created_at: dbRow[0], + userId: dbRow[1], + timeSpent: dbRow[2], + joinCount: dbRow[3], + displayName: dbRow[4] + }; + data.push(row); + }, + `SELECT + g.created_at, + g.user_id, + SUM(g.time) AS timeSpent, + COUNT(DISTINCT g.location) AS joinCount, + g.display_name, + MAX(g.id) AS max_id + FROM + gamelog_join_leave g + WHERE + g.user_id IN (${userIdsString}) + OR g.display_name IN (${displayNamesString}) + GROUP BY + g.user_id, + g.display_name + ORDER BY + g.user_id DESC + ` + ); + return data; + } + async lookupFeedDatabase(search, filters, vipList) { var search = search.replaceAll("'", "''"); var vipQuery = '';