From b66912a4880a79dd58ed471f79a1956ee5313849 Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 18 Mar 2022 15:31:40 -0400 Subject: [PATCH] feat: add open/close functionality --- src/app/web/route/livesplit.rs | 49 ++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/app/web/route/livesplit.rs b/src/app/web/route/livesplit.rs index aee69d9..d77a8d3 100644 --- a/src/app/web/route/livesplit.rs +++ b/src/app/web/route/livesplit.rs @@ -46,6 +46,11 @@ const REWARDS: &[RewardPauseInfo] = &[ }, ]; +const REQUIRE_OPEN_LIVESPLIT: &[&str] = &[ + "7b66ffcd-8bd3-44c2-8d1d-3bc5ab1fa6ff", + "a598e71b-4521-4d74-8099-2859a81c4233", +]; + async fn set_reward_paused(state: Arc, id: String, paused: bool) -> anyhow::Result<()> { let request = UpdateCustomRewardRequest::builder() .broadcaster_id(state.user_config.twitch.channel_id.to_string()) @@ -59,6 +64,20 @@ async fn set_reward_paused(state: Arc, id: String, paused: bool) -> anyho Ok(()) } +async fn set_open_close_rewards_paused(state: Arc, open: bool) -> Vec> { + let mut results = Vec::with_capacity(REQUIRE_OPEN_LIVESPLIT.len()); + for id in REQUIRE_OPEN_LIVESPLIT { + let is_paused = state.rewards_paused.read().await.get(*id).copied(); + if is_paused == Some(!open) { + continue; + } + + state.rewards_paused.write().await.insert(id.to_string(), !open); + results.push(set_reward_paused(Arc::clone(&state), id.to_string(), !open).await); + } + results +} + async fn set_rewards_paused(state: Arc, data: LiveSplitBody, paused: bool) -> Vec> { let mut results = Vec::with_capacity(REWARDS.len()); for info in REWARDS { @@ -105,8 +124,19 @@ fn rewards_filter(state: Arc, data: LiveSplitBody, paused: bool) -> impl } } +fn open_close_filter(state: Arc, open: bool) -> impl Future> { + async move { + for result in set_open_close_rewards_paused(state, open).await { + if result.is_err() { + return Err(warp::reject::custom(CustomRejection::TwitchError)); + } + } + Ok(()) + } +} + fn livesplit_open(state: Arc) -> BoxedFilter<(impl Reply, )> { - livesplit_route(state, "open", true) + livesplit_open_close_route(state, "open", true) } fn livesplit_start(state: Arc) -> BoxedFilter<(impl Reply, )> { @@ -126,7 +156,7 @@ fn livesplit_finish(state: Arc) -> BoxedFilter<(impl Reply, )> { } fn livesplit_close(state: Arc) -> BoxedFilter<(impl Reply, )> { - livesplit_route(state, "close", true) + livesplit_open_close_route(state, "close", false) } fn livesplit_route(state: Arc, path: &'static str, paused: bool) -> BoxedFilter<(impl Reply,)> { @@ -153,6 +183,21 @@ fn livesplit_route(state: Arc, path: &'static str, paused: bool) -> Boxed .boxed() } +fn livesplit_open_close_route(state: Arc, path: &'static str, open: bool) -> BoxedFilter<(impl Reply, )> { + warp::path("livesplit") + .and(warp::path(path)) + .and(warp::path::end()) + .and_then(move || { + let state = Arc::clone(&state); + async move { + open_close_filter(state, open).await + } + }) + .untuple_one() + .map(|| warp::reply()) + .boxed() +} + #[derive(Deserialize)] #[serde(rename_all = "PascalCase")] #[allow(dead_code)]