SquadJS/squad-server/plugins/db-log.js

203 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-12-06 15:21:41 -06:00
import Sequelize from 'sequelize';
import BasePlugin from './base-plugin.js';
const { DataTypes } = Sequelize;
export default class DBLog extends BasePlugin {
static get description() {
return 'The <code>DBLog</code> plugin will log server information to a Sequlize compatible DB.';
}
static get defaultEnabled() {
return false;
}
static get optionsSpecification() {
return {
database: {
required: true,
connector: 'sequelize',
description: 'The Sequelize connector to log server information to.',
default: 'mysql'
},
overrideServerID: {
required: false,
description: 'A overridden server ID.',
default: null
}
};
}
async prepareToMount() {
2020-12-06 15:23:05 -06:00
this.createModel('Server', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING
2020-12-06 15:21:41 -06:00
}
2020-12-06 15:23:05 -06:00
});
this.createModel('TickRate', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
time: {
type: DataTypes.DATE,
notNull: true
},
tickRate: {
type: DataTypes.FLOAT,
notNull: true
2020-12-06 15:21:41 -06:00
}
2020-12-06 15:23:05 -06:00
});
2020-12-06 15:21:41 -06:00
2020-12-06 15:45:06 -06:00
this.createModel('PlayerCount', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
time: {
type: DataTypes.DATE,
notNull: true,
defaultValue: DataTypes.NOW
},
players: {
type: DataTypes.INTEGER,
notNull: true
},
publicQueue: {
type: DataTypes.INTEGER,
notNull: true
},
reserveQueue: {
type: DataTypes.INTEGER,
notNull: true
}
});
2020-12-06 17:09:33 -06:00
this.createModel('Match', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
dlc: {
type: DataTypes.STRING
},
map: {
type: DataTypes.STRING
},
layer: {
type: DataTypes.STRING
},
startTime: {
type: DataTypes.DATE,
notNull: true
},
endTime: {
type: DataTypes.DATE
},
winner: {
type: DataTypes.STRING
}
});
2020-12-06 15:23:05 -06:00
this.models.Server.hasMany(this.models.TickRate, {
foreignKey: { name: 'server', allowNull: false },
onDelete: 'CASCADE'
});
2020-12-06 15:21:41 -06:00
2020-12-06 15:45:06 -06:00
this.models.Server.hasMany(this.models.PlayerCount, {
foreignKey: { name: 'server', allowNull: false },
onDelete: 'CASCADE'
});
2020-12-06 17:09:33 -06:00
this.models.Server.hasMany(this.models.Match, {
foreignKey: { name: 'server', allowNull: false },
onDelete: 'CASCADE'
});
2020-12-06 15:21:41 -06:00
await this.models.Server.sync();
await this.models.TickRate.sync();
2020-12-06 15:45:06 -06:00
await this.models.PlayerCount.sync();
2020-12-06 17:09:33 -06:00
await this.models.Match.sync();
2020-12-06 15:21:41 -06:00
let server = await this.models.Server.findOne({ id: this.server.id });
if (server === null) {
server = await this.models.Server.create({ id: this.server.id });
}
2020-12-06 17:09:33 -06:00
2020-12-06 15:21:41 -06:00
server.name = this.server.serverName;
2020-12-06 17:09:33 -06:00
// await server.save();
2020-12-06 15:21:41 -06:00
}
createModel(name, schema) {
2020-12-06 15:45:06 -06:00
this.models[name] = this.options.database.define(`DBLog_${name}`, schema, {
timestamps: false
});
2020-12-06 15:21:41 -06:00
}
constructor(server, options, connectors) {
super(server, options, connectors);
this.models = {};
this.onTickRate = this.onTickRate.bind(this);
2020-12-06 15:45:06 -06:00
this.onUpdatedA2SInformation = this.onUpdatedA2SInformation.bind(this);
2020-12-06 17:09:33 -06:00
this.onNewGame = this.onNewGame.bind(this);
2020-12-06 15:21:41 -06:00
}
mount() {
this.server.on('TICK_RATE', this.onTickRate);
2020-12-06 15:45:06 -06:00
this.server.on('UPDATED_A2S_INFORMATION', this.onUpdatedA2SInformation);
2020-12-06 17:09:33 -06:00
this.server.on('NEW_GAME', this.onNewGame);
2020-12-06 15:21:41 -06:00
}
unmount() {
2020-12-06 15:23:05 -06:00
this.server.removeEventListener('TICK_RATE', this.onTickRate);
2020-12-06 15:45:06 -06:00
this.server.removeEventListener('UPDATED_A2S_INFORMATION', this.onTickRate);
2020-12-06 17:09:33 -06:00
this.server.removeEventListener('NEW_GAME', this.onNewGame);
2020-12-06 15:21:41 -06:00
}
async onTickRate(info) {
2020-12-06 15:23:05 -06:00
await this.models.TickRate.create({
server: this.server.id,
time: info.time,
tickRate: info.tickRate
});
2020-12-06 15:21:41 -06:00
}
2020-12-06 15:45:06 -06:00
async onUpdatedA2SInformation() {
await this.models.PlayerCount.create({
server: this.server.id,
players: this.server.a2sPlayerCount,
publicQueue: this.server.publicQueue,
reserveQueue: this.server.reserveQueue
});
}
2020-12-06 17:09:33 -06:00
async onNewGame(info) {
console.log(info);
await this.models.Match.update(
{ endTime: info.time, winner: info.winner },
{ where: { server: this.server.id, endTime: null } }
);
await this.models.Match.create({
server: this.server.id,
dlc: info.dlc,
map: info.layer ? info.layer.map : null,
layer: info.layer ? info.layer.layer : null,
startTime: info.time
});
}
2020-12-06 15:21:41 -06:00
}