New DatabaseHelper

This commit is contained in:
tgpethan 2020-09-07 19:23:06 +01:00
parent 36715a7682
commit dd2d167c72
10 changed files with 54 additions and 26 deletions

1
.gitignore vendored
View file

@ -1 +0,0 @@
server/DatabaseHelper.js

View file

@ -3,7 +3,8 @@ console.clear();
const app = require("express")(), const app = require("express")(),
fs = require("fs"), fs = require("fs"),
busboy = require("connect-busboy"), busboy = require("connect-busboy"),
osu = require("osu-packet"); osu = require("osu-packet"),
config = require("./config.json");
const debugMode = true; const debugMode = true;

31
server/DatabaseHelper.js Normal file
View file

@ -0,0 +1,31 @@
const tcpx = require('tcp-netx'),
aes256 = require('aes256');
module.exports = class {
constructor(databaseAddress, databasePort, databaseKey) {
this.databaseKey = databaseKey;
// Create a new instance of tcp-netx and connect to the database
this.databaseServer = new tcpx.server(databaseAddress, databasePort);
this.databaseServer.connect();
// First response will always be broken
this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `p|`)});
this.databaseServer.read();
}
executeInDB(query) {
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `r|${query}`)});
if (result.ok == 1) return this.databaseServer.read();
else throw "Database error"
}
getFromDB(query) {
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `g|${query}`)});
if (result.ok == 1) return JSON.parse(aes256.decrypt(this.databaseKey, this.databaseServer.read()["data"]));
else throw "Database error";
}
}

View file

@ -1,6 +1,5 @@
const osu = require("osu-packet"), const osu = require("osu-packet"),
getUserById = require("./util/getUserById.js"), getUserById = require("./util/getUserById.js"),
DatabaseHelper = require("./DatabaseHelper.js"),
StatusUpdate = require("./Packets/StatusUpdate.js"); StatusUpdate = require("./Packets/StatusUpdate.js");
module.exports = { module.exports = {
@ -32,7 +31,7 @@ module.exports = {
const User = getUserById(slot.playerId); const User = getUserById(slot.playerId);
// Get user score info from the database // Get user score info from the database
const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`);
let UserStatusObject = { let UserStatusObject = {
userId: User.id, userId: User.id,
@ -88,7 +87,7 @@ module.exports = {
const User = getUserById(slot.playerId); const User = getUserById(slot.playerId);
// Get user score info from the database // Get user score info from the database
const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`);
let UserStatusObject = { let UserStatusObject = {
userId: User.id, userId: User.id,

View file

@ -1,5 +1,3 @@
const DatabaseHelper = require("../DatabaseHelper.js");
module.exports = function(CurrentUser, FriendToAdd) { module.exports = function(CurrentUser, FriendToAdd) {
DatabaseHelper.getFromDB(`INSERT INTO friends (user, friendsWith) VALUES (${CurrentUser.id}, ${FriendToAdd});`); global.DatabaseHelper.executeInDB(`INSERT INTO friends (user, friendsWith) VALUES (${CurrentUser.id}, ${FriendToAdd});`);
} }

View file

@ -1,5 +1,3 @@
const DatabaseHelper = require("../DatabaseHelper.js");
module.exports = function(CurrentUser, FriendToRemove) { module.exports = function(CurrentUser, FriendToRemove) {
DatabaseHelper.getFromDB(`DELETE FROM friends WHERE user = ${CurrentUser.id} AND friendsWith = ${FriendToRemove} LIMIT 1`); global.DatabaseHelper.executeInDB(`DELETE FROM friends WHERE user = ${CurrentUser.id} AND friendsWith = ${FriendToRemove} LIMIT 1`);
} }

View file

@ -1,5 +1,3 @@
const DatabaseHelper = require("./DatabaseHelper.js");
module.exports = class { module.exports = class {
constructor(id, username, uuid, connectTime) { constructor(id, username, uuid, connectTime) {
this.id = id; this.id = id;
@ -52,8 +50,8 @@ module.exports = class {
// Gets the user's score information from the database and caches it // Gets the user's score information from the database and caches it
getNewUserInformationFromDatabase() { getNewUserInformationFromDatabase() {
const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`); const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`);
const userRankDB = DatabaseHelper.getFromDB(`SELECT user_id, pp_raw, FIND_IN_SET( pp_raw, ( SELECT GROUP_CONCAT( pp_raw ORDER BY pp_raw DESC ) FROM users_modes_info WHERE mode_id = ${this.playMode} ) ) AS rank FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode}`); const userRankDB = global.DatabaseHelper.getFromDB(`SELECT user_id, pp_raw, FIND_IN_SET( pp_raw, ( SELECT GROUP_CONCAT( pp_raw ORDER BY pp_raw DESC ) FROM users_modes_info WHERE mode_id = ${this.playMode} ) ) AS rank FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode}`);
if (userScoreDB == null || userRankDB == null) throw "fuck"; if (userScoreDB == null || userRankDB == null) throw "fuck";

View file

@ -3,7 +3,6 @@ const osu = require("osu-packet"),
{ v4: uuid } = require('uuid'), { v4: uuid } = require('uuid'),
request = require("sync-request"), request = require("sync-request"),
DatabaseHelper = require("./DatabaseHelper.js"),
getUserByUsername = require("./util/getUserByUsername.js"), getUserByUsername = require("./util/getUserByUsername.js"),
getUserByToken = require("./util/getUserByToken.js"), getUserByToken = require("./util/getUserByToken.js"),
countryHelper = require("./countryHelper.js"), countryHelper = require("./countryHelper.js"),
@ -38,7 +37,7 @@ module.exports = function(req, res, loginInfo) {
} }
// Get information about the user from the database // Get information about the user from the database
const userDB = DatabaseHelper.getFromDB(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`); const userDB = global.DatabaseHelper.getFromDB(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
// Create a token for the client // Create a token for the client
const newClientToken = uuid(); const newClientToken = uuid();
@ -88,7 +87,7 @@ module.exports = function(req, res, loginInfo) {
// Permission level 4 is osu!supporter // Permission level 4 is osu!supporter
osuPacketWriter.LoginPermissions(4); osuPacketWriter.LoginPermissions(4);
// Construct user's friends list // Construct user's friends list
const userFriends = DatabaseHelper.getListFromDB(`SELECT friendsWith FROM friends WHERE user = ${userClass.id}`); const userFriends = global.DatabaseHelper.getFromDB(`SELECT friendsWith FROM friends WHERE user = ${userClass.id}`);
let friendsArray = []; let friendsArray = [];
for (let i = 0; i < userFriends.length; i++) { for (let i = 0; i < userFriends.length; i++) {
friendsArray.push(userFriends[i].friendsWith); friendsArray.push(userFriends[i].friendsWith);

View file

@ -1,5 +1,4 @@
const DatabaseHelper = require("./DatabaseHelper.js"), const osu = require("osu-packet");
osu = require("osu-packet");
module.exports = { module.exports = {
checkLogin:function(loginInfo) { checkLogin:function(loginInfo) {
@ -8,7 +7,7 @@ module.exports = {
// Check if there is any login information provided // Check if there is any login information provided
if (loginInfo == null) return incorrectDetailsResponse; if (loginInfo == null) return incorrectDetailsResponse;
const userDBData = DatabaseHelper.getFromDB(`SELECT * FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`); const userDBData = global.DatabaseHelper.getFromDB(`SELECT * FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
// Make sure a user was found in the database // Make sure a user was found in the database
if (Object.keys(userDBData).length < 1) return incorrectDetailsResponse; if (Object.keys(userDBData).length < 1) return incorrectDetailsResponse;

View file

@ -6,12 +6,22 @@ const osu = require("osu-packet"),
User = require("./User.js"), User = require("./User.js"),
getUserFromToken = require("./util/getUserByToken.js"), getUserFromToken = require("./util/getUserByToken.js"),
bakedResponses = require("./bakedResponses.js"), bakedResponses = require("./bakedResponses.js"),
Streams = require("./Streams.js"); Streams = require("./Streams.js"),
DatabaseHelperClass = require("./DatabaseHelper.js"),
config = require("../config.json");
global.users = [ global.users = [
new User(3, "SillyBot", "SillyBot", new Date().getTime()) new User(3, "SillyBot", "SillyBot", new Date().getTime())
]; ];
// Set the bot's position on the map
// First user will always be the bot
global.users[0].location[0] = 50;
global.users[0].location[1] = -32;
global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseKey);
// Start a loop that gets new data for users from the database for use on the user panel // Start a loop that gets new data for users from the database for use on the user panel
// 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)
@ -25,10 +35,6 @@ setInterval(() => {
} }
}, 10000); }, 10000);
// Set the bot's position on the map
global.users[0].location[0] = 50;
global.users[0].location[1] = -32;
// An array containing all currently active multiplayer matches // An array containing all currently active multiplayer matches
global.matches = []; global.matches = [];
// An array containing the last 15 messages in chat // An array containing the last 15 messages in chat