Skip to content

Commit

Permalink
🔀 Merge pull request #549
Browse files Browse the repository at this point in the history
✨ option to show detailed time taken
  • Loading branch information
idinium96 authored Apr 4, 2021
2 parents 125d837 + 27f6a35 commit 4a94ea1
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/classes/Carts/UserCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ export default class UserCart extends Cart {
return Promise.reject('cart is empty');
}

const start = Date.now();

const offer = this.bot.manager.createOffer(this.partner);

const alteredMessages: string[] = [];
Expand Down Expand Up @@ -1044,6 +1046,10 @@ export default class UserCart extends Cart {
// clear memory
theirInventory.clearFetch();

const timeTaken = Date.now() - start;
offer.data('constructOfferTime', timeTaken);
log.debug(`Constructing offer took ${timeTaken} ms`);

return alteredMessages.length === 0 ? undefined : alteredMessages.join(', ');
}

Expand Down
2 changes: 1 addition & 1 deletion src/classes/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default abstract class Handler {
* @param offer - The offer that changed
* @param oldState - The old state of the offer
*/
onTradeOfferChanged(offer: TradeOfferManager.TradeOffer, oldState: number, processTime?: number): void {
onTradeOfferChanged(offer: TradeOfferManager.TradeOffer, oldState: number, timeTakenToComplete?: number): void {
// empty function
}

Expand Down
4 changes: 2 additions & 2 deletions src/classes/MyHandler/MyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ export default class MyHandler extends Handler {
};
}

onTradeOfferChanged(offer: TradeOffer, oldState: number, processTime?: number): void {
onTradeOfferChanged(offer: TradeOffer, oldState: number, timeTakenToComplete?: number): void {
// Not sure if it can go from other states to active
if (oldState === TradeOfferManager.ETradeOfferState['Accepted']) {
offer.data('switchedState', oldState);
Expand Down Expand Up @@ -1906,7 +1906,7 @@ export default class MyHandler extends Handler {

this.autokeys.check();

const result = processAccepted(offer, this.bot, this.isTradingKeys, processTime);
const result = processAccepted(offer, this.bot, this.isTradingKeys, timeTakenToComplete);
this.isTradingKeys = false; // reset

highValue.isDisableSKU = result.isDisableSKU;
Expand Down
27 changes: 22 additions & 5 deletions src/classes/MyHandler/offer/accepted/processAccepted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function processAccepted(
offer: i.TradeOffer,
bot: Bot,
isTradingKeys: boolean,
processTime: number
timeTakenToComplete: number
): { theirHighValuedItems: string[]; isDisableSKU: string[]; items: i.Items | undefined } {
const opt = bot.options;

Expand Down Expand Up @@ -172,10 +172,20 @@ export default function processAccepted(
}
}

const isOfferSent = offer?.data('action') === undefined;
const isOfferSent = offer.data('action') === undefined;
const timeTakenToProcessOrConstruct = (offer.data('constructOfferTime') ||
offer.data('processOfferTime')) as number;

if (isWebhookEnabled) {
void sendTradeSummary(offer, accepted, bot, processTime, isTradingKeys, isOfferSent);
void sendTradeSummary(
offer,
accepted,
bot,
timeTakenToComplete,
timeTakenToProcessOrConstruct,
isTradingKeys,
isOfferSent
);
} else {
const slots = bot.tf2.backpackSlots;
const itemsName = {
Expand All @@ -195,7 +205,8 @@ export default function processAccepted(
const autokeys = bot.handler.autokeys;
const status = autokeys.getOverallStatus;

const cT = bot.options.tradeSummary.customText;
const tSum = bot.options.tradeSummary;
const cT = tSum.customText;
const cTKeyRate = cT.keyRate.steamChat ? cT.keyRate.steamChat : '🔑 Key rate:';
const cTPureStock = cT.pureStock.steamChat ? cT.pureStock.steamChat : '💰 Pure stock:';
const cTTotalItems = cT.totalItems.steamChat ? cT.totalItems.steamChat : '🎒 Total items:';
Expand Down Expand Up @@ -226,7 +237,13 @@ export default function processAccepted(
`\n${cTTotalItems} ${bot.inventoryManager.getInventory.getTotalItems}${
slots !== undefined ? `/${slots}` : ''
}` +
`\n${cTTimeTaken} ${t.convertTime(processTime, opt.tradeSummary.showTimeTakenInMS)}` +
`\n${cTTimeTaken} ${t.convertTime(
timeTakenToComplete,
timeTakenToProcessOrConstruct,
isOfferSent,
tSum.showDetailedTimeTaken,
tSum.showTimeTakenInMS
)}` +
`\n\nVersion ${process.env.BOT_VERSION}`,
[]
);
Expand Down
2 changes: 2 additions & 0 deletions src/classes/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const DEFAULTS = {
tradeSummary: {
showStockChanges: false,
showTimeTakenInMS: false,
showDetailedTimeTaken: true,
showItemPrices: true,
showPureInEmoji: false,
showProperName: false,
Expand Down Expand Up @@ -1100,6 +1101,7 @@ interface OnlyAllow {
interface TradeSummary {
showStockChanges?: boolean;
showTimeTakenInMS?: boolean;
showDetailedTimeTaken?: boolean;
showItemPrices?: boolean;
showPureInEmoji?: boolean;
showProperName?: boolean;
Expand Down
13 changes: 8 additions & 5 deletions src/classes/Trades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ export default class Trades {
}

offer.data('handledByUs', true);
offer.data('handleTime', dayjs().valueOf() - start);
const timeTaken = dayjs().valueOf() - start;

offer.data('processOfferTime', timeTaken);
log.debug(`Processing offer #${offer.id} took ${timeTaken} ms`);

offer.log('debug', 'handler is done with offer', {
response: response
Expand Down Expand Up @@ -1029,7 +1032,7 @@ export default class Trades {

const finishTimestamp = dayjs().valueOf();

const processTime = finishTimestamp - offer.data('handleTimestamp');
const timeTakenToComplete = finishTimestamp - offer.data('handleTimestamp');

if (
offer.state === TradeOfferManager.ETradeOfferState['Active'] ||
Expand Down Expand Up @@ -1060,10 +1063,10 @@ export default class Trades {

offer.data('finishTimestamp', finishTimestamp);

log.debug(`Took ${isNaN(processTime) ? 'unknown' : processTime} ms to process offer`, {
log.debug(`Took ${isNaN(timeTakenToComplete) ? 'unknown' : timeTakenToComplete} ms to complete the offer`, {
offerId: offer.id,
state: offer.state,
finishTime: processTime
finishTime: timeTakenToComplete
});
}

Expand All @@ -1085,7 +1088,7 @@ export default class Trades {
this.bot.client.gamesPlayed([]);

void this.bot.inventoryManager.getInventory.fetch().asCallback(() => {
this.bot.handler.onTradeOfferChanged(offer, oldState, processTime);
this.bot.handler.onTradeOfferChanged(offer, oldState, timeTakenToComplete);
});
}

Expand Down
14 changes: 11 additions & 3 deletions src/lib/DiscordWebhook/sendTradeSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default async function sendTradeSummary(
offer: TradeOffer,
accepted: Accepted,
bot: Bot,
processTime: number,
timeTakenToComplete: number,
timeTakenToProcessOrConstruct: number,
isTradingKeys: boolean,
isOfferSent: boolean | undefined
): Promise<void> {
Expand Down Expand Up @@ -104,7 +105,8 @@ export default async function sendTradeSummary(
const autokeys = bot.handler.autokeys;
const status = autokeys.getOverallStatus;

const cT = bot.options.tradeSummary.customText;
const tSum = optBot.tradeSummary;
const cT = tSum.customText;
const cTTimeTaken = cT.timeTaken.discordWebhook ? cT.timeTaken.discordWebhook : '⏱ **Time taken:**';
const cTKeyRate = cT.keyRate.discordWebhook ? cT.keyRate.discordWebhook : '🔑 Key rate:';
const cTPureStock = cT.pureStock.discordWebhook ? cT.pureStock.discordWebhook : '💰 Pure stock:';
Expand All @@ -126,7 +128,13 @@ export default async function sendTradeSummary(
},
description:
summary +
`\n${cTTimeTaken} ${t.convertTime(processTime, optBot.tradeSummary.showTimeTakenInMS)}\n\n` +
`\n${cTTimeTaken} ${t.convertTime(
timeTakenToComplete,
timeTakenToProcessOrConstruct,
isOfferSent,
tSum.showDetailedTimeTaken,
tSum.showTimeTakenInMS
)}\n\n` +
(misc.showQuickLinks ? `${quickLinks(t.replace.specialChar(details.personaName), links)}\n` : '\n'),
fields: [
{
Expand Down
20 changes: 16 additions & 4 deletions src/lib/tools/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,23 @@ export function timeNow(opt: Options): { timeUnix: number; time: string; emoji:
};
}

export function convertTime(time: number, showInMS: boolean): string {
export function convertTime(
completeTime: number,
processOrConstructTime: number,
isOfferSent: boolean,
showDetailedTimeTaken: boolean,
showInMS: boolean
): string {
const now = dayjs();
const timeText = `${dayjs.unix(Math.round((now.valueOf() - time) / 1000)).fromNow(true)}${
showInMS ? ` (${time} ms)` : ''
}`;
const timeText = showDetailedTimeTaken
? `\n- ${isOfferSent ? 'To construct offer' : 'To process offer'}: ${dayjs
.unix(Math.round((now.valueOf() - processOrConstructTime) / 1000))
.fromNow(true)}${showInMS ? ` (${processOrConstructTime} ms)` : ''}\n- To complete: ${dayjs
.unix(Math.round((now.valueOf() - completeTime) / 1000))
.fromNow(true)}${showInMS ? ` (${completeTime} ms)` : ''}`
: `${dayjs.unix(Math.round((now.valueOf() - completeTime) / 1000)).fromNow(true)}${
showInMS ? ` (${completeTime} ms)` : ''
}`;
return timeText;
}

Expand Down
13 changes: 12 additions & 1 deletion src/schemas/options-json/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ export const optionsSchema: jsonschema.Schema = {
'onUpdate',
'onSuccessUpdatePartialPriced',
'onFailedUpdatePartialPriced',
'onBulkUpdatePartialPriced',
'onResetAfterThreshold'
],
additionalProperties: false
Expand Down Expand Up @@ -601,6 +602,9 @@ export const optionsSchema: jsonschema.Schema = {
showTimeTakenInMS: {
type: 'boolean'
},
showDetailedTimeTaken: {
type: 'boolean'
},
showItemPrices: {
type: 'boolean'
},
Expand Down Expand Up @@ -675,7 +679,14 @@ export const optionsSchema: jsonschema.Schema = {
additionalProperties: false
}
},
required: ['showStockChanges', 'showTimeTakenInMS', 'showItemPrices', 'showPureInEmoji', 'customText'],
required: [
'showStockChanges',
'showTimeTakenInMS',
'showDetailedTimeTaken',
'showItemPrices',
'showPureInEmoji',
'customText'
],
additionalProperties: false
},

Expand Down
3 changes: 2 additions & 1 deletion src/types/modules/@tf2autobot/tradeoffer-manager/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ declare module '@tf2autobot/tradeoffer-manager' {
value?: ItemsValue;
prices?: Prices;
handledByUs?: boolean;
handleTime?: number;
processOfferTime?: number;
constructOfferTime?: number;
actionTimestamp?: number;
confirmationTime?: number;
finishTimestamp?: number;
Expand Down

0 comments on commit 4a94ea1

Please sign in to comment.