SquadJSDocker/squadjsPlugins/db-log-addOn.js

185 lines
5.9 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;
2023-03-02 04:18:57 -06:00
this.repairSessions = true;
this.lastTickTime = null;
2023-02-13 05:33:08 -06:00
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();
2023-02-13 05:33:08 -06:00
}
async mount() {
2023-03-04 09:02:59 -06:00
console.log('Mounting db-log');
2023-02-13 05:33:08 -06:00
await super.mount();
2023-03-04 09:02:59 -06:00
console.log('finished mounting db-log');
this.server.on('PLAYER_CONNECTED', this.onPlayerConnected);
this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
2023-03-04 09:02:59 -06:00
console.log('finished mounting db-log-addOn');
}
async repairDB() {
2023-03-04 09:02:59 -06:00
console.log('starting DB repair');
await super.repairDB();
2023-03-04 09:02:59 -06:00
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']]}
);
2023-03-04 09:02:59 -06:00
console.log('last tick found:', lastTickTime);
let lastServerTime = lastTickTime.time;
2023-03-04 09:27:04 -06:00
console.log('last time found:', lastServerTime);
let playerOnlineID = [];
2023-03-04 09:54:13 -06:00
playerOnlineID.push(0);
2023-03-05 15:57:28 -06:00
for (const player of this.server.players){
playerOnlineID.push(player.steamID);
}
2023-03-04 09:02:59 -06:00
console.log('players online:', playerOnlineID);
2023-03-04 10:05:36 -06:00
const {ne} = Sequelize.Op;
2023-03-05 16:10:38 -06:00
// let rowUpdate = await this.models.PlayerTime.update(
// { leaveTime: lastServerTime },
// { where: {
// leaveTime: null,
// server: this.options.overrideServerID || this.server.id,
// player: { [ne]: playerOnlineID }
// } },
// { logging: console.log }
// );
2023-03-04 09:02:59 -06:00
console.log('updated playerTimes row count: %i', rowUpdate[0]);
console.log('finish DB repair');
2023-02-13 05:33:08 -06:00
}
async unmount() {
this.models.PlayerTime.update(
{ leaveTime: 0 },
{ where: { leaveTime: null , server: this.options.overrideServerID || this.server.id } }
);
2023-02-13 05:33:08 -06:00
await super.unmount();
this.server.removeEventListener('PLAYER_CONNECTED', this.onPlayerConnected);
this.server.removeEventListener('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
2023-02-13 05:33:08 -06:00
}
async onUpdatedA2SInformation(info) {
await super.onUpdatedA2SInformation(info);
2023-02-15 23:45:05 -06:00
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;
}
2023-02-13 05:33:08 -06:00
}
async onPlayerConnected(info) {
2023-02-16 00:40:56 -06:00
console.log(info);
2023-03-04 07:24:09 -06:00
if(info.player){
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-03-04 07:24:09 -06:00
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');
2023-02-13 05:33:08 -06:00
}
async onPlayerDisconnected(info) {
await sleep (500);
2023-02-16 00:40:56 -06:00
console.log(info);
2023-03-04 07:24:09 -06:00
if(info.player){
await this.models.SteamUser.upsert({
2023-02-15 10:25:24 -06:00
steamID: info.player.steamID,
lastName: info.player.name
2023-03-04 07:24:09 -06:00
});
}
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]);
2023-02-13 05:33:08 -06:00
}
}