refactor/logs handler to support migrated player stats
This commit is contained in:
12 files changed
+258
-222
No files matched your search
+34
-44
@@ -1,14 +1,15 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { HandleAlarmsAndUAVs } = require('./AlarmsHandler');
|
||||
const { SendConnectionLogs, DetectCombatLog } = require('./AdminLogsHandler');
|
||||
const { getDefaultPlayerStats } = require('../database/playerStatistics');
|
||||
const { getDefaultPlayer } = require('../database/player');
|
||||
const { FetchServerSettings } = require('../util/NitradoAPI');
|
||||
const { UpdatePlayer } = require('../database/player')
|
||||
|
||||
let lastSendMessage;
|
||||
|
||||
module.exports = {
|
||||
|
||||
HandlePlayerLogs: async (client, guildId, stats, line, combatLogTimer = 5) => {
|
||||
HandlePlayerLogs: async (client, GuildDB, line, combatLogTimer = 5) => {
|
||||
|
||||
const connectTemplate = /(.*) \| Player \"(.*)\" is connected \(id=(.*)\)/g;
|
||||
const disconnectTemplate = /(.*) \| Player \"(.*)\"\(id=(.*)\) has been disconnected/g;
|
||||
@@ -18,7 +19,7 @@ module.exports = {
|
||||
|
||||
if (line.includes(' connected')) {
|
||||
const data = [...line.matchAll(connectTemplate)][0];
|
||||
if (!data) return stats;
|
||||
if (!data) return;
|
||||
|
||||
const info = {
|
||||
time: data[1],
|
||||
@@ -26,12 +27,10 @@ module.exports = {
|
||||
playerID: data[3],
|
||||
};
|
||||
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return stats;
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return;
|
||||
|
||||
let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID});
|
||||
if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID);
|
||||
const newDt = await client.getDateEST(info.time);
|
||||
|
||||
playerStat.lastConnectionDate = newDt;
|
||||
@@ -51,20 +50,19 @@ module.exports = {
|
||||
client.playerSessions.set(info.playerID, newSession);
|
||||
}
|
||||
|
||||
if (playerStatIndex === -1) stats.push(playerStat);
|
||||
else stats[playerStatIndex] = playerStat;
|
||||
|
||||
await SendConnectionLogs(client, guildId, {
|
||||
await SendConnectionLogs(client, GuildDB, {
|
||||
time: info.time,
|
||||
player: info.player,
|
||||
connected: true,
|
||||
lastConnectionDate: null,
|
||||
});
|
||||
|
||||
return await UpdatePlayer(client, playerStat);
|
||||
}
|
||||
|
||||
if (line.includes(' disconnected')) {
|
||||
const data = [...line.matchAll(disconnectTemplate)][0];
|
||||
if (!data) return stats;
|
||||
if (!data) return;
|
||||
|
||||
const info = {
|
||||
time: data[1],
|
||||
@@ -72,11 +70,10 @@ module.exports = {
|
||||
playerID: data[3],
|
||||
};
|
||||
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return stats;
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return;
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID});
|
||||
if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID);
|
||||
|
||||
const newDt = await client.getDateEST(info.time);
|
||||
const unixTime = Math.round(newDt.getTime() / 1000); // Seconds
|
||||
@@ -90,10 +87,7 @@ module.exports = {
|
||||
playerStat.lastDisconnectionDate = newDt;
|
||||
playerStat.connected = false;
|
||||
|
||||
if (playerStatIndex === -1) stats.push(playerStat);
|
||||
else stats[playerStatIndex] = playerStat;
|
||||
|
||||
await SendConnectionLogs(client, guildId, {
|
||||
await SendConnectionLogs(client, GuildDB, {
|
||||
time: info.time,
|
||||
player: info.player,
|
||||
connected: false,
|
||||
@@ -101,7 +95,7 @@ module.exports = {
|
||||
});
|
||||
|
||||
if (combatLogTimer != 0) {
|
||||
DetectCombatLog(client, guildId, {
|
||||
await DetectCombatLog(client, GuildDB, {
|
||||
time: info.time,
|
||||
player: info.player,
|
||||
pos: playerStat.pos,
|
||||
@@ -111,11 +105,13 @@ module.exports = {
|
||||
combatLogTimer: combatLogTimer,
|
||||
});
|
||||
}
|
||||
|
||||
return await UpdatePlayer(client, playerStat);
|
||||
}
|
||||
|
||||
if (line.includes('pos=<') && !line.includes('hit by')) {
|
||||
const data = [...line.matchAll(positionTemplate)][0];
|
||||
if (!data) return stats;
|
||||
if (!data) return;
|
||||
|
||||
const info = {
|
||||
time: data[1],
|
||||
@@ -124,11 +120,10 @@ module.exports = {
|
||||
pos: data[4].split(', ').map(v => parseFloat(v))
|
||||
};
|
||||
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return stats;
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return;
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID});
|
||||
if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID);
|
||||
if (!client.exists(playerStat.lastConnectionDate)) playerStat.lastConnectionDate = await client.getDateEST(info.time);
|
||||
|
||||
playerStat.lastPos = playerStat.pos;
|
||||
@@ -138,22 +133,21 @@ module.exports = {
|
||||
playerStat.time = `${info.time} EST`;
|
||||
playerStat.date = await client.getDateEST(info.time);
|
||||
|
||||
if (playerStatIndex === -1) stats.push(playerStat);
|
||||
else stats[playerStatIndex] = playerStat;
|
||||
if (line.includes('hit by') || line.includes('killed by')) return; // prevent additional information from being fed to Alarms & UAVs
|
||||
|
||||
if (line.includes('hit by') || line.includes('killed by')) return stats; // prevent additional information from being fed to Alarms & UAVs
|
||||
|
||||
HandleAlarmsAndUAVs(client, guildId, {
|
||||
await HandleAlarmsAndUAVs(client, GuildDB, {
|
||||
time: info.time,
|
||||
player: info.player,
|
||||
playerID: info.playerID,
|
||||
pos: info.pos,
|
||||
});
|
||||
|
||||
return await UpdatePlayer(client, playerStat)
|
||||
}
|
||||
|
||||
if (line.includes('hit by Player')) {
|
||||
const data = line.includes('(DEAD)') ? [...line.matchAll(deadTemplate)][0] : [...line.matchAll(damageTemplate)][0];
|
||||
if (!data) return stats;
|
||||
if (!data) return;
|
||||
|
||||
const info = {
|
||||
time: data[1],
|
||||
@@ -163,23 +157,21 @@ module.exports = {
|
||||
attackerID: data[7]
|
||||
};
|
||||
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return stats;
|
||||
if (!client.exists(info.player) || !client.exists(info.playerID)) return;
|
||||
|
||||
let playerStat = stats.find(stat => stat.playerID == info.playerID);
|
||||
let playerStatIndex = stats.indexOf(playerStat);
|
||||
if (playerStat === undefined) playerStat = getDefaultPlayerStats(info.player, info.playerID);
|
||||
let playerStat = await client.dbo.collection("players").findOne({"playerID": info.playerID});
|
||||
if (!client.exits(playerStat)) playerStat = getDefaultPlayer(info.player, info.playerID);
|
||||
|
||||
playerStat.lastDamageDate = await client.getDateEST(info.time);
|
||||
playerStat.lastHitBy = info.attacker;
|
||||
|
||||
if (playerStatIndex === -1) stats.push(playerStat);
|
||||
else stats[playerStatIndex] = playerStat;
|
||||
return await UpdatePlayer(client, playerStat);
|
||||
}
|
||||
|
||||
return stats;
|
||||
return;
|
||||
},
|
||||
|
||||
HandleActivePlayersList: async (client, guildId) => {
|
||||
HandleActivePlayersList: async (client, guild) => {
|
||||
client.activePlayersTick = 0; // reset hour tick
|
||||
|
||||
const data = await FetchServerSettings(client, 'HandleActivePlayersList'); // Fetch server status
|
||||
@@ -207,12 +199,10 @@ module.exports = {
|
||||
statusText = "Unknown Status";
|
||||
}
|
||||
|
||||
let guild = await client.GetGuild(guildId);
|
||||
if (!client.exists(guild.playerstats)) guild.playerstats = [];
|
||||
if (!client.exists(guild.activePlayersChannel)) return;
|
||||
|
||||
const channel = client.GetChannel(guild.activePlayersChannel);
|
||||
let activePlayers = guild.playerstats.filter(p => p.connected === true);
|
||||
let activePlayers = client.dbo.collection("players").find({"connected": true})
|
||||
|
||||
let des = ``;
|
||||
for (let i = 0; i < activePlayers.length; i++) {
|
||||
|
||||
Reference in new issue
Block a user