better id randomization

This commit is contained in:
Ethan O'Brien 2024-04-29 13:59:18 -05:00
parent 12f21c1312
commit 05ed74f3c6

View file

@ -9,6 +9,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use base64::{Engine as _, engine::general_purpose}; use base64::{Engine as _, engine::general_purpose};
use crate::router::userdata; use crate::router::userdata;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use rand::Rng;
pub const ASSET_VERSION: &str = "13177023d4b7ad41ff52af4cefba5c55"; pub const ASSET_VERSION: &str = "13177023d4b7ad41ff52af4cefba5c55";
pub const ASSET_HASH_ANDROID: &str = "017ec1bcafbeea6a7714f0034b15bd0f"; pub const ASSET_HASH_ANDROID: &str = "017ec1bcafbeea6a7714f0034b15bd0f";
@ -91,6 +92,12 @@ pub fn timestamp() -> u64 {
let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap(); let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap();
return unix_timestamp.as_secs(); return unix_timestamp.as_secs();
} }
pub fn timestamp_msec() -> u32 {
let now = SystemTime::now();
let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap();
return unix_timestamp.subsec_nanos();
}
pub fn timestamp_since_midnight() -> u64 { pub fn timestamp_since_midnight() -> u64 {
let now = SystemTime::now(); let now = SystemTime::now();
let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap(); let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap();
@ -235,10 +242,17 @@ pub fn gift_item(item: &JsonValue, reason: &str, user: &mut JsonValue) -> JsonVa
user["home"]["gift_list"].push(to_push.clone()).unwrap(); user["home"]["gift_list"].push(to_push.clone()).unwrap();
return to_push; return to_push;
} }
fn random_number(lowest: usize, highest: usize) -> usize {
if lowest == highest {
return lowest;
}
assert!(lowest < highest);
rand::thread_rng().gen_range(lowest..highest + 1)
}
pub fn gift_item_basic(id: i32, value: i64, ty_pe: i32, reason: &str, user: &mut JsonValue) -> JsonValue { pub fn gift_item_basic(id: i32, value: i64, ty_pe: i32, reason: &str, user: &mut JsonValue) -> JsonValue {
gift_item(&object!{ gift_item(&object!{
id: timestamp(), id: random_number(0, timestamp_msec() as usize),
type: ty_pe, type: ty_pe,
level: 0, level: 0,
amount: value, amount: value,