Refactor lp_modification function

This commit is contained in:
Ethan O'Brien
2026-07-03 18:56:18 -05:00
parent 9cf0ee8924
commit 128f53998b
3 changed files with 36 additions and 27 deletions

View File

@@ -267,31 +267,38 @@ pub fn gift_item_basic(id: i32, value: i64, ty_pe: i32, reason: &str, user: &mut
}, reason, user)
}
pub fn lp_modification(user: &mut JsonValue, change_amount: u64, remove: bool) {
pub fn lp_modification(user: &mut JsonValue, change_amount: u64, remove: bool) -> bool {
let now = global::set_time(global::timestamp(), user["user"]["id"].as_i64().unwrap_or(0), false);
let max = get_user_rank_data(user["user"]["exp"].as_i64().unwrap())["maxLp"].as_u64().unwrap();
let speed = 285; //4 mins, 45 sec
let since_last = global::timestamp() - user["stamina"]["last_updated_time"].as_u64().unwrap();
let orig_stamina = user["stamina"]["stamina"].as_u64().unwrap();
let orig_anchor = user["stamina"]["last_updated_time"].as_u64().unwrap();
let diff = since_last % speed;
let restored = (since_last - diff) / speed;
user["stamina"]["last_updated_time"] = (global::timestamp() - diff).into();
let mut stamina = orig_stamina;
let mut anchor = orig_anchor;
let mut stamina = user["stamina"]["stamina"].as_u64().unwrap();
if stamina < max {
stamina += restored;
if stamina > max {
stamina = max;
}
if stamina < max && now > anchor {
let restored = (now - anchor) / 300;
stamina = (stamina + restored).min(max);
anchor += restored * 300;
}
if change_amount > 0 {
if remove {
let was_full = stamina >= max;
stamina -= change_amount;
if was_full {
anchor = now;
}
} else {
stamina += change_amount;
}
}
user["stamina"]["stamina"] = stamina.into();
user["stamina"]["last_updated_time"] = anchor.into();
stamina != orig_stamina || anchor != orig_anchor
}
pub fn get_rarity(id: i64) -> i32 {
@@ -360,7 +367,6 @@ pub fn give_exp(amount: i32, user: &mut JsonValue, mission: &mut JsonValue, rv:
let new_rank = get_user_rank_data(user["user"]["exp"].as_i64().unwrap());
if current_rank["rank"] != new_rank["rank"] {
user["stamina"]["stamina"] = (user["stamina"]["stamina"].as_i64().unwrap() + new_rank["maxLp"].as_i64().unwrap()).into();
user["stamina"]["last_updated_time"] = global::timestamp().into();
let status = get_mission_status(get_variable_mission_num(1101001, 1101030, mission), mission);
if status.is_empty() {

View File

@@ -16,7 +16,7 @@ async fn tutorial(req: HttpRequest, body: String) -> impl Responder {
if user["tutorial_step"].as_i32().unwrap() < 130 {
user["tutorial_step"] = body["step"].clone();
user["stamina"]["stamina"] = (100).into();
user["stamina"]["last_updated_time"] = global::timestamp().into();
user["stamina"]["last_updated_time"] = global::set_time(global::timestamp(), user["user"]["id"].as_i64().unwrap(), false).into();
userdata::save_acc(&key, user);
}

View File

@@ -268,11 +268,14 @@ fn remove_deleted_custom_songs(user: &mut JsonValue) -> bool {
pub fn get_acc(auth_key: &str) -> JsonValue {
let mut user = get_data(auth_key, "userdata");
cleanup_account(&mut user);
if remove_deleted_custom_songs(&mut user) {
let mut changed = remove_deleted_custom_songs(&mut user);
if items::lp_modification(&mut user, 0, false) {
changed = true;
}
if changed {
save_data(auth_key, "userdata", user.clone());
}
items::lp_modification(&mut user, 0, false);
user
}