From b27bc662d7f957de8bcac0ba8d10c8a46c5dff0b Mon Sep 17 00:00:00 2001 From: Skillet Date: Mon, 6 Mar 2023 18:11:56 -0500 Subject: [PATCH 1/4] Update db-log-addOn.js --- squadjsPlugins/db-log-addOn.js | 370 ++++++++++++++++++--------------- 1 file changed, 208 insertions(+), 162 deletions(-) diff --git a/squadjsPlugins/db-log-addOn.js b/squadjsPlugins/db-log-addOn.js index a59804b..44f6ea1 100644 --- a/squadjsPlugins/db-log-addOn.js +++ b/squadjsPlugins/db-log-addOn.js @@ -2,191 +2,237 @@ import Sequelize from 'sequelize'; import DBLog from './db-log.js'; -const { DataTypes } = Sequelize; +const {DataTypes} = Sequelize; +const ServerState = { + init: 0, + seeding: 1, + live: 2 +}; export default class DBLogPlayerTime extends DBLog { - static get description() { - return ( - 'replacement add-on to dblog for player join/seeding times' - ); - } + static get description() { + return ( + 'replacement add-on to dblog for player join/seeding times' + ); + } - static get defaultEnabled() { - return false; - } + static get defaultEnabled() { + return false; + } - static get optionsSpecification() { - return { - ...DBLog.optionsSpecification, - seedingThreshold: { + static get optionsSpecification() { + return { + ...DBLog.optionsSpecification, + seedingThreshold: { required: false, description: 'seeding Threshold.', default: 50 - } - }; - } + } + }; + } - constructor(server, options, connectors) { - super(server, options, connectors); + constructor(server, options, connectors) { + super(server, options, connectors); - this.seeding = false; - this.repairSessions = true; - this.lastTickTime = null; + this.seeding = ServerState.init; this.createModel( - 'PlayerTime', - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - }, - joinTime: { - type: DataTypes.DATE - }, - leaveTime: { - type: DataTypes.DATE - }, - seedTime: { - type: DataTypes.DATE - }, - joinedSeeding: { - type: DataTypes.BOOLEAN - } + 'PlayerTimeNew', + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true }, - { - charset: 'utf8mb4', - collate: 'utf8mb4_unicode_ci' + startTime: { + type: DataTypes.DATE + }, + endTime: { + type: DataTypes.DATE + }, + serverState: { + type: DataTypes.INTEGER } + }, + { + charset: 'utf8mb4', + collate: 'utf8mb4_unicode_ci' + } ); - this.models.Server.hasMany(this.models.PlayerTime, { - foreignKey: { name: 'server', allowNull: false }, - onDelete: 'CASCADE' + this.models.Server.hasMany(this.models.PlayerTimeNew, { + foreignKey: {name: 'server', allowNull: false}, + onDelete: 'CASCADE' }); - this.models.SteamUser.hasMany(this.models.PlayerTime, { - foreignKey: {name: 'player' }, - onDelete: 'CASCADE' + this.models.SteamUser.hasMany(this.models.PlayerTimeNew, { + foreignKey: {name: 'player'}, + onDelete: 'CASCADE' }); this.onPlayerConnected = this.onPlayerConnected.bind(this); this.onPlayerDisconnected = this.onPlayerDisconnected.bind(this); - } - - async prepareToMount() { - await super.prepareToMount(); - await this.models.PlayerTime.sync(); - - } - - async mount() { - console.log('Mounting db-log'); - await super.mount(); - console.log('finished mounting db-log'); - this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); - this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); - console.log('finished mounting db-log-addOn'); - } - - async repairDB() { - console.log('starting DB repair'); - await super.repairDB(); - console.log('starting DB repair for addOn'); - let lastTickTime = await this.models.TickRate.findOne({ - where: { server: this.options.overrideServerID || this.server.id}, - order: [['id', 'DESC']], - logging: console.log - } - ); - console.log('last tick found:', lastTickTime); - let lastServerDate = lastTickTime.time; - let lastServerTime = lastServerDate.getFullYear() + '-' + (lastServerDate.getMonth() + 1) + '-' + lastServerDate.getDate()+' '+lastServerDate.getHours()+':'+lastServerDate.getMinutes()+':'+lastServerDate.getSeconds(); - console.log('last time found:', lastServerTime); - let playerOnlineID = []; - playerOnlineID.push(0); - for (const player of this.server.players){ - playerOnlineID.push(player.steamID); } - console.log('players online:', playerOnlineID); - const {ne, not, notIn, is} = Sequelize.Op; - let updateVals = { leaveTime: lastServerTime }; - let whereStuff = { - leaveTime: {[is]: null}, - server: this.options.overrideServerID || this.server.id, - player: {[notIn]: playerOnlineID} - }; - console.log(updateVals); - console.log(whereStuff); - let rowUpdate = await this.models.PlayerTime.update( - updateVals, { - where: whereStuff, - logging: console.log - } - ); - console.log('updated playerTimes row count: %i', rowUpdate[0]); - console.log('finish DB repair'); - } - async unmount() { - 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); - } + async prepareToMount() { + await super.prepareToMount(); + await this.models.PlayerTimeNew.sync(); - 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; - } - } - - async onPlayerConnected(info) { - console.log(info); - if(info.player){ - await this.models.SteamUser.upsert({ - steamID: info.player.steamID, - lastName: info.player.name - }); - await this.models.PlayerTime.create({ - server: this.options.overrideServerID || this.server.id, - player: info.steamID, - joinTime: info.time, - joinedSeeding: this.seeding - }); - console.log('player connect complete'); - } else console.log('player is null'); - } - - async onPlayerDisconnected(info) { - await sleep (500); - console.log(info); - if(info.player){ - await this.models.SteamUser.upsert({ - steamID: info.player.steamID, - lastName: info.player.name - }); } - let rowAffect = await this.models.PlayerTime.update( - { leaveTime: info.time }, - { where: { player: info.steamID, leaveTime: null, server: this.options.overrideServerID || this.server.id } } - ); - console.log('player disconnect rows update: %i', rowAffect[0]); - } + + async mount() { + console.log('Mounting db-log'); + await super.mount(); + console.log('finished mounting db-log'); + this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); + this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); + console.log('finished mounting db-log-addOn'); + } + + async repairDB() { + console.log('starting DB repair'); + await super.repairDB(); + + console.log('starting DB repair for addOn'); + + const lastTickTime = await this.models.TickRate.findOne({ + where: {server: this.options.overrideServerID || this.server.id}, + order: [['id', 'DESC']], + logging: console.log + } + ); + console.log('last tick found:', lastTickTime); + + const lastServerDate = lastTickTime.time; + const lastServerTime = lastServerDate.getFullYear() + '-' + (lastServerDate.getMonth() + 1) + '-' + lastServerDate.getDate() + ' ' + lastServerDate.getHours() + ':' + lastServerDate.getMinutes() + ':' + lastServerDate.getSeconds(); + console.log('last time found:', lastServerTime); + + const playerOnlineID = []; + playerOnlineID.push(0); + for (const player of this.server.players) { + playerOnlineID.push(player.steamID); + } + console.log('players online:', playerOnlineID); + + const {notIn, is} = Sequelize.Op; + const updateVals = {endTime: lastServerTime}; + const whereStuff = { + endTime: {[is]: null}, + server: this.options.overrideServerID || this.server.id, + player: {[notIn]: playerOnlineID} + }; + console.log(updateVals); + console.log(whereStuff); + + const rowUpdate = await this.models.PlayerTimeNew.update( + updateVals, { + where: whereStuff, + logging: console.log + } + ); + + console.log('updated playerTimes row count: %i', rowUpdate[0]); + console.log('finish DB repair'); + this.seeding = ServerState.live; + } + + async unmount() { + this.models.PlayerTimeNew.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); + } + + async updateCurrentTimeState(date, oldState, newState){ + const timeNow = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); + console.log(timeNow); + const curPlayer = await this.models.PlayerTimeNew.findAll({ + where: { + leaveTime: null, + serverState: oldState, + server: this.options.overrideServerID || this.server.id + } + }); + await this.models.PlayerTimeNew.update( + { endTime: timeNow }, + { + where: { + endTime: null, + serverState: oldState, + server: this.options.overrideServerID || this.server.id + } + } + ); + await this.models.PlayerTimeNew.bulkCreate(curPlayer,{ + fields: ['startTime', 'endTime','serverState','session','server','player'] + }); + await this.models.PlayerTimeNew.update( + { + serverState: newState, + startTime: timeNow + }, + { + where: { + endTime: null, + serverState: oldState, + server: this.options.overrideServerID || this.server.id + } + } + ); + this.seeding = newState; + } + + async onUpdatedA2SInformation(info) { + await super.onUpdatedA2SInformation(info); + + const curDateTime = new Date(); + if ((this.seeding !== ServerState.live) && (info.a2sPlayerCount >= this.options.seedingThreshold)) { + console.log('switching to Live'); + await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live); + } else if (this.seeding === false && (info.a2sPlayerCount - 20) < this.options.seedingThreshold) { + console.log('switching to seeding'); + await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.seeding); + } + } + + async onPlayerConnected(info) { + console.log(info); + if (info.player) { + await this.models.SteamUser.upsert({ + steamID: info.player.steamID, + lastName: info.player.name + }); + await this.models.PlayerTimeNew.create({ + server: this.options.overrideServerID || this.server.id, + player: info.steamID, + startTime: info.time, + serverState: this.seeding + }); + console.log('player connect complete'); + } else console.log('player is null'); + } + + async onPlayerDisconnected(info) { + await sleep(500); + console.log(info); + if (info.player) { + await this.models.SteamUser.upsert({ + steamID: info.player.steamID, + lastName: info.player.name + }); + } + const rowAffect = await this.models.PlayerTimeNew.update( + {endTime: info.time}, + {where: + { + player: info.steamID, + endTime: null, + server: this.options.overrideServerID || this.server.id + }} + ); + console.log('player disconnect rows update: %i', rowAffect[0]); + } } From b2ede5df250e1d5fbe4cc0364cd7fec778082be1 Mon Sep 17 00:00:00 2001 From: Skillet Date: Mon, 6 Mar 2023 18:20:33 -0500 Subject: [PATCH 2/4] Revert "Update db-log-addOn.js" This reverts commit b27bc662d7f957de8bcac0ba8d10c8a46c5dff0b. --- squadjsPlugins/db-log-addOn.js | 370 +++++++++++++++------------------ 1 file changed, 162 insertions(+), 208 deletions(-) diff --git a/squadjsPlugins/db-log-addOn.js b/squadjsPlugins/db-log-addOn.js index 44f6ea1..a59804b 100644 --- a/squadjsPlugins/db-log-addOn.js +++ b/squadjsPlugins/db-log-addOn.js @@ -2,237 +2,191 @@ import Sequelize from 'sequelize'; import DBLog from './db-log.js'; -const {DataTypes} = Sequelize; -const ServerState = { - init: 0, - seeding: 1, - live: 2 -}; +const { DataTypes } = Sequelize; export default class DBLogPlayerTime extends DBLog { - static get description() { - return ( - 'replacement add-on to dblog for player join/seeding times' - ); - } + static get description() { + return ( + 'replacement add-on to dblog for player join/seeding times' + ); + } - static get defaultEnabled() { - return false; - } + static get defaultEnabled() { + return false; + } - static get optionsSpecification() { - return { - ...DBLog.optionsSpecification, - seedingThreshold: { + static get optionsSpecification() { + return { + ...DBLog.optionsSpecification, + seedingThreshold: { required: false, description: 'seeding Threshold.', default: 50 - } - }; - } + } + }; + } - constructor(server, options, connectors) { - super(server, options, connectors); + constructor(server, options, connectors) { + super(server, options, connectors); - this.seeding = ServerState.init; + this.seeding = false; + this.repairSessions = true; + this.lastTickTime = null; this.createModel( - 'PlayerTimeNew', - { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true + 'PlayerTime', + { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, + joinTime: { + type: DataTypes.DATE + }, + leaveTime: { + type: DataTypes.DATE + }, + seedTime: { + type: DataTypes.DATE + }, + joinedSeeding: { + type: DataTypes.BOOLEAN + } }, - startTime: { - type: DataTypes.DATE - }, - endTime: { - type: DataTypes.DATE - }, - serverState: { - type: DataTypes.INTEGER + { + charset: 'utf8mb4', + collate: 'utf8mb4_unicode_ci' } - }, - { - charset: 'utf8mb4', - collate: 'utf8mb4_unicode_ci' - } ); - this.models.Server.hasMany(this.models.PlayerTimeNew, { - foreignKey: {name: 'server', allowNull: false}, - onDelete: 'CASCADE' + this.models.Server.hasMany(this.models.PlayerTime, { + foreignKey: { name: 'server', allowNull: false }, + onDelete: 'CASCADE' }); - this.models.SteamUser.hasMany(this.models.PlayerTimeNew, { - foreignKey: {name: 'player'}, - onDelete: 'CASCADE' + this.models.SteamUser.hasMany(this.models.PlayerTime, { + foreignKey: {name: 'player' }, + onDelete: 'CASCADE' }); this.onPlayerConnected = this.onPlayerConnected.bind(this); this.onPlayerDisconnected = this.onPlayerDisconnected.bind(this); + } + + async prepareToMount() { + await super.prepareToMount(); + await this.models.PlayerTime.sync(); + + } + + async mount() { + console.log('Mounting db-log'); + await super.mount(); + console.log('finished mounting db-log'); + this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); + this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); + console.log('finished mounting db-log-addOn'); + } + + async repairDB() { + console.log('starting DB repair'); + await super.repairDB(); + console.log('starting DB repair for addOn'); + let lastTickTime = await this.models.TickRate.findOne({ + where: { server: this.options.overrideServerID || this.server.id}, + order: [['id', 'DESC']], + logging: console.log + } + ); + console.log('last tick found:', lastTickTime); + let lastServerDate = lastTickTime.time; + let lastServerTime = lastServerDate.getFullYear() + '-' + (lastServerDate.getMonth() + 1) + '-' + lastServerDate.getDate()+' '+lastServerDate.getHours()+':'+lastServerDate.getMinutes()+':'+lastServerDate.getSeconds(); + console.log('last time found:', lastServerTime); + let playerOnlineID = []; + playerOnlineID.push(0); + for (const player of this.server.players){ + playerOnlineID.push(player.steamID); } + console.log('players online:', playerOnlineID); + const {ne, not, notIn, is} = Sequelize.Op; + let updateVals = { leaveTime: lastServerTime }; + let whereStuff = { + leaveTime: {[is]: null}, + server: this.options.overrideServerID || this.server.id, + player: {[notIn]: playerOnlineID} + }; + console.log(updateVals); + console.log(whereStuff); + let rowUpdate = await this.models.PlayerTime.update( + updateVals, { + where: whereStuff, + logging: console.log + } + ); + console.log('updated playerTimes row count: %i', rowUpdate[0]); + console.log('finish DB repair'); + } - async prepareToMount() { - await super.prepareToMount(); - await this.models.PlayerTimeNew.sync(); + async unmount() { + 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); + } + 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; + } + } + + async onPlayerConnected(info) { + console.log(info); + if(info.player){ + await this.models.SteamUser.upsert({ + steamID: info.player.steamID, + lastName: info.player.name + }); + await this.models.PlayerTime.create({ + server: this.options.overrideServerID || this.server.id, + player: info.steamID, + joinTime: info.time, + joinedSeeding: this.seeding + }); + console.log('player connect complete'); + } else console.log('player is null'); + } + + async onPlayerDisconnected(info) { + await sleep (500); + console.log(info); + if(info.player){ + await this.models.SteamUser.upsert({ + steamID: info.player.steamID, + lastName: info.player.name + }); } - - async mount() { - console.log('Mounting db-log'); - await super.mount(); - console.log('finished mounting db-log'); - this.server.on('PLAYER_CONNECTED', this.onPlayerConnected); - this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected); - console.log('finished mounting db-log-addOn'); - } - - async repairDB() { - console.log('starting DB repair'); - await super.repairDB(); - - console.log('starting DB repair for addOn'); - - const lastTickTime = await this.models.TickRate.findOne({ - where: {server: this.options.overrideServerID || this.server.id}, - order: [['id', 'DESC']], - logging: console.log - } - ); - console.log('last tick found:', lastTickTime); - - const lastServerDate = lastTickTime.time; - const lastServerTime = lastServerDate.getFullYear() + '-' + (lastServerDate.getMonth() + 1) + '-' + lastServerDate.getDate() + ' ' + lastServerDate.getHours() + ':' + lastServerDate.getMinutes() + ':' + lastServerDate.getSeconds(); - console.log('last time found:', lastServerTime); - - const playerOnlineID = []; - playerOnlineID.push(0); - for (const player of this.server.players) { - playerOnlineID.push(player.steamID); - } - console.log('players online:', playerOnlineID); - - const {notIn, is} = Sequelize.Op; - const updateVals = {endTime: lastServerTime}; - const whereStuff = { - endTime: {[is]: null}, - server: this.options.overrideServerID || this.server.id, - player: {[notIn]: playerOnlineID} - }; - console.log(updateVals); - console.log(whereStuff); - - const rowUpdate = await this.models.PlayerTimeNew.update( - updateVals, { - where: whereStuff, - logging: console.log - } - ); - - console.log('updated playerTimes row count: %i', rowUpdate[0]); - console.log('finish DB repair'); - this.seeding = ServerState.live; - } - - async unmount() { - this.models.PlayerTimeNew.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); - } - - async updateCurrentTimeState(date, oldState, newState){ - const timeNow = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds(); - console.log(timeNow); - const curPlayer = await this.models.PlayerTimeNew.findAll({ - where: { - leaveTime: null, - serverState: oldState, - server: this.options.overrideServerID || this.server.id - } - }); - await this.models.PlayerTimeNew.update( - { endTime: timeNow }, - { - where: { - endTime: null, - serverState: oldState, - server: this.options.overrideServerID || this.server.id - } - } - ); - await this.models.PlayerTimeNew.bulkCreate(curPlayer,{ - fields: ['startTime', 'endTime','serverState','session','server','player'] - }); - await this.models.PlayerTimeNew.update( - { - serverState: newState, - startTime: timeNow - }, - { - where: { - endTime: null, - serverState: oldState, - server: this.options.overrideServerID || this.server.id - } - } - ); - this.seeding = newState; - } - - async onUpdatedA2SInformation(info) { - await super.onUpdatedA2SInformation(info); - - const curDateTime = new Date(); - if ((this.seeding !== ServerState.live) && (info.a2sPlayerCount >= this.options.seedingThreshold)) { - console.log('switching to Live'); - await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live); - } else if (this.seeding === false && (info.a2sPlayerCount - 20) < this.options.seedingThreshold) { - console.log('switching to seeding'); - await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.seeding); - } - } - - async onPlayerConnected(info) { - console.log(info); - if (info.player) { - await this.models.SteamUser.upsert({ - steamID: info.player.steamID, - lastName: info.player.name - }); - await this.models.PlayerTimeNew.create({ - server: this.options.overrideServerID || this.server.id, - player: info.steamID, - startTime: info.time, - serverState: this.seeding - }); - console.log('player connect complete'); - } else console.log('player is null'); - } - - async onPlayerDisconnected(info) { - await sleep(500); - console.log(info); - if (info.player) { - await this.models.SteamUser.upsert({ - steamID: info.player.steamID, - lastName: info.player.name - }); - } - const rowAffect = await this.models.PlayerTimeNew.update( - {endTime: info.time}, - {where: - { - player: info.steamID, - endTime: null, - server: this.options.overrideServerID || this.server.id - }} - ); - console.log('player disconnect rows update: %i', rowAffect[0]); - } + let rowAffect = await this.models.PlayerTime.update( + { leaveTime: info.time }, + { where: { player: info.steamID, leaveTime: null, server: this.options.overrideServerID || this.server.id } } + ); + console.log('player disconnect rows update: %i', rowAffect[0]); + } } From b1f523c67c95ea200571d63f11058288dbeb9dcd Mon Sep 17 00:00:00 2001 From: Skillet Date: Mon, 6 Mar 2023 21:30:12 -0500 Subject: [PATCH 3/4] Update db-log-addOn.js --- squadjsPlugins/db-log-addOn.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/squadjsPlugins/db-log-addOn.js b/squadjsPlugins/db-log-addOn.js index a59804b..d5963c6 100644 --- a/squadjsPlugins/db-log-addOn.js +++ b/squadjsPlugins/db-log-addOn.js @@ -175,7 +175,7 @@ export default class DBLogPlayerTime extends DBLog { } async onPlayerDisconnected(info) { - await sleep (500); + await new Promise(r => setTimeout(r, 500)); console.log(info); if(info.player){ await this.models.SteamUser.upsert({ From 7427f70dfcd5295fc98ee3a827dfbd0a1296e49e Mon Sep 17 00:00:00 2001 From: Skillet Date: Mon, 20 Mar 2023 13:01:46 -0400 Subject: [PATCH 4/4] add squad name validator --- squadjsPlugins/squad-name-validator.js | 156 +++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 squadjsPlugins/squad-name-validator.js diff --git a/squadjsPlugins/squad-name-validator.js b/squadjsPlugins/squad-name-validator.js new file mode 100644 index 0000000..cc50439 --- /dev/null +++ b/squadjsPlugins/squad-name-validator.js @@ -0,0 +1,156 @@ +import DiscordBasePlugin from './discord-base-plugin.js'; + +export default class SquadNameValidator extends DiscordBasePlugin { + static get description() { + return "Squad Name Validator plugin"; + } + + static get defaultEnabled() { + return true; + } + + static get optionsSpecification() { + return { + ...DiscordBasePlugin.optionsSpecification, + channelID: { + required: true, + description: 'The ID of the channel to log admin broadcasts to.', + default: '', + example: '667741905228136459' + }, + warningMessage: { + required: false, + description: "", + default: "Your squad has been disbanded due to non-compliant name.\n\nForbidden: %FORBIDDEN%", + }, + rules: { + required: false, + description: "", + default: [ + { + type: "regex", + logic: "match=allow", + rule: /a-z\d=\$\[\]\!\.\s\-/ + } + ], + example: [ + { + type: "regex", + logic: "match=disband", + logic: "match=allow", + rule: /[^a-z\d=\$\[\]\!\.\s\-]/ + }, + { + type: "equals", + rule: "ARMOUR" + }, + { + type: "includes", + rule: "F*CK" + } + ] + } + }; + } + + constructor(server, options, connectors) { + super(server, options, connectors); + + this.onSquadCreated = this.onSquadCreated.bind(this) + this.discordLog = this.discordLog.bind(this) + + this.broadcast = (msg) => { this.server.rcon.broadcast(msg); }; + this.warn = (steamid, msg) => { this.server.rcon.warn(steamid, msg); }; + } + + async mount() { + this.server.on('SQUAD_CREATED', this.onSquadCreated); + } + + onSquadCreated(info) { + let disband = false; + let rule = null; + for (let r of this.options.rules) { + switch (r.type.toLowerCase()) { + case 'regex': + r.rule = r.rule.replace(/^\//, '').replace(/\/$/, '') + + const reg = new RegExp(r.rule, "gi"); + const regRes = info.squadName.match(reg) + + switch (r.logic.toLowerCase()) { + case 'match=allow': + if (!regRes) disband = info.squadName; + break; + case 'match=disband': + default: + if (regRes) disband = regRes.join(', ') + } + // this.verbose(1, "Testing rule", info.squadName, reg, disband) + break; + case 'equals': + disband = info.squadName.toLowerCase() === r.rule.toLowerCase() ? info.squadName : false; + break; + case 'includes': + disband = info.squadName.toLowerCase().includes(r.rule.toLowerCase()) ? r.rule : false + break; + case 'startsWith': + disband = info.squadName.toLowerCase().startsWith(r.rule.toLowerCase()) ? r.rule : false + break; + case 'endsWith': + disband = info.squadName.toLowerCase().endsWith(r.rule.toLowerCase()) ? r.rule : false + break; + default: + } + + rule = r; + + if (disband) break + } + this.verbose(1, "Squad Created:", info.player.teamID, info.player.squadID, disband) + + if (disband) { + const disbandMessage = rule.warningMessage || this.options.warningMessage; + this.server.rcon.execute(`AdminDisbandSquad ${info.player.teamID} ${info.player.squadID}`); + this.warn(info.player.steamID, disbandMessage.replace(/\%FORBIDDEN\%/ig, disband)) + this.discordLog(info, disband, rule) + } + } + + async discordLog(info, forbidden, rule = null) { + let regex = rule ? new RegExp(rule.rule, "gi").toString() : null; + await this.sendDiscordMessage({ + embed: { + title: `Squad Disbanded: ${info.squadName}`, + color: "ee1111", + fields: [ + { + name: 'Creator\'s Username', + value: info.player.name, + inline: true + }, + { + name: 'Creator\'s SteamID', + value: `[${info.player.steamID}](https://steamcommunity.com/profiles/${info.player.steamID})`, + inline: true + }, + { + name: 'Team & Squad', + value: `Team: ${info.player.teamID}, Squad: ${info.player.squadID || 'Unassigned'}` + }, + { + name: 'Forbidden Chars/Word', + value: forbidden + }, + (regex ? { name: 'Logic', value: rule.logic.toLowerCase(), inline: true } : null), + (regex ? { name: 'Regex', value: regex.toString(), inline: true } : null) + ].filter(e => e), + timestamp: info.time.toISOString() + } + }); + } + + async unmount() { + this.verbose(1, 'Squad Name Validator was un-mounted.'); + } +}