Skip to content

Commit

Permalink
Refactor integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
javster101 committed Nov 28, 2023
1 parent 420e35a commit 49011eb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 271 deletions.
12 changes: 6 additions & 6 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ pub fn parse_cmd<'a>(
layouts: &'a LayoutFile,
) -> Result<Command<'a>, Error> {
match cmd {
CommandSource::Toggle(runner) => project.find_or_err(runner).map(|u| Command::Toggle(u)),
CommandSource::Toggle(runner) => project.find_player(runner).map(|u| Command::Toggle(u)),
CommandSource::Swap(r1, r2)=> {
let p1 = project.find_or_err(&r1)?;
let p2 = project.find_or_err(&r2)?;
let p1 = project.find_player(&r1)?;
let p2 = project.find_player(&r2)?;

if p1 == p2 {
Err(Error::CommandError(
Expand All @@ -59,16 +59,16 @@ pub fn parse_cmd<'a>(
},
CommandSource::SetPlayers(players) => players
.into_iter()
.map(|e| project.find_or_err(&e))
.map(|e| project.find_player(&e))
.collect::<Result<Vec<&Player>, Error>>()
.map(|ps| Command::SetPlayers(ps)),
CommandSource::SetPlayerFields(player, fields) => {
let player = project.find_or_err(player)?;
let player = project.find_player(player)?;

Ok(Command::SetPlayerFields(player, fields.clone()))
}
CommandSource::Refresh(users) if users.len() != 0 => users.into_iter()
.map(|e| project.find_or_err(e))
.map(|e| project.find_player(e))
.collect::<Result<Vec<&Player>, Error>>()
.map(|ps| Command::Refresh(ps)),
CommandSource::Refresh(users) if users.len() == 0 => Ok(Command::Refresh(project.players.iter().collect())),
Expand Down
File renamed without changes.
51 changes: 51 additions & 0 deletions src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::sync::Arc;

use tokio::{sync::mpsc::UnboundedSender, task::JoinSet};

use crate::{
error::Error,
obs::LayoutFile,
settings::Settings,
state::{self, Project},
CommandMessage,
};

pub mod discord;
pub mod therun;
pub mod web;

pub fn init_integrations(
tasks: &mut JoinSet<Result<(), Error>>,
command_sender: UnboundedSender<CommandMessage>,
settings: Arc<Settings>,
layouts: Arc<LayoutFile>,
project: Arc<Project>,
) {
// Add TheRun.gg module to tasks
if project.integrations.contains(&state::Integration::TheRun) {
for player in &project.players {
let therun = player
.therun
.to_owned()
.unwrap_or(player.stream.split('/').last().unwrap().to_string());
tasks.spawn(therun::create_player_websocket_monitor(
player.name.clone(),
therun.to_string(),
command_sender.clone(),
));
}
}

// Add discord integration to tasks
if project.integrations.contains(&state::Integration::Discord) {
tasks.spawn(discord::init_discord(
command_sender.clone(),
settings.clone(),
project.clone(),
layouts.clone(),
));
}

// Add webserver to tasks
tasks.spawn(web::run_http_server(command_sender.clone()));
}
File renamed without changes.
1 change: 1 addition & 0 deletions src/web.rs → src/integrations/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub async fn run_http_server(tx: UnboundedSender<CommandMessage>) -> Result<(),
"Access-Control-Allow-Headers",
])
.allow_methods(&[Method::GET, Method::POST, Method::DELETE]);

let user_endpoint = warp::path("users")
.and(warp::path::end())
.and_then(move || {
Expand Down
Loading

0 comments on commit 49011eb

Please sign in to comment.