From 182c30b4ee112f08c80a5d2051d315fe1d6654b4 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 17 Mar 2024 17:14:44 -0400 Subject: [PATCH] Fix loading of main menu community profiles --- .../src/api/save_setting.rs | 2 +- backend/src/api/web.rs | 22 +++++++------------ src/backend.ts | 8 +++---- src/consts.ts | 2 ++ src/index.tsx | 11 ++++++++-- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/backend/community_settings_srv/src/api/save_setting.rs b/backend/community_settings_srv/src/api/save_setting.rs index 39d610e..173364f 100644 --- a/backend/community_settings_srv/src/api/save_setting.rs +++ b/backend/community_settings_srv/src/api/save_setting.rs @@ -11,7 +11,7 @@ pub async fn save_setting_handler( content_type: web::Header, cli: web::Data<&'static Cli>, ) -> std::io::Result { - println!("Content-Type: {}", content_type.to_string()); + //println!("Content-Type: {}", content_type.to_string()); let bytes = match data.to_bytes_limited(PAYLOAD_LIMIT).await { Ok(Ok(x)) => x, Ok(Err(e)) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, format!("wut: {}", e))), diff --git a/backend/src/api/web.rs b/backend/src/api/web.rs index acd467c..9301064 100644 --- a/backend/src/api/web.rs +++ b/backend/src/api/web.rs @@ -5,10 +5,8 @@ use std::sync::{Arc, Mutex}; use usdpl_back::core::serdes::Primitive; use usdpl_back::AsyncCallable; -use chrono::{offset::Utc, DateTime}; -use serde::{Deserialize, Serialize}; - use super::handler::{ApiMessage, GeneralMessage}; +use crate::utility::CachedData; #[cfg(feature = "online")] const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us"; @@ -18,12 +16,6 @@ static BASE_URL: RwLock> = RwLock::new(None); const MAX_CACHE_DURATION: std::time::Duration = std::time::Duration::from_secs(60 * 60 * 24 * 7 /* 7 days */); -#[derive(Serialize, Deserialize, Clone, Debug)] -struct CachedData { - data: T, - updated: DateTime, -} - type StoreCache = std::collections::HashMap>>; @@ -112,14 +104,14 @@ fn save_cache(_cache: &StoreCache) {} fn get_maybe_cached(steam_app_id: u32) -> Vec { let mut cache = load_cache(); let data = if let Some(cached_result) = cache.get(&steam_app_id) { - if cached_result.updated < (Utc::now() - MAX_CACHE_DURATION) { + if cached_result.needs_update(MAX_CACHE_DURATION) { // cache needs update if let Ok(result) = search_by_app_id_online(steam_app_id) { cache.insert( steam_app_id, CachedData { data: result.clone(), - updated: Utc::now(), + updated: chrono::offset::Utc::now(), }, ); save_cache(&cache); @@ -138,7 +130,7 @@ fn get_maybe_cached(steam_app_id: u32) -> Vec impl AsyncCallable { }; super::async_utils::AsyncIsh { trans_setter: |params| { - if let Some(Primitive::F64(app_id)) = params.get(0) { - Ok(*app_id as u32) + if let Some(Primitive::String(s)) = params.get(0) { + s.parse::().map_err(|e| format!("search_by_app_id invalid parameter 0: {}", e)) } else { Err("search_by_app_id missing/invalid parameter 0".to_owned()) } @@ -329,6 +321,8 @@ fn settings_to_web_config( username: String, settings: crate::persist::SettingsJson, ) -> community_settings_core::v1::Metadata { + #[cfg(any(not(debug_assertions), not(feature = "dev_stuff")))] + let app_id = if app_id == 0 { 1 } else { app_id }; community_settings_core::v1::Metadata { name: settings.name, steam_app_id: app_id, diff --git a/src/backend.ts b/src/backend.ts index 84be8a9..fa3d276 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -376,8 +376,8 @@ export type StoreMetadata = { //config: any, } -export async function searchStoreByAppId(id: number): Promise { - console.log("WEB_search_by_app"); +export async function searchStoreByAppId(id: string): Promise { + //console.log("WEB_search_by_app"); return (await call_backend("WEB_search_by_app", [id]))[0]; } @@ -395,11 +395,11 @@ export async function storeUpload(steam_id: string, steam_username: string): Pro } export async function getAllSettingVariants(): Promise { - console.log("GENERAL_get_all_variants"); + //console.log("GENERAL_get_all_variants"); return (await call_backend("GENERAL_get_all_variants", [])); } export async function getCurrentSettingVariant(): Promise { - console.log("GENERAL_get_current_variant"); + //console.log("GENERAL_get_current_variant"); return (await call_backend("GENERAL_get_current_variant", []))[0]; } diff --git a/src/consts.ts b/src/consts.ts index 8fc068a..b938fe4 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -44,4 +44,6 @@ export const PERIODICAL_BACKEND_PERIOD = 5000; // milliseconds export const AUTOMATIC_REAPPLY_WAIT = 2000; // milliseconds export const STORE_RESULTS_URI = "/plugins/PowerTools/settings_store"; +export const STORE_MAIN_APP_ID = "1"; +export const STORE_MAIN_APP_ID_DEV = "0"; diff --git a/src/index.tsx b/src/index.tsx index 257db52..f392f20 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -67,6 +67,7 @@ import { STORE_RESULTS, STORE_RESULTS_URI, + STORE_MAIN_APP_ID, PERIODICAL_BACKEND_PERIOD, AUTOMATIC_REAPPLY_WAIT, @@ -130,7 +131,7 @@ const reload = function() { }); if (!get_value(STORE_RESULTS)) { - backend.resolve(backend.searchStoreByAppId(0), (results) => set_value(STORE_RESULTS, results)); + backend.resolve(backend.searchStoreByAppId(STORE_MAIN_APP_ID), (results) => set_value(STORE_RESULTS, results)); } backend.resolve(backend.getBatteryCurrent(), (rate: number) => { set_value(CURRENT_BATT, rate) }); @@ -216,13 +217,19 @@ const registerCallbacks = function(autoclear: boolean) { }); } ); + backend.resolve( + backend.searchStoreByAppId(STORE_MAIN_APP_ID), + (results: backend.StoreMetadata[]) => { + set_value(STORE_RESULTS, results); + } + ); } }); //@ts-ignore startHook = SteamClient.Apps.RegisterForGameActionStart((actionType, id) => { //@ts-ignore let gameInfo: any = appStore.GetAppOverviewByGameID(id); - let appId = gameInfo.appid.toString(); + let appId: string = gameInfo.appid.toString(); backend.log(backend.LogLevel.Info, "RegisterForGameActionStart callback(" + actionType + ", " + id + ")"); backend.resolve(