fix: kb datasource
This commit is contained in:
parent
b35c9c147c
commit
fb766738bc
1 changed files with 83 additions and 66 deletions
|
@ -3,6 +3,7 @@ use crate::ChampInfo;
|
|||
use crate::Champion as ChampionLoL;
|
||||
use crate::USER_AGENT_VALUE;
|
||||
use indexmap::IndexMap;
|
||||
use log::error;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::time::Duration;
|
||||
|
@ -46,38 +47,61 @@ struct Position {
|
|||
|
||||
#[derive(Deserialize)]
|
||||
struct BuildResponse {
|
||||
builds2: Vec<KBBuild>,
|
||||
builds3: Vec<KBBuild>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct KBBuild {
|
||||
position: String,
|
||||
position: PositionResponse,
|
||||
#[serde(rename = "itemSets")]
|
||||
item_sets: Vec<ItemSets>,
|
||||
#[serde(rename = "strItemSets")]
|
||||
str_item_sets: Vec<StrItemSets>,
|
||||
#[serde(rename = "skillSets")]
|
||||
skill_sets: Vec<SkillSet>,
|
||||
wins: u32,
|
||||
games: u32,
|
||||
kda: f32,
|
||||
summoner: Summoner,
|
||||
patch: Patch,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct PositionResponse {
|
||||
#[serde(rename = "positionName")]
|
||||
position_name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SkillSet {
|
||||
#[serde(rename = "skillOrder")]
|
||||
skill_order: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ItemSets {
|
||||
item0: KBItem,
|
||||
item1: KBItem,
|
||||
item2: KBItem,
|
||||
item3: KBItem,
|
||||
item4: KBItem,
|
||||
item5: KBItem,
|
||||
item6: KBItem,
|
||||
#[serde(rename = "startItem0")]
|
||||
start_item0: KBItem,
|
||||
#[serde(rename = "startItem1")]
|
||||
start_item1: KBItem,
|
||||
#[serde(rename = "startItem2")]
|
||||
start_item2: KBItem,
|
||||
#[serde(rename = "startItem3")]
|
||||
start_item3: KBItem,
|
||||
#[serde(rename = "startItem4")]
|
||||
start_item4: KBItem,
|
||||
#[serde(rename = "startItem5")]
|
||||
start_item5: KBItem,
|
||||
#[serde(rename = "skillOrder")]
|
||||
skill_order: String,
|
||||
wins: u32,
|
||||
games: u32,
|
||||
kda: f32,
|
||||
summoner: Summoner,
|
||||
patch: Patch,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct StrItemSets {
|
||||
#[serde(rename = "strItem0")]
|
||||
str_item0: KBItem,
|
||||
#[serde(rename = "strItem1")]
|
||||
str_item1: KBItem,
|
||||
#[serde(rename = "strItem2")]
|
||||
str_item2: KBItem,
|
||||
#[serde(rename = "strItem3")]
|
||||
str_item3: KBItem,
|
||||
#[serde(rename = "strItem4")]
|
||||
str_item4: KBItem,
|
||||
#[serde(rename = "strItem5")]
|
||||
str_item5: KBItem,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -105,15 +129,9 @@ fn get_auth_token() -> Option<String> {
|
|||
},
|
||||
Err(_) => return None,
|
||||
};
|
||||
let auth_position = match bundle.find("Authorization") {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
let auth_position = bundle.find("Authorization")?;
|
||||
bundle = bundle[(auth_position + 13)..].to_string();
|
||||
let q_position = match bundle.find('"') {
|
||||
Some(position) => position,
|
||||
None => return None,
|
||||
};
|
||||
let q_position = bundle.find('"')?;
|
||||
bundle = bundle[(q_position + 1)..].to_string();
|
||||
bundle
|
||||
.find('"')
|
||||
|
@ -177,86 +195,83 @@ impl KBDataSource {
|
|||
fn get_build(&self, build: &KBBuild) -> (String, Vec<Value>, Stat) {
|
||||
let mut starting_items: Vec<Item> = vec![];
|
||||
let mut blocks = vec![];
|
||||
if build.start_item0.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item0.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item0.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item0.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.start_item1.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item1.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item1.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item1.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.start_item2.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item2.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item2.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item2.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.start_item3.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item3.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item3.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item3.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.start_item4.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item4.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item4.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item4.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.start_item5.item_id != 0 {
|
||||
if build.str_item_sets[0].str_item5.item_id != 0 {
|
||||
starting_items.push(Item {
|
||||
id: build.start_item5.item_id.to_string(),
|
||||
id: build.str_item_sets[0].str_item5.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
blocks.push(json!(Build {
|
||||
type_: format!("Early game items | skillOrder : {}", build.skill_order),
|
||||
type_: format!(
|
||||
"Early game items | skillOrder : {}",
|
||||
build.skill_sets[0].skill_order
|
||||
),
|
||||
items: starting_items
|
||||
}));
|
||||
let mut final_items: Vec<Item> = vec![];
|
||||
if build.item0.item_id != 0 {
|
||||
if build.item_sets[0].item0.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item0.item_id.to_string(),
|
||||
id: build.item_sets[0].item0.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item1.item_id != 0 {
|
||||
if build.item_sets[0].item1.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item1.item_id.to_string(),
|
||||
id: build.item_sets[0].item1.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item2.item_id != 0 {
|
||||
if build.item_sets[0].item2.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item2.item_id.to_string(),
|
||||
id: build.item_sets[0].item2.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item3.item_id != 0 {
|
||||
if build.item_sets[0].item3.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item3.item_id.to_string(),
|
||||
id: build.item_sets[0].item3.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item4.item_id != 0 {
|
||||
if build.item_sets[0].item4.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item4.item_id.to_string(),
|
||||
id: build.item_sets[0].item4.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item5.item_id != 0 {
|
||||
if build.item_sets[0].item5.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item5.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
if build.item6.item_id != 0 {
|
||||
final_items.push(Item {
|
||||
id: build.item6.item_id.to_string(),
|
||||
id: build.item_sets[0].item5.item_id.to_string(),
|
||||
count: 1,
|
||||
})
|
||||
}
|
||||
|
@ -269,7 +284,7 @@ impl KBDataSource {
|
|||
}));
|
||||
|
||||
(
|
||||
build.position.to_owned().to_uppercase(),
|
||||
build.position.position_name.to_uppercase(),
|
||||
blocks,
|
||||
Stat {
|
||||
win_rate: (build.wins as f32 / build.games as f32) * 100f32,
|
||||
|
@ -327,18 +342,20 @@ impl DataSource for KBDataSource {
|
|||
{
|
||||
Ok(resp) => match resp.into_json() {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
Err(x) => {
|
||||
error!("Cant json: {}", x);
|
||||
return vec![];
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
Err(x) => {
|
||||
error!("Call failed: {}", x);
|
||||
return vec![];
|
||||
}
|
||||
};
|
||||
|
||||
for pos in position {
|
||||
for b in &data.builds2 {
|
||||
if b.position.to_uppercase() == *pos {
|
||||
for b in &data.builds3 {
|
||||
if b.position.position_name.to_uppercase() == *pos {
|
||||
champ_data.push(self.get_build(b));
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue