4 commit
This commit is contained in:
parent
2fb380e9b4
commit
1267474d93
88
main.js
88
main.js
@ -202,6 +202,7 @@ function handleConsoleOutput(line) {
|
|||||||
const playerScores = {};
|
const playerScores = {};
|
||||||
|
|
||||||
if (eventClientConnect(line, line, playerQueue)) return;
|
if (eventClientConnect(line, line, playerQueue)) return;
|
||||||
|
if (eventPlayer(line, line, currentPlayers, playerQueue)) return;
|
||||||
if (eventDisconnect(line, line, currentPlayers, playerQueue)) return;
|
if (eventDisconnect(line, line, currentPlayers, playerQueue)) return;
|
||||||
if (eventKill(line, line, currentPlayers)) return;
|
if (eventKill(line, line, currentPlayers)) return;
|
||||||
if (eventSay(line, line, currentPlayers)) return;
|
if (eventSay(line, line, currentPlayers)) return;
|
||||||
@ -249,23 +250,6 @@ async function eventClientConnect(event, eventContent, playerQueue) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the player already exists in the database
|
|
||||||
const playerExists = await checkIfPlayerExists(guid);
|
|
||||||
|
|
||||||
if (!playerExists) {
|
|
||||||
// Create a new player in the database
|
|
||||||
const newPlayer = {
|
|
||||||
guid: guid,
|
|
||||||
id: pid,
|
|
||||||
ip: null, // You might want to extract the IP from the eventContent if available
|
|
||||||
name: null, // Player name can be updated later
|
|
||||||
elo: 1000, // Default starting Elo rating
|
|
||||||
aliases: [] // Start with an empty list of aliases
|
|
||||||
};
|
|
||||||
await insertNewPlayer(newPlayer);
|
|
||||||
console.log(`New player added to the database: [GUID: ${guid}]`);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Adding player to queue: [PID: ${pid}] [GUID: ${guid}]`);
|
console.log(`Adding player to queue: [PID: ${pid}] [GUID: ${guid}]`);
|
||||||
playerQueue.push({ guid: guid, id: pid, ip: null, name: null });
|
playerQueue.push({ guid: guid, id: pid, ip: null, name: null });
|
||||||
|
|
||||||
@ -274,6 +258,61 @@ async function eventClientConnect(event, eventContent, playerQueue) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function eventPlayer(event, eventContent, currentPlayers, playerQueue) {
|
||||||
|
if (event.startsWith("Player")) {
|
||||||
|
try {
|
||||||
|
// Extract GUID, name, and IP from eventContent
|
||||||
|
const guid = eventContent.split("\\ja_guid\\", 1)[1].split("\\", 1)[0];
|
||||||
|
const name = eventContent.split("\\name\\", 1)[1].split("\\", 1)[0];
|
||||||
|
const ip = eventContent.split("\\ip\\", 1)[1].split("\\", 1)[0].split(":", 1)[0];
|
||||||
|
|
||||||
|
if (guid === "0") {
|
||||||
|
console.log(`Skipping player with invalid GUID: ${guid}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use GUID to load or create the player
|
||||||
|
const playerInfo = await loadPlayerByGuid(guid, name, ip, currentPlayers);
|
||||||
|
playerInfo.id = parseInt(event.split(" ")[1], 10); // Set session-specific ID
|
||||||
|
console.log(`Player event detected. Player ID: ${playerInfo.id}`);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error processing player event:", error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPlayerByGuid(guid, name, ip, currentPlayers) {
|
||||||
|
let player = currentPlayers.find(p => p.guid === guid);
|
||||||
|
|
||||||
|
if (!player) {
|
||||||
|
// Check if the player already exists in the database
|
||||||
|
const playerExists = await checkIfPlayerExists(guid);
|
||||||
|
|
||||||
|
if (playerExists) {
|
||||||
|
player = await getPlayerFromDatabase(guid);
|
||||||
|
console.log(`Loaded player from database: ${player.name} (GUID: ${guid})`);
|
||||||
|
} else {
|
||||||
|
// Create a new player in the database
|
||||||
|
player = new Player(null, ip, name, guid);
|
||||||
|
await insertNewPlayer(player);
|
||||||
|
console.log(`New player added to the database: ${name} (GUID: ${guid})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to current players
|
||||||
|
currentPlayers.push(player);
|
||||||
|
} else {
|
||||||
|
console.log(`Player already in current session: ${name} (GUID: ${guid})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if a player with the given GUID already exists in the database
|
// Check if a player with the given GUID already exists in the database
|
||||||
async function checkIfPlayerExists(guid) {
|
async function checkIfPlayerExists(guid) {
|
||||||
const connection = await pool.getConnection();
|
const connection = await pool.getConnection();
|
||||||
@ -286,11 +325,24 @@ async function checkIfPlayerExists(guid) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get player data from the database
|
||||||
|
async function getPlayerFromDatabase(guid) {
|
||||||
|
const connection = await pool.getConnection();
|
||||||
|
try {
|
||||||
|
const query = `SELECT * FROM players WHERE guid = ?`;
|
||||||
|
const [rows] = await connection.execute(query, [guid]);
|
||||||
|
const playerData = rows[0];
|
||||||
|
return new Player(null, playerData.ip, playerData.name, playerData.guid, playerData.elo);
|
||||||
|
} finally {
|
||||||
|
if (connection) connection.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Insert a new player into the database
|
// Insert a new player into the database
|
||||||
async function insertNewPlayer(player) {
|
async function insertNewPlayer(player) {
|
||||||
const connection = await pool.getConnection();
|
const connection = await pool.getConnection();
|
||||||
try {
|
try {
|
||||||
const aliasesStr = JSON.stringify(player.aliases);
|
const aliasesStr = JSON.stringify(player.aliases || []);
|
||||||
const insertQuery = `
|
const insertQuery = `
|
||||||
INSERT INTO players (guid, name, ip, elo, aliases)
|
INSERT INTO players (guid, name, ip, elo, aliases)
|
||||||
VALUES (?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
Loading…
Reference in New Issue
Block a user