Make everything tabs instead of spaces & don't rely on a global consoleHelper

This commit is contained in:
Holly Stubbs 2022-01-04 03:39:53 +00:00
parent 26806438b7
commit e65fc13419
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
38 changed files with 2432 additions and 2422 deletions

View file

@ -1,20 +1,21 @@
console.clear();
// Globals
global.consoleHelper = require("./consoleHelper.js");
global.protocolVersion = 19;
const app = require("express")(),
consoleHelper = require("./consoleHelper.js"),
prometheusApp = require("express")(),
compression = require("compression"),
fs = require("fs"),
prom = require("prom-client"),
serverHandler = require("./server/serverHandler.js"),
config = require("./config.json");
const debugMode = true;
if (config.prometheusEnabled) {
// We only need to require this if prom metrics are on.
const prom = require("prom-client");
const register = new prom.Registry();
register.setDefaultLabels({ app: "nodejs_binato" });
@ -27,13 +28,13 @@ if (config.prometheusEnabled) {
res.end(await register.metrics());
});
prometheusApp.listen(config.prometheusPort, () => global.consoleHelper.printBancho(`Prometheus metrics listening at port ${config.prometheusPort}`));
} else global.consoleHelper.printWarn("Prometheus is disabled!");
prometheusApp.listen(config.prometheusPort, () => consoleHelper.printBancho(`Prometheus metrics listening at port ${config.prometheusPort}`));
} else consoleHelper.printWarn("Prometheus is disabled!");
if (config.compression) {
app.use(compression());
global.consoleHelper.printBancho("Gzip Compression is enabled.");
} else global.consoleHelper.printWarn("Gzip Compression is disabled!");
app.use(require("compression")());
consoleHelper.printBancho("Gzip Compression is enabled.");
} else consoleHelper.printWarn("Gzip Compression is disabled!");
app.use((req, res) => {
req.packet = new Buffer.alloc(0);
@ -83,4 +84,4 @@ app.use((req, res) => {
});
});
app.listen(config.port, () => global.consoleHelper.printBancho(`Binato is up! Listening at port ${config.port}`));
app.listen(config.port, () => consoleHelper.printBancho(`Binato is up! Listening at port ${config.port}`));

View file

@ -1,9 +1,10 @@
const osu = require("osu-packet");
const osu = require("osu-packet"),
consoleHelper = require("../../consoleHelper.js");
module.exports = function(CurrentUser, channelName = "") {
// Make sure the user is not already in the channel
if (global.StreamsHandler.isUserInStream(channelName, CurrentUser.uuid))
return global.consoleHelper.printBancho(`Did not add user to channel ${channelName} because they are already in it`);
return consoleHelper.printBancho(`Did not add user to channel ${channelName} because they are already in it`);
const osuPacketWriter = new osu.Bancho.Writer;

View file

@ -1,7 +1,8 @@
const osu = require("osu-packet");
const osu = require("osu-packet"),
consoleHelper = require("../../consoleHelper.js");
module.exports = function(CurrentUser) {
const logoutStartTime = new Date().getTime();
const logoutStartTime = Date.now();
const streamList = global.StreamsHandler.getStreams();
@ -23,5 +24,5 @@ module.exports = function(CurrentUser) {
});
global.StreamsHandler.sendToStream("#userlog", osuPacketWriter.toBuffer);
global.consoleHelper.printBancho(`User logged out, took ${new Date().getTime() - logoutStartTime}ms. [User: ${CurrentUser.username}]`);
consoleHelper.printBancho(`User logged out, took ${Date.now() - logoutStartTime}ms. [User: ${CurrentUser.username}]`);
}

View file

@ -1,5 +1,6 @@
const osu = require("osu-packet"),
botCommandHandler = require("../BotCommandHandler.js");
botCommandHandler = require("../BotCommandHandler.js"),
consoleHelper = require("../../consoleHelper.js");
module.exports = function(CurrentUser, CurrentPacket) {
let isSendingChannelLocked = false;
@ -27,7 +28,7 @@ module.exports = function(CurrentUser, CurrentPacket) {
return;
}
global.consoleHelper.printChat(`${CurrentUser.username} in ${CurrentPacket.target} sent: ${CurrentPacket.message}`);
consoleHelper.printChat(`${CurrentUser.username} in ${CurrentPacket.target} sent: ${CurrentPacket.message}`);
const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({

View file

@ -1,4 +1,5 @@
const osu = require("osu-packet");
const osu = require("osu-packet"),
consoleHelper = require("./consoleHelper.js");
module.exports = function(CurrentUser, MatchID) {
const match = global.MultiplayerManager.getMatch(MatchID);
@ -13,7 +14,7 @@ module.exports = function(CurrentUser, MatchID) {
}
if (global.StreamsHandler.isUserInStream(match.matchChatStreamName, CurrentUser.uuid))
return global.consoleHelper.printBancho(`Did not add user to channel ${match.matchChatStreamName} because they are already in it`);
return consoleHelper.printBancho(`Did not add user to channel ${match.matchChatStreamName} because they are already in it`);
const osuPacketWriter = new osu.Bancho.Writer;

View file

@ -1,4 +1,5 @@
const osu = require("osu-packet");
const osu = require("osu-packet"),
consoleHelper = require("../../consoleHelper.js");
module.exports = function(CurrentUser, MatchID) {
const match = global.MultiplayerManager.getMatch(MatchID);
@ -9,7 +10,7 @@ module.exports = function(CurrentUser, MatchID) {
match.tourneyClientUsers = [];
if (global.StreamsHandler.isUserInStream(match.matchChatStreamName, CurrentUser.uuid))
return global.consoleHelper.printBancho(`Did not add user to channel ${match.matchChatStreamName} because they are already in it`);
return consoleHelper.printBancho(`Did not add user to channel ${match.matchChatStreamName} because they are already in it`);
const osuPacketWriter = new osu.Bancho.Writer;

View file

@ -1,4 +1,5 @@
const getUserByToken = require("./util/getUserByToken.js");
const getUserByToken = require("./util/getUserByToken.js"),
consoleHelper = require("../consoleHelper.js");
module.exports = class {
constructor() {
@ -9,7 +10,7 @@ module.exports = class {
addStream(streamName = "", removeIfEmpty = false, spectatorHostId = null) {
// Make sure a stream with the same name doesn't exist already
if (this.avaliableStreamKeys.includes(streamName))
return global.consoleHelper.printBancho(`Did not add stream [${streamName}] A stream with the same name already exists`);
return consoleHelper.printBancho(`Did not add stream [${streamName}] A stream with the same name already exists`);
// Add new stream to the list of streams
this.avaliableStreams[streamName] = {
streamUsers: [], // An array containing a list of user tokens of the users in a given stream
@ -17,7 +18,7 @@ module.exports = class {
removeIfEmpty: removeIfEmpty
}
this.avaliableStreamKeys = Object.keys(this.avaliableStreams);
global.consoleHelper.printBancho(`Added stream [${streamName}]`);
consoleHelper.printBancho(`Added stream [${streamName}]`);
}
// Checks if a stream has no users in it
@ -33,17 +34,17 @@ module.exports = class {
// And if the stream has no users in it
if (currentStream.removeIfEmpty && currentStream.streamUsers.length == 0) {
global.StreamsHandler.removeStream(streams[i]);
global.consoleHelper.printBancho(`Removed stream [${streams[i]}] There were no users in stream`);
consoleHelper.printBancho(`Removed stream [${streams[i]}] There were no users in stream`);
}
}
}, interval);
global.consoleHelper.printBancho(`BinatoStream is running! Checks running at a ${interval}ms interval`);
consoleHelper.printBancho(`BinatoStream is running! Checks running at a ${interval}ms interval`);
}
sendToStream(streamName, streamData, initUser = null) {
// Make sure the stream we are attempting to send to even exists
if (!this.doesStreamExist(streamName))
return global.consoleHelper.printBancho(`Did not send to stream [${streamName}] because it does not exist!`);
return consoleHelper.printBancho(`Did not send to stream [${streamName}] because it does not exist!`);
// Get the stream to send the data to
const currentStream = this.avaliableStreams[streamName];
@ -69,33 +70,33 @@ module.exports = class {
addUserToStream(streamName, userToken) {
// Make sure the stream we are attempting to add this user to even exists
if (!this.doesStreamExist(streamName))
return global.consoleHelper.printBancho(`Did not add user to stream [${streamName}] because it does not exist!`);
return consoleHelper.printBancho(`Did not add user to stream [${streamName}] because it does not exist!`);
// Make sure the user isn't already in the stream
if (this.avaliableStreams[streamName].streamUsers.includes(userToken))
return global.consoleHelper.printBancho(`Did not add user to stream [${streamName}] because they are already in it!`);
return consoleHelper.printBancho(`Did not add user to stream [${streamName}] because they are already in it!`);
// Make sure this isn't an invalid user (userId can't be lower than 1)
if (userToken == "" || userToken == null)
return global.consoleHelper.printBancho(`Did not add user to stream [${streamName}] because their token is invalid!`);
return consoleHelper.printBancho(`Did not add user to stream [${streamName}] because their token is invalid!`);
// Add user's token to the stream's user list
this.avaliableStreams[streamName].streamUsers.push(userToken);
global.consoleHelper.printBancho(`Added user [${userToken}] to stream ${streamName}`);
consoleHelper.printBancho(`Added user [${userToken}] to stream ${streamName}`);
}
removeUserFromStream(streamName, userToken) {
// Make sure the stream we are attempting to add this user to even exists
if (!this.doesStreamExist(streamName))
return global.consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because it does not exist!`);
return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because it does not exist!`);
// Make sure the user isn't already in the stream
if (!this.avaliableStreams[streamName].streamUsers.includes(userToken))
return global.consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because they are not in it!`);
return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because they are not in it!`);
// Make sure this isn't an invalid user (userId can't be lower than 1)
if (userToken == "" || userToken == null)
return global.consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because their userId is invalid!`);
return consoleHelper.printBancho(`Did not remove user from stream [${streamName}] because their userId is invalid!`);
try {
// Find index of user to remove
let userCurrentIndex;
@ -108,8 +109,8 @@ module.exports = class {
// Remove user from stream's user list
this.avaliableStreams[streamName].streamUsers.splice(userCurrentIndex, 1);
global.consoleHelper.printBancho(`Removed user [${userToken}] from stream ${streamName}`);
} catch (e) { global.consoleHelper.printBancho(`Can't Remove user [${userToken}] from stream ${streamName}`); }
consoleHelper.printBancho(`Removed user [${userToken}] from stream ${streamName}`);
} catch (e) { consoleHelper.printBancho(`Can't Remove user [${userToken}] from stream ${streamName}`); }
}
doesStreamExist(streamName) {
@ -130,6 +131,6 @@ module.exports = class {
try {
delete this.avaliableStreams[streamName];
this.avaliableStreamKeys = Object.keys(this.avaliableStreams);
} catch (e) { global.consoleHelper.printError(`Was not able to remove stream [${streamName}]`) }
} catch (e) { consoleHelper.printError(`Was not able to remove stream [${streamName}]`) }
}
}

View file

@ -3,7 +3,9 @@ const osu = require("osu-packet"),
uuid = require("./util/shortUUID.js"),
ahttp = require("./util/AsyncHttpRequest.js"),
RequestType = require("./util/RequestType.json"),
consoleHelper = require("../consoleHelper.js"),
// Packets
getUserByUsername = require("./util/getUserByUsername.js"),
getUserByToken = require("./util/getUserByToken.js"),
countryHelper = require("./countryHelper.js"),
@ -14,7 +16,7 @@ const osu = require("osu-packet"),
module.exports = async function(req, res, loginInfo) {
// Get time at the start of login
const loginStartTime = new Date().getTime(),
const loginStartTime = Date.now(),
isTourneyClient = loginInfo.osuversion.includes("tourney");
// Check login
@ -72,7 +74,7 @@ module.exports = async function(req, res, loginInfo) {
}
// Create user object
global.addUser(newClientToken, new User(userDB.id, loginInfo.username, newClientToken, new Date().getTime(), isTourneyClient));
global.addUser(newClientToken, new User(userDB.id, loginInfo.username, newClientToken, Date.now(), isTourneyClient));
// Retreive the newly created user
const NewUser = getUserByToken(newClientToken);
@ -150,7 +152,7 @@ module.exports = async function(req, res, loginInfo) {
"Content-Type": "text/html; charset=UTF-8"
});
res.end(osuPacketWriter.toBuffer, () => {
global.consoleHelper.printBancho(`User login finished, took ${new Date().getTime() - loginStartTime}ms. [User: ${loginInfo.username}]`);
consoleHelper.printBancho(`User login finished, took ${Date.now() - loginStartTime}ms. [User: ${loginInfo.username}]`);
});
} catch (err) {
console.error(err);

View file

@ -1,5 +1,6 @@
const osu = require("osu-packet"),
fs = require("fs"),
consoleHelper = require("../consoleHelper.js"),
packetIDs = require("./packetIDs.js"),
loginHandler = require("./loginHandler.js"),
parseUserData = require("./util/parseUserData.js"),
@ -28,7 +29,7 @@ global.refreshUserKeys = function() {
}
// Add the bot user
global.addUser("bot", new User(3, "SillyBot", "bot", new Date().getTime()));
global.addUser("bot", new User(3, "SillyBot", "bot", Date.now()));
// Set the bot's position on the map
global.users["bot"].location[0] = 50;
global.users["bot"].location[1] = -32;
@ -97,7 +98,7 @@ setInterval(() => {
fs.appendFile(
"server-stats.log",
`${global.usersOnline}|${global.multiplayerMatches[0]},${global.multiplayerMatches[1]}|${global.httpRequestsPerLogInterval}|${new Date().getTime()}@`,
`${global.usersOnline}|${global.multiplayerMatches[0]},${global.multiplayerMatches[1]}|${global.httpRequestsPerLogInterval}|${Date.now()}@`,
() => {}
);
@ -151,7 +152,7 @@ module.exports = async function(req, res) {
if (requestTokenString == null) {
// Client doesn't have a token yet, let's auth them!
const userData = parseUserData(requestData);
global.consoleHelper.printBancho(`New client connection. [User: ${userData.username}]`);
consoleHelper.printBancho(`New client connection. [User: ${userData.username}]`);
await loginHandler(req, res, userData);
} else {
// Client has a token, let's see what they want.
@ -348,7 +349,7 @@ module.exports = async function(req, res) {
}
} else {
// User's token is invlid, force a reconnect
global.consoleHelper.printBancho(`Forced client re-login (Token is invalid)`);
consoleHelper.printBancho(`Forced client re-login (Token is invalid)`);
responseData = bakedResponses("reconnect");
}
} catch (e) {