supporter carousel weighted chance

This commit is contained in:
galister 2026-08-09 19:05:37 +09:00
parent 345fe7b3ac
commit e030a9b115
2 changed files with 23 additions and 2 deletions

View File

@ -39,7 +39,13 @@ fn get_supporter_anim(
prev_supporter_name: String,
) -> Animation {
let username = loop {
let random_supporter = &supporters.supporters[rand::random_range(0..supporters.supporters.len() - 1)];
let total_tickets: u32 = supporters.supporters.iter().map(|s| s.tickets()).sum();
let jackpot = rand::random_range(0..total_tickets);
let mut cumulative = 0u32;
let random_supporter = supporters.supporters.iter().find(|s| {
cumulative += s.tickets();
cumulative >= jackpot
}).unwrap();
let username = random_supporter.username.clone();
if username != prev_supporter_name {
break username;

View File

@ -24,7 +24,7 @@ impl SupporterTier {
format!("{self:?} Tier")
}
pub fn color_str(self) -> &'static str {
pub const fn color_str(self) -> &'static str {
match self {
Self::Platinum => "#aaffff",
Self::Gold => "#ffffaa",
@ -32,6 +32,15 @@ impl SupporterTier {
Self::Bronze => "#ffaa66",
}
}
pub const fn tickets(self) -> u32 {
match self {
Self::Platinum => 20,
Self::Gold => 10,
Self::Silver => 5,
Self::Bronze => 1,
}
}
}
#[derive(Serialize, Deserialize)]
@ -42,6 +51,12 @@ pub struct Supporter {
pub contribution_count: u32,
}
impl Supporter {
pub const fn tickets(&self) -> u32 {
self.tier.tickets()
}
}
#[derive(Serialize, Deserialize)]
pub struct Supporters {
pub time_window_days: u32,