mirror of
https://github.com/AsgardEternal/DBLogExtension.git
synced 2025-01-02 15:39:31 -06:00
Update db-log-addOn.js
This commit is contained in:
parent
d06e9d353d
commit
d2a93a4436
116
db-log-addOn.js
116
db-log-addOn.js
@ -1,6 +1,10 @@
|
||||
import Sequelize from 'sequelize';
|
||||
// eslint-disable-file all
|
||||
import Sequelize, { QueryTypes } from 'sequelize';
|
||||
|
||||
import DBLog from './db-log.js';
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { open } from "node:fs/promises"
|
||||
|
||||
const { DataTypes } = Sequelize;
|
||||
const ServerState = {
|
||||
@ -11,9 +15,7 @@ const ServerState = {
|
||||
|
||||
export default class DBLogPlayerTime extends DBLog {
|
||||
static get description() {
|
||||
return (
|
||||
'replacement add-on to dblog for player join/seeding times'
|
||||
);
|
||||
return 'replacement add-on to dblog for player join/seeding times';
|
||||
}
|
||||
|
||||
static get defaultEnabled() {
|
||||
@ -27,6 +29,21 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
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
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -80,13 +97,12 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
async prepareToMount() {
|
||||
await super.prepareToMount();
|
||||
await this.models.PlayerTime.sync();
|
||||
|
||||
}
|
||||
|
||||
async mount() {
|
||||
console.log('Mounting db-log');
|
||||
if (this.server.currentLayer) {
|
||||
if(this.server.currentLayer.gamemode === "Seed"){
|
||||
if (this.server.currentLayer.gamemode === 'Seed') {
|
||||
console.log('starting to seeding');
|
||||
this.seeding = ServerState.seeding;
|
||||
} else {
|
||||
@ -94,7 +110,7 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
this.seeding = ServerState.live;
|
||||
}
|
||||
} else {
|
||||
if(this.server.currentLayerRcon.layer.includes("Seed")){
|
||||
if (this.server.currentLayerRcon.layer.includes('Seed')) {
|
||||
console.log('starting to seeding');
|
||||
this.seeding = ServerState.seeding;
|
||||
} else {
|
||||
@ -119,13 +135,23 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
where: { server: this.options.overrideServerID || this.server.id },
|
||||
order: [['id', 'DESC']],
|
||||
logging: console.log
|
||||
}
|
||||
);
|
||||
});
|
||||
console.log('last tick found:', lastTickTime);
|
||||
|
||||
if(!lastTickTime) return;
|
||||
const lastServerDate = lastTickTime.time;
|
||||
const lastServerTime = lastServerDate.getFullYear() + '-' + (lastServerDate.getMonth() + 1) + '-' + lastServerDate.getDate() + ' ' + lastServerDate.getHours() + ':' + lastServerDate.getMinutes() + ':' + lastServerDate.getSeconds();
|
||||
const lastServerTime =
|
||||
lastServerDate.getFullYear() +
|
||||
'-' +
|
||||
(lastServerDate.getMonth() + 1) +
|
||||
'-' +
|
||||
lastServerDate.getDate() +
|
||||
' ' +
|
||||
lastServerDate.getHours() +
|
||||
':' +
|
||||
lastServerDate.getMinutes() +
|
||||
':' +
|
||||
lastServerDate.getSeconds();
|
||||
console.log('last time found:', lastServerTime);
|
||||
|
||||
const playerOnlineID = [];
|
||||
@ -145,12 +171,10 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
console.log(updateVals);
|
||||
console.log(whereStuff);
|
||||
|
||||
const rowUpdate = await this.models.PlayerTime.update(
|
||||
updateVals, {
|
||||
const 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');
|
||||
@ -168,7 +192,19 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
|
||||
async updateCurrentTimeState(date, oldState, newState) {
|
||||
if (oldState === newState) return;
|
||||
const timeNow = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
|
||||
this.seeding = newState;
|
||||
const timeNow =
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
(date.getMonth() + 1) +
|
||||
'-' +
|
||||
date.getDate() +
|
||||
' ' +
|
||||
date.getHours() +
|
||||
':' +
|
||||
date.getMinutes() +
|
||||
':' +
|
||||
date.getSeconds();
|
||||
console.log(timeNow);
|
||||
const curPlayer = await this.models.PlayerTime.findAll({
|
||||
where: {
|
||||
@ -178,7 +214,7 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
}
|
||||
});
|
||||
console.log(curPlayer);
|
||||
let curplayerarr = [];
|
||||
const curplayerarr = [];
|
||||
for (const oneplayer of curPlayer) {
|
||||
console.log(oneplayer);
|
||||
curplayerarr.push({
|
||||
@ -204,7 +240,7 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
await this.models.PlayerTime.bulkCreate(curplayerarr, {
|
||||
fields: ['startTime', 'endTime', 'serverState', 'session', 'server', 'player']
|
||||
});
|
||||
this.seeding = newState;
|
||||
await this.updateAutoWL();
|
||||
}
|
||||
|
||||
async onUpdatedA2SInformation(info) {
|
||||
@ -220,6 +256,38 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
// }
|
||||
}
|
||||
|
||||
async updateAutoWL() {
|
||||
if(!this.options.whitelistfilepath) return;
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const seedTimes = await this.query(
|
||||
'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 ' +
|
||||
'where startTime between (now() - INTERVAL 1 MONTH) and now() and server != 3 and serverState=1 ' +
|
||||
'group by player ' +
|
||||
'order by seedTime desc',
|
||||
{ type: QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const lcladminpath = path.resolve(__dirname, "../../../", this.options.whitelistfilepath);
|
||||
if(!fs.existsSync(lcladminpath)) {
|
||||
this.verbose(1, "WARNING: auto whitelist admins file not found");
|
||||
return;
|
||||
}
|
||||
|
||||
const adminfile = await open(lcladminpath, 'rw');
|
||||
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`);
|
||||
}
|
||||
}
|
||||
|
||||
async onNewGame(info) {
|
||||
await super.onNewGame(info);
|
||||
|
||||
@ -234,7 +302,7 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live);
|
||||
}
|
||||
} else {
|
||||
if(info.layerClassname.includes("Seed")){
|
||||
if (info.layerClassname.includes('Seed')) {
|
||||
console.log('switching to seeding');
|
||||
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.seeding);
|
||||
} else {
|
||||
@ -242,6 +310,10 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
await this.updateCurrentTimeState(curDateTime, this.seeding, ServerState.live);
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-empty
|
||||
if (this.seeding !== ServerState.seeding) {
|
||||
}
|
||||
}
|
||||
|
||||
async onPlayerConnected(info) {
|
||||
@ -262,7 +334,8 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
}
|
||||
|
||||
async onPlayerDisconnected(info) {
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
// eslint-disable-next-line promise/param-names
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
console.log(info);
|
||||
if (info.player) {
|
||||
await this.models.SteamUser.upsert({
|
||||
@ -272,12 +345,13 @@ export default class DBLogPlayerTime extends DBLog {
|
||||
}
|
||||
const rowAffect = await this.models.PlayerTime.update(
|
||||
{ endTime: info.time },
|
||||
{where:
|
||||
{
|
||||
where: {
|
||||
player: info.steamID,
|
||||
endTime: null,
|
||||
server: this.options.overrideServerID || this.server.id
|
||||
}}
|
||||
}
|
||||
}
|
||||
);
|
||||
console.log('player disconnect rows update: %i', rowAffect[0]);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user