const { app, BrowserWindow, screen, ipcMain } = require('electron'); const path = require('path'); const fs = require('fs'); let mainWindow; let screenWindow; let currentSharedData = null; // Store latest data for initial sync let currentSelectedVenue = 'Select Venue'; let currentSelectedRace = 1; let currentStopMessage = ''; let currentBtid = null; // Store in-memory BTID function createWindows() { const displays = screen.getAllDisplays(); const primary = screen.getPrimaryDisplay(); const secondary = displays.find(d => d.id !== primary.id) || primary; mainWindow = new BrowserWindow({ x: primary.bounds.x, y: primary.bounds.y, width: primary.bounds.width, height: primary.bounds.height, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, }, }); mainWindow.loadURL('http://10.150.40.124:4200/login'); screenWindow = new BrowserWindow({ x: secondary.bounds.x, y: secondary.bounds.y, width: secondary.bounds.width, height: secondary.bounds.height, frame: false, show: false, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, }, }); screenWindow.loadURL('http://10.150.40.124:4200/shared-display'); // Debugging: Open DevTools for second screen to verify logs // screenWindow.webContents.openDevTools({ mode: 'detach' }); // Handle opening second screen and send initial data ipcMain.on('open-second-screen', () => { screenWindow.show(); if (currentSharedData && screenWindow.webContents) { screenWindow.webContents.send('update-shared-data', currentSharedData); } if (screenWindow.webContents) { screenWindow.webContents.send('update-selected-venue', currentSelectedVenue); screenWindow.webContents.send('update-selected-race', currentSelectedRace); screenWindow.webContents.send('update-stop-message', currentStopMessage); screenWindow.webContents.send('update-btid', currentBtid); } }); ipcMain.on('close-second-screen', () => screenWindow.hide()); // Handle syncing data ipcMain.on('sync-shared-data', (event, data) => { currentSharedData = data; // Store latest data if (screenWindow && screenWindow.webContents) { screenWindow.webContents.send('update-shared-data', data); } }); // Handle selected venue sync ipcMain.on('sync-selected-venue', (event, venue) => { currentSelectedVenue = venue; if (screenWindow && screenWindow.webContents) { screenWindow.webContents.send('update-selected-venue', venue); } }); // Handle selected race sync ipcMain.on('sync-selected-race', (event, race) => { currentSelectedRace = race; if (screenWindow && screenWindow.webContents) { screenWindow.webContents.send('update-selected-race', race); } }); // Handle stop message sync ipcMain.on('sync-stop-message', (event, message) => { currentStopMessage = message; if (screenWindow && screenWindow.webContents) { screenWindow.webContents.send('update-stop-message', message); } }); // Handle BTID sync ipcMain.on('sync-btid', (event, btid) => { console.log('[MAIN] sync-btid received:', btid); currentBtid = btid; // Store in-memory BTID if (screenWindow && screenWindow.webContents) { screenWindow.webContents.send('update-btid', btid); } }); // Handle BTID request from file ipcMain.handle('get-btid', () => { try { const filePath = path.join(process.env.HOME || process.env.USERPROFILE, 'BTID', 'betting.txt'); const content = fs.readFileSync(filePath, 'utf-8'); const match = content.match(/Btid\s*=\s*(\d+)/i); return match ? match[1] : null; } catch (err) { console.error('[MAIN] Error reading betting.txt:', err); return null; } }); // Allow renderers to request current in-memory BTID on demand ipcMain.handle('request-current-btid', async () => { console.log('[MAIN] request-current-btid =>', currentBtid); return currentBtid || null; }); } app.whenReady().then(createWindows); app.on('window-all-closed', () => app.quit());