SquadJSDocker/squadjsPlugins/db-log-addOn.js

144 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-02-13 05:33:08 -06:00
import Sequelize from 'sequelize';
import DBLog from './db-log.js';
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 defaultEnabled() {
return false;
}
static get optionsSpecification() {
return {
...DBLog.optionsSpecification,
seedingThreshold: {
required: false,
description: 'seeding Threshold.',
default: 50
}
};
}
constructor(server, options, connectors) {
super(server, options, connectors);
this.seeding = false;
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
}
},
{
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci'
}
);
this.models.Server.hasMany(this.models.PlayerTime, {
foreignKey: { name: 'server', allowNull: false },
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() {
await super.mount();
this.server.on('PLAYER_CONNECTED', this.onPlayerConnected);
this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
}
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);
2023-02-15 23:45:05 -06:00
2023-02-16 18:12:29 -06:00
if((this.seeding == true) && (info.a2sPlayerCount >= this.options.seedingThreshold)){
2023-02-16 00:48:46 -06:00
console.log('switching to Live');
2023-02-15 23:45:05 -06:00
this.seeding = false;
2023-02-16 19:32:58 -06:00
let curDateTime = new Date();
let timeNow = curDateTime.getFullYear() + '-' + (curDateTime.getMonth() + 1) + '-' + curDateTime.getDate()+' '+curDateTime.getHours()+':'+curDateTime.getMinutes()+':'+curDateTime.getSeconds();
console.log(timeNow);
2023-02-15 23:45:05 -06:00
await this.models.PlayerTime.update(
2023-02-16 19:32:58 -06:00
{ seedTime: timeNow },
2023-02-13 05:33:08 -06:00
{ where: { seedTime: null, joinedSeeding: 1, leaveTime: null, server: this.options.overrideServerID || this.server.id } }
2023-02-15 23:45:05 -06:00
);
2023-02-16 18:12:29 -06:00
}else if(this.seeding == false && (info.a2sPlayerCount-20) < this.options.seedingThreshold){
2023-02-16 00:48:46 -06:00
console.log('switching to seeding');
this.seeding = true;
}
2023-02-13 05:33:08 -06:00
}
async onPlayerConnected(info) {
2023-02-16 00:40:56 -06:00
console.log(info);
2023-02-15 01:13:30 -06:00
if(info.player){
2023-02-15 10:17:07 -06:00
await this.models.SteamUser.upsert({
2023-02-15 10:25:24 -06:00
steamID: info.player.steamID,
lastName: info.player.name
2023-02-20 04:34:30 -06:00
});
2023-02-13 05:33:08 -06:00
await this.models.PlayerTime.create({
server: this.options.overrideServerID || this.server.id,
2023-02-18 14:59:13 -06:00
player: info.steamID,
2023-02-13 05:33:08 -06:00
joinTime: info.time,
joinedSeeding: this.seeding
2023-02-20 04:34:30 -06:00
});}
else console.log('player is null');
2023-02-13 05:33:08 -06:00
}
async onPlayerDisconnected(info) {
2023-02-16 00:40:56 -06:00
console.log(info);
2023-02-15 01:13:30 -06:00
if(info.player){
2023-02-15 10:17:07 -06:00
await this.models.SteamUser.upsert({
2023-02-15 10:25:24 -06:00
steamID: info.player.steamID,
lastName: info.player.name
2023-02-18 14:59:13 -06:00
});}
2023-02-15 01:13:30 -06:00
await this.models.PlayerTime.update(
2023-02-13 05:33:08 -06:00
{ leaveTime: info.time },
2023-02-18 14:59:13 -06:00
{ where: { player: info.steamID, leaveTime: null, server: this.options.overrideServerID || this.server.id } }
2023-02-15 01:13:30 -06:00
);
2023-02-13 05:33:08 -06:00
}
}