feat: add Buy Mode functionality for manual purchase monitoring and point tracking

This commit is contained in:
2025-11-04 21:08:11 +01:00
parent 8e1be00618
commit 57e2bc392d
7 changed files with 414 additions and 58 deletions

View File

@@ -20,6 +20,7 @@ import JobState from './util/JobState'
import { StartupValidator } from './util/StartupValidator'
import { MobileRetryTracker } from './util/MobileRetryTracker'
import { SchedulerManager } from './util/SchedulerManager'
import { BuyModeSelector, BuyModeMonitor } from './util/BuyMode'
import { Login } from './functions/Login'
import { Workers } from './functions/Workers'
@@ -53,7 +54,8 @@ export class MicrosoftRewardsBot {
private accounts: Account[]
private workers: Workers
private login = new Login(this)
private buyMode: { enabled: boolean; email?: string } = { enabled: false }
private buyModeEnabled: boolean = false
private buyModeArgument?: string
// Summary collection (per process)
private accountSummaries: AccountSummary[] = []
@@ -94,25 +96,23 @@ export class MicrosoftRewardsBot {
// Buy mode: CLI args take precedence over config
const idx = process.argv.indexOf('-buy')
if (idx >= 0) {
const target = process.argv[idx + 1]
this.buyMode = target && /@/.test(target)
? { enabled: true, email: target }
: { enabled: true }
this.buyModeEnabled = true
this.buyModeArgument = process.argv[idx + 1]
} else {
// Fallback to config if no CLI flag
const buyModeConfig = this.config.buyMode as { enabled?: boolean } | undefined
if (buyModeConfig?.enabled === true) {
this.buyMode.enabled = true
this.buyModeEnabled = true
}
}
}
public isBuyModeEnabled(): boolean {
return this.buyMode.enabled === true
return this.buyModeEnabled === true
}
public getBuyModeTarget(): string | undefined {
return this.buyMode.email
return this.buyModeArgument
}
async initialize() {
@@ -168,10 +168,7 @@ export class MicrosoftRewardsBot {
log('main', 'MAIN', `Bot started with ${this.config.clusters} clusters`)
// If buy mode is enabled, run single-account interactive session without automation
if (this.buyMode.enabled) {
const targetInfo = this.buyMode.email ? ` for ${this.buyMode.email}` : ''
log('main', 'BUY-MODE', `Buy mode ENABLED${targetInfo}. We'll open 2 tabs: (1) a monitor tab that auto-refreshes to track points, (2) your browsing tab to redeem/purchase freely.`, 'log', 'green')
log('main', 'BUY-MODE', 'The monitor tab may refresh every ~10s. Use the other tab for your actions; monitoring is passive and non-intrusive.', 'log', 'yellow')
if (this.buyModeEnabled) {
await this.runBuyMode()
return
}
@@ -192,9 +189,22 @@ export class MicrosoftRewardsBot {
private async runBuyMode() {
try {
await this.initialize()
const email = this.buyMode.email || (this.accounts[0]?.email)
const account = this.accounts.find(a => a.email === email) || this.accounts[0]
if (!account) throw new Error('No account available for buy mode')
const buyModeConfig = this.config.buyMode as { maxMinutes?: number } | undefined
const maxMinutes = buyModeConfig?.maxMinutes ?? 45
const selector = new BuyModeSelector(this.accounts)
const selection = await selector.selectAccount(this.buyModeArgument, maxMinutes)
if (!selection) {
log('main', 'BUY-MODE', 'Buy mode cancelled: no account selected', 'warn')
return
}
const { account, maxMinutes: sessionMaxMinutes } = selection
log('main', 'BUY-MODE', `Buy mode ENABLED for ${account.email}. Opening 2 tabs: (1) monitor tab (auto-refresh), (2) your browsing tab`, 'log', 'green')
log('main', 'BUY-MODE', `Session duration: ${sessionMaxMinutes} minutes. Monitor tab refreshes every ~10s. Use the other tab for your actions.`, 'log', 'yellow')
this.isMobile = false
this.axios = new Axios(account.proxy)
@@ -232,22 +242,21 @@ export class MicrosoftRewardsBot {
this.log(false, 'BUY-MODE', `Failed to send spend notice: ${e instanceof Error ? e.message : e}`, 'warn')
}
}
// Get initial points
let initial = 0
try {
const data = await this.browser.func.getDashboardData(monitor)
initial = data.userStatus.availablePoints || 0
} catch {/* ignore */}
this.log(false, 'BUY-MODE', `Logged in as ${account.email}. Buy mode is active: monitor tab auto-refreshes; user tab is free for your actions. We'll observe points passively.`)
const pointMonitor = new BuyModeMonitor(initial)
this.log(false, 'BUY-MODE', `Logged in as ${account.email}. Starting passive point monitoring (session: ${sessionMaxMinutes} min)`)
// Passive watcher: poll points periodically without clicking.
const start = Date.now()
let last = initial
let spent = 0
const buyModeConfig = this.config.buyMode as { maxMinutes?: number } | undefined
const maxMinutes = Math.max(10, buyModeConfig?.maxMinutes ?? 45)
const endAt = start + maxMinutes * 60 * 1000
const endAt = start + sessionMaxMinutes * 60 * 1000
while (Date.now() < endAt) {
await this.utils.wait(10000)
@@ -265,16 +274,11 @@ export class MicrosoftRewardsBot {
try {
const data = await this.browser.func.getDashboardData(monitor)
const nowPts = data.userStatus.availablePoints || 0
if (nowPts < last) {
// Points decreased -> likely spent
const delta = last - nowPts
spent += delta
last = nowPts
this.log(false, 'BUY-MODE', `Detected spend: -${delta} points (current: ${nowPts})`)
// Immediate spend notice
await sendSpendNotice(delta, nowPts, spent)
} else if (nowPts > last) {
last = nowPts
const spendInfo = pointMonitor.checkSpending(nowPts)
if (spendInfo) {
this.log(false, 'BUY-MODE', `Detected spend: -${spendInfo.spent} points (current: ${spendInfo.current})`)
await sendSpendNotice(spendInfo.spent, spendInfo.current, spendInfo.total)
}
} catch (err) {
// If we lost the page context, recreate the monitor tab and continue
@@ -305,14 +309,15 @@ export class MicrosoftRewardsBot {
}
// Send a final minimal conclusion webhook for this manual session
const monitorSummary = pointMonitor.getSummary()
const summary: AccountSummary = {
email: account.email,
durationMs: Date.now() - start,
durationMs: monitorSummary.duration,
desktopCollected: 0,
mobileCollected: 0,
totalCollected: -spent, // negative indicates spend
initialTotal: initial,
endTotal: last,
totalCollected: -monitorSummary.spent, // negative indicates spend
initialTotal: monitorSummary.initial,
endTotal: monitorSummary.current,
errors: [],
banned: { status: false, reason: '' }
}
@@ -328,13 +333,13 @@ export class MicrosoftRewardsBot {
if (this.config.clusters > 1 && !cluster.isPrimary) return
const version = this.getVersion()
const mode = this.buyMode.enabled ? 'Manual Mode' : 'Automated Mode'
const mode = this.buyModeEnabled ? 'Manual Mode' : 'Automated Mode'
log('main', 'BANNER', `Microsoft Rewards Bot v${version} - ${mode}`)
log('main', 'BANNER', `PID: ${process.pid} | Workers: ${this.config.clusters}`)
if (this.buyMode.enabled) {
log('main', 'BANNER', `Target: ${this.buyMode.email || 'First account'}`)
if (this.buyModeEnabled) {
log('main', 'BANNER', `Target: ${this.buyModeArgument || 'Interactive selection'}`)
} else {
const upd = this.config.update || {}
const updTargets: string[] = []

217
src/util/BuyMode.ts Normal file
View File

@@ -0,0 +1,217 @@
import { createInterface } from 'readline'
import type { Account } from '../interface/Account'
import { log } from './Logger'
export interface BuyModeSelection {
account: Account
maxMinutes: number
}
export class BuyModeSelector {
private accounts: Account[]
constructor(accounts: Account[]) {
this.accounts = accounts.filter(acc => acc.enabled !== false)
}
/**
* Parse the buy mode argument from CLI.
* Supports: email, numeric index (1-based), or undefined for interactive selection.
*/
async selectAccount(
argument?: string,
maxMinutes: number = 45
): Promise<BuyModeSelection | null> {
if (this.accounts.length === 0) {
log('main', 'BUY-MODE', 'No enabled accounts found. Please enable at least one account in accounts.jsonc', 'error')
return null
}
let selectedAccount: Account | null = null
if (!argument) {
selectedAccount = await this.promptInteractiveSelection()
} else if (this.isNumericIndex(argument)) {
selectedAccount = this.selectByIndex(argument)
} else if (this.isEmail(argument)) {
selectedAccount = this.selectByEmail(argument)
} else {
log('main', 'BUY-MODE', `Invalid argument: "${argument}". Expected email or numeric index.`, 'error')
return null
}
if (!selectedAccount) {
return null
}
return {
account: selectedAccount,
maxMinutes: Math.max(10, maxMinutes)
}
}
private isNumericIndex(value: string): boolean {
return /^\d+$/.test(value)
}
private isEmail(value: string): boolean {
return /@/.test(value)
}
private selectByIndex(indexStr: string): Account | null {
const index = parseInt(indexStr, 10)
if (index < 1 || index > this.accounts.length) {
log('main', 'BUY-MODE', `Invalid account index: ${index}. Valid range: 1-${this.accounts.length}`, 'error')
this.displayAccountList()
return null
}
const account = this.accounts[index - 1]
log('main', 'BUY-MODE', `Selected account #${index}: ${this.maskEmail(account!.email)}`, 'log', 'green')
return account!
}
private selectByEmail(email: string): Account | null {
const account = this.accounts.find(acc => acc.email.toLowerCase() === email.toLowerCase())
if (!account) {
log('main', 'BUY-MODE', `Account not found: ${email}`, 'error')
this.displayAccountList()
return null
}
log('main', 'BUY-MODE', `Selected account: ${this.maskEmail(account.email)}`, 'log', 'green')
return account
}
private async promptInteractiveSelection(): Promise<Account | null> {
log('main', 'BUY-MODE', 'No account specified. Please select an account:', 'log', 'cyan')
this.displayAccountList()
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise<Account | null>((resolve) => {
rl.question('\nEnter account number (1-' + this.accounts.length + ') or email: ', (answer) => {
rl.close()
const trimmed = answer.trim()
if (!trimmed) {
log('main', 'BUY-MODE', 'No selection made. Exiting buy mode.', 'warn')
resolve(null)
return
}
let selected: Account | null = null
if (this.isNumericIndex(trimmed)) {
selected = this.selectByIndex(trimmed)
} else if (this.isEmail(trimmed)) {
selected = this.selectByEmail(trimmed)
} else {
log('main', 'BUY-MODE', `Invalid input: "${trimmed}". Expected number or email.`, 'error')
}
resolve(selected)
})
})
}
private displayAccountList(): void {
console.log('\nAvailable accounts:')
console.log('─'.repeat(60))
this.accounts.forEach((acc, idx) => {
const num = `[${idx + 1}]`.padEnd(5)
const email = this.maskEmail(acc.email).padEnd(35)
const proxy = acc.proxy?.url ? '🔒 Proxy' : 'Direct'
console.log(`${num} ${email} ${proxy}`)
})
console.log('─'.repeat(60))
}
private maskEmail(email: string): string {
const [local, domain] = email.split('@')
if (!local || !domain) return email
if (local.length <= 3) {
return `${local[0]}***@${domain}`
}
const visibleStart = local.slice(0, 2)
const visibleEnd = local.slice(-1)
return `${visibleStart}***${visibleEnd}@${domain}`
}
}
export class BuyModeMonitor {
private initialPoints: number = 0
private lastPoints: number = 0
private totalSpent: number = 0
private monitorStartTime: number = 0
constructor(initialPoints: number) {
this.initialPoints = initialPoints
this.lastPoints = initialPoints
this.monitorStartTime = Date.now()
}
/**
* Update the current points and detect spending.
* Returns spending info if points decreased, null otherwise.
*/
checkSpending(currentPoints: number): { spent: number; current: number; total: number } | null {
if (currentPoints < this.lastPoints) {
const spent = this.lastPoints - currentPoints
this.totalSpent += spent
this.lastPoints = currentPoints
return {
spent,
current: currentPoints,
total: this.totalSpent
}
}
if (currentPoints > this.lastPoints) {
this.lastPoints = currentPoints
}
return null
}
getTotalSpent(): number {
return this.totalSpent
}
getSessionDuration(): number {
return Date.now() - this.monitorStartTime
}
getCurrentPoints(): number {
return this.lastPoints
}
getInitialPoints(): number {
return this.initialPoints
}
getSummary(): {
initial: number
current: number
spent: number
duration: number
} {
return {
initial: this.initialPoints,
current: this.lastPoints,
spent: this.totalSpent,
duration: this.getSessionDuration()
}
}
}