122 lines
3.4 KiB
Rust
122 lines
3.4 KiB
Rust
use crate::smashquery::{MyQuery, MyQueryVariables, Set, SetSlot,StreamQueue};
|
|
use cynic::{ GraphQlResponse, QueryBuilder};
|
|
//use futures::executor;
|
|
static URL_SMASH: &str = "https://api.start.gg/gql/alpha";
|
|
|
|
#[derive(Debug,Clone)]
|
|
pub struct Match {
|
|
pub player1 : String,
|
|
pub player2 : String,
|
|
pub station : String,
|
|
pub round : i32,
|
|
pub full_round_test: String
|
|
}
|
|
|
|
#[derive(Debug,Clone)]
|
|
pub struct SmashQueue {
|
|
pub name : String,
|
|
pub matches : Vec<Match>
|
|
}
|
|
|
|
impl Match {
|
|
pub fn new() -> Self {
|
|
Self { player1: String::from(""), player2: String::from("") , station: String::from("") , round: 0 , full_round_test: String::from("")}
|
|
}
|
|
|
|
}
|
|
|
|
impl SmashQueue{
|
|
pub fn new() -> Self{
|
|
Self { name:String::from(""),matches : vec![] }
|
|
}
|
|
}
|
|
|
|
fn build_query(tournament :&str) -> cynic::Operation<MyQuery,MyQueryVariables> {
|
|
|
|
MyQuery::build(MyQueryVariables
|
|
{
|
|
slug:Some(tournament)
|
|
} )
|
|
}
|
|
|
|
fn request_stream_queue(tournament :&str, key : &str ) -> GraphQlResponse<MyQuery>{
|
|
|
|
// let resp = async_std::task::block_on(async {
|
|
|
|
let query = MyQuery::build(MyQueryVariables
|
|
{
|
|
slug:Some(tournament)
|
|
} );
|
|
|
|
|
|
let resp = reqwest::blocking::Client::new().post(URL_SMASH).header("Authorization", key).json(&query).send().unwrap();
|
|
return resp.json().unwrap();
|
|
|
|
// });
|
|
|
|
// return resp;
|
|
|
|
}
|
|
|
|
pub fn get_matches(tournament : &str, key : &str) -> Vec<SmashQueue> {
|
|
let mut queues :Vec<SmashQueue> = Vec::new();
|
|
let query = request_stream_queue(tournament,key);
|
|
let st :Vec<StreamQueue> = query.data.unwrap()
|
|
.tournament.unwrap()
|
|
.stream_queue.unwrap().into_iter()
|
|
.filter(|x| x.is_some()).map(|x| x.unwrap()).collect();
|
|
|
|
for s in st {
|
|
let mut current_queue = SmashQueue {
|
|
name : s.stream.unwrap().stream_name.unwrap_or(String::from("Stream Queue Not Named")),
|
|
matches : Vec::new() };
|
|
|
|
if s.sets.is_some() {
|
|
let sets : Vec<Set> = s.sets.unwrap().into_iter().filter(|x| x.is_some()).map(|x| x.unwrap()).collect();
|
|
for set in sets {
|
|
|
|
let slot: Vec<SetSlot> = set.slots.unwrap().into_iter().filter(|x| x.is_some()).map(|x| x.unwrap()).collect();
|
|
let mut iter = slot.into_iter();
|
|
let p1 = iter.next();
|
|
let p2 = iter.next();
|
|
|
|
|
|
let p1name = match p1 {
|
|
Some(x) => match x.entrant {
|
|
Some(n) => n.name.unwrap(),
|
|
_ => String::new()
|
|
},
|
|
_ => String::new()
|
|
};
|
|
|
|
|
|
let p2name = match p2 {
|
|
Some(x) => match x.entrant {
|
|
Some(n) => n.name.unwrap(),
|
|
_ => String::new()
|
|
},
|
|
_ => String::new()
|
|
};
|
|
|
|
|
|
if !(p1name.is_empty() || p2name.is_empty()){
|
|
|
|
let mat = Match { full_round_test : String::from(set.full_round_text.unwrap()) ,
|
|
player1: String::from(p1name),
|
|
player2: String::from(p2name),
|
|
round: set.round.unwrap(),
|
|
station: String::from("LOL")
|
|
};
|
|
|
|
current_queue.matches.push(mat);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
queues.push(current_queue)
|
|
}
|
|
return queues;
|
|
}
|
|
|