refactor/logs handler to support migrated player stats

This commit is contained in:
SowinskiBraeden committed 2023-10-27 15:42:48 -07:00
1 parent ffc2e40351
commit 9b849dfc39
12 files changed
+258 -222

No files matched your search

+3 -6
View File
@@ -4,8 +4,7 @@ const { calculateVector } = require('./vector');
module.exports = {
SendConnectionLogs: async (client, guildId, data) => {
let guild = await client.GetGuild(guildId);
SendConnectionLogs: async (client, guild, data) => {
if (!client.exists(guild.connectionLogsChannel)) return;
const channel = client.GetChannel(guild.connectionLogsChannel);
@@ -27,10 +26,8 @@ module.exports = {
if (client.exists(channel)) await channel.send({ embeds: [connectionLog] });
},
DetectCombatLog: async (client, guildId, data) => {
DetectCombatLog: async (client, guild, data) => {
if (!client.exists(data.lastDamageDate)) return;
let guild = await client.GetGuild(guildId);
if (!client.exists(guild.connectionLogsChannel)) return;
const newDt = await client.getDateEST(data.time);
@@ -43,7 +40,7 @@ module.exports = {
// If lastHitBy (attacker) died after shooting this player
// then it does not count as combat logging, (the combat ended due to death)
let attacker = guild.playerstats.find(stat => stat.gamertag = data.lastHitBy);
let attacker = await client.dbo.collection("players").findOne({"gamertag": data.lastHitBy});
if (attacker.lastDeathDate > data.lastDamageDate) return;
const channel = client.GetChannel(guild.connectionLogsChannel);
+5 -12
View File
@@ -20,7 +20,7 @@ const ExpireEvent = async(client, guild, e) => {
}
const HandlePlayerTrackEvent = async (client, guild, e) => {
let player = guild.playerstats.find(stat => stat.gamertag == e.gamertag );
let player = await client.dbo.collection("players").findOne({"gamertag": e.gamertag});
let newDt = await client.getDateEST(player.time);
let unixTime = Math.floor(newDt.getTime()/1000);
@@ -58,8 +58,7 @@ const HandlePlayerTrackEvent = async (client, guild, e) => {
module.exports = {
HandleAlarmsAndUAVs: async (client, guildId, data) => {
let guild = await client.GetGuild(guildId);
HandleAlarmsAndUAVs: async (client, guild, data) => {
for (let i = 0; i < guild.alarms.length; i++) {
let alarm = guild.alarms[i];
@@ -137,8 +136,7 @@ module.exports = {
}
},
HandleExpiredUAVs: async (client, guildId) => {
let guild = await client.GetGuild(guildId);
HandleExpiredUAVs: async (client, guild) => {
let uavs = guild.uavs;
let update = false;
@@ -199,7 +197,7 @@ module.exports = {
return;
},
PlaceFireplaceInAlarm: async (client, guildId, line) => {
PlaceFireplaceInAlarm: async (client, guild, line) => {
let fireplacePlacement = /(.*) \| Player \"(.*)\" \(id=(.*) pos=<(.*)>\) placed Fireplace/g;
let data = [...line.matchAll(fireplacePlacement)][0];
@@ -212,8 +210,6 @@ module.exports = {
victimPOS: data[4].split(', ').map(v => parseFloat(v)),
};
let guild = await client.GetGuild(guildId);
for (let i = 0; i < guild.alarms.length; i++) {
let alarm = guild.alarms[i];
if (alarm.disabled || !alarm.rules.includes('ban_on_fireplace_placement')) continue;
@@ -242,10 +238,7 @@ module.exports = {
return;
},
HandleEvents: async (client, guildId) => {
let guild = await client.GetGuild(guildId);
HandleEvents: async (client, guild) => {
for (let i = 0; i < guild.events.length; i++) {
let event = guild.events[i];
if (event.type == 'player-track') HandlePlayerTrackEvent(client, guild, event);
+21 -32
View File
@@ -3,7 +3,7 @@ const { createUser, addUser } = require('../database/user');
const { KillInAlarm } = require('./AlarmsHandler');
const { destinations } = require('../database/destinations');
const { calculateVector } = require('./vector');
const { getDefaultPlayerStats } = require('../database/playerStatistics');
const { getDefaultPlayer, UpdatePlayer } = require('../database/player');
const Templates = {
Killed: 1,
@@ -42,7 +42,7 @@ const Vehicles = {
module.exports = {
// Update last death date for non PVP deaths
UpdateLastDeathDate: async (client, stats, line) => {
UpdateLastDeathDate: async (client, line) => {
let killedByZmb = /(.*) \| Player \"(.*)\" \(DEAD\) \(id=(.*) pos=<(.*)>\) killed by (.*)/g;
let diedTemplate = /(.*) \| Player \"(.*)\" \(DEAD\) \(id=(.*) pos=<(.*)>\) died\. Stats> Water: (.*) Energy: (.*) Bleed sources: (.*)/g;
@@ -58,19 +58,16 @@ module.exports = {
const newDt = await client.getDateEST(info.time);
let victimStat = stats.find(stat => stat.playerID == info.victimID);
let victimStatIndex = stats.indexOf(victimStat);
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
victimStat.lastDeathDate = newDt;
if (victimStatIndex == -1) stats.push(victimStat);
else stats[victimStatIndex] = victimStat;
let victimStat = await client.dbo.collection("players").findOne({"playerID": info.playerID});
if (!client.exits(victimStat)) playerStat = getDefaultPlayer(info.player, info.playerID);
return stats;
victimStat.lastDeathDate = newDt;
return await UpdatePlayer(client, victimStat);
},
HandleKillfeed: async (client, guildId, stats, line) => {
HandleKillfeed: async (client, guild, line) => {
let guild = await client.GetGuild(guildId);
const channel = client.GetChannel(guild.killfeedChannel);
let templateKilled = /(.*) \| Player \"(.*)\" \(DEAD\) \(id=(.*) pos=<(.*)>\) killed by Player \"(.*)\" \(id=(.*) pos=<(.*)>\) with (.*) from (.*) meters /g;
@@ -96,7 +93,7 @@ module.exports = {
killedBy == Templates.Vehicle ? [...line.matchAll(vehicleTemplate)][0] :
[...line.matchAll(explosionTemplate)][0];
if (!data) return stats;
if (!data) return;
// Create base data
let info = {
@@ -124,7 +121,7 @@ module.exports = {
}
else if (killedBy == Templates.Vehicle) info.causeOfDeath = data[6];
else if (killedBy == Templates.Explosion) info.causeOfDeath = data[5];
else return stats; // Unknown template;
else return; // Unknown template;
const newDt = await client.getDateEST(info.time);
const unixTime = Math.floor(newDt.getTime()/1000);
@@ -145,12 +142,9 @@ module.exports = {
const destination = lastDist > 500 ? `${destination_dir} of ${tempDest}` : `Near ${tempDest}`;
if (killedBy == Templates.LandMine || killedBy == Templates.Explosion || killedBy == Templates.Vehicle) {
let victimStat = stats.find(stat => stat.playerID == info.victimID)
let victimStatIndex = stats.indexOf(victimStat);
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
let victimStat = await client.dbo.collection("players").findOne({"playerID": info.victimID});
if (!client.exits(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID);
victimStat.lastDeathDate = newDt;
if (victimStatIndex == -1) stats.push(victimStat);
else stats[victimStatIndex] = victimStat;
const cod = killedBy == Templates.LandMine ? `Land Mine Trap` :
killedBy == Templates.Vehicle ? Vehicles[info.causeOfDeath] : info.causeOfDeath;
@@ -162,19 +156,17 @@ module.exports = {
.setDescription(`**Death Event** - <t:${unixTime}>\n**${info.victim}** ${killMessage} a **${cod}.**${coord}`);
if (client.exists(channel)) await channel.send({ embeds: [killEvent] });
return stats;
return await UpdatePlayer(client, victimStat);
}
killerStat
KillInAlarm(client, guildId, info); // check if kill happened in a no kill zone
if (!client.exists(info.victim) || !client.exists(info.victimID) || !client.exists(info.killer) || !client.exists(info.killerID)) return stats;
let killerStat = stats.find(stat => stat.playerID == info.killerID)
let victimStat = stats.find(stat => stat.playerID == info.victimID)
let killerStatIndex = stats.indexOf(killerStat);
let victimStatIndex = stats.indexOf(victimStat);
if (killerStat == undefined) killerStat = getDefaultPlayerStats(info.killer, info.killerID);
if (victimStat == undefined) victimStat = getDefaultPlayerStats(info.victim, info.victimID);
let victimStat = await client.dbo.collection("players").findOne({"playerID": info.victimID});
let killerStat = await client.dbo.collection("players").findOne({"playerID": info.killerID});
if (!client.exits(victimStat)) victimStat = getDefaultPlayer(info.victim, info.victimID);
if (!client.exits(killerStat)) killerStat = getDefaultPlayer(info.killer, info.killerID);
killerStat.kills++;
killerStat.killStreak++;
@@ -198,7 +190,6 @@ module.exports = {
let banking = await client.dbo.collection("users").findOne({"user.userID": killerStat.discordID}).then(banking => banking);
if (!banking) {
banking = await createUser(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, client)
if (!client.exists(banking)) return client.sendInternalError(interaction, err);
@@ -227,10 +218,8 @@ module.exports = {
victimStat.bounties = []; // clear bounties after claimed
}
if (killerStatIndex == -1) stats.push(killerStat);
else stats[killerStatIndex] = killerStat;
if (victimStatIndex == -1) stats.push(victimStat);
else stats[victimStatIndex] = victimStat;
await UpdatePlayer(client, victimStat);
await UpdatePlayer(client, killerStat);
const coord = showCoords ? `\n***Location [${info.victimPOS[0]}, ${info.victimPOS[1]}](https://www.izurvive.com/chernarusplussatmap/#location=${info.victimPOS[0]};${info.victimPOS[1]})***\n${destination}` : '';
@@ -241,6 +230,6 @@ module.exports = {
if (client.exists(channel)) await channel.send({ embeds: [killEvent] });
if (client.exists(receivedBounty) && client.exists(channel)) await channel.send({ content: `<@${killerStat.discordID}>`, embeds: [receivedBounty] });
return stats;
return;
}
}
+34 -44
View File
@@ -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++) {