Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 50 additions & 20 deletions ipc/pythonHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ function closePython(){
}
}


class TcpClientManager {
constructor() {
this.client = null;
Expand All @@ -54,12 +53,36 @@ class TcpClientManager {
this.messageBuffer = ''; // For handling partial messages
}

connect(host, port, onData, onMessage, onComplete, onError, onClose) {
async connect(host, port, onData, onMessage, onComplete, onError, onClose, maxRetries = 20, initialDelay = 100) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
await this._attemptConnect(host, port, onData, onMessage, onComplete, onError, onClose);
console.log(`Successfully connected to TCP server on port ${port}`);
return; // Success!
} catch (error) {
if (attempt === maxRetries - 1) {
throw new Error(`Failed to connect after ${maxRetries} attempts: ${error.message}`);
}

const delay = Math.min(initialDelay * Math.pow(1.5, attempt), 2000);
console.log(`Connection attempt ${attempt + 1} failed, retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}

_attemptConnect(host, port, onData, onMessage, onComplete, onError, onClose) {
return new Promise((resolve, reject) => {
// crate tcp client
// Create tcp client
this.client = new net.Socket();

//start registering handlers
// Set connection timeout
const connectionTimeout = setTimeout(() => {
this.client.destroy();
reject(new Error('Connection timeout'));
}, 2000);

// Start registering handlers
this.client.on('data', (data) => {
const chunk = data.toString();
this.dataBuffer += chunk;
Expand All @@ -79,7 +102,7 @@ class TcpClientManager {
onMessage(jsonMessage);
} catch (e) {
console.warn('Received non-JSON message:', message);
onMessage({ type:"unparsed" , raw:message });
onMessage({ type: "unparsed", raw: message });
}
}
}
Expand All @@ -90,45 +113,46 @@ class TcpClientManager {
this.connected = false;

if (onComplete) {
onComplete()
};
onComplete();
}
});

this.client.on('close', () => {
console.log('TCP connection closed');
this.connected = false;
if (onClose) {
onClose()
};
onClose();
}
});

this.client.on('error', (err) => {
clearTimeout(connectionTimeout);
console.error('TCP client error:', err);
if (onError) {

// Only call onError for errors after successful connection
if (this.connected && onError) {
onError(err);
}

this.connected = false;

reject(err);
});

// Actually connect
this.client.connect(port, host, () => {
clearTimeout(connectionTimeout);
console.log(`TCP client connected to ${host}:${port}`);
this.connected = true;
resolve();
});


});
}

disconnect(){
disconnect() {
if (this.client && this.connected) {
this.client.end()
this.client.end();
}
this.connected = false

this.connected = false;
}

sendInterrupt() {
Expand All @@ -141,6 +165,8 @@ class TcpClientManager {
}
}



function setupPythonScriptHandlers(store, enginePath) {

ipcMain.handle('run-python-script', async (event, scriptArgs) => {
Expand All @@ -163,7 +189,11 @@ function setupPythonScriptHandlers(store, enginePath) {
// Create new TCP client manager
tcpClientManager = new TcpClientManager();

pythonProcess = spawn(`"${pythonpath} ${gameScript}"`, [...scriptArgs], {

console.log("running")
console.log(`"${pythonpath} ${gameScript} ${[...scriptArgs]}"`)

pythonProcess = spawn(`"${pythonpath}" "${gameScript}"`, [...scriptArgs], {
cwd: enginePath,
shell: true
});
Expand Down Expand Up @@ -197,7 +227,7 @@ function setupPythonScriptHandlers(store, enginePath) {
if (code !== 0) {
reject(new Error(`Python script error: ${scriptError}`));
} else {
resolve(tcpResult);
resolve(scriptOutput);
}
});

Expand All @@ -219,7 +249,7 @@ function setupPythonScriptHandlers(store, enginePath) {
console.log('TCP data received');
},
(jsonData) => { // onMessage
event.sender.send('stream-tcp-message', jsonData);
event.sender.send('stream-tcp-json', jsonData);
console.log('TCP data parsed');
tcpResult = jsonData;
},
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "bytefight-client",
"name": "bytefight-client-2026",
"version": "1.1.0",
"private": true,
"main": "main.js",
"homepage": "./",
"description": "Bytefight Desktop Client",
"description": "Bytefight 2026 Desktop Client",
"author": "Henry Liao <hliao62@gatech.edu>",
"scripts": {
"dev": "next dev",
Expand Down Expand Up @@ -57,8 +57,8 @@
"typescript": "^5.8.2"
},
"build": {
"appId": "com.bytefight.bytefightclient2025",
"productName": "Bytefight Client 2025",
"appId": "com.bytefight.bytefightclient2026",
"productName": "Bytefight Client 2026",
"forceCodeSigning": false,
"files": [
{
Expand All @@ -70,7 +70,8 @@
},
"main.js",
"preload.js",
"package.json"
"package.json",
"ipc/**"
],
"extraResources": [
{
Expand Down
34 changes: 18 additions & 16 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ contextBridge.exposeInMainWorld('electron', {
sendTCPInterrupt: () => ipcRenderer.invoke('tcp-send-interrupt'),
disconnectTCP: () => ipcRenderer.invoke('tcp-disconnect'),

onTcpData: (callback) => {
const handler = (_, data) => callback(data);
ipcRenderer.on('stream-tcp-data', handler);
return () => ipcRenderer.removeListener('stream-tcp-data', handler);
},

onTcpJson: (callback) => {
const handler = (_, data) => callback(data);
ipcRenderer.on('stream-tcp-message', handler);
return () => ipcRenderer.removeListener('stream-tcp-message', handler);
},

onTcpStatus: (callback) => {
const handler = (_, status) => callback(status);
ipcRenderer.on('stream-tcp-status', handler);
return () => ipcRenderer.removeListener('stream-tcp-status', handler);
},

onStreamOutput: (callback) => {
const handler = (_, chunk) => callback(chunk);
ipcRenderer.on('stream-output', handler);
Expand All @@ -49,20 +67,4 @@ contextBridge.exposeInMainWorld('electron', {
ipcRenderer.on('stream-error-full', handler);
return () => ipcRenderer.removeListener('stream-error-full', handler);
},

onTcpData: (callback) => {
const handler = (_, data) => callback(data);
ipcRenderer.on('stream-tcp-data', handler);
return () => ipcRenderer.removeListener('stream-tcp-data', handler);
},
onTcpJson: (callback) => {
const handler = (_, data) => callback(data);
ipcRenderer.on('stream-tcp-message', handler);
return () => ipcRenderer.removeListener('stream-tcp-message', handler);
},
onTcpStatus: (callback) => {
const handler = (_, status) => callback(status);
ipcRenderer.on('stream-tcp-status', handler);
return () => ipcRenderer.removeListener('stream-tcp-status', handler);
},
});
2 changes: 1 addition & 1 deletion src/components/CellSelector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'

export default function CellSelector({handleCellChange}) {
const [ids, setIds] = useState(["Space", "Wall", "Snake A", "Snake B", "Portal 1", "Portal 2"]);
const [ids, setIds] = useState(["Space", "Wall", "Player 1", "Player 2", "Hill"]);


return (
Expand Down
Loading