SquadJS/plugins/mysql-log/index.js

139 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-05-15 12:42:39 -05:00
import {
LOG_PARSER_NEW_GAME,
LOG_PARSER_PLAYER_WOUNDED,
LOG_PARSER_PLAYER_DIED,
LOG_PARSER_PLAYER_REVIVED,
LOG_PARSER_SERVER_TICK_RATE
} from 'squad-server/events/log-parser';
import { SERVER_PLAYERS_UPDATED } from 'squad-server/events/server';
export default {
name: 'mysql-log',
2020-08-22 08:32:39 -05:00
description:
'The `mysql-log` plugin will log various server statistics and events to a MySQL database. This is great for ' +
'server performance monitoring and/or player stat tracking.' +
'\n\n' +
'Installation:\n' +
' * Obtain/Install MySQL. MySQL v8.x.x has been tested with this plugin and is recommended.\n' +
' * Enable legacy authentication in your database using [this guide](https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server).\n' +
2020-09-02 03:30:37 -05:00
' * Execute the [schema](https://github.com/Thomas-Smyth/SquadJS/blob/master/plugins/mysql-log/mysql-schema.sql) to setup the database.\n' +
2020-08-22 08:32:39 -05:00
' * Add a server to the database with `INSERT INTO Server (name) VALUES ("Your Server Name");`.\n' +
' * Find the ID of the server you just inserted with `SELECT * FROM Server;`.\n' +
' * Replace the server ID in your config with the ID from the inserted record in the database.\n' +
'\n\n' +
2020-08-22 11:16:49 -05:00
'If you encounter any issues you can enable `"debug": true` in your MySQL connector to get more error logs in the console.\n' +
2020-08-22 08:32:39 -05:00
'\n\n' +
'Grafana:\n' +
' * [Grafana](https://grafana.com/) is a cool way of viewing server statistics stored in the database.\n' +
' * Install Grafana.\n' +
' * Add your MySQL database as a datasource named `SquadJS - MySQL`.\n' +
' * Import the [SquadJS Dashboard](SquadJS-Dashboard.json) to get a preconfigured MySQL only Grafana dashboard.\n' +
' * Install any missing Grafana plugins.',
2020-05-15 12:42:39 -05:00
defaultEnabled: false,
optionsSpec: {
mysqlPool: {
type: 'MySQLPoolConnector',
required: true,
default: 'mysql',
description: 'The name of the MySQL Pool Connector to use.'
},
overrideServerID: {
type: 'Int',
required: false,
default: null,
description: 'A overridden server ID.'
}
},
2020-05-15 12:42:39 -05:00
init: async (server, options) => {
const serverID = options.overrideServerID === null ? server.id : options.overrideServerID;
server.on(LOG_PARSER_SERVER_TICK_RATE, (info) => {
options.mysqlPool.query(
'INSERT INTO ServerTickRate(time, server, tick_rate) VALUES (?,?,?)',
[info.time, serverID, info.tickRate]
);
});
2020-05-15 12:42:39 -05:00
server.on(SERVER_PLAYERS_UPDATED, (players) => {
options.mysqlPool.query(
'INSERT INTO PlayerCount(time, server, player_count) VALUES (NOW(),?,?)',
[serverID, players.length]
);
});
2020-05-15 12:42:39 -05:00
server.on(LOG_PARSER_NEW_GAME, (info) => {
options.mysqlPool.query('call NewMatch(?,?,?,?,?,?,?)', [
serverID,
info.time,
info.dlc,
info.mapClassname,
info.layerClassname,
info.map,
info.layer
]);
});
2020-05-15 12:42:39 -05:00
server.on(LOG_PARSER_PLAYER_WOUNDED, (info) => {
options.mysqlPool.query('call InsertPlayerWounded(?,?,?,?,?,?,?,?,?,?,?,?,?)', [
serverID,
info.time,
info.victim ? info.victim.steamID : null,
info.victim ? info.victim.name : null,
info.victim ? info.victim.teamID : null,
info.victim ? info.victim.squadID : null,
info.attacker ? info.attacker.steamID : null,
info.attacker ? info.attacker.name : null,
info.attacker ? info.attacker.teamID : null,
info.attacker ? info.attacker.squadID : null,
info.damage,
info.weapon,
info.teamkill
]);
});
2020-05-15 12:42:39 -05:00
server.on(LOG_PARSER_PLAYER_DIED, (info) => {
options.mysqlPool.query('call InsertPlayerDied(?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [
serverID,
info.time,
info.woundTime,
info.victim ? info.victim.steamID : null,
info.victim ? info.victim.name : null,
info.victim ? info.victim.teamID : null,
info.victim ? info.victim.squadID : null,
info.attacker ? info.attacker.steamID : null,
info.attacker ? info.attacker.name : null,
info.attacker ? info.attacker.teamID : null,
info.attacker ? info.attacker.squadID : null,
info.damage,
info.weapon,
info.teamkill
]);
});
2020-05-15 12:42:39 -05:00
server.on(LOG_PARSER_PLAYER_REVIVED, (info) => {
options.mysqlPool.query('call InsertPlayerRevived(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', [
serverID,
info.time,
info.woundTime,
info.victim ? info.victim.steamID : null,
info.victim ? info.victim.name : null,
info.victim ? info.victim.teamID : null,
info.victim ? info.victim.squadID : null,
info.attacker ? info.attacker.steamID : null,
info.attacker ? info.attacker.name : null,
info.attacker ? info.attacker.teamID : null,
info.attacker ? info.attacker.squadID : null,
info.damage,
info.weapon,
info.teamkill,
info.reviver ? info.reviver.steamID : null,
info.reviver ? info.reviver.name : null,
info.reviver ? info.reviver.teamID : null,
info.reviver ? info.reviver.squadID : null
]);
});
}
};