Skip to content

Commit

Permalink
🔀Merge pull request #399 from TF2Autobot/refactor-Bot_ts
Browse files Browse the repository at this point in the history
- Refactor Bot.ts - @arik123
  • Loading branch information
idinium96 authored Mar 4, 2021
2 parents 41b7558 + 1ac70b7 commit 20ca897
Showing 1 changed file with 19 additions and 84 deletions.
103 changes: 19 additions & 84 deletions src/classes/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import semver from 'semver';
import request from 'request-retry-dayjs';

import InventoryManager from './InventoryManager';
import Pricelist, { Entry, EntryData } from './Pricelist';
import Pricelist, { EntryData } from './Pricelist';
import Friends from './Friends';
import Trades from './Trades';
import Listings from './Listings';
Expand Down Expand Up @@ -106,48 +106,6 @@ export default class Bot {

private ready = false;

private handleLoggedOn: OmitThisParameter<() => void>;

private handleMessage: OmitThisParameter<(steamID: SteamID, message: string) => void>;

private handleFriendRelationship: OmitThisParameter<(steamID: SteamID, relationship: number) => void>;

private handleGroupRelationship: OmitThisParameter<(steamID: SteamID, relationship: number) => void>;

private handleWebSession: OmitThisParameter<(sessionID: string, cookies: string[]) => void>;

private handleSteamGuard: OmitThisParameter<
(domain: string, callback: (authCode: string) => void, lastCodeWrong: boolean) => void
>;

private handleLoginKey: OmitThisParameter<(loginKey: string) => void>;

private handleError: OmitThisParameter<(err: CustomError) => void>;

private handleSessionExpired: OmitThisParameter<() => void>;

private handleConfKeyNeeded: OmitThisParameter<
(tag: string, callback: (err: Error | null, time: number, confKey: string) => void) => void
>;

private handlePollData: OmitThisParameter<(pollData: TradeOfferManager.PollData) => void>;

private handleNewOffer: OmitThisParameter<(offer: TradeOfferManager.TradeOffer) => void>;

private handleOfferChanged: OmitThisParameter<(offer: TradeOfferManager.TradeOffer, oldState: number) => void>;

private handleOfferList: OmitThisParameter<
(filter: number, sent: TradeOfferManager.TradeOffer[], received: TradeOfferManager.TradeOffer[]) => void
>;

private handleHeartbeat: OmitThisParameter<(bumped: number) => void>;

private handlePricelist: OmitThisParameter<(pricelist: Entry[]) => void>;

private handlePriceChange: OmitThisParameter<(sku: string, price: Entry | null) => void>;

private receivedOfferChanged: OmitThisParameter<(offer: TradeOfferManager.TradeOffer, oldState: number) => void>;

constructor(public readonly botManager: BotManager, public options: Options, private priceSource: Pricer) {
this.botManager = botManager;

Expand Down Expand Up @@ -208,29 +166,6 @@ export default class Bot {
throw new Error('Invalid Item stats whitelist steamID');
}
});

this.handleLoggedOn = this.handler.onLoggedOn.bind(this.handler);
this.handleMessage = this.onMessage.bind(this);
this.handleFriendRelationship = this.handler.onFriendRelationship.bind(this.handler);
this.handleGroupRelationship = this.handler.onGroupRelationship.bind(this.handler);
this.handleWebSession = this.onWebSession.bind(this);
this.handleSteamGuard = this.onSteamGuard.bind(this);
this.handleLoginKey = this.handler.onLoginKey.bind(this.handler);
this.handleError = this.onError.bind(this);

this.handleSessionExpired = this.onSessionExpired.bind(this);
this.handleConfKeyNeeded = this.onConfKeyNeeded.bind(this);

this.handlePollData = this.handler.onPollData.bind(this.handler);
this.handleNewOffer = this.trades.onNewOffer.bind(this.trades);
this.handleOfferChanged = this.trades.onOfferChanged.bind(this.trades);
this.receivedOfferChanged = this.trades.onOfferChanged.bind(this.trades);
this.handleOfferList = this.trades.onOfferList.bind(this.trades);

this.handleHeartbeat = this.handler.onHeartbeat.bind(this);

this.handlePricelist = this.handler.onPricelist.bind(this.handler);
this.handlePriceChange = this.handler.onPriceChange.bind(this.handler);
}

isAdmin(steamID: SteamID | string): boolean {
Expand Down Expand Up @@ -376,28 +311,28 @@ export default class Bot {
};
let cookies: string[];

this.addListener(this.client, 'loggedOn', this.handleLoggedOn, false);
this.addListener(this.client, 'friendMessage', this.handleMessage, true);
this.addListener(this.client, 'friendRelationship', this.handleFriendRelationship, true);
this.addListener(this.client, 'groupRelationship', this.handleGroupRelationship, true);
this.addListener(this.client, 'webSession', this.handleWebSession, false);
this.addListener(this.client, 'steamGuard', this.handleSteamGuard, false);
this.addListener(this.client, 'loginKey', this.handleLoginKey, false);
this.addListener(this.client, 'error', this.handleError, false);
this.addListener(this.client, 'loggedOn', this.handler.onLoggedOn.bind(this.handler), false);
this.addListener(this.client, 'friendMessage', this.onMessage.bind(this), true);
this.addListener(this.client, 'friendRelationship', this.handler.onFriendRelationship.bind(this.handler), true);
this.addListener(this.client, 'groupRelationship', this.handler.onGroupRelationship.bind(this.handler), true);
this.addListener(this.client, 'webSession', this.onWebSession.bind(this), false);
this.addListener(this.client, 'steamGuard', this.onSteamGuard.bind(this), false);
this.addListener(this.client, 'loginKey', this.handler.onLoginKey.bind(this.handler), false);
this.addListener(this.client, 'error', this.onError.bind(this), false);

this.addListener(this.community, 'sessionExpired', this.handleSessionExpired, false);
this.addListener(this.community, 'confKeyNeeded', this.handleConfKeyNeeded, false);
this.addListener(this.community, 'sessionExpired', this.onSessionExpired.bind(this), false);
this.addListener(this.community, 'confKeyNeeded', this.onConfKeyNeeded.bind(this), false);

this.addListener(this.manager, 'pollData', this.handlePollData, false);
this.addListener(this.manager, 'newOffer', this.handleNewOffer, true);
this.addListener(this.manager, 'sentOfferChanged', this.handleOfferChanged, true);
this.addListener(this.manager, 'receivedOfferChanged', this.receivedOfferChanged, true);
this.addListener(this.manager, 'offerList', this.handleOfferList, true);
this.addListener(this.manager, 'pollData', this.handler.onPollData.bind(this.handler), false);
this.addListener(this.manager, 'newOffer', this.trades.onNewOffer.bind(this.trades), true);
this.addListener(this.manager, 'sentOfferChanged', this.trades.onOfferChanged.bind(this.trades), true);
this.addListener(this.manager, 'receivedOfferChanged', this.trades.onOfferChanged.bind(this.trades), true);
this.addListener(this.manager, 'offerList', this.trades.onOfferList.bind(this.trades), true);

this.addListener(this.listingManager, 'heartbeat', this.handleHeartbeat, true);
this.addListener(this.listingManager, 'heartbeat', this.handler.onHeartbeat.bind(this), true);

this.addListener(this.pricelist, 'pricelist', this.handlePricelist, false);
this.addListener(this.pricelist, 'price', this.handlePriceChange, true);
this.addListener(this.pricelist, 'pricelist', void this.handler.onPricelist.bind(this.handler), false);
this.addListener(this.pricelist, 'price', this.handler.onPriceChange.bind(this.handler), true);

return new Promise((resolve, reject) => {
async.eachSeries(
Expand Down

0 comments on commit 20ca897

Please sign in to comment.