Skip to content

Commit

Permalink
Add link to download demos on wishlists
Browse files Browse the repository at this point in the history
  • Loading branch information
candela97 committed Jul 14, 2024
1 parent 819495c commit e818f54
Show file tree
Hide file tree
Showing 33 changed files with 128 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/js/Content/Features/Store/Wishlist/CWishlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import FExportWishlist from "./FExportWishlist";
import FKeepEditableRanking from "./FKeepEditableRanking";
import FOneClickRemoveFromWishlist from "./FOneClickRemoveFromWishlist";
import FWishlistProfileLink from "./FWishlistProfileLink";
import FWishlistDemoLink from "./FWishlistDemoLink";
import CStoreBase from "@Content/Features/Store/Common/CStoreBase";
import {ContextType} from "@Content/Modules/Context/ContextType";
import User from "@Content/Modules/User";
Expand Down Expand Up @@ -46,6 +47,7 @@ export default class CWishlist extends CStoreBase {
FKeepEditableRanking,
FOneClickRemoveFromWishlist,
FWishlistProfileLink,
FWishlistDemoLink,
] : []
);

Expand Down
90 changes: 90 additions & 0 deletions src/js/Content/Features/Store/Wishlist/FWishlistDemoLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type CWishlist from "@Content/Features/Store/Wishlist/CWishlist";
import Feature from "@Content/Modules/Context/Feature";
//import Settings from "@Options/Data/Settings";
import {L} from "@Core/Localization/Localization";
import {__downloadDemo} from "@Strings/_strings";
import LocalStorage from "@Core/Storage/LocalStorage";
import RequestData from "@Content/Modules/RequestData";

export default class FWishlistDemoLink extends Feature<CWishlist> {

private demoMap: Map<number, number> = new Map();

override async checkPrerequisites(): Promise<boolean> {

const now = Date.now();
let appids: number[] = this.context.wishlistData.map(({appid}) => Number(appid));

let cache = await LocalStorage.get("wl_demo_appids") ?? [];
if (cache.length > 0) {
const _appids = new Set(appids);
cache = cache.filter(({appid, demoAppid, expiry}) => {
if (!_appids.has(appid) || (expiry < now)) {
return false;
}
if (demoAppid) {
this.demoMap.set(appid, demoAppid);
}
_appids.delete(appid);
return true;
});
appids = Array.from(_appids);
}

// Split params into chunks as Steam may throw `HTTP 414 Request-URI Too Large`
const chunkSize = 400;

for (let i = 0; i < appids.length; i += chunkSize) {
const chunk = appids.slice(i, i + chunkSize);
const params = chunk.map(appid => `appids[]=${appid}`).join("&");

const data = await RequestData.getJson<{
success: number,
info?: {
appid: number,
demo_appid: number, // 0 if no demo
demo_package_id: number, // Not sure what this is for
}[],
}>(`https://store.steampowered.com/saleaction/ajaxgetdemoevents?${params}`).catch(err => console.error(err));
if (!data || !data.success) { continue; }

// Cache appids for 24 hrs if fetch is successful
chunk.forEach(appid => {

// `data.info` will be undefined if there're no demos for all given appids
const demoAppid = data.info?.find(val => Number(val.appid) === appid)?.demo_appid;
cache.push({
appid,
demoAppid,
expiry: now + (24 * 60 * 60 * 1000),
});

if (demoAppid) {
this.demoMap.set(appid, demoAppid);
}
});
}

await LocalStorage.set("wl_demo_appids", cache);
return this.demoMap.size > 0;
}

override apply(): void {
this.context.onWishlistUpdate.subscribe(e => {
const nodes = e.data;

for (const node of nodes) {

const appid = Number(node.dataset.appId);
const demoAppid = this.demoMap.get(appid);
if (demoAppid === undefined) { continue; }

const link = document.createElement("a");
link.href = `steam://install/${demoAppid}`;
link.textContent = L(__downloadDemo);
link.classList.add("earlyaccess");
node.querySelector(".platform_icons")!.prepend(link);
}
});
}
}
7 changes: 6 additions & 1 deletion src/js/Core/Storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ interface LocalStorageSchema extends StorageSchema {
language: string,
minPlaytime?: string,
maxPlaytime?: string
}
},
wl_demo_appids: Array<{
appid: number,
demoAppid: number|undefined,
expiry: number
}>,
}

class LocalStorage<Schema extends StorageSchema> extends Storage<ns.LocalStorageArea, Schema>{
Expand Down
1 change: 1 addition & 0 deletions src/localization/compiled/_strings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const __deleteCommentPrompt = "deleteCommentPrompt";
export const __downloadDemoHeader = "downloadDemoHeader";
export const __downloadDemo = "downloadDemo";
export const __deckCompat_header = "deckCompat_header";
export const __deckCompat_unknown = "deckCompat_unknown";
export const __deckCompat_unsupported = "deckCompat_unsupported";
Expand Down
2 changes: 1 addition & 1 deletion src/localization/compiled/bg.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/cs.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/da.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/de.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/el.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/en.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/es-419.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/es-ES.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/fi.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/fr.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/hu.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/it.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/ja.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/ko.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/nl.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/no.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/pl.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/pt-BR.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/pt-PT.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/ro.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/ru.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/sv-SE.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/th.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/tr.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/ua.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/vi.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/zh-CN.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/localization/compiled/zh-TW.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/localization/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"delete_comment_prompt": "Are you sure you want to delete this comment?",
"download_demo_header": "Download __gamename__ Demo",
"download_demo": "Download Demo",
"deck_compat": {
"header": "Steam Deck Compatibility",
"unknown": "Unknown",
Expand Down

0 comments on commit e818f54

Please sign in to comment.