Skip to content

Commit

Permalink
Merge pull request #56 from the-hideout/separate-kvs
Browse files Browse the repository at this point in the history
Separate kvs
  • Loading branch information
Razzmatazzz authored Jun 20, 2022
2 parents 437aa82 + 7f04cda commit 2a41915
Show file tree
Hide file tree
Showing 25 changed files with 202 additions and 305 deletions.
15 changes: 0 additions & 15 deletions datasources/ammo.js

This file was deleted.

2 changes: 1 addition & 1 deletion datasources/barters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class BartersAPI extends WorkerKV {
constructor() {
super('BARTER_DATA_V2');
super('barter_data');
}

async getList() {
Expand Down
2 changes: 1 addition & 1 deletion datasources/crafts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const WorkerKV = require('../utils/worker-kv');

class CraftsAPI extends WorkerKV {
constructor(){
super('CRAFT_DATA_V2');
super('craft_data');
}

async getList() {
Expand Down
62 changes: 0 additions & 62 deletions datasources/hideout-legacy.js

This file was deleted.

17 changes: 16 additions & 1 deletion datasources/hideout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class HideoutAPI extends WorkerKV {
constructor() {
super('HIDEOUT_DATA_V3');
super('hideout_data');
}

async getList(){
Expand Down Expand Up @@ -42,6 +42,21 @@ class HideoutAPI extends WorkerKV {
}
return Promise.reject(new Error(`No hideout station found with id ${id}`));
}

async getLegacyList() {
await this.init();
return this.cache.legacy;
}

async getLegacyModule(name, level) {
await this.init();
for (const module of this.cache.legacy) {
if (module.name === name && module.quantity === level) {
return module;
}
}
return Promise.reject(new Error(`No hideout module with id ${id} found`));
}
}

module.exports = HideoutAPI;
6 changes: 3 additions & 3 deletions datasources/historical-prices.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ const WorkerKV = require('../utils/worker-kv');

class historicalPricesAPI extends WorkerKV {
constructor() {
super('HISTORICAL_PRICES');
super('historical_price_data');
}

async getByItemId(itemId) {
await this.init();
if (!this.cache) {
return Promise.reject(new Error('Historical prices cache is empty'));
}
if (!this.cache[itemId]) return [];
return this.cache[itemId];
if (!this.cache.data[itemId]) return [];
return this.cache.data[itemId];
}
}

Expand Down
19 changes: 9 additions & 10 deletions datasources/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
const AmmoAPI = require('./ammo');
const BartersAPI = require('./barters');
const CraftsAPI = require('./crafts');
const HideoutLegacyAPI = require('./hideout-legacy');
const HideoutAPI = require('./hideout');
const HistoricalPricesAPI = require('./historical-prices');
const ItemsAPI = require('./items');
const QuestsAPI = require('./quests');
const MapAPI = require('./maps');
//const QuestsAPI = require('./quests');
const status = require('./status');
const TasksAPI = require('./tasks');
const TraderInventoryAPI = require('./trader-inventory');
const TradersAPI = require('./traders');
const TasksAPI = require('./tasks');
const MapAPI = require('./maps');

class DataSource {
constructor(){
this.ammo = new AmmoAPI();
this.barter = new BartersAPI();
this.craft = new CraftsAPI();
this.hideoutLegacy = new HideoutLegacyAPI();
this.hideout = new HideoutAPI();
this.historicalPrice = new HistoricalPricesAPI();
this.item = new ItemsAPI();
this.quest = new QuestsAPI();
this.map = new MapAPI();
this.status = status;
this.traderInventory = new TraderInventoryAPI();
this.trader = new TradersAPI();
this.task = new TasksAPI();
this.status = status,
this.map = new MapAPI();

this.initialized = false;
this.loading = false;
Expand Down Expand Up @@ -57,6 +52,7 @@ class DataSource {
this.barter.init(),
this.craft.init(),
this.hideout.init(),
this.historicalPrice.init(),
this.item.init(),
this.map.init(),
this.task.init(),
Expand All @@ -65,6 +61,9 @@ class DataSource {
]).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);
Expand Down
16 changes: 15 additions & 1 deletion datasources/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class ItemsAPI extends WorkerKV {
constructor() {
super('ITEM_CACHE_V4');
super('item_data');
}

formatItem(rawItem) {
Expand All @@ -16,6 +16,7 @@ class ItemsAPI extends WorkerKV {
return {
price: traderPrice.price,
currency: traderPrice.currency,
currencyItem: traderPrice.currencyItem,
priceRUB: traderPrice.priceRUB,
vendor: {
trader: traderPrice.trader,
Expand Down Expand Up @@ -348,6 +349,19 @@ class ItemsAPI extends WorkerKV {
await this.init();
return this.cache.armorMats[matKey];
}

async getAmmoList() {
const allAmmo = await this.getItemsByBsgCategoryId('5485a8684bdc2da71d8b4567').then(ammoItems => {
// ignore bb
return ammoItems.filter(item => item.id !== '6241c316234b593b5676b637');
});
return allAmmo.map(item => {
return {
...item,
...item.properties
};
});
}
}

module.exports = ItemsAPI;
2 changes: 1 addition & 1 deletion datasources/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class MapAPI extends WorkerKV {
constructor() {
super('MAP_DATA_V2');
super('map_data');
}

async getList() {
Expand Down
82 changes: 0 additions & 82 deletions datasources/quests.js

This file was deleted.

18 changes: 17 additions & 1 deletion datasources/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class TasksAPI extends WorkerKV {
constructor() {
super('TASK_DATA_V2');
super('quest_data');
}

async getList() {
Expand Down Expand Up @@ -104,6 +104,22 @@ class TasksAPI extends WorkerKV {
return rawTask;
});
}

async getQuests() {
await this.init();
return this.cache.legacy;
}

async getQuest(id) {
await this.init();
const quests = await this.getQuests();
for (const quest of quests) {
if (quest.id === id) {
return quest;
}
}
return Promise.reject(new Error(`No quest with id ${id} found`));
}
}

module.exports = TasksAPI;
9 changes: 4 additions & 5 deletions datasources/trader-inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WorkerKV = require('../utils/worker-kv');

class TraderInventoryAPI extends WorkerKV {
constructor() {
super('TRADER_ITEMS_V2');
super('trader_price_data');
this.traderCache = false;
}

Expand All @@ -14,8 +14,7 @@ class TraderInventoryAPI extends WorkerKV {

try {
const traderCache = {};
for (const id in this.cache) {
const itemOffers = this.cache[id];
for (const itemOffers of Object.values(this.cache.data)) {
for (const offer of itemOffers) {
if (!traderCache[offer.vendor.trader_id]) traderCache[offer.vendor.trader_id] = [];
traderCache[offer.vendor.trader_id].push(offer);
Expand All @@ -29,10 +28,10 @@ class TraderInventoryAPI extends WorkerKV {

async getByItemId(itemId) {
await this.init();
if(!this.cache[itemId]){
if(!this.cache.data[itemId]){
return [];
}
return this.cache[itemId];
return this.cache.data[itemId];
}

async getPricesForTrader(traderId) {
Expand Down
Loading

0 comments on commit 2a41915

Please sign in to comment.