mirror of https://github.com/wayvr-org/wayvr.git
Supporter chance (#624)
* refactor SupporterTier * supporter carousel weighted chance * fix light mode woes
This commit is contained in:
parent
77b5004541
commit
669043035e
Binary file not shown.
|
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 25 KiB |
|
|
@ -64,35 +64,21 @@ async fn request_supporters(tasks: Tasks<Task>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn tier_pretty_print(tier: &str) -> String {
|
||||
format!("{} Tier", wlx_common::locale::capitalize_string(tier))
|
||||
}
|
||||
|
||||
fn tier_color(tier: &str) -> &'static str {
|
||||
match tier {
|
||||
"platinum" => "#aaffff",
|
||||
"gold" => "#ffffaa",
|
||||
"silver" => "#cccccc",
|
||||
"bronze" => "#ffaa66",
|
||||
_ => "on_background",
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> TabDonate<T> {
|
||||
fn set_supporters(&mut self, layout: &mut Layout, supporters: cached_fetcher::Supporters) -> anyhow::Result<()> {
|
||||
let globals = layout.state.globals.clone();
|
||||
layout.remove_children(self.id_current_supporters);
|
||||
|
||||
let mut current_tier = "";
|
||||
let mut current_tier = None;
|
||||
let mut tier_parent = WidgetID::default();
|
||||
|
||||
for supporter in &supporters.supporters {
|
||||
if supporter.tier != current_tier {
|
||||
current_tier = &supporter.tier;
|
||||
if Some(supporter.tier) != current_tier {
|
||||
current_tier = Some(supporter.tier);
|
||||
|
||||
let mut params = TemplateParams::new();
|
||||
params.insert_str("text", tier_pretty_print(&supporter.tier));
|
||||
params.insert("color", tier_color(&supporter.tier));
|
||||
params.insert_str("text", supporter.tier.pretty_str());
|
||||
params.insert("color", supporter.tier.color_str());
|
||||
self.state.realize_template(
|
||||
&doc_params(&globals),
|
||||
"TierCell",
|
||||
|
|
|
|||
|
|
@ -39,7 +39,17 @@ 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;
|
||||
|
|
@ -60,7 +70,7 @@ fn get_supporter_anim(
|
|||
|
||||
btn.set_text_color(
|
||||
common,
|
||||
WguiColor::Named(WguiNamedColor::with_alpha(WguiColorName::OnPrimary, opacity)),
|
||||
WguiColor::Named(WguiNamedColor::with_alpha(WguiColorName::OnBackgroundVariant, opacity)),
|
||||
);
|
||||
|
||||
// first iter
|
||||
|
|
|
|||
|
|
@ -3,20 +3,60 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||
use crate::util::{networking::http_client, steam_utils::AppID};
|
||||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use strum::AsRefStr;
|
||||
use wlx_common::cache_dir;
|
||||
|
||||
fn get_unix_timestamp() -> u64 {
|
||||
SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, AsRefStr)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SupporterTier {
|
||||
Platinum,
|
||||
Gold,
|
||||
Silver,
|
||||
Bronze,
|
||||
}
|
||||
|
||||
impl SupporterTier {
|
||||
pub fn pretty_str(self) -> String {
|
||||
format!("{self:?} Tier")
|
||||
}
|
||||
|
||||
pub const fn color_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Platinum => "#aaffff",
|
||||
Self::Gold => "#ffffaa",
|
||||
Self::Silver => "#cccccc",
|
||||
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)]
|
||||
pub struct Supporter {
|
||||
pub username: String,
|
||||
pub date: String,
|
||||
pub tier: String,
|
||||
pub tier: SupporterTier,
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{
|
||||
frontend::{FrontendTask, FrontendTasks},
|
||||
tab::TabType,
|
||||
util::popup_manager::{MountPopupOnceParams, PopupHolder},
|
||||
views::{ViewTrait, ViewUpdateParams},
|
||||
};
|
||||
|
|
@ -52,6 +53,8 @@ impl ViewTrait for View {
|
|||
globals.palette = new_palette;
|
||||
par.general_config.color_palette = profile.into();
|
||||
par.layout.tasks.push(LayoutTask::RefreshPalette);
|
||||
// this works because LookAndFeel is the first tab
|
||||
self.frontend_tasks.push(FrontendTask::SetTab(TabType::Settings));
|
||||
par.config_change_kind.replace(ConfigChangeKind::WguiColorPaletteChange);
|
||||
if let Some(c) = self.on_close_request.take() {
|
||||
c();
|
||||
|
|
|
|||
Loading…
Reference in New Issue