From 68bc81d14e72f41604defd3f2c4bd1d1981c68e9 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Tue, 5 Sep 2023 10:58:04 -0700 Subject: [PATCH 1/2] fix/issue #6 --- package.json | 2 +- structures/DayzRBot.js | 42 ++++++++++++++++++++++++++++++++++++---- util/AdminLogsHandler.js | 8 ++++---- util/LogsHandler.js | 20 +++++++++---------- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index cd9bfb9..338be6d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "9.7.3", + "version": "10.0.0", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": { diff --git a/structures/DayzRBot.js b/structures/DayzRBot.js index fd7cecb..579f6e9 100644 --- a/structures/DayzRBot.js +++ b/structures/DayzRBot.js @@ -10,6 +10,7 @@ const { DownloadNitradoFile, CheckServerStatus } = require('../util/NitradoAPI') const { HandlePlayerLogs, HandleActivePlayersList } = require('../util/LogsHandler'); const { HandleKillfeed } = require('../util/KillfeedHandler'); const { HandleExpiredUAVs, HandleEvents } = require('../util/AlarmsHandler'); +const { SendConnectionLogs } = require('../util/AdminLogsHandler'); const path = require("path"); const fs = require('fs'); @@ -140,7 +141,10 @@ class DayzRBot extends Client { if (!this.exists(guild.playerstats)) guild.playerstats = []; let s = guild.playerstats; + s.map(p => p.connected = false) // assume all players not connected + for (let i = logIndex + 1; i < lines.length; i++) { + if (line.includes('| ####')) continue; if (lines[i].includes("(id=Unknown") || lines[i].includes("Player \"Unknown Entity\"")) continue; if ((i - 1) >= 0 && lines[i] == lines[i-1]) continue; // continue if this line is a duplicate of the last line if (lines[i].includes('connected') || lines[i].includes('pos=<') || lines[1].includes('hit by Player')) s = await HandlePlayerLogs(this, guildId, s, lines[i]); @@ -151,7 +155,9 @@ class DayzRBot extends Client { } const playerTemplate = /(.*) \| Player \"(.*)\" \(id=(.*) pos=<(.*)>\)/g; - s.map(p => p.connected = false) // make all connections false + let previouslyConnected = s.filter(p => p.connected); // All players with connection log captured above and no disconnect log + let detectedAsConnected = []; + let lastDetectedTime; for (let i = lines.length - 1; i > 0; i--) { if (lines[i].includes('PlayerList log:')) { @@ -168,22 +174,50 @@ class DayzRBot extends Client { playerID: data[3], }; + lastDetectedTime = info.time; + if (!this.exists(info.player) || !this.exists(info.playerID)) continue; - let playerStat = s.find(stat => stat.playerID == info.playerID) + let playerStat = s.find(stat => stat.playerID == info.playerID); let playerStatIndex = s.indexOf(playerStat); if (playerStat == undefined) playerStat = this.getDefaultPlayerStats(info.player, info.playerID); + if (!previouslyConnected.includes(playerStat)) { - playerStat.connected = true; + // This player was not connected before, i.e missing connection log? + playerStat.connected = true; + playerStat.lastConnectionDate = this.getDateEST(info.time); // Assume connected now + detectedAsConnected.push() + } if (playerStatIndex == -1) s.push(playerStat); else s[playerStatIndex] = playerStat; - + detectedAsConnected.push(playerStat); } break; } } + for (let i = 0; i < previouslyConnected.length; i++) { + if (!detectedAsConnected.includes(previouslyConnected[i])) { + + // This player disconnected without a disconnect log appearing. + client.log('Players disconnected withouth disconnect log: this can happen!!!') // debug proof + + let playerStat = spreviouslyConnected[i]; + let playerStatIndex = s.indexOf(playerStat); + + playerStat.connected = false; + s[playerStatIndex] = playerStat; + + SendConnectionLogs(client, guildId, { + time: lastDetectedTime, + player: playerStat.gamertag, + connected: false, + lastConnectionDate: playerStat.lastConnectionDate, + }); + } + } + await this.dbo.collection("guilds").updateOne({ "server.serverID": guildId }, { $set: { "server.playerstats": s diff --git a/util/AdminLogsHandler.js b/util/AdminLogsHandler.js index 730aecb..8572d3e 100644 --- a/util/AdminLogsHandler.js +++ b/util/AdminLogsHandler.js @@ -31,12 +31,12 @@ module.exports = { let newDt = await client.getDateEST(data.time); - let diff = Math.round((newDt.getTime() - data.lastDamageDate.getTime()) / 1000 / 60); // diff minutes + let diffSeconds = Math.round((newDt.getTime() - data.lastDamageDate.getTime()) / 1000); // If diff is greater than 5 minutes, not a combat log - // or if death after last combat and death was before logout event - if (data.lastDamageDate <= data.lastDeathDate && data.lastDeathDate < newDt) return; - if (diff > 5) return; + // or if death after last combat + if (data.lastDamageDate <= data.lastDeathDate) return; + if (diffSeconds > (5 * 60)) return; let guild = await client.GetGuild(guildId); if (!client.exists(guild.connectionLogsChannel)) return; diff --git a/util/LogsHandler.js b/util/LogsHandler.js index 40d17ec..5200e5e 100644 --- a/util/LogsHandler.js +++ b/util/LogsHandler.js @@ -20,7 +20,6 @@ module.exports = { time: data[1], player: data[2], playerID: data[3], - connected: true, }; if (!client.exists(info.player) || !client.exists(info.playerID)) return stats; @@ -32,6 +31,7 @@ module.exports = { let newDt = await client.getDateEST(info.time); playerStat.lastConnectionDate = newDt; + playerStat.connected = true; if (playerStatIndex == -1) stats.push(playerStat); else stats[playerStatIndex] = playerStat; @@ -39,7 +39,7 @@ module.exports = { SendConnectionLogs(client, guildId, { time: info.time, player: info.player, - connected: info.connected, + connected: true, lastConnectionDate: null, }); } @@ -52,7 +52,6 @@ module.exports = { time: data[1], player: data[2], playerID: data[3], - connected: false } if (!client.exists(info.player) || !client.exists(info.playerID)) return stats; @@ -62,14 +61,15 @@ module.exports = { if (playerStat == undefined) playerStat = client.getDefaultPlayerStats(info.player, info.playerID); let newDt = await client.getDateEST(info.time); - let unixTime = Math.floor(newDt.getTime()/1000); - let oldUnixTime = Math.floor(playerStat.lastConnectionDate.getTime()/1000); - let seconds = unixTime - oldUnixTime; + let unixTime = Math.round(newDt.getTime()/1000); // Seconds + let oldUnixTime = Math.round(playerStat.lastConnectionDate.getTime()/1000); // Seconds + let sessionTimeSeconds = unixTime - oldUnixTime; if (!client.exists(playerStat.longestSessionTime)) playerStat.longestSessionTime = 0; - playerStat.totalSessionTime = playerStat.totalSessionTime + seconds; - playerStat.lastSessionTime = seconds; - playerStat.longestSessionTime = seconds > playerStat.longestSessionTime ? seconds : playerStat.longestSessionTime; + playerStat.totalSessionTime = playerStat.totalSessionTime + sessionTimeSeconds; + playerStat.lastSessionTime = sessionTimeSeconds; + playerStat.longestSessionTime = sessionTimeSeconds > playerStat.longestSessionTime ? sessionTimeSeconds : playerStat.longestSessionTime; + playerStat.connected = false; if (playerStatIndex == -1) stats.push(playerStat); else stats[playerStatIndex] = playerStat; @@ -77,7 +77,7 @@ module.exports = { SendConnectionLogs(client, guildId, { time: info.time, player: info.player, - connected: info.connected, + connected: false, lastConnectionDate: playerStat.lastConnectionDate, }); From 7e6b469f18770ff0539071aefa96d284e0817705 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Tue, 5 Sep 2023 11:10:16 -0700 Subject: [PATCH 2/2] fix/errors --- package.json | 2 +- structures/DayzRBot.js | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 338be6d..845379f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "10.0.0", + "version": "9.8.0", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": { diff --git a/structures/DayzRBot.js b/structures/DayzRBot.js index 579f6e9..c6b47a6 100644 --- a/structures/DayzRBot.js +++ b/structures/DayzRBot.js @@ -144,7 +144,7 @@ class DayzRBot extends Client { s.map(p => p.connected = false) // assume all players not connected for (let i = logIndex + 1; i < lines.length; i++) { - if (line.includes('| ####')) continue; + if (lines[i].includes('| ####')) continue; if (lines[i].includes("(id=Unknown") || lines[i].includes("Player \"Unknown Entity\"")) continue; if ((i - 1) >= 0 && lines[i] == lines[i-1]) continue; // continue if this line is a duplicate of the last line if (lines[i].includes('connected') || lines[i].includes('pos=<') || lines[1].includes('hit by Player')) s = await HandlePlayerLogs(this, guildId, s, lines[i]); @@ -200,16 +200,13 @@ class DayzRBot extends Client { for (let i = 0; i < previouslyConnected.length; i++) { if (!detectedAsConnected.includes(previouslyConnected[i])) { - // This player disconnected without a disconnect log appearing. - client.log('Players disconnected withouth disconnect log: this can happen!!!') // debug proof - - let playerStat = spreviouslyConnected[i]; + let playerStat = previouslyConnected[i]; let playerStatIndex = s.indexOf(playerStat); playerStat.connected = false; s[playerStatIndex] = playerStat; - SendConnectionLogs(client, guildId, { + SendConnectionLogs(this, guildId, { time: lastDetectedTime, player: playerStat.gamertag, connected: false,