Skip to content

Commit

Permalink
Merge pull request #307 from the-hideout/game-mode
Browse files Browse the repository at this point in the history
Support multiple game modes
  • Loading branch information
Razzmatazzz authored Jun 25, 2024
2 parents 6514957 + f5bbdb5 commit 792c234
Show file tree
Hide file tree
Showing 32 changed files with 986 additions and 870 deletions.
28 changes: 18 additions & 10 deletions custom-endpoints/nightbot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ export default async function (request, data) {
//console.log(`Skipping cache in ${ENVIRONMENT} environment`);
}

const context = {
data,
util: graphqlUtil,
requestId,
lang: {},
warnings: [],
errors: [],
};
const context = graphqlUtil.getDefaultContext(data, requestId);
const info = {
path: {
key: 'query',
Expand All @@ -68,7 +61,22 @@ export default async function (request, data) {
value: 'lang',
},
value: {
value: url.searchParams.get('l') || 'en'
value: url.searchParams.get('l') || 'en',
}
}
]
},
{
name: {
value: 'query'
},
arguments: [
{
name: {
value: 'gameMode',
},
value: {
value: url.searchParams.get('m') || 'regular',
}
}
]
Expand All @@ -77,7 +85,7 @@ export default async function (request, data) {
}
}
};
const items = await data.item.getItemsByName(context, url.searchParams.get('q'), info);
const items = await data.item.getItemsByName(context, info, url.searchParams.get('q'));

let responseBody = 'Found no item matching that name';

Expand Down
10 changes: 5 additions & 5 deletions datasources/archived-prices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import WorkerKVSplit from '../utils/worker-kv-split.mjs';
class ArchivedPricesAPI extends WorkerKVSplit {
constructor(dataSource) {
super('archived_price_data', dataSource);
this.addGameMode('pve');
}

async getByItemId(context, itemId) {
await this.init(context, itemId);
const data = await this.getKVData(context, itemId);
if (!data) {
async getByItemId(context, info, itemId) {
const { cache } = await this.getCache(context, info, itemId);
if (!cache) {
return Promise.reject(new Error('Archived prices data is empty'));
}

let prices = data.ArchivedPrices[itemId];
let prices = cache.ArchivedPrices[itemId];
if (!prices) {
return [];
}
Expand Down
31 changes: 16 additions & 15 deletions datasources/barters.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ const isBothDogtags = id => {
class BartersAPI extends WorkerKV {
constructor(dataSource) {
super('barter_data', dataSource);
this.gameModes.push('pve');
}

async getList(context) {
await this.init(context);
return this.cache.Barter;
async getList(context, info) {
const { cache } = await this.getCache(context, info);
return cache.Barter;
}

async getBartersForItem(context, id) {
await this.init(context);
return this.cache.Barter.filter(barter => {
async getBartersForItem(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Barter.filter(barter => {
for (const item of barter.rewardItems) {
if (item.item === id) return true;
if (item.baseId === id) return true;
Expand All @@ -29,9 +30,9 @@ class BartersAPI extends WorkerKV {
});
}

async getBartersUsingItem(context, id) {
await this.init(context);
return this.cache.Barter.filter(barter => {
async getBartersUsingItem(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Barter.filter(barter => {
for (const item of barter.requiredItems) {
if (item.item === id) return true;
if (isBothDogtags(id) && isAnyDogtag(item.item)) {
Expand All @@ -45,17 +46,17 @@ class BartersAPI extends WorkerKV {
});
}

async getBartersForTrader(context, id) {
await this.init(context);
return this.cache.Barter.filter(barter => {
async getBartersForTrader(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Barter.filter(barter => {
if (barter.trader_id === id) return true;
return false;
});
}

async getBartersForTraderLevel(context, id, level) {
await this.init(context);
return this.cache.Barter.filter(barter => {
async getBartersForTraderLevel(context, info, id, level) {
const { cache } = await this.getCache(context, info);
return cache.Barter.filter(barter => {
if (barter.trader_id === id && barter.level === level) return true;
return false;
});
Expand Down
36 changes: 18 additions & 18 deletions datasources/crafts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,40 @@ class CraftsAPI extends WorkerKV {
super('craft_data', dataSource);
}

async getList(context) {
await this.init(context);
return this.cache.Craft;
async getList(context, info) {
const { cache } = await this.getCache(context, info);
return cache.Craft;
}

async get(context, id) {
await this.init(context);
return this.cache.Craft.filter(c => c.id === id);
async get(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Craft.filter(c => c.id === id);
}

async getCraftsForItem(context, id) {
await this.init(context);
return this.cache.Craft.filter(craft => {
async getCraftsForItem(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Craft.filter(craft => {
return craft.rewardItems.some(rew => rew.item === id);
});
}

async getCraftsUsingItem(context, id) {
await this.init(context);
return this.cache.Craft.filter(craft => {
async getCraftsUsingItem(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Craft.filter(craft => {
return craft.requiredItems.some(req => req.item === id);
});
}

async getCraftsForStation(context, id) {
await this.init(context);
return this.cache.Craft.filter(craft => {
async getCraftsForStation(context, info, id) {
const { cache } = await this.getCache(context, info);
return cache.Craft.filter(craft => {
return craft.station_id === id;
});
}

async getCraftsForStationLevel(context, id, level) {
await this.init(context);
return this.cache.Craft.filter(craft => {
async getCraftsForStationLevel(context, info, id, level) {
const { cache } = await this.getCache(context, info);
return cache.Craft.filter(craft => {
return craft.station_id === id && craft.level === level;
});
}
Expand Down
36 changes: 18 additions & 18 deletions datasources/hideout.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class HideoutAPI extends WorkerKV {
super('hideout_data', dataSource);
}

async getList(context) {
await this.init(context);
return this.cache.HideoutStation;
async getList(context, info) {
const { cache } = await this.getCache(context, info);
return cache.HideoutStation;
}

async getModuleById(context, id) {
await this.init(context);
for (const hideoutStation of this.cache.HideoutStation) {
async getModuleById(context, info, id) {
const { cache } = await this.getCache(context, info);
for (const hideoutStation of cache.HideoutStation) {
for (const stage of hideoutStation.levels) {
if (stage.id === id) {
return stage;
Expand All @@ -22,9 +22,9 @@ class HideoutAPI extends WorkerKV {
return Promise.reject(new Error(`No hideout station level found with id ${id}`));
}

async getModuleByLevel(context, stationId, level) {
await this.init(context);
for (const hideoutStation of this.cache.HideoutStation) {
async getModuleByLevel(context, info, stationId, level) {
const { cache } = await this.getCache(context, info);
for (const hideoutStation of cache.HideoutStation) {
if (hideoutStation.id !== stationId) continue;
for (const stage of hideoutStation.levels) {
if (stage.level === level) {
Expand All @@ -35,22 +35,22 @@ class HideoutAPI extends WorkerKV {
return Promise.reject(new Error(`No hideout station level found with id ${stationId} and level ${level}`));
}

async getStation(context, id) {
await this.init(context);
for (const station of this.cache.HideoutStation) {
async getStation(context, info, id) {
const { cache } = await this.getCache(context, info);
for (const station of cache.HideoutStation) {
if (station.id === id) return station;
}
return Promise.reject(new Error(`No hideout station found with id ${id}`));
}

async getLegacyList(context) {
await this.init(context);
return this.cache.HideoutModule;
async getLegacyList(context, info) {
const { cache } = await this.getCache(context, info);
return cache.HideoutModule;
}

async getLegacyModule(context, name, level) {
await this.init(context);
for (const module of this.cache.HideoutModule) {
async getLegacyModule(context, info, name, level) {
const { cache } = await this.getCache(context, info);
for (const module of cache.HideoutModule) {
if (module.name === name && module.quantity === level) {
return module;
}
Expand Down
10 changes: 5 additions & 5 deletions datasources/historical-prices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import WorkerKVSplit from '../utils/worker-kv-split.mjs';
class historicalPricesAPI extends WorkerKVSplit {
constructor(dataSource) {
super('historical_price_data', dataSource);
this.addGameMode('pve');
this.defaultDays = 7;
this.maxDays = 7;
this.itemLimitDays = 2;
}

async getByItemId(context, itemId, days = this.defaultDays, halfResults = false) {
await this.init(context, itemId);
const data = await this.getKVData(context, itemId);
if (!data) {
async getByItemId(context, info, itemId, days = this.defaultDays, halfResults = false) {
const { cache } = await this.getCache(context, info, itemId);
if (!cache) {
return Promise.reject(new Error('Historical prices cache is empty'));
}

Expand All @@ -23,7 +23,7 @@ class historicalPricesAPI extends WorkerKVSplit {
}
}

let prices = data.historicalPricePoint[itemId];
let prices = cache.historicalPricePoint[itemId];
if (!prices) {
return [];
}
Expand Down
48 changes: 0 additions & 48 deletions datasources/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,54 +47,6 @@ class DataSource {
};
}

async init() {
try {
if (this.initialized) return;
if (this.loading) {
return new Promise((resolve) => {
let loadingTimedOut = false;
const loadingTimeout = setTimeout(() => {
loadingTimedOut = true;
}, 3000);
const loadingInterval = setInterval(() => {
if (this.loading === false) {
clearTimeout(loadingTimeout);
clearInterval(loadingInterval);
return resolve();
}
if (loadingTimedOut) {
console.log(`DataSource init timed out; forcing init`);
clearInterval(loadingInterval);
this.loading = false;
return resolve(this.init());
}
}, 100);
});
}
this.loading = true;
return Promise.all([
/*this.barter.init(),
this.craft.init(),
this.hideout.init(),
this.historicalPrice.init(),
this.item.init(),
this.map.init(),
this.task.init(),
this.trader.init(),
this.traderInventory.init(),*/
this.schema.init(),
]).then(() => {
this.initialized = true;
this.loading = false;
}).catch(error => {
this.loading = false;
return Promise.reject(error);
});
} catch (error) {
console.error('error initializing data api', error.stack);
}
}

kvLoadedForRequest(kvName, requestId) {
if (!requestId) {
return false;
Expand Down
Loading

0 comments on commit 792c234

Please sign in to comment.