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

+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;
}
}