From 4f0612fab7d41709b6d07331b83b6ce59c337a63 Mon Sep 17 00:00:00 2001 From: Skillet Date: Wed, 1 Mar 2023 13:32:18 -0500 Subject: [PATCH] add database repair for playertimes table --- squadjsPlugins/db-log-addOn.js | 69 ++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/squadjsPlugins/db-log-addOn.js b/squadjsPlugins/db-log-addOn.js index f73008d..f2e58bd 100644 --- a/squadjsPlugins/db-log-addOn.js +++ b/squadjsPlugins/db-log-addOn.js @@ -74,42 +74,65 @@ export default class DBLogPlayerTime extends DBLog { async prepareToMount() { await super.prepareToMount(); - await this.models.PlayerTime.sync(); + await this.models.PlayerTime.sync(); + } async mount() { await super.mount(); - this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); - this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); + this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); + this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); + } + + async repairDB() { + await super.repairDB(); + let lastTickTime = this.models.TickRate.findOne( + { where: { server: this.options.overrideServerID || this.server.id}, + order: [['id', 'DESC']]} + ); + let lastServerTime = lastTickTime.time; + let playerOnlineID = []; + for (player of this.players){ + playerOnlineID.push(player.steamID); + } + const {not} = Sequelize.Op; + this.models.PlayerTime.update( + { leaveTime: lastServerTime }, + { where: { + leaveTime: null, + server: this.options.overrideServerID || this.server.id, + [not]: [{player: playerOnlineID}] + } } + ); } async unmount() { - this.models.PlayerTime.update( - { leaveTime: 0 }, - { where: { leaveTime: null , server: this.options.overrideServerID || this.server.id } } - ); + this.models.PlayerTime.update( + { leaveTime: 0 }, + { where: { leaveTime: null , server: this.options.overrideServerID || this.server.id } } + ); await super.unmount(); - this.server.removeEventListener('PLAYER_CONNECTED', this.onPlayerConnected); - this.server.removeEventListener('PLAYER_DISCONNECTED', this.onPlayerDisconnected); + this.server.removeEventListener('PLAYER_CONNECTED', this.onPlayerConnected); + this.server.removeEventListener('PLAYER_DISCONNECTED', this.onPlayerDisconnected); } async onUpdatedA2SInformation(info) { await super.onUpdatedA2SInformation(info); - if((this.seeding == true) && (info.a2sPlayerCount >= this.options.seedingThreshold)){ - console.log('switching to Live'); - this.seeding = false; - let curDateTime = new Date(); - let timeNow = curDateTime.getFullYear() + '-' + (curDateTime.getMonth() + 1) + '-' + curDateTime.getDate()+' '+curDateTime.getHours()+':'+curDateTime.getMinutes()+':'+curDateTime.getSeconds(); - console.log(timeNow); - await this.models.PlayerTime.update( - { seedTime: timeNow }, - { where: { seedTime: null, joinedSeeding: 1, leaveTime: null, server: this.options.overrideServerID || this.server.id } } - ); - }else if(this.seeding == false && (info.a2sPlayerCount-20) < this.options.seedingThreshold){ - console.log('switching to seeding'); - this.seeding = true; - } + if((this.seeding == true) && (info.a2sPlayerCount >= this.options.seedingThreshold)){ + console.log('switching to Live'); + this.seeding = false; + let curDateTime = new Date(); + let timeNow = curDateTime.getFullYear() + '-' + (curDateTime.getMonth() + 1) + '-' + curDateTime.getDate()+' '+curDateTime.getHours()+':'+curDateTime.getMinutes()+':'+curDateTime.getSeconds(); + console.log(timeNow); + await this.models.PlayerTime.update( + { seedTime: timeNow }, + { where: { seedTime: null, joinedSeeding: 1, leaveTime: null, server: this.options.overrideServerID || this.server.id } } + ); + }else if(this.seeding == false && (info.a2sPlayerCount-20) < this.options.seedingThreshold){ + console.log('switching to seeding'); + this.seeding = true; + } } async onPlayerConnected(info) {