Binato/server/User.js

119 lines
3 KiB
JavaScript
Raw Normal View History

const StatusUpdate = require("./Packets/StatusUpdate.js");
2022-02-23 03:22:35 +00:00
const rankingModes = [
"pp_raw",
"ranked_score",
"avg_accuracy"
];
2020-08-27 13:09:35 +01:00
module.exports = class {
2022-02-23 03:22:35 +00:00
constructor(id, username, uuid) {
2021-09-03 23:47:46 +01:00
this.id = id;
this.username = username;
this.uuid = uuid;
2022-02-23 03:22:35 +00:00
this.connectTime = Date.now();
this.queue = Buffer.alloc(0);
2021-09-03 23:47:46 +01:00
// Binato specific
this.rankingMode = 0;
this.playMode = 0;
this.countryID = 0;
this.spectators = [];
this.spectating = 0;
this.location = [0,0];
this.joinedChannels = [];
// Presence data
this.actionID = 0;
this.actionText = "";
this.actionMods = 0;
this.beatmapChecksum = "";
this.beatmapID = 0;
this.currentMods = 0;
// Cached db data
this.rankedScore = 0;
this.accuracy = 0;
this.playCount = 0;
this.totalScore = 0;
this.rank = 0;
this.pp = 0;
// Multiplayer data
this.currentMatch = null;
this.matchSlotId = -1;
2022-02-23 03:22:35 +00:00
this.isTourneyUser = false;
2021-09-03 23:47:46 +01:00
}
// Adds new actions to the user's queue
addActionToQueue(newData) {
this.queue = Buffer.concat([this.queue, newData], this.queue.length + newData.length);
}
// Updates the user's current action
updatePresence(action) {
this.actionID = action.status;
this.actionText = action.statusText;
this.beatmapChecksum = action.beatmapChecksum;
this.currentMods = action.currentMods;
this.actionMods = action.currentMods;
this.playMode = action.playMode;
this.beatmapID = action.beatmapId;
}
// Gets the user's score information from the database and caches it
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`);
2022-02-23 03:22:35 +00:00
const mappedRankingMode = rankingModes[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`);
2021-09-03 23:47:46 +01:00
if (userScoreDB == null || userRankDB == null) throw "fuck";
2022-02-23 03:22:35 +00:00
// Handle "if we should update" checks for each rankingMode
2021-09-03 23:47:46 +01:00
let userScoreUpdate = false;
2022-02-23 03:22:35 +00:00
switch (this.rankingMode) {
case 0:
if (this.pp != userScoreDB.pp_raw)
userScoreUpdate = true;
2021-09-03 23:47:46 +01:00
break;
2022-02-23 03:22:35 +00:00
case 1:
if (this.rankedScore != userScoreDB.ranked_score)
userScoreUpdate = true;
2021-09-03 23:47:46 +01:00
break;
2022-02-23 03:22:35 +00:00
case 2:
if (this.accuracy != userScoreDB.avg_accuracy)
userScoreUpdate = true;
2021-09-03 23:47:46 +01:00
break;
}
this.rankedScore = userScoreDB.ranked_score;
this.totalScore = userScoreDB.total_score;
this.accuracy = userScoreDB.avg_accuracy;
this.playCount = userScoreDB.playcount;
2022-02-23 03:22:35 +00:00
// Fetch rank
2021-09-03 23:47:46 +01:00
for (let i = 0; i < userRankDB.length; i++) {
2022-02-23 03:22:35 +00:00
if (userRankDB[i]["user_id"] == this.id) {
this.rank = i + 1;
break;
}
2021-09-03 23:47:46 +01:00
}
2022-02-23 03:22:35 +00:00
// Set PP to none if ranking mode is not PP
if (this.rankingMode == 0) this.pp = userScoreDB.pp_raw;
else this.pp = 0;
2021-09-03 23:47:46 +01:00
2022-02-23 03:22:35 +00:00
if (userScoreUpdate || forceUpdate) {
2021-09-03 23:47:46 +01:00
StatusUpdate(this, this.id);
}
}
// Clears out the user's queue
clearQueue() {
this.queue = Buffer.alloc(0);
2021-09-03 23:47:46 +01:00
}
2020-08-27 13:09:35 +01:00
}