feat: add open/close functionality
This commit is contained in:
parent
a446b2ecdd
commit
819996873f
|
@ -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<State>, 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<State>, id: String, paused: bool) -> anyho
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_open_close_rewards_paused(state: Arc<State>, open: bool) -> Vec<anyhow::Result<()>> {
|
||||
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<State>, data: LiveSplitBody, paused: bool) -> Vec<anyhow::Result<()>> {
|
||||
let mut results = Vec::with_capacity(REWARDS.len());
|
||||
for info in REWARDS {
|
||||
|
@ -105,8 +124,19 @@ fn rewards_filter(state: Arc<State>, data: LiveSplitBody, paused: bool) -> impl
|
|||
}
|
||||
}
|
||||
|
||||
fn open_close_filter(state: Arc<State>, open: bool) -> impl Future<Output=Result<(), Rejection>> {
|
||||
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<State>) -> BoxedFilter<(impl Reply, )> {
|
||||
livesplit_route(state, "open", true)
|
||||
livesplit_open_close_route(state, "open", true)
|
||||
}
|
||||
|
||||
fn livesplit_start(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
||||
|
@ -126,7 +156,7 @@ fn livesplit_finish(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
|||
}
|
||||
|
||||
fn livesplit_close(state: Arc<State>) -> BoxedFilter<(impl Reply, )> {
|
||||
livesplit_route(state, "close", true)
|
||||
livesplit_open_close_route(state, "close", false)
|
||||
}
|
||||
|
||||
fn livesplit_route(state: Arc<State>, path: &'static str, paused: bool) -> BoxedFilter<(impl Reply,)> {
|
||||
|
@ -153,6 +183,21 @@ fn livesplit_route(state: Arc<State>, path: &'static str, paused: bool) -> Boxed
|
|||
.boxed()
|
||||
}
|
||||
|
||||
fn livesplit_open_close_route(state: Arc<State>, 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)]
|
||||
|
|
Loading…
Reference in New Issue