60 lines
2.0 KiB
JavaScript
60 lines
2.0 KiB
JavaScript
|
// main.js
|
||
|
const { spawn } = require('child_process');
|
||
|
const { gameEventEmitter } = require('./events');
|
||
|
const { Player } = require('./player');
|
||
|
|
||
|
let currentPlayers = [];
|
||
|
let playerScores = {};
|
||
|
|
||
|
function runCommand(command) {
|
||
|
const p = spawn('sh', ['-c', command], { stdio: 'pipe' });
|
||
|
|
||
|
p.stderr.on('data', (data) => {
|
||
|
const line = data.toString().trim();
|
||
|
console.log(">>> " + line);
|
||
|
|
||
|
if (line.startsWith('>>> red:') && line.includes(' blue:')) {
|
||
|
const redScore = parseInt(line.split('red:')[1].split(' ')[0]);
|
||
|
const blueScore = parseInt(line.split('blue:')[1]);
|
||
|
gameEventEmitter.emit('RoundEnd', redScore, blueScore, playerScores, currentPlayers);
|
||
|
playerScores = {}; // Reset playerScores after round end
|
||
|
} else if (line.startsWith('>>> score:')) {
|
||
|
const parts = line.split(' ');
|
||
|
const score = parseInt(parts[2]);
|
||
|
const clientId = parseInt(parts[6]);
|
||
|
const playerName = parts.slice(7).join(' ');
|
||
|
|
||
|
const player = currentPlayers.find(p => p.id === clientId);
|
||
|
if (player) {
|
||
|
player.score = score;
|
||
|
playerScores[clientId] = score;
|
||
|
console.log(`Player ${playerName} (ID: ${clientId}) has a score of ${score}`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (checkLine(line)) {
|
||
|
// Additional event handling can go here
|
||
|
gameEventEmitter.emit('SomeOtherEvent', line);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
p.on('close', (code) => {
|
||
|
console.log(`Process exited with code ${code}`);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function checkLine(line) {
|
||
|
if (line.startsWith("Pl")) return true;
|
||
|
if (line.startsWith("Shut")) return true;
|
||
|
if (line.startsWith("ClientConne")) return true;
|
||
|
if (line.startsWith("ClientDisconn")) return true;
|
||
|
if (line.startsWith("Kill")) return true;
|
||
|
if (line.includes("say:")) return true;
|
||
|
if (line.startsWith("ClientUser")) return true;
|
||
|
if (line.startsWith("broadcast")) return true;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// Running the game command
|
||
|
runCommand("sh start.sh");
|