[HQ] Enhance Logging
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
lib
|
||||
test.js
|
||||
test.js
|
||||
logs
|
||||
@@ -6,6 +6,7 @@ import faker from 'faker';
|
||||
import { discord, hq } from '../../config';
|
||||
import { getTokens, updateTokens } from '../utils/tokens';
|
||||
import { getPhone, getCode } from '../utils/sms';
|
||||
import Logger from './Logger';
|
||||
|
||||
request.promise = promisify(request);
|
||||
|
||||
@@ -53,6 +54,8 @@ export default class Client {
|
||||
this.questionCount = 0;
|
||||
this.questionNumber = 0;
|
||||
this.extraLivesUsed = 0;
|
||||
|
||||
this.logger = new Logger(this.index, this.userId);
|
||||
}
|
||||
|
||||
async login() {
|
||||
@@ -113,6 +116,12 @@ export default class Client {
|
||||
});
|
||||
}
|
||||
|
||||
this.ws.sendAndLog = (data) => {
|
||||
this.ws.send(data);
|
||||
|
||||
this.logger.sent(data);
|
||||
};
|
||||
|
||||
this.ws.onopen = () => {
|
||||
if (this.logging) console.log(`${` Client ${this.index} `.bgBlue.black}${' Connected '.bgGreen.black}`);
|
||||
|
||||
@@ -133,6 +142,8 @@ export default class Client {
|
||||
this.ws.onclose = () => {
|
||||
if (this.logging) console.log(`${` Client ${this.index} `.bgBlue.black}${' Disconnected '.bgRed.black}`);
|
||||
this.inTheGame = false;
|
||||
|
||||
this.logger.save();
|
||||
};
|
||||
|
||||
this.ws.on('message', this.parse.bind(this));
|
||||
@@ -171,7 +182,7 @@ export default class Client {
|
||||
subscribe() {
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log('No WebSocket connection is active.');
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
type: 'subscribe',
|
||||
broadcastId: this.broadcastID,
|
||||
}));
|
||||
@@ -184,7 +195,7 @@ export default class Client {
|
||||
toggleChat() {
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log('No WebSocket connection is active.');
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
type: 'chatVisibilityToggled',
|
||||
chatVisible: !this.chatVisible,
|
||||
}));
|
||||
@@ -195,6 +206,8 @@ export default class Client {
|
||||
parse(msg) {
|
||||
const data = JSON.parse(msg);
|
||||
|
||||
if (data.type !== 'interaction') this.logger.incoming(msg);
|
||||
|
||||
switch (data.type) {
|
||||
case 'question':
|
||||
this.questionID = data.questionId;
|
||||
@@ -273,7 +286,7 @@ export default class Client {
|
||||
|
||||
if (!answerId) return;
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
type: 'answer',
|
||||
broadcastId: this.broadcastID,
|
||||
questionId: this.questionID,
|
||||
@@ -288,7 +301,7 @@ export default class Client {
|
||||
if (!this.hasExtraLife) return false;
|
||||
if (this.extraLivesUsed >= hq.maxLivesPerGame) return false;
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
type: 'useExtraLife',
|
||||
broadcastId: this.broadcastID,
|
||||
questionId: this.questionID,
|
||||
@@ -306,7 +319,7 @@ export default class Client {
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log(`Client ${this.index} | No WebSocket connection is active.`);
|
||||
if (!this.hasEraser) return false;
|
||||
|
||||
this.ws.send(JSON.stringify({ type: 'erase1', questionId: this.questionID, broadcastId: this.broadcastID }));
|
||||
this.ws.sendAndLog(JSON.stringify({ type: 'erase1', questionId: this.questionID, broadcastId: this.broadcastID }));
|
||||
|
||||
this.hasEraser = false;
|
||||
|
||||
@@ -316,7 +329,7 @@ export default class Client {
|
||||
claimEraser() {
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log(`Client ${this.index} | No WebSocket connection is active.`);
|
||||
|
||||
this.ws.send(JSON.stringify({ type: 'erase1Earned', broadcastId: this.broadcastID, friendIds: this.friends }));
|
||||
this.ws.sendAndLog(JSON.stringify({ type: 'erase1Earned', broadcastId: this.broadcastID, friendIds: this.friends }));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -327,7 +340,7 @@ export default class Client {
|
||||
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log(`Client ${this.index} | No WebSocket connection is active.`);
|
||||
|
||||
this.ws.send(JSON.stringify({ type: 'checkpointResponse', winNow, checkpointId: this.checkpointID }));
|
||||
this.ws.sendAndLog(JSON.stringify({ type: 'checkpointResponse', winNow, checkpointId: this.checkpointID }));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -335,7 +348,7 @@ export default class Client {
|
||||
spinWheel(letter) {
|
||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return console.log(`Client ${this.index} | No WebSocket connection is active.`);
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
showId: this.showID,
|
||||
type: 'spin',
|
||||
nearbyIds: [],
|
||||
@@ -350,7 +363,7 @@ export default class Client {
|
||||
|
||||
if (!this.inTheGame) return false;
|
||||
|
||||
this.ws.send(JSON.stringify({
|
||||
this.ws.sendAndLog(JSON.stringify({
|
||||
type: 'guess',
|
||||
letter: this.letter,
|
||||
roundId: this.roundID,
|
||||
|
||||
39
src/classes/Logger.js
Normal file
39
src/classes/Logger.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import fs from 'fs';
|
||||
|
||||
export default class Logger {
|
||||
constructor(id, userId) {
|
||||
this.id = id;
|
||||
this.userId = userId;
|
||||
|
||||
this.logs = [];
|
||||
}
|
||||
|
||||
sent(message) {
|
||||
if (typeof message === 'object') message = JSON.stringify(message);
|
||||
|
||||
this.logs.push(`← ${message}`);
|
||||
}
|
||||
|
||||
incoming(message) {
|
||||
if (typeof message === 'object') message = JSON.stringify(message);
|
||||
|
||||
this.logs.push(`→ ${message}`);
|
||||
}
|
||||
|
||||
save() {
|
||||
const hour = new Date().getHours();
|
||||
|
||||
const date = new Date().toLocaleString()
|
||||
.split(', ')
|
||||
.shift()
|
||||
.replace(/(\/)|(:)/g, '-');
|
||||
|
||||
if (!fs.existsSync('logs')) fs.mkdirSync('logs');
|
||||
if (!fs.existsSync(`logs/${date}|${hour}`)) fs.mkdirSync(`logs/${date}|${hour}`);
|
||||
|
||||
|
||||
const log = this.logs.join('\n');
|
||||
|
||||
fs.writeFileSync(`logs/${date}|${hour}/${this.id}|${this.userId}.txt`, log);
|
||||
}
|
||||
}
|
||||
@@ -44,13 +44,9 @@ export default async (client, prize) => {
|
||||
let winners;
|
||||
let winnings;
|
||||
|
||||
const wsLog = [];
|
||||
|
||||
master.ws.on('message', async (data) => {
|
||||
const json = JSON.parse(data);
|
||||
|
||||
wsLog.push(data);
|
||||
|
||||
if (json.type === 'question') {
|
||||
const botWithEraser = client.hqClients.find(c => c.hasEraser && c.inTheGame);
|
||||
|
||||
@@ -231,18 +227,8 @@ export default async (client, prize) => {
|
||||
client.activeGames.splice(client.activeGames.indexOf('hq-trivia'), 1);
|
||||
|
||||
client.hqClients = [];
|
||||
|
||||
const { body } = await request.promise('https://pastebin.com/api/api_post.php?', {
|
||||
method: 'POST',
|
||||
form: {
|
||||
api_option: 'paste',
|
||||
api_dev_key: 'b7ac578d7d735606fde6339131885f38',
|
||||
api_paste_code: wsLog.join('\n'),
|
||||
},
|
||||
});
|
||||
|
||||
if (!emuOnly) channels.forEach(c => c.send(`WebSocket Log: ${body}`));
|
||||
});
|
||||
|
||||
tokens.forEach((t, i) => client.hqClients.push(new Client(t, i + 1, client.weights.hq)));
|
||||
|
||||
client.hqClients.forEach(async (c) => {
|
||||
|
||||
Reference in New Issue
Block a user