Skip to content

Commit

Permalink
Refactor game id objects, merge with factories
Browse files Browse the repository at this point in the history
  • Loading branch information
tfedor committed Nov 29, 2024
1 parent 9cd4909 commit 8c25359
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/js/Content/Features/Store/Wishlist/FWishlistHighlights.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type CWishlist from "@Content/Features/Store/Wishlist/CWishlist";
import Feature from "@Content/Modules/Context/Feature";
import HighlightsTagsUtils2, {EHighlightStyle, type ItemSetup} from "@Content/Modules/Highlights/HighlightsTagsUtils2";
import {Appid} from "@Content/Modules/Highlights/StoreIds";
import AppId from "@Core/GameId/AppId";

export default class FWishlistHighlights extends Feature<CWishlist> {

Expand All @@ -26,7 +26,7 @@ export default class FWishlistHighlights extends Feature<CWishlist> {
return;
}

const appids = this.context.wishlistData?.map(entry => new Appid(entry.appid)) ?? [];
const appids = this.context.wishlistData?.map(entry => new AppId(entry.appid)) ?? [];

if (appids.length === 0) {
return;
Expand Down
8 changes: 4 additions & 4 deletions src/js/Content/Features/Store/Wishlist/Utils/WishlistDOM.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Appid} from "@Content/Modules/Highlights/StoreIds";
import ASEventHandler from "@Content/Modules/ASEventHandler";
import AppId from "@Core/GameId/AppId";

interface TDOMGame {
node: HTMLElement,
appid?: Appid,
appid?: AppId,
title?: {
node: HTMLElement,
value: string|null,
Expand Down Expand Up @@ -76,9 +76,9 @@ export class WishlistDOM {
return parent.querySelector<HTMLAnchorElement>("a.Fuz2JeT4RfI-[href*='/app/']");
}

appid(anchorNode: HTMLAnchorElement): Appid|null {
appid(anchorNode: HTMLAnchorElement): AppId|null {
const m = anchorNode.href.match(/app\/(\d+)/)!;
return m ? new Appid(Number(m[1])) : null;
return m ? new AppId(Number(m[1])) : null;
}

observe(): void {
Expand Down
3 changes: 3 additions & 0 deletions src/js/Content/Modules/Highlights/HighlightsTagsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Options = {
coupon: boolean,
}

/**
* @deprecated
*/
export default class HighlightsTagsUtils {

private static _highlightCssLoaded: boolean = false;
Expand Down
6 changes: 3 additions & 3 deletions src/js/Content/Modules/Highlights/HighlightsTagsUtils2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import Settings from "@Options/Data/Settings";
import DynamicStore from "@Content/Modules/Data/DynamicStore";
import ITAD from "@Content/Modules/ITAD";
import InventoryApiFacade from "@Content/Modules/Facades/InventoryApiFacade";
import type {StoreId} from "@Content/Modules/Highlights/StoreIds";
import DOMHelper from "@Content/Modules/DOMHelper";
import Tags from "@Content/Modules/Highlights/Tags.svelte";
import type GameId from "@Core/GameId/GameId";

export const enum EHighlightStyle {
BgGradient,
Expand Down Expand Up @@ -97,14 +97,14 @@ export default class HighlightsTagsUtils2 {
DOMHelper.insertCSS(css.join("\n"));
}

public async query(ids: StoreId[]): Promise<Map<string, ItemSetup>> {
public async query(ids: GameId[]): Promise<Map<string, ItemSetup>> {
const result: Map<string, ItemSetup> = new Map();

if (ids.length === 0) {
return result;
}

let query: StoreId[] = [];
let query: GameId[] = [];

for(let id of ids) {
if (this.cache.has(id.string)) {
Expand Down
42 changes: 0 additions & 42 deletions src/js/Content/Modules/Highlights/StoreIds.ts

This file was deleted.

8 changes: 7 additions & 1 deletion src/js/Core/GameId/AppId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import GameId from "@Core/GameId/GameId";

export default class AppId {
// TODO refactor to return instance
export default class AppId extends GameId {

static fromUrl(url: string) {
const m = url.match(/(?:store\.steampowered|steamcommunity)\.com\/(?:app|games|market\/listings)\/(\d+)\/?/);
Expand Down Expand Up @@ -65,4 +67,8 @@ export default class AppId {
}
return result;
}

constructor(id: number) {
super("app", id);
}
}
8 changes: 7 additions & 1 deletion src/js/Core/GameId/BundleId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import GameId from "@Core/GameId/GameId";

export default class BundleId {
// TODO refactor to return instance
export default class BundleId extends GameId{

static fromUrl(url: string): number|null {
const m = url.match(/(?:store\.steampowered|steamcommunity)\.com\/bundle\/(\d+)\/?/);
Expand All @@ -25,4 +27,8 @@ export default class BundleId {

return this.fromUrl(href);
}

constructor(id: number) {
super("bundle", id);
}
}
24 changes: 21 additions & 3 deletions src/js/Core/GameId/GameId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
export default class GameId {

static trimStoreId(storeId: string): number {
return Number(storeId.slice(storeId.indexOf("/") + 1));
export default abstract class GameId {

private readonly full: string;

protected constructor(
private type_: "app"|"sub"|"bundle",
private id_: number
) {
this.full = `${this.type_}/${this.id_}`;
}

get type(): "app"|"sub"|"bundle" {
return this.type_;
}

get number(): number {
return this.id_;
}

get string(): string {
return this.full;
};
}
9 changes: 8 additions & 1 deletion src/js/Core/GameId/SubId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default class SubId {
import GameId from "@Core/GameId/GameId";

// TODO refactor to return instance
export default class SubId extends GameId{

static fromUrl(url: string): number|null {
const m = url.match(/(?:store\.steampowered|steamcommunity)\.com\/sub\/(\d+)\/?/);
Expand All @@ -24,4 +27,8 @@ export default class SubId {

return this.fromUrl(href);
}

constructor(id: number) {
super("sub", id);
}
}

0 comments on commit 8c25359

Please sign in to comment.