Clean up the User class

This commit is contained in:
Holly Stubbs 2022-02-23 03:22:35 +00:00
parent 79bbe5a38d
commit 5f5b8457de
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
3 changed files with 39 additions and 55 deletions

View file

@ -1,11 +1,17 @@
const StatusUpdate = require("./Packets/StatusUpdate.js"); const StatusUpdate = require("./Packets/StatusUpdate.js");
const rankingModes = [
"pp_raw",
"ranked_score",
"avg_accuracy"
];
module.exports = class { module.exports = class {
constructor(id, username, uuid, connectTime, isTourneyUser = false) { constructor(id, username, uuid) {
this.id = id; this.id = id;
this.username = username; this.username = username;
this.uuid = uuid; this.uuid = uuid;
this.connectTime = connectTime; this.connectTime = Date.now();
this.queue = Buffer.alloc(0); this.queue = Buffer.alloc(0);
// Binato specific // Binato specific
@ -38,7 +44,7 @@ module.exports = class {
this.currentMatch = null; this.currentMatch = null;
this.matchSlotId = -1; this.matchSlotId = -1;
this.isTourneyUser = isTourneyUser; this.isTourneyUser = false;
} }
// Adds new actions to the user's queue // Adds new actions to the user's queue
@ -60,70 +66,48 @@ 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
async getNewUserInformationFromDatabase(forceUpdate = false) { async getNewUserInformationFromDatabase(forceUpdate = false) {
const userScoreDB = await global.DatabaseHelper.query(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`); const userScoreDB = await global.DatabaseHelper.query(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`);
let userRankDB = null; const mappedRankingMode = rankingModes[this.rankingMode];
switch (this.rankingMode) { const userRankDB = await global.DatabaseHelper.query(`SELECT user_id, ${mappedRankingMode} FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY ${mappedRankingMode} DESC`);
case 0:
userRankDB = await global.DatabaseHelper.query(`SELECT user_id, pp_raw FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY pp_raw DESC`);
break;
case 1:
userRankDB = await global.DatabaseHelper.query(`SELECT user_id, ranked_score FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY ranked_score DESC`);
break;
case 2:
userRankDB = await global.DatabaseHelper.query(`SELECT user_id, avg_accuracy FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY avg_accuracy DESC`);
break;
}
if (userScoreDB == null || userRankDB == null) throw "fuck"; if (userScoreDB == null || userRankDB == null) throw "fuck";
// Handle "if we should update" checks for each rankingMode
let userScoreUpdate = false; let userScoreUpdate = false;
if (forceUpdate) { switch (this.rankingMode) {
userScoreUpdate = true; case 0:
} else { if (this.pp != userScoreDB.pp_raw)
switch (this.rankingMode) { userScoreUpdate = true;
case 0:
if (this.pp != userScoreDB.pp_raw) {
userScoreUpdate = true;
}
break; break;
case 1: case 1:
if (this.rankedScore != userScoreDB.ranked_score) { if (this.rankedScore != userScoreDB.ranked_score)
userScoreUpdate = true; userScoreUpdate = true;
}
break; break;
case 2: case 2:
if (this.accuracy != userScoreDB.avg_accuracy) { if (this.accuracy != userScoreDB.avg_accuracy)
userScoreUpdate = true; userScoreUpdate = true;
}
break; break;
}
} }
this.rankedScore = userScoreDB.ranked_score; this.rankedScore = userScoreDB.ranked_score;
this.totalScore = userScoreDB.total_score; this.totalScore = userScoreDB.total_score;
this.accuracy = userScoreDB.avg_accuracy; this.accuracy = userScoreDB.avg_accuracy;
this.playCount = userScoreDB.playcount; this.playCount = userScoreDB.playcount;
// Fetch rank
for (let i = 0; i < userRankDB.length; i++) { for (let i = 0; i < userRankDB.length; i++) {
if (userRankDB[i]["user_id"] == this.id) this.rank = i + 1; if (userRankDB[i]["user_id"] == this.id) {
} this.rank = i + 1;
switch (this.rankingMode) { break;
case 0: }
this.pp = userScoreDB.pp_raw;
break;
case 1:
this.pp = 0;
break;
case 2:
this.pp = 0;
break;
} }
if (userScoreUpdate) { // Set PP to none if ranking mode is not PP
if (this.rankingMode == 0) this.pp = userScoreDB.pp_raw;
else this.pp = 0;
if (userScoreUpdate || forceUpdate) {
StatusUpdate(this, this.id); StatusUpdate(this, this.id);
} }
} }

View file

@ -4,7 +4,6 @@ const osu = require("osu-packet"),
ahttp = require("./util/AsyncHttpRequest.js"), ahttp = require("./util/AsyncHttpRequest.js"),
RequestType = require("./util/RequestType.json"), RequestType = require("./util/RequestType.json"),
consoleHelper = require("../consoleHelper.js"), consoleHelper = require("../consoleHelper.js"),
// Packets // Packets
getUserByUsername = require("./util/getUserByUsername.js"), getUserByUsername = require("./util/getUserByUsername.js"),
getUserByToken = require("./util/getUserByToken.js"), getUserByToken = require("./util/getUserByToken.js"),
@ -76,10 +75,12 @@ module.exports = async function(req, res, loginInfo) {
} }
// Create user object // Create user object
global.addUser(newClientToken, new User(userDB.id, loginInfo.username, newClientToken, Date.now(), isTourneyClient)); 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 = getUserByToken(newClientToken);
// Set tourney client flag
NewUser.isTourneyUser = isTourneyClient;
// Get user's data from the database // Get user's data from the database
NewUser.getNewUserInformationFromDatabase(); NewUser.getNewUserInformationFromDatabase();

View file

@ -29,14 +29,13 @@ global.refreshUserKeys = function() {
} }
// Add the bot user // Add the bot user
global.addUser("bot", new User(3, "SillyBot", "bot", Date.now())); global.addUser("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.users["bot"].location[0] = 50;
global.users["bot"].location[1] = -32; global.users["bot"].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);
// 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)