første commit

This commit is contained in:
MrKritio 2024-08-17 00:46:46 +02:00
commit 3f86ab488e
5 changed files with 180 additions and 0 deletions

35
database.js Normal file
View File

@ -0,0 +1,35 @@
// database.js
const mysql = require('mysql2/promise');
const dbConfig = {
host: "95.217.113.161",
user: "Likima",
password: "27As$r0q1",
database: "jedaiito",
charset: "utf8"
};
const pool = mysql.createPool({
...dbConfig,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
async function updatePlayerElo(player) {
const connection = await pool.getConnection();
try {
const updateQuery = `
UPDATE players
SET elo = ?
WHERE guid = ?
`;
await connection.query(updateQuery, [player.elo, player.guid]);
} finally {
connection.release();
}
}
module.exports = {
updatePlayerElo,
};

10
elo.js Normal file
View File

@ -0,0 +1,10 @@
// elo.js
function calculateElo(currentRating, opponentRating, actualScore, kFactor = 32) {
const expectedScore = 1 / (1 + Math.pow(10, (opponentRating - currentRating) / 400));
const newRating = currentRating + kFactor * (actualScore - expectedScore);
return newRating;
}
module.exports = {
calculateElo,
};

54
events.js Normal file
View File

@ -0,0 +1,54 @@
// events.js
const events = require('events');
const { calculateElo } = require('./elo');
const { updatePlayerElo } = require('./database');
class GameEventEmitter extends events.EventEmitter {}
const gameEventEmitter = new GameEventEmitter();
function handleRoundEnd(redScore, blueScore, playerScores, currentPlayers) {
console.log(`Round ended. Red: ${redScore}, Blue: ${blueScore}`);
let winningTeam, losingTeam;
if (redScore > blueScore) {
winningTeam = 'red';
losingTeam = 'blue';
} else if (blueScore > redScore) {
winningTeam = 'blue';
losingTeam = 'red';
} else {
console.log("It's a draw. No Elo change.");
return;
}
const kFactor = 32; // You can adjust this as needed
const winningPlayers = currentPlayers.filter(player => player.team === winningTeam);
const losingPlayers = currentPlayers.filter(player => player.team === losingTeam);
const averageWinningElo = winningPlayers.reduce((acc, player) => acc + player.elo, 0) / winningPlayers.length;
const averageLosingElo = losingPlayers.reduce((acc, player) => acc + player.elo, 0) / losingPlayers.length;
winningPlayers.forEach(async (player) => {
const individualPerformanceFactor = playerScores[player.id] / redScore; // Adjust based on individual contribution
player.elo = calculateElo(player.elo, averageLosingElo, 1 * individualPerformanceFactor, kFactor);
await updatePlayerElo(player);
});
losingPlayers.forEach(async (player) => {
const individualPerformanceFactor = playerScores[player.id] / blueScore; // Adjust based on individual contribution
player.elo = calculateElo(player.elo, averageWinningElo, 0 * individualPerformanceFactor, kFactor);
await updatePlayerElo(player);
});
console.log("Elo ratings updated.");
}
gameEventEmitter.on('RoundEnd', handleRoundEnd);
module.exports = {
gameEventEmitter,
handleRoundEnd,
};

59
main.js Normal file
View File

@ -0,0 +1,59 @@
// 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");

22
player.js Normal file
View File

@ -0,0 +1,22 @@
// player.js
class Player {
constructor(id, ip, name, guid, elo = 1000, score = 0, team = '') {
this.id = id;
this.ip = ip;
this.name = name;
this.guid = guid;
this.elo = elo;
this.score = score;
this.team = team;
this.is_alive = true;
}
}
function removeColorCodes(name) {
return name.replace(/\^\d/g, '');
}
module.exports = {
Player,
removeColorCodes,
};