You've already forked tf2wikipricing
refactor: moved components to separate modules
`content.ts` is now half less than the size, exported functions can be unit tested
This commit is contained in:
109
src/content/schemaService.ts
Normal file
109
src/content/schemaService.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { getStorageValue, setStorageValue } from './storage'
|
||||
import { logDebug, log, logError } from './utils/log'
|
||||
import './config'
|
||||
declare function GM_fetch(input: string | URL | globalThis.Request, init?: RequestInit): Promise<Response>
|
||||
import './GM_fetch'
|
||||
import { storage_version, storage_schema, storage_lastUpdateTime } from './config'
|
||||
const semver = require('semver')
|
||||
|
||||
export declare const __VERSION__: string;
|
||||
|
||||
function isDateAfterOneDay(date1: Date, date2: Date): boolean {
|
||||
var diff = date2.getTime() - date1.getTime();
|
||||
var diffDays = Math.round(diff / (1000 * 3600 * 24));
|
||||
return diffDays > 1;
|
||||
}
|
||||
|
||||
export class ItemSchema { [key: string]: {name: string, tradable: Boolean}; }
|
||||
|
||||
export function getItemIndexByName(schema: ItemSchema, name: string) {
|
||||
for (const [defindex, value] of Object.entries(schema)) {
|
||||
if (value['name'] == name) {
|
||||
return parseInt(defindex)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function getTradableStatusByDefindex(schema: ItemSchema, defindex: number) {
|
||||
return schema[defindex.toString()].tradable
|
||||
}
|
||||
|
||||
export function getTradableStatusByName(schema: ItemSchema, name: string) {
|
||||
for (const [defindex, value] of Object.entries(schema)) {
|
||||
if (value['name'] == name) {
|
||||
return value.tradable
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
export async function wipeSchema(): Promise<void> {
|
||||
await setStorageValue(storage_version, __VERSION__)
|
||||
await setStorageValue(storage_schema, null)
|
||||
await setStorageValue(storage_lastUpdateTime, new Date().toISOString())
|
||||
logDebug(`Schema wiped`)
|
||||
}
|
||||
|
||||
export async function prepareSchema(): Promise<ItemSchema> {
|
||||
var needsUpdate: Boolean = false
|
||||
var itemSchema: ItemSchema | null = null
|
||||
|
||||
const storedVersion: string | null = await getStorageValue(storage_version, null)
|
||||
if(!storedVersion || !semver.valid(storedVersion)) {
|
||||
log(`Cache is from an unknown version of the extension. Updating for version ${__VERSION__}`);
|
||||
needsUpdate = true
|
||||
} else if(semver.valid(storedVersion) && semver.lt(storedVersion, __VERSION__)) {
|
||||
log(`Cache is from a previous version (${storedVersion}) of the extension. Updating for version ${__VERSION__}`);
|
||||
needsUpdate = true
|
||||
} else {
|
||||
itemSchema = await getStorageValue(storage_schema, null);
|
||||
}
|
||||
|
||||
const update = await getStorageValue(storage_lastUpdateTime, null)
|
||||
if (update) {
|
||||
const lastUpdateTime = new Date(update);
|
||||
log(`Item schema updated at ${lastUpdateTime}`);
|
||||
if (itemSchema == null || Object.keys(itemSchema).length === 0 || isDateAfterOneDay(lastUpdateTime, new Date())) {
|
||||
needsUpdate = true
|
||||
}
|
||||
}
|
||||
|
||||
if(needsUpdate) {
|
||||
log("Item Schema out of Date. Rebuilding...");
|
||||
const url = "https://raw.githubusercontent.com/danocmx/node-tf2-static-schema/master/static/items.json"
|
||||
const response = await GM_fetch(url);
|
||||
if (response.ok) {
|
||||
await setStorageValue(storage_lastUpdateTime, new Date().getTime());
|
||||
|
||||
var cacheItems = {}
|
||||
|
||||
var responseItems: any[] = await response.json()
|
||||
// We want to keep the keys `defindex`, `item_name`, and `attributes`
|
||||
responseItems.forEach((item: any) => {
|
||||
const defindex = item['defindex']
|
||||
const name = item['item_name']
|
||||
var tradable: Boolean = true
|
||||
try {
|
||||
if(item['attributes'] != null) {
|
||||
if(item['attributes'].find((attribute: {}) => (attribute as any)['class'] == "cannot_trade")) {
|
||||
tradable = false
|
||||
}
|
||||
}
|
||||
} catch(error) {
|
||||
logError(error)
|
||||
log(item)
|
||||
}
|
||||
(cacheItems as any)[defindex.toString()] = { "name": name, "tradable": tradable }
|
||||
});
|
||||
|
||||
await setStorageValue(storage_schema, (cacheItems));
|
||||
itemSchema = cacheItems
|
||||
await setStorageValue(storage_version, __VERSION__);
|
||||
logDebug(`Item schema updated at ${new Date()}`)
|
||||
} else {
|
||||
logError("Could not fetch item schema.");
|
||||
}
|
||||
}
|
||||
return itemSchema
|
||||
}
|
||||
Reference in New Issue
Block a user