mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-29 00:31:01 +00:00
feat: Refactor and modularize flow handling for improved maintainability
- Extracted BuyModeHandler, DesktopFlow, MobileFlow, and SummaryReporter into separate modules for better organization and testability. - Enhanced type safety and added interfaces for various return types in Load, Logger, UserAgent, and flow modules. - Implemented comprehensive error handling and logging throughout the new modules. - Added unit tests for DesktopFlow, MobileFlow, and SummaryReporter to ensure functionality and correctness. - Updated existing utility functions to support new flow structures and improve code clarity.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { dashboardState } from './state'
|
||||
import type { MicrosoftRewardsBot } from '../index'
|
||||
import { log as botLog } from '../util/Logger'
|
||||
import { getErrorMessage } from '../util/Utils'
|
||||
import { dashboardState } from './state'
|
||||
|
||||
export class BotController {
|
||||
private botInstance: MicrosoftRewardsBot | null = null
|
||||
@@ -11,7 +12,7 @@ export class BotController {
|
||||
}
|
||||
|
||||
private log(message: string, level: 'log' | 'warn' | 'error' = 'log'): void {
|
||||
console.log(`[BotController] ${message}`)
|
||||
botLog('main', 'BOT-CONTROLLER', message, level)
|
||||
|
||||
dashboardState.addLog({
|
||||
timestamp: new Date().toISOString(),
|
||||
|
||||
@@ -39,8 +39,8 @@ export function loadPointsFromSessions(email: string): number | undefined {
|
||||
}
|
||||
|
||||
return undefined
|
||||
} catch (error) {
|
||||
console.error(`[Dashboard] Error loading points for ${email}:`, error)
|
||||
} catch {
|
||||
// Silently ignore: session loading is optional fallback
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
@@ -77,8 +77,8 @@ export function loadAllPointsFromSessions(): Map<string, number> {
|
||||
continue
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[Dashboard] Error loading points from sessions:', error)
|
||||
} catch {
|
||||
// Silently ignore: session loading is optional fallback
|
||||
}
|
||||
|
||||
return pointsMap
|
||||
@@ -112,8 +112,8 @@ export function loadPointsFromJobState(email: string): number | undefined {
|
||||
}
|
||||
|
||||
return undefined
|
||||
} catch (error) {
|
||||
console.error(`[Dashboard] Error loading job state for ${email}:`, error)
|
||||
} catch {
|
||||
// Silently ignore: job state loading is optional fallback
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Router, Request, Response } from 'express'
|
||||
import { Request, Response, Router } from 'express'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { dashboardState } from './state'
|
||||
import { loadAccounts, loadConfig, getConfigPath } from '../util/Load'
|
||||
import { getConfigPath, loadAccounts, loadConfig } from '../util/Load'
|
||||
import { botController } from './BotController'
|
||||
import { dashboardState } from './state'
|
||||
|
||||
export const apiRouter = Router()
|
||||
|
||||
@@ -17,8 +17,8 @@ function ensureAccountsLoaded(): void {
|
||||
try {
|
||||
const loadedAccounts = loadAccounts()
|
||||
dashboardState.initializeAccounts(loadedAccounts.map(a => a.email))
|
||||
} catch (error) {
|
||||
console.error('[Dashboard] Failed to load accounts:', error)
|
||||
} catch {
|
||||
// Silently ignore: accounts loading is optional for API fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import express from 'express'
|
||||
import { createServer } from 'http'
|
||||
import { WebSocketServer, WebSocket } from 'ws'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import { apiRouter } from './routes'
|
||||
import { dashboardState, DashboardLog } from './state'
|
||||
import { createServer } from 'http'
|
||||
import path from 'path'
|
||||
import { WebSocket, WebSocketServer } from 'ws'
|
||||
import { log as botLog } from '../util/Logger'
|
||||
import { apiRouter } from './routes'
|
||||
import { DashboardLog, dashboardState } from './state'
|
||||
|
||||
// Dashboard logging helper
|
||||
const dashLog = (message: string, type: 'log' | 'warn' | 'error' = 'log'): void => {
|
||||
botLog('main', 'DASHBOARD', message, type)
|
||||
}
|
||||
|
||||
const PORT = process.env.DASHBOARD_PORT ? parseInt(process.env.DASHBOARD_PORT) : 3000
|
||||
const HOST = process.env.DASHBOARD_HOST || '127.0.0.1'
|
||||
@@ -80,15 +85,15 @@ export class DashboardServer {
|
||||
private setupWebSocket(): void {
|
||||
this.wss.on('connection', (ws: WebSocket) => {
|
||||
this.clients.add(ws)
|
||||
console.log('[Dashboard] WebSocket client connected')
|
||||
dashLog('WebSocket client connected')
|
||||
|
||||
ws.on('close', () => {
|
||||
this.clients.delete(ws)
|
||||
console.log('[Dashboard] WebSocket client disconnected')
|
||||
dashLog('WebSocket client disconnected')
|
||||
})
|
||||
|
||||
ws.on('error', (error) => {
|
||||
console.error('[Dashboard] WebSocket error:', error)
|
||||
dashLog(`WebSocket error: ${error instanceof Error ? error.message : String(error)}`, 'error')
|
||||
})
|
||||
|
||||
// Send initial data on connect
|
||||
@@ -140,7 +145,7 @@ export class DashboardServer {
|
||||
try {
|
||||
client.send(payload)
|
||||
} catch (error) {
|
||||
console.error('[Dashboard] Error broadcasting update:', error)
|
||||
dashLog(`Error broadcasting update: ${error instanceof Error ? error.message : String(error)}`, 'error')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,15 +153,15 @@ export class DashboardServer {
|
||||
|
||||
public start(): void {
|
||||
this.server.listen(PORT, HOST, () => {
|
||||
console.log(`[Dashboard] Server running on http://${HOST}:${PORT}`)
|
||||
console.log('[Dashboard] WebSocket ready for live logs')
|
||||
dashLog(`Server running on http://${HOST}:${PORT}`)
|
||||
dashLog('WebSocket ready for live logs')
|
||||
})
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
this.wss.close()
|
||||
this.server.close()
|
||||
console.log('[Dashboard] Server stopped')
|
||||
dashLog('Server stopped')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,8 @@ class DashboardState {
|
||||
try {
|
||||
listener(type, data)
|
||||
} catch (error) {
|
||||
console.error('[Dashboard State] Error notifying listener:', error)
|
||||
// Silently ignore listener errors to prevent state corruption
|
||||
// Listeners are non-critical (UI updates, logging)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user