mirror of
https://github.com/AsgardEternal/SquadJSDocker.git
synced 2025-01-02 12:09:20 -06:00
parent
b27bc662d7
commit
b2ede5df25
@ -2,237 +2,191 @@ import Sequelize from 'sequelize';
|
|||||||
|
|
||||||
import DBLog from './db-log.js';
|
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 {
|
export default class DBLogPlayerTime extends DBLog {
|
||||||
static get description() {
|
static get description() {
|
||||||
return (
|
return (
|
||||||
'replacement add-on to dblog for player join/seeding times'
|
'replacement add-on to dblog for player join/seeding times'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static get defaultEnabled() {
|
static get defaultEnabled() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get optionsSpecification() {
|
static get optionsSpecification() {
|
||||||
return {
|
return {
|
||||||
...DBLog.optionsSpecification,
|
...DBLog.optionsSpecification,
|
||||||
seedingThreshold: {
|
seedingThreshold: {
|
||||||
required: false,
|
required: false,
|
||||||
description: 'seeding Threshold.',
|
description: 'seeding Threshold.',
|
||||||
default: 50
|
default: 50
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(server, options, connectors) {
|
constructor(server, options, connectors) {
|
||||||
super(server, options, connectors);
|
super(server, options, connectors);
|
||||||
|
|
||||||
this.seeding = ServerState.init;
|
this.seeding = false;
|
||||||
|
this.repairSessions = true;
|
||||||
|
this.lastTickTime = null;
|
||||||
|
|
||||||
this.createModel(
|
this.createModel(
|
||||||
'PlayerTimeNew',
|
'PlayerTime',
|
||||||
{
|
{
|
||||||
id: {
|
id: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
primaryKey: true,
|
primaryKey: true,
|
||||||
autoIncrement: true
|
autoIncrement: true
|
||||||
|
},
|
||||||
|
joinTime: {
|
||||||
|
type: DataTypes.DATE
|
||||||
|
},
|
||||||
|
leaveTime: {
|
||||||
|
type: DataTypes.DATE
|
||||||
|
},
|
||||||
|
seedTime: {
|
||||||
|
type: DataTypes.DATE
|
||||||
|
},
|
||||||
|
joinedSeeding: {
|
||||||
|
type: DataTypes.BOOLEAN
|
||||||
|
}
|
||||||
},
|
},
|
||||||
startTime: {
|
{
|
||||||
type: DataTypes.DATE
|
charset: 'utf8mb4',
|
||||||
},
|
collate: 'utf8mb4_unicode_ci'
|
||||||
endTime: {
|
|
||||||
type: DataTypes.DATE
|
|
||||||
},
|
|
||||||
serverState: {
|
|
||||||
type: DataTypes.INTEGER
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
{
|
|
||||||
charset: 'utf8mb4',
|
|
||||||
collate: 'utf8mb4_unicode_ci'
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
this.models.Server.hasMany(this.models.PlayerTimeNew, {
|
this.models.Server.hasMany(this.models.PlayerTime, {
|
||||||
foreignKey: {name: 'server', allowNull: false},
|
foreignKey: { name: 'server', allowNull: false },
|
||||||
onDelete: 'CASCADE'
|
onDelete: 'CASCADE'
|
||||||
});
|
});
|
||||||
|
|
||||||
this.models.SteamUser.hasMany(this.models.PlayerTimeNew, {
|
this.models.SteamUser.hasMany(this.models.PlayerTime, {
|
||||||
foreignKey: {name: 'player'},
|
foreignKey: {name: 'player' },
|
||||||
onDelete: 'CASCADE'
|
onDelete: 'CASCADE'
|
||||||
});
|
});
|
||||||
|
|
||||||
this.onPlayerConnected = this.onPlayerConnected.bind(this);
|
this.onPlayerConnected = this.onPlayerConnected.bind(this);
|
||||||
this.onPlayerDisconnected = this.onPlayerDisconnected.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() {
|
async unmount() {
|
||||||
await super.prepareToMount();
|
this.models.PlayerTime.update(
|
||||||
await this.models.PlayerTimeNew.sync();
|
{ 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
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
let rowAffect = await this.models.PlayerTime.update(
|
||||||
async mount() {
|
{ leaveTime: info.time },
|
||||||
console.log('Mounting db-log');
|
{ where: { player: info.steamID, leaveTime: null, server: this.options.overrideServerID || this.server.id } }
|
||||||
await super.mount();
|
);
|
||||||
console.log('finished mounting db-log');
|
console.log('player disconnect rows update: %i', rowAffect[0]);
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user