|
@@ -1,40 +1,17 @@
|
|
|
import {osu_fetch} from './api.js';
|
|
|
-import bancho from './bancho.js';
|
|
|
import db from './database.js';
|
|
|
import {sync_discord_info} from './discord_updates.js';
|
|
|
|
|
|
|
|
|
-async function init_user(user_id) {
|
|
|
- for (let ruleset = 0; ruleset < 4; ruleset++) {
|
|
|
- db.prepare(`INSERT INTO
|
|
|
- rating (user_id, mode, s3_scores, total_scores, elo, division)
|
|
|
- VALUES (?, ?, 0, 0, 1500, 'Unranked')
|
|
|
- RETURNING *`,
|
|
|
- ).run(user_id, ruleset);
|
|
|
+async function get_user(user_id) {
|
|
|
+ const user = db.prepare(`SELECT * FROM user WHERE user_id = ?`).get(user_id);
|
|
|
+ if (!user) {
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
- const user_data = {
|
|
|
- username: 'New user',
|
|
|
- country_code: 'Unknown',
|
|
|
- country_name: 'Unknown country',
|
|
|
- last_visit: new Date(0).toISOString(),
|
|
|
- };
|
|
|
- const user = db.prepare(`
|
|
|
- INSERT INTO user (user_id, username, country_code, profile_data, discord_user_id)
|
|
|
- VALUES (?, ?, ?, ?, NULL)
|
|
|
- RETURNING *`,
|
|
|
- ).get(user_id, user_data.username, user_data.country_code, JSON.stringify(user_data));
|
|
|
- await populate_ratings_and_update_profile(user);
|
|
|
- return user;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-async function populate_ratings_and_update_profile(user) {
|
|
|
- user.ratings = db.prepare(`SELECT * FROM rating WHERE user_id = ? ORDER BY mode ASC`).all(user.user_id);
|
|
|
-
|
|
|
let data = JSON.parse(user.profile_data);
|
|
|
const seconds_since_last_update = (Date.now() - Date.parse(data.last_visit)) / 1000;
|
|
|
- if (bancho.connected && (seconds_since_last_update > 24 * 3600)) {
|
|
|
+ if (seconds_since_last_update > 24 * 3600) {
|
|
|
const res = await osu_fetch(`https://osu.ppy.sh/api/v2/users?ids[0]=${user.user_id}`);
|
|
|
if (!res.users[0]) {
|
|
|
throw new Error('User profile unavailable. Probably banned.');
|
|
@@ -56,50 +33,42 @@ async function populate_ratings_and_update_profile(user) {
|
|
|
user.username = data.username;
|
|
|
}
|
|
|
|
|
|
+ user.ratings = db.prepare(`SELECT * FROM rating WHERE user_id = ? ORDER BY mode ASC`).all(user_id);
|
|
|
user.pps = [
|
|
|
(data.statistics_rulesets?.osu?.pp || 0) / 20,
|
|
|
(data.statistics_rulesets?.taiko?.pp || 0) / 20,
|
|
|
(data.statistics_rulesets?.fruits?.pp || 0) / 20,
|
|
|
(data.statistics_rulesets?.mania?.pp || 0) / 20,
|
|
|
];
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-async function get_user_by_id(user_id, create) {
|
|
|
- let user = db.prepare(`SELECT * FROM user WHERE user_id = ?`).get(user_id);
|
|
|
- if (user) {
|
|
|
- await populate_ratings_and_update_profile(user);
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- if (!create) {
|
|
|
- return null;
|
|
|
- }
|
|
|
|
|
|
- user = await init_user(user_id);
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
-async function get_user_by_name(name) {
|
|
|
- let user = db.prepare(`SELECT * FROM user WHERE username = ?`).get(name);
|
|
|
+async function get_or_create_user(user_id) {
|
|
|
+ const user = await get_user(user_id);
|
|
|
if (user) {
|
|
|
- await populate_ratings_and_update_profile(user);
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
- let user_id;
|
|
|
- try {
|
|
|
- user_id = await bancho.whois(name);
|
|
|
- } catch (err) {
|
|
|
- user_id = await osu_fetch(`https://osu.ppy.sh/api/v2/users/${name}?key=username`).id;
|
|
|
+ for (let ruleset = 0; ruleset < 4; ruleset++) {
|
|
|
+ db.prepare(`INSERT INTO
|
|
|
+ rating (user_id, mode, s3_scores, total_scores, elo, division)
|
|
|
+ VALUES (?, ?, 0, 0, 1500, 'Unranked')`,
|
|
|
+ ).run(user_id, ruleset);
|
|
|
}
|
|
|
|
|
|
- if (!user_id) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ const user_data = {
|
|
|
+ username: 'New user',
|
|
|
+ country_code: 'Unknown',
|
|
|
+ country_name: 'Unknown country',
|
|
|
+ last_visit: new Date(0).toISOString(),
|
|
|
+ };
|
|
|
+ db.prepare(`
|
|
|
+ INSERT INTO user (user_id, username, country_code, profile_data, discord_user_id)
|
|
|
+ VALUES (?, ?, ?, ?, NULL)`,
|
|
|
+ ).get(user_id, user_data.username, user_data.country_code, JSON.stringify(user_data));
|
|
|
|
|
|
- user = await get_user_by_id(user_id);
|
|
|
- return user;
|
|
|
+ return await get_user(user_id);
|
|
|
}
|
|
|
|
|
|
-export {get_user_by_id, get_user_by_name};
|
|
|
+export {get_or_create_user, get_user};
|