Skip to content

Commit

Permalink
Return info about how many times game was bundled, and a lot of relat…
Browse files Browse the repository at this point in the history
…ed refactors
  • Loading branch information
tfedor committed Mar 15, 2024
1 parent c97941d commit b5eca00
Show file tree
Hide file tree
Showing 18 changed files with 291 additions and 154 deletions.
1 change: 1 addition & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- remove some deprecated features
- fix bundle header showing even when there are no bundles listed
- fix historical low showing incorrect shop
- return info about how many times game was bundled
3.0.0: |
Maintenance release to make sure Augmented Steam plays well with the updated ITAD and backing server.
Expand Down
40 changes: 0 additions & 40 deletions src/css/augmentedsteam.css
Original file line number Diff line number Diff line change
Expand Up @@ -304,46 +304,6 @@ img.astats_icon {
margin-top: 10px;
}

.itad-pricing {
padding: 5px 5px 5px 42px;
height: auto !important;
border-bottom: 0;
font-size: 12px;
color: #a8b2ba;
background-color: rgba(0, 0, 0, 0.2);
background-repeat: no-repeat;
background-position: 9px center;
background-size: 24px;
display: grid;
grid-template-columns: min-content min-content auto;
grid-column-gap: 10px;
white-space: nowrap;
line-height: 1.5;
align-items: baseline;
}
.itad-pricing__main {
white-space: normal;
}
.itad-pricing__cut {
color: #a4d007;
}
.itad-pricing__price {
font-size: 1.1em;
color: #acdbf5;
text-align: right;
}
.itad-pricing__voucher {
font-weight: bold;
font-size: 1.1em;
}
.itad-pricing__drm {
text-transform: uppercase;
color: #626366;
}
.itad-pricing__bundled {
grid-column: 2 / span 2
}

.wishlist_row {
overflow: visible !important;
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/Background/EStoreName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export enum EStoreName {
StoreList = "storeList"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Errors} from "../../modulesCore";
import {IndexedDB} from "./IndexedDB";
import Config from "../../config";
import {Api} from "./Api";
import {Errors} from "../../../modulesCore";
import {IndexedDB} from "../IndexedDB";
import Config from "../../../config";
import {Api} from "../Api";
import type {TFetchPricesResponse} from "./_types";

class AugmentedSteamApi extends Api {

Expand Down Expand Up @@ -35,19 +36,19 @@ class AugmentedSteamApi extends Api {
return AugmentedSteamApi.endpointFactory(`similar/${appid}/v2`)({"count": 15});
}

static async fetchPrices(params/*: {
static async fetchPrices(
country: string,
apps?: number[],
subs?: number[],
bundles?: number[],
apps: number[],
subs: number[],
bundles: number[],
voucher: boolean,
shops: number[]
}*/) {
): Promise<TFetchPricesResponse> {
const url = new URL("prices/v2", Config.ApiServerHost);

let response = await fetch(url, {
method: "POST",
body: JSON.stringify(params)
body: JSON.stringify({country, apps, subs, bundles, voucher, shops})
});

if (response.ok) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface TShop {
interface TShop {
id: number,
name: string,
}
Expand All @@ -9,17 +9,17 @@ export interface TPrice {
currency: string
}

export interface TDrm {
interface TDrm {
id: number,
name: string
}

export interface TPlatform {
interface TPlatform {
id: number,
name: string
}

export interface TCurrent {
interface TCurrent {
shop: TShop,
price: TPrice,
regular: TPrice,
Expand All @@ -33,7 +33,7 @@ export interface TCurrent {
url: string
}

export interface TLowest {
interface TLowest {
shop: TShop,
price: TPrice,
regular: TPrice,
Expand All @@ -44,6 +44,7 @@ export interface TLowest {
export interface TPriceOverview {
current: TCurrent|null,
lowest: TLowest|null,
bundled: number,
urls: {
info: string,
history: string
Expand All @@ -69,15 +70,33 @@ export interface TBundle {
tiers: TTier[]
}

export interface TTier {
interface TTier {
price: TPrice|null,
games: TBundleGame[]
}

export interface TBundleGame {
interface TBundleGame {
id: string,
slug: string,
title: string,
type: string|null,
mature: boolean
}


export interface TFetchPricesMessage {
action: "prices",
params: {
country: string,
apps: number[],
subs: number[],
bundles: number[],
voucher: boolean,
shops: number[]
}
}

export interface TFetchPricesResponse {
prices: Record<string, TPriceOverview>,
bundles: TBundle[]
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {Errors, LocalStorage, SyncedStorage, TimeUtils} from "../../modulesCore";
import {Api} from "./Api";
import Config from "../../config";
import {IndexedDB} from "./IndexedDB";
import {Errors, LocalStorage, SyncedStorage, TimeUtils} from "../../../modulesCore";
import {Api} from "../Api";
import Config from "../../../config";
import {IndexedDB} from "../IndexedDB";
import type {TGetStoreListResponse} from "./_types";
import {EStoreName} from "../../EStoreName";

const MAX_ITEMS_PER_REQUEST = 1000;

class ITADApi extends Api {

static async getStoreList() {
return Object.values(await IndexedDB.getAll("storeList"));
static async getStoreList(): Promise<TGetStoreListResponse> {
return Object.values(await IndexedDB.getAll(EStoreName.StoreList));
}

static async fetchStoreList() {
Expand Down
14 changes: 14 additions & 0 deletions src/js/Background/Modules/IsThereAnyDeal/_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

interface TShopInfo {
id: number,
title: string,
deals: number,
games: number,
update: string
}

export interface TGetStoreListMessage {
action: "itad.storelist"
}

export type TGetStoreListResponse = TShopInfo[]
106 changes: 76 additions & 30 deletions src/js/Background/background.js → src/js/Background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import {IndexedDB} from "./Modules/IndexedDB";
import {SteamCommunityApi} from "./Modules/SteamCommunityApi";
import {SteamStoreApi} from "./Modules/SteamStoreApi";
import {StaticResources} from "./Modules/StaticResources";
import {ITADApi} from "./Modules/ITADApi";
import {AugmentedSteamApi} from "./Modules/AugmentedSteamApi";
import {ITADApi} from "./Modules/IsThereAnyDeal/ITADApi";
import {AugmentedSteamApi} from "./Modules/AugmentedSteam/AugmentedSteamApi";
import {ExtensionData} from "./Modules/ExtensionData";
import CacheStorage from "./Modules/CacheStorage";
import browser, {type Runtime} from "webextension-polyfill";
import type {TFetchPricesMessage} from "./Modules/AugmentedSteam/_types";
import type {TGetStoreListMessage} from "./Modules/IsThereAnyDeal/_types";

type MessageSender = Runtime.MessageSender;

// Functions that are called when an object store (or one of its entries) has expired
IndexedDB.objStoreFetchFns = new Map([
Expand Down Expand Up @@ -72,7 +77,6 @@ const actionCallbacks = new Map([
["dlcinfo", AugmentedSteamApi.fetchDlcInfo],
["storepagedata", AugmentedSteamApi.storePageData],
["storepagedata.expire", AugmentedSteamApi.expireStorePageData],
["prices", AugmentedSteamApi.fetchPrices],
["rates", AugmentedSteamApi.rates],
["clearrates", AugmentedSteamApi.clearRates],
["isea", AugmentedSteamApi.isEA],
Expand Down Expand Up @@ -117,41 +121,83 @@ const actionCallbacks = new Map([
["itad.removefromwaitlist", ITADApi.removeFromWaitlist],
["itad.incollection", ITADApi.inCollection],
["itad.getfromcollection", ITADApi.getFromCollection],
["itad.storelist", ITADApi.getStoreList],

["error.test", () => { return Promise.reject(new Error("This is a TEST Error. Please ignore.")); }],
]);

/*
* new Map() for Map.prototype.get() in lieu of:
* Object.prototype.hasOwnProperty.call(actionCallbacks, message.action)
*/

browser.runtime.onMessage.addListener(async(message, sender) => {
if (!sender || !sender.tab) { return null; } // not from a tab, ignore
if (!message || !message.action) { return null; }
/** @deprecated */
type GenericMessage = {
action: string,
params?: any
};

type Message =
// ITADApi
| TGetStoreListMessage
// AugmentedSteamApi
| TFetchPricesMessage
// old
| GenericMessage;

browser.runtime.onMessage.addListener((
message: Message,
sender: MessageSender,
sendResponse: (...params: any) => void
): true|void => {

if (!sender || !sender.tab) { // not from a tab, ignore
return;
}
if (!message || !message.action) {
return;
}

const callback = actionCallbacks.get(message.action);
if (!callback) {
(async function() {
try {
let response: any;

// requested action not recognized, reply with error immediately
throw new Error(`Did not recognize "${message.action}" as an action.`);
}
switch(message.action) { // TODO rename to "api"?

message.params = message.params || [];
let res;
try {
await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]);
res = await callback(...message.params);
} catch (err) {
console.group(`Callback: "${message.action}"`);
console.error('Failed to execute callback "%s" with params %o', message.action, message.params);
console.error(err);
console.groupEnd();

throw new Error(err.toString());
}
return res;
case "itad.storelist":
response = await ITADApi.getStoreList();
break;

case "prices":
let {country, apps, subs, bundles, voucher, shops} = message.params;
response = await AugmentedSteamApi.fetchPrices(country, apps, subs, bundles, voucher, shops);
break

default:
/*
* TODO deprecated
* Old handling, remove once we rewrite all handlers to explicit style above
*/
const callback = actionCallbacks.get(message.action);
if (!callback) {

// requested action not recognized, reply with error immediately
throw new Error(`Did not recognize "${message.action}" as an action.`);
}

const params = (<GenericMessage>message).params || []

await Promise.all([IndexedDB, CacheStorage, LocalStorage, SyncedStorage.then(() => { setup(); })]);
response = await callback(...params);

}

sendResponse(response);
} catch (err) {
console.group(`Callback: "${message.action}"`);
console.error('Failed to execute %o', message);
console.error(err);
console.groupEnd();

throw new Error((<any>err).toString());
}
})();
return true;
});

browser.runtime.onStartup.addListener(ContextMenu.update);
Expand Down
2 changes: 1 addition & 1 deletion src/js/Content/Features/Store/Common/FITADPrices.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class FITADPrices extends Feature {
new PriceOverview({
target,
anchor,
props: {id, data}
props: {data}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export default class FWishlistITADPrices extends CallbackFeature {
return new PriceOverview({
target: node,
props: {
id,
data,
setBottom: true
}
Expand Down
19 changes: 19 additions & 0 deletions src/js/Content/Modules/Facades/AugmentedSteamApiFacade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {BackgroundSender} from "../../../Core/BackgroundSimple";
import type {TFetchPricesMessage, TFetchPricesResponse} from "../../../Background/Modules/AugmentedSteam/_types";

export default class AugmentedSteamApiFacade {

static fetchPrices(
country: string,
apps: number[],
subs: number[],
bundles: number[],
voucher: boolean,
shops: number[]
): Promise<TFetchPricesResponse> {
return BackgroundSender.send<TFetchPricesMessage, TFetchPricesResponse>({
action: "prices",
params: {country, apps, subs, bundles, voucher, shops}
});
}
}
Loading

0 comments on commit b5eca00

Please sign in to comment.