2023-05-08 12:58:18 -05:00
|
|
|
// eslint-disable-file all
|
|
|
|
import Sequelize, { QueryTypes } from 'sequelize';
|
2023-04-03 10:29:04 -05:00
|
|
|
|
|
|
|
import DBLog from './db-log.js';
|
2023-05-08 12:58:18 -05:00
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
|
|
|
import { open } from "node:fs/promises"
|
2023-05-08 15:45:01 -05:00
|
|
|
import {fileURLToPath} from "url";
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 15:41:37 -05:00
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
const { DataTypes } = Sequelize;
|
2023-04-03 10:29:04 -05:00
|
|
|
const ServerState = {
|
2023-05-08 12:58:18 -05:00
|
|
|
init: 0,
|
|
|
|
seeding: 1,
|
|
|
|
live: 2
|
2023-04-03 10:29:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default class DBLogPlayerTime extends DBLog {
|
2023-05-08 12:58:18 -05:00
|
|
|
static get description() {
|
|
|
|
return 'replacement add-on to dblog for player join/seeding times';
|
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
static get defaultEnabled() {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
static get optionsSpecification() {
|
|
|
|
return {
|
|
|
|
...DBLog.optionsSpecification,
|
|
|
|
seedingThreshold: {
|
|
|
|
required: false,
|
|
|
|
description: 'seeding Threshold.',
|
|
|
|
default: 50
|
|
|
|
},
|
|
|
|
whitelistfilepath: {
|
|
|
|
required: false,
|
|
|
|
description: 'path to a file to write out auto-wl',
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
incseed: {
|
|
|
|
required: false,
|
|
|
|
description: 'rate of increase as a percentage to whitelist',
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
decseed: {
|
|
|
|
required: false,
|
|
|
|
description: 'rate of decrease as a percentage to whitelist',
|
|
|
|
default: 0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
constructor(server, options, connectors) {
|
|
|
|
super(server, options, connectors);
|
|
|
|
|
|
|
|
this.seeding = ServerState.init;
|
|
|
|
|
|
|
|
this.createModel(
|
|
|
|
'PlayerTime',
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
primaryKey: true,
|
|
|
|
autoIncrement: true
|
|
|
|
},
|
|
|
|
startTime: {
|
|
|
|
type: DataTypes.DATE
|
|
|
|
},
|
|
|
|
endTime: {
|
|
|
|
type: DataTypes.DATE
|
|
|
|
},
|
|
|
|
serverState: {
|
|
|
|
type: DataTypes.INTEGER
|
|
|
|
},
|
|
|
|
session: {
|
|
|
|
type: DataTypes.INTEGER
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
charset: 'utf8mb4',
|
|
|
|
collate: 'utf8mb4_unicode_ci'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.models.Server.hasMany(this.models.PlayerTime, {
|
|
|
|
foreignKey: { name: 'server', allowNull: false },
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
});
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
this.models.SteamUser.hasMany(this.models.PlayerTime, {
|
|
|
|
foreignKey: { name: 'player' },
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
});
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
this.onPlayerConnected = this.onPlayerConnected.bind(this);
|
|
|
|
this.onPlayerDisconnected = this.onPlayerDisconnected.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async prepareToMount() {
|
|
|
|
await super.prepareToMount();
|
|
|
|
await this.models.PlayerTime.sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
async mount() {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'Mounting db-log');
|
2023-05-08 12:58:18 -05:00
|
|
|
if (this.server.currentLayer) {
|
|
|
|
if (this.server.currentLayer.gamemode === 'Seed') {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting to seeding');
|
2023-05-08 12:58:18 -05:00
|
|
|
this.seeding = ServerState.seeding;
|
|
|
|
} else {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting to Live');
|
2023-05-08 12:58:18 -05:00
|
|
|
this.seeding = ServerState.live;
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-12 16:24:50 -06:00
|
|
|
if (this.server.currentLayerRcon.layer?.includes('Seed')) {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting to seeding');
|
2023-05-08 12:58:18 -05:00
|
|
|
this.seeding = ServerState.seeding;
|
|
|
|
} else {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting to Live');
|
2023-05-08 12:58:18 -05:00
|
|
|
this.seeding = ServerState.live;
|
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-05-08 12:58:18 -05:00
|
|
|
await super.mount();
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'finished mounting db-log');
|
2023-05-08 12:58:18 -05:00
|
|
|
this.server.on('PLAYER_CONNECTED', this.onPlayerConnected);
|
|
|
|
this.server.on('PLAYER_DISCONNECTED', this.onPlayerDisconnected);
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'finished mounting add-on');
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
async repairDB() {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting DB repair');
|
2023-05-08 12:58:18 -05:00
|
|
|
await super.repairDB();
|
|
|
|
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'starting DB repair for addOn');
|
2023-05-08 12:58:18 -05:00
|
|
|
|
|
|
|
const lastTickTime = await this.models.TickRate.findOne({
|
|
|
|
where: { server: this.options.overrideServerID || this.server.id },
|
2023-06-01 17:52:23 -05:00
|
|
|
order: [['id', 'DESC']]
|
|
|
|
//logging: console.log
|
2023-05-08 12:58:18 -05:00
|
|
|
});
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log('last tick found:', lastTickTime);
|
2023-05-08 12:58:18 -05:00
|
|
|
|
|
|
|
if(!lastTickTime) return;
|
|
|
|
const lastServerDate = lastTickTime.time;
|
|
|
|
const lastServerTime =
|
|
|
|
lastServerDate.getFullYear() +
|
|
|
|
'-' +
|
|
|
|
(lastServerDate.getMonth() + 1) +
|
|
|
|
'-' +
|
|
|
|
lastServerDate.getDate() +
|
|
|
|
' ' +
|
|
|
|
lastServerDate.getHours() +
|
|
|
|
':' +
|
|
|
|
lastServerDate.getMinutes() +
|
|
|
|
':' +
|
|
|
|
lastServerDate.getSeconds();
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'last time found:' + lastServerTime);
|
2023-05-08 12:58:18 -05:00
|
|
|
|
|
|
|
const playerOnlineID = [];
|
|
|
|
playerOnlineID.push(0);
|
|
|
|
for (const player of this.server.players) {
|
|
|
|
playerOnlineID.push(player.steamID);
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log('players online:', playerOnlineID);
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
const { notIn, is } = Sequelize.Op;
|
|
|
|
const updateVals = { endTime: lastServerTime };
|
|
|
|
const whereStuff = {
|
|
|
|
endTime: { [is]: null },
|
|
|
|
server: this.options.overrideServerID || this.server.id,
|
|
|
|
player: { [notIn]: playerOnlineID }
|
|
|
|
};
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(updateVals);
|
|
|
|
//console.log(whereStuff);
|
2023-05-08 12:58:18 -05:00
|
|
|
|
|
|
|
const rowUpdate = await this.models.PlayerTime.update(updateVals, {
|
2023-06-01 17:52:23 -05:00
|
|
|
where: whereStuff
|
|
|
|
//logging: console.log
|
2023-05-08 12:58:18 -05:00
|
|
|
});
|
|
|
|
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'updated playerTimes row count: %i', rowUpdate[0]);
|
|
|
|
this.verbose(1,'finish DB repair');
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 updateCurrentTimeState(date, oldState, newState) {
|
|
|
|
if (oldState === newState) return;
|
|
|
|
this.seeding = newState;
|
|
|
|
const timeNow =
|
|
|
|
date.getFullYear() +
|
|
|
|
'-' +
|
|
|
|
(date.getMonth() + 1) +
|
|
|
|
'-' +
|
|
|
|
date.getDate() +
|
|
|
|
' ' +
|
|
|
|
date.getHours() +
|
|
|
|
':' +
|
|
|
|
date.getMinutes() +
|
|
|
|
':' +
|
|
|
|
date.getSeconds();
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,"Current time for switch: " + timeNow);
|
2023-05-08 12:58:18 -05:00
|
|
|
const curPlayer = await this.models.PlayerTime.findAll({
|
|
|
|
where: {
|
|
|
|
endTime: null,
|
|
|
|
serverState: oldState,
|
|
|
|
server: this.options.overrideServerID || this.server.id
|
|
|
|
}
|
|
|
|
});
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(curPlayer);
|
2023-05-08 12:58:18 -05:00
|
|
|
const curplayerarr = [];
|
|
|
|
for (const oneplayer of curPlayer) {
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(oneplayer);
|
2023-05-08 12:58:18 -05:00
|
|
|
curplayerarr.push({
|
|
|
|
startTime: timeNow,
|
|
|
|
endTime: null,
|
|
|
|
serverState: newState,
|
|
|
|
session: oneplayer.session,
|
|
|
|
server: oneplayer.server,
|
|
|
|
player: oneplayer.player
|
|
|
|
});
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(curplayerarr);
|
2023-05-08 12:58:18 -05:00
|
|
|
await this.models.PlayerTime.update(
|
|
|
|
{ endTime: timeNow },
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
endTime: null,
|
|
|
|
serverState: oldState,
|
|
|
|
server: this.options.overrideServerID || this.server.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
await this.models.PlayerTime.bulkCreate(curplayerarr, {
|
|
|
|
fields: ['startTime', 'endTime', 'serverState', 'session', 'server', 'player']
|
|
|
|
});
|
|
|
|
await this.updateAutoWL();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 updateAutoWL() {
|
|
|
|
if(!this.options.whitelistfilepath) return;
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2023-05-08 15:27:27 -05:00
|
|
|
const seedTimes = await this.options.database.query(
|
2023-05-08 12:58:18 -05:00
|
|
|
'select lastName as playername, discordID, steamID, ' +
|
|
|
|
'sum(time_to_sec(timediff(ifnull(endTime,now()), startTime))/3600) as seedTime ' +
|
|
|
|
'from DBLog_PlayerTimes join DBLog_SteamUsers DLSU on DBLog_PlayerTimes.player = DLSU.steamID ' +
|
2023-05-08 13:50:48 -05:00
|
|
|
'where startTime between (now() - INTERVAL 1 WEEK) and now() and server != 3 and serverState=1 ' +
|
2023-05-08 12:58:18 -05:00
|
|
|
'group by player ' +
|
|
|
|
'order by seedTime desc',
|
|
|
|
{ type: QueryTypes.SELECT }
|
|
|
|
);
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
const topTime = seedTimes[0].seedTime;
|
|
|
|
const seedid = [];
|
|
|
|
for(const seeder of seedTimes){
|
|
|
|
if(((this.options.incseed*seeder.seedTime) - (this.options.decseed*(topTime-seeder.seedTime))) > 100) seedid.push(seeder);
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
|
|
|
|
2023-05-08 15:48:38 -05:00
|
|
|
const lcladminpath = path.resolve(__dirname, "../../", this.options.whitelistfilepath);
|
2023-05-08 15:32:45 -05:00
|
|
|
this.verbose(1, "trying to write to directory: ", lcladminpath);
|
2023-05-08 12:58:18 -05:00
|
|
|
if(!fs.existsSync(lcladminpath)) {
|
|
|
|
this.verbose(1, "WARNING: auto whitelist admins file not found");
|
|
|
|
return;
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
|
|
|
|
2023-05-08 16:02:33 -05:00
|
|
|
const adminfile = await open(lcladminpath, 'r+');
|
2023-05-08 12:58:18 -05:00
|
|
|
await adminfile.write(`Group=server-${this.server.options.id}-autowl:reserve\n`);
|
|
|
|
for(const seeding of seedid){
|
|
|
|
await adminfile.write(`Admin=${seeding.steamID}:server-${this.server.options.id}-autowl //name:${seeding.playername}, discord ID: ${seeding.discordID}\n`);
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-05-08 16:02:33 -05:00
|
|
|
await adminfile.close();
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async onNewGame(info) {
|
|
|
|
await super.onNewGame(info);
|
|
|
|
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(info);
|
2023-05-08 12:58:18 -05:00
|
|
|
const curDateTime = info.time;
|
|
|
|
if (info.layer) {
|
|
|
|
if (info.layer.gamemode === 'Seed') {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'switching to seeding');
|
2023-05-08 12:58:18 -05:00
|
|
|
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.seeding);
|
|
|
|
} else {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'switching to Live');
|
2023-05-08 12:58:18 -05:00
|
|
|
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (info.layerClassname.includes('Seed')) {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'switching to seeding');
|
2023-05-08 12:58:18 -05:00
|
|
|
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.seeding);
|
|
|
|
} else {
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1,'switching to Live');
|
2023-05-08 12:58:18 -05:00
|
|
|
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live);
|
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
// eslint-disable-next-line no-empty
|
|
|
|
if (this.seeding !== ServerState.seeding) {
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
|
2023-05-08 12:58:18 -05:00
|
|
|
async onPlayerConnected(info) {
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(info);
|
2023-05-08 12:58:18 -05:00
|
|
|
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,
|
2023-12-12 17:03:27 -06:00
|
|
|
player: info.player.steamID,
|
2023-05-08 12:58:18 -05:00
|
|
|
startTime: info.time,
|
|
|
|
serverState: this.seeding
|
|
|
|
});
|
2023-06-01 17:52:23 -05:00
|
|
|
this.verbose(1, 'player connect complete');
|
|
|
|
} else this.verbose(1, 'player is null');
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async onPlayerDisconnected(info) {
|
|
|
|
// eslint-disable-next-line promise/param-names
|
|
|
|
await new Promise((r) => setTimeout(r, 500));
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log(info);
|
2023-05-08 12:58:18 -05:00
|
|
|
if (info.player) {
|
|
|
|
await this.models.SteamUser.upsert({
|
|
|
|
steamID: info.player.steamID,
|
|
|
|
lastName: info.player.name
|
|
|
|
});
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|
2023-05-08 12:58:18 -05:00
|
|
|
const rowAffect = await this.models.PlayerTime.update(
|
|
|
|
{ endTime: info.time },
|
|
|
|
{
|
|
|
|
where: {
|
2023-12-12 17:13:49 -06:00
|
|
|
player: info.player.steamID,
|
2023-05-08 12:58:18 -05:00
|
|
|
endTime: null,
|
|
|
|
server: this.options.overrideServerID || this.server.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-06-01 17:52:23 -05:00
|
|
|
//console.log('player disconnect rows update: %i', rowAffect[0]);
|
2023-05-08 12:58:18 -05:00
|
|
|
}
|
2023-04-03 10:29:04 -05:00
|
|
|
}
|