use funkyArray for user session storage

This commit is contained in:
Holly Stubbs 2022-02-23 05:35:10 +00:00
parent 7544e9a7dd
commit f9fe6e2a27
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
12 changed files with 131 additions and 79 deletions

View file

@ -4,6 +4,7 @@ const osu = require("osu-packet"),
module.exports = function(User, Message, Stream, IsCalledFromMultiplayer = false) { module.exports = function(User, Message, Stream, IsCalledFromMultiplayer = false) {
if (Message[0] != "!") return; if (Message[0] != "!") return;
const command = Message.split(" ")[0]; const command = Message.split(" ")[0];
const args = Message.split(" "); const args = Message.split(" ");
@ -117,18 +118,18 @@ module.exports = function(User, Message, Stream, IsCalledFromMultiplayer = false
if (countdown != 0 && countdown > 0) countdown--; if (countdown != 0 && countdown > 0) countdown--;
if (countdown <= 10 && countdown > 0) { if (countdown <= 10 && countdown > 0) {
local_osuPacketWriter.SendMessage({ local_osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "Starting in " + countdown, message: "Starting in " + countdown,
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
global.StreamsHandler.sendToStream(Stream, local_osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(Stream, local_osuPacketWriter.toBuffer, null);
} else if (countdown == 0) { } else if (countdown == 0) {
local_osuPacketWriter.SendMessage({ local_osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "Good luck, have fun!", message: "Good luck, have fun!",
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
global.StreamsHandler.sendToStream(Stream, local_osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(Stream, local_osuPacketWriter.toBuffer, null);
User.currentMatch.matchStartCountdownActive = false; User.currentMatch.matchStartCountdownActive = false;
@ -152,10 +153,10 @@ module.exports = function(User, Message, Stream, IsCalledFromMultiplayer = false
if (User.currentMatch.multiplayerExtras.name == "osu! Battle Royale") { if (User.currentMatch.multiplayerExtras.name == "osu! Battle Royale") {
commandBanchoPacketWriter = new osu.Bancho.Writer; commandBanchoPacketWriter = new osu.Bancho.Writer;
commandBanchoPacketWriter.SendMessage({ commandBanchoPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "osu! Battle Royale has been disabled!", message: "osu! Battle Royale has been disabled!",
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
User.currentMatch.multiplayerExtras = null; User.currentMatch.multiplayerExtras = null;
global.StreamsHandler.sendToStream(Stream, commandBanchoPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(Stream, commandBanchoPacketWriter.toBuffer, null);
@ -175,17 +176,17 @@ module.exports = function(User, Message, Stream, IsCalledFromMultiplayer = false
if (responseMessage != "") { if (responseMessage != "") {
if (Stream.includes("#")) { if (Stream.includes("#")) {
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: responseMessage, message: responseMessage,
target: Stream, target: Stream,
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
} else { } else {
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: responseMessage, message: responseMessage,
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
} }
} }
@ -196,16 +197,16 @@ function enableOBR(User, Stream, commandBanchoPacketWriter) {
User.currentMatch.multiplayerExtras = new OsuBattleRoyale(User.currentMatch); User.currentMatch.multiplayerExtras = new OsuBattleRoyale(User.currentMatch);
commandBanchoPacketWriter = new osu.Bancho.Writer; commandBanchoPacketWriter = new osu.Bancho.Writer;
commandBanchoPacketWriter.SendMessage({ commandBanchoPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "osu! Battle Royale has been enabled!", message: "osu! Battle Royale has been enabled!",
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
commandBanchoPacketWriter.SendMessage({ commandBanchoPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "New Multiplayer Rules Added:\n - Players that are in a failed state by the end of the map get eliminated\n - The player(s) with the lowest score get eliminated", message: "New Multiplayer Rules Added:\n - Players that are in a failed state by the end of the map get eliminated\n - The player(s) with the lowest score get eliminated",
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
global.StreamsHandler.sendToStream(Stream, commandBanchoPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(Stream, commandBanchoPacketWriter.toBuffer, null);
} }

View file

@ -35,10 +35,10 @@ function kickLowScorers(playerScores = [{playerId:0,slotId:0,score:0,isCurrently
// Inform the kicked user's client that they were kicked // Inform the kicked user's client that they were kicked
osuPacketWriter.MatchUpdate(MultiplayerMatch.createOsuMatchJSON()); osuPacketWriter.MatchUpdate(MultiplayerMatch.createOsuMatchJSON());
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "You were eliminated from the match!", message: "You were eliminated from the match!",
target: global.users["bot"].username, target: global.botUser.username,
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
kickedPlayer.addActionToQueue(osuPacketWriter.toBuffer); kickedPlayer.addActionToQueue(osuPacketWriter.toBuffer);
@ -46,10 +46,10 @@ function kickLowScorers(playerScores = [{playerId:0,slotId:0,score:0,isCurrently
osuPacketWriter = new osu.Bancho.Writer; osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: `${kickedPlayer.username} was eliminated from the match!`, message: `${kickedPlayer.username} was eliminated from the match!`,
target: "#multiplayer", target: "#multiplayer",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
global.StreamsHandler.sendToStream(MultiplayerMatch.matchChatStreamName, osuPacketWriter.toBuffer, null); global.StreamsHandler.sendToStream(MultiplayerMatch.matchChatStreamName, osuPacketWriter.toBuffer, null);
@ -111,10 +111,10 @@ module.exports = class {
case 0: case 0:
remainingWriterContainer = new osu.Bancho.Writer; remainingWriterContainer = new osu.Bancho.Writer;
remainingWriterContainer.SendMessage({ remainingWriterContainer.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "Everyone was eliminated from the match! Nobody wins.", message: "Everyone was eliminated from the match! Nobody wins.",
target: global.users["bot"].username, target: global.botUser.username,
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
for (i = 0; i < playerScores.length; i++) { for (i = 0; i < playerScores.length; i++) {
playerClassContainer = getUserById(playerScores[i].playerId); playerClassContainer = getUserById(playerScores[i].playerId);
@ -125,10 +125,10 @@ module.exports = class {
case 1: case 1:
remainingWriterContainer = new osu.Bancho.Writer; remainingWriterContainer = new osu.Bancho.Writer;
remainingWriterContainer.SendMessage({ remainingWriterContainer.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "You are the last one remaining, you win!", message: "You are the last one remaining, you win!",
target: global.users["bot"].username, target: global.botUser.username,
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
playerClassContainer.addActionToQueue(remainingWriterContainer.toBuffer); playerClassContainer.addActionToQueue(remainingWriterContainer.toBuffer);
break; break;

View file

@ -13,14 +13,14 @@ module.exports = function(CurrentUser) {
} }
// Remove user from user list // Remove user from user list
global.removeUser(CurrentUser); global.users.remove(CurrentUser.uuid);
const osuPacketWriter = new osu.Bancho.Writer(); const osuPacketWriter = new osu.Bancho.Writer();
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: `User ${CurrentUser.username} has logged out.`, message: `User ${CurrentUser.username} has logged out.`,
target: "#userlog", target: "#userlog",
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
global.StreamsHandler.sendToStream("#userlog", osuPacketWriter.toBuffer); global.StreamsHandler.sendToStream("#userlog", osuPacketWriter.toBuffer);

View file

@ -18,10 +18,10 @@ module.exports = function(CurrentUser, CurrentPacket) {
} else { } else {
const osuPacketWriter = new osu.Bancho.Writer; const osuPacketWriter = new osu.Bancho.Writer;
osuPacketWriter.SendMessage({ osuPacketWriter.SendMessage({
sendingClient: global.users["bot"].username, sendingClient: global.botUser.username,
message: "The channel you are currently trying to send to is locked, please check back later!", message: "The channel you are currently trying to send to is locked, please check back later!",
target: CurrentPacket.target, target: CurrentPacket.target,
senderId: global.users["bot"].id senderId: global.botUser.id
}); });
CurrentUser.addActionToQueue(osuPacketWriter.toBuffer); CurrentUser.addActionToQueue(osuPacketWriter.toBuffer);
} }

View file

@ -7,9 +7,9 @@ module.exports = function(CurrentUser, MatchID) {
if (match != null) { if (match != null) {
match.isTourneyMatch = true; match.isTourneyMatch = true;
for (let i = 0; i < global.userKeys.length; i++) { for (let user of global.users.getIterableItems()) {
if (global.users[global.userKeys[i]].id == CurrentUser.id) { if (user.id == CurrentUser.id) {
match.tourneyClientUsers.push(global.users[global.userKeys[i]]); match.tourneyClientUsers.push(user);
} }
} }

View file

@ -5,8 +5,8 @@ module.exports = function(currentUser, sendImmidiate = true) {
let userIds = []; let userIds = [];
for (let i = 0; i < global.userKeys.length; i++) { for (let user of global.users.getIterableItems()) {
userIds.push(global.users[global.userKeys[i]].id); userIds.push(user.id);
} }
osuPacketWriter.UserPresenceBundle(userIds); osuPacketWriter.UserPresenceBundle(userIds);

View file

@ -9,6 +9,7 @@ const osu = require("osu-packet"),
getUserByToken = require("./util/getUserByToken.js"), getUserByToken = require("./util/getUserByToken.js"),
countryHelper = require("./countryHelper.js"), countryHelper = require("./countryHelper.js"),
loginHelper = require("./loginHelper.js"), loginHelper = require("./loginHelper.js"),
Logout = require("./Packets/Logout.js"),
UserPresenceBundle = require("./Packets/UserPresenceBundle.js"), UserPresenceBundle = require("./Packets/UserPresenceBundle.js"),
UserPresence = require("./Packets/UserPresence.js"), UserPresence = require("./Packets/UserPresence.js"),
StatusUpdate = require("./Packets/StatusUpdate.js"); StatusUpdate = require("./Packets/StatusUpdate.js");
@ -65,20 +66,15 @@ module.exports = async function(req, res, loginInfo) {
// Make sure user is not already connected, kick off if so. // Make sure user is not already connected, kick off if so.
const checkForPreexistingUser = getUserByUsername(loginInfo.username); const checkForPreexistingUser = getUserByUsername(loginInfo.username);
if (checkForPreexistingUser != null && !isTourneyClient) { if (checkForPreexistingUser != null && !isTourneyClient) {
for (let i = 0; i < global.userKeys.length; i++) { for (let user of global.users.getIterableItems()) {
const user = global.users[global.userKeys[i]]; // Log them out if they are not a tourney user
// Make sure they are not a tourney user if (!user.isTourneyUser && user.uuid != newClientToken)
if (!user.isTourneyUser && user.uuid != newClientToken) { Logout(user);
global.removeUser(user);
} }
} }
}
// Create user object
global.addUser(newClientToken, new User(userDB.id, loginInfo.username, newClientToken));
// Retreive the newly created user // Retreive the newly created user
const NewUser = getUserByToken(newClientToken); const NewUser = global.users.add(newClientToken, new User(userDB.id, loginInfo.username, newClientToken));
// Set tourney client flag // Set tourney client flag
NewUser.isTourneyUser = isTourneyClient; NewUser.isTourneyUser = isTourneyClient;

View file

@ -9,35 +9,17 @@ const osu = require("osu-packet"),
bakedResponses = require("./bakedResponses.js"), bakedResponses = require("./bakedResponses.js"),
Streams = require("./Streams.js"), Streams = require("./Streams.js"),
DatabaseHelperClass = require("./DatabaseHelper.js"), DatabaseHelperClass = require("./DatabaseHelper.js"),
funkyArray = require("./util/funkyArray.js"),
config = require("../config.json"); config = require("../config.json");
global.users = {}; // Users funkyArray for session storage
global.userKeys = Object.keys(global.users); global.users = new funkyArray();
global.addUser = function(uuid, userToAdd) {
global.users[uuid] = userToAdd;
global.refreshUserKeys();
}
global.removeUser = function(userToRemove) {
// This should be safe to do since we should be in a try-catch at all times
// I just want a trace of how this is happening.
if (userToRemove.uuid == "bot")
throw "Tried to remove the bot user!";
delete userToRemove;
global.refreshUserKeys();
}
global.refreshUserKeys = function() {
global.userKeys = Object.keys(global.users);
}
// Add the bot user // Add the bot user
global.addUser("bot", new User(3, "SillyBot", "bot")); global.botUser = global.users.add("bot", new User(3, "SillyBot", "bot"));
// Set the bot's position on the map // Set the bot's position on the map
global.users["bot"].location[0] = 50; global.botUser.location[0] = 50;
global.users["bot"].location[1] = -32; global.botUser.location[1] = -32;
global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseUsername, config.databasePassword, config.databaseName); global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseUsername, config.databasePassword, config.databaseName);
@ -45,8 +27,7 @@ global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.d
// TODO: Some way of informing bancho that a user has set a score so details can be pulled down quickly // TODO: Some way of informing bancho that a user has set a score so details can be pulled down quickly
// Possible solution, TCP socket between the score submit server and bancho? redis? (score submit is on a different server, redis probably wouldn't work) // Possible solution, TCP socket between the score submit server and bancho? redis? (score submit is on a different server, redis probably wouldn't work)
setInterval(() => { setInterval(() => {
for (let i = 0; i < global.userKeys.length; i++) { for (let User of global.users.getIterableItems()) {
const User = global.users[global.userKeys[i]];
if (User.id == 3) continue; // Ignore the bot if (User.id == 3) continue; // Ignore the bot
// Bot: :( // Bot: :(

75
server/util/funkyArray.js Normal file
View file

@ -0,0 +1,75 @@
class FunkyArray {
constructor() {
this.items = {};
this.itemKeys = Object.keys(this.items);
this.iterableArray = [];
}
add(uuid, item, regenerate = true) {
this.items[uuid] = item;
if (regenerate) {
this.itemKeys = Object.keys(this.items);
this.regenerateIterableArray();
}
return this.items[uuid];
}
remove(uuid, regenerate = true) {
delete this.items[uuid];
if (regenerate) {
this.itemKeys = Object.keys(this.items);
this.regenerateIterableArray();
}
}
removeFirstItem(regenerate = true) {
delete this.items[this.itemKeys[0]];
this.itemKeys = Object.keys(this.items);
if (regenerate) this.regenerateIterableArray();
}
regenerateIterableArray() {
this.iterableArray = new Array();
for (let itemKey of this.itemKeys) {
this.iterableArray.push(this.items[itemKey]);
}
this.itemKeys = Object.keys(this.items);
}
getFirstItem() {
return this.items[this.itemKeys[0]];
}
getLength() {
return this.itemKeys.length;
}
getKeyById(id) {
return this.itemKeys[id];
}
getById(id) {
return this.items[this.itemKeys[id]];
}
getByKey(key) {
return this.items[key];
}
getKeys() {
return this.itemKeys;
}
getItems() {
return this.items;
}
getIterableItems() {
return this.iterableArray;
}
}
module.exports = FunkyArray;

View file

@ -1,6 +1,6 @@
module.exports = function(id) { module.exports = function(id) {
for (let i = 0; i < global.userKeys.length; i++) { for (let user of global.users.getIterableItems()) {
if (global.users[global.userKeys[i]].id == id) if (user.id == id)
return global.users[userKeys[i]]; return user;
} }
} }

View file

@ -1,4 +1,3 @@
module.exports = function(token) { module.exports = function(token) {
if (global.userKeys.includes(token)) return global.users[token]; return global.users.getByKey(token);
else return null;
} }

View file

@ -1,6 +1,6 @@
module.exports = function(username) { module.exports = function(username) {
for (let i = 0; i < global.userKeys.length; i++) { for (let user of global.users.getIterableItems()) {
if (global.users[global.userKeys[i]].username == username) if (user.username == username)
return global.users[global.userKeys[i]]; return user;
} }
} }