Skip to content

Commit

Permalink
feat: Listen and handle new items in shop
Browse files Browse the repository at this point in the history
  • Loading branch information
PixelBoii committed Feb 9, 2022
1 parent 8ec8ab8 commit 840d0a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/CSGOEmpire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,15 @@ export default class CSGOEmpire {
return res.data;
}

async queryWithdrawItems(options: WithdrawalFilters = { per_page: 160, page: 1, price_max_above: 15, sort: 'desc', order: 'market_value' }) {
async queryWithdrawItems(options: WithdrawalFilters = { per_page: 160, page: 1, price_max_above: 15, sort: 'desc', order: 'market_value' }, addNewItems: boolean = false) {
const res = await this.get('/trading/items', {
params: options
});

return new Shop(
res.data.data.map((item: Object) => new WithdrawItem(item, this)),
this,
addNewItems,
);
}

Expand Down
19 changes: 17 additions & 2 deletions src/models/Shop.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import { EventEmitter } from "events";
import CSGOEmpire from "../CSGOEmpire";
import WithdrawItem from "./WithdrawItem";

export default class Shop {
export default class Shop extends EventEmitter {
items: WithdrawItem[];
csgoempireInstance: CSGOEmpire;
addNewItems: boolean;

constructor(items: WithdrawItem[], csgoempireInstance: CSGOEmpire, addNewItems: boolean) {
super();

constructor(items: WithdrawItem[], csgoempireInstance: CSGOEmpire) {
this.items = items;
this.csgoempireInstance = csgoempireInstance;
this.addNewItems = addNewItems;

if (this.csgoempireInstance.connectToSocket) {
this.listenForEvents();
}
}

listenForEvents() {
this.csgoempireInstance.tradingSocket.on('new_item', (data: any) => {
let item = new WithdrawItem(data, this.csgoempireInstance);

if (this.addNewItems) {
this.items.push(item);
}

this.emit('new-item', item);
});

this.csgoempireInstance.tradingSocket.on('deleted_item', (itemId: number) => {
this.items = this.items.filter((item: WithdrawItem) => item.id !== itemId);
})
Expand Down

0 comments on commit 840d0a9

Please sign in to comment.