Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ module.exports = {
'bulk_key': '',
// OPTIONAL: Maximum queue size allowed before dropping requests
'max_queue_size': -1,
// OPTIONAL: Enable caching of charms to the local DB for quick retrieval
'cache_charms': false,
};
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const winston = require('winston'),
InspectURL = require('./lib/inspect_url'),
botController = new (require('./lib/bot_controller'))(),
CONFIG = require(args.config),
postgres = new (require('./lib/postgres'))(CONFIG.database_url, CONFIG.enable_bulk_inserts),
postgres = new (require('./lib/postgres'))(CONFIG.database_url, CONFIG.enable_bulk_inserts, CONFIG.cache_charms),
gameData = new (require('./lib/game_data'))(CONFIG.game_files_update_interval, CONFIG.enable_game_file_updates),
errors = require('./errors'),
Job = require('./lib/job');
Expand Down Expand Up @@ -247,6 +247,7 @@ queue.process(CONFIG.logins.length, botController, async (job) => {

itemData.iteminfo = utils.removeNullValues(itemData.iteminfo);
itemData.iteminfo.stickers = itemData.iteminfo.stickers.map((s) => utils.removeNullValues(s));
itemData.iteminfo.keychains = itemData.iteminfo.keychains.map((s) => utils.removeNullValues(s));

job.data.job.setResponse(job.data.link.getParams().a, itemData.iteminfo);

Expand Down
13 changes: 13 additions & 0 deletions lib/game_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ class GameData {

if (name) sticker.name = name;
}
// Get keychain name
const keychainDefinitions = this.items_game.keychain_definitions;
for (const keychain of iteminfo.keychains || []) {
const kit = keychainDefinitions[keychain.sticker_id];

if (!kit) continue;

let name = this.csgo_english[kit.loc_name.replace('#', '')];

if (name) keychain.name = name;
}

// Get the skin name
let skin_name = '';
Expand Down Expand Up @@ -330,6 +341,8 @@ class GameData {

if (iteminfo.weapon_type === 'Sticker' || iteminfo.weapon_type === 'Sealed Graffiti') {
name += `| ${iteminfo.stickers[0].name}`;
} else if (iteminfo.weapon_type === 'Charm') {
name += `| ${iteminfo.keychains[0].name}`;
}

// Vanilla items have an item_name of '-'
Expand Down
64 changes: 57 additions & 7 deletions lib/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ function flatten(arr){
}

class Postgres {
constructor(url, enableBulkInserts) {
constructor(url, enableBulkInserts, cacheCharms) {
this.pool = new Pool({
connectionString: url
});

this.enableBulkInserts = enableBulkInserts || false;
this.cacheCharms = cacheCharms || false;

if (enableBulkInserts) {
this.queuedInserts = [];
Expand Down Expand Up @@ -84,6 +85,7 @@ class Postgres {
ALTER TABLE items ADD COLUMN IF NOT EXISTS floatid BIGINT;
ALTER TABLE items ADD COLUMN IF NOT EXISTS price INTEGER;
ALTER TABLE items ADD COLUMN IF NOT EXISTS listed_price INTEGER;
ALTER TABLE items ADD COLUMN IF NOT EXISTS keychains JSONB;
ALTER TABLE history ADD COLUMN IF NOT EXISTS price INTEGER;

-- Float ID is defined as the first asset id we've seen for an item
Expand Down Expand Up @@ -209,7 +211,11 @@ class Postgres {
buf.writeFloatBE(item.floatvalue, 0);
item.paintwear = buf.readInt32BE(0);

if (item.floatvalue <= 0 && item.defindex !== 507) {
if (item.defindex === 1355 && !this.cacheCharms) {
// Charms (defindex 1355): skip the current item if cacheCharms is false.
continue
}
if (item.floatvalue <= 0 && item.defindex !== 507 && item.defindex !== 1355) {
// Only insert weapons, naive check
// Special case for the 0 float Karambit
continue;
Expand Down Expand Up @@ -249,6 +255,35 @@ class Postgres {
}
}

const keychains = item.keychains.length > 0 ? item.keychains.map((s) => {
const res = {s: s.slot, i: s.sticker_id};
if (s.wear) {
res.w = s.wear;
}
if (s.scale) {
res.scale = s.scale;
}
if (s.rotation) {
res.r = s.rotation;
}
if (s.tint_id) {
res.tint_id = s.tint_id;
}
if (s.offset_x) {
res.x = s.offset_x;
}
if (s.offset_y) {
res.y = s.offset_y;
}
if (s.offset_z) {
res.z = s.offset_z;
}
if (s.pattern) {
res.pattern = s.pattern;
}
return res;
}) : null;

const ms = item.s !== '0' ? item.s : item.m;
const isStattrak = item.killeatervalue !== null;
const isSouvenir = item.quality === 12;
Expand All @@ -266,7 +301,7 @@ class Postgres {
}

values.push([ms, item.a, item.d, item.paintseed, item.paintwear, item.defindex, item.paintindex, isStattrak,
isSouvenir, props, JSON.stringify(stickers), item.rarity, price]);
isSouvenir, props, JSON.stringify(stickers), JSON.stringify(keychains), item.rarity, price]);
}

if (values.length === 0) {
Expand All @@ -286,13 +321,13 @@ class Postgres {
const values = [];
let i = 1;

// Builds binding pattern such as ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb, now(), $12, NULL, $13)
// Builds binding pattern such as ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb, $12::jsonb, now(), $13, NULL, $14)
for (let c = 0; c < itemCount; c++) {
values.push(`($${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}::jsonb, now(), $${i++}, NULL, $${i++})`);
values.push(`($${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}, $${i++}::jsonb, $${i++}::jsonb, now(), $${i++}, NULL, $${i++})`);
}

return `INSERT INTO items (ms, a, d, paintseed, paintwear, defindex, paintindex, stattrak, souvenir, props, stickers, updated, rarity, floatid, price)
VALUES ${values.join(', ')} ON CONFLICT(defindex, paintindex, paintwear, paintseed) DO UPDATE SET ms=excluded.ms, a=excluded.a, d=excluded.d, stickers=excluded.stickers, updated=now()`;
return `INSERT INTO items (ms, a, d, paintseed, paintwear, defindex, paintindex, stattrak, souvenir, props, stickers, keychains, updated, rarity, floatid, price)
VALUES ${values.join(', ')} ON CONFLICT(defindex, paintindex, paintwear, paintseed) DO UPDATE SET ms=excluded.ms, a=excluded.a, d=excluded.d, stickers=excluded.stickers, keychains=excluded.keychains, updated=now()`;
}

updateItemPrice(assetId, price) {
Expand Down Expand Up @@ -358,6 +393,21 @@ class Postgres {
offset_y: s.y,
}
});
item.keychains = item.keychains || [];
item.keychains = item.keychains.map((s) => {
return {
sticker_id: s.i,
slot: s.s,
wear: s.w,
scale: s.scale,
rotation: s.r,
tint_id: s.tint_id,
offset_x: s.x,
offset_y: s.y,
offset_z: s.z,
pattern: s.pattern,
}
});

item = Object.assign(Postgres.extractProperties(item.props), item);

Expand Down
Loading