fix: kb datasource

This commit is contained in:
nyyu 2025-05-31 18:53:21 +02:00
parent b35c9c147c
commit fb766738bc

View file

@ -3,6 +3,7 @@ use crate::ChampInfo;
use crate::Champion as ChampionLoL; use crate::Champion as ChampionLoL;
use crate::USER_AGENT_VALUE; use crate::USER_AGENT_VALUE;
use indexmap::IndexMap; use indexmap::IndexMap;
use log::error;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use std::time::Duration; use std::time::Duration;
@ -46,38 +47,61 @@ struct Position {
#[derive(Deserialize)] #[derive(Deserialize)]
struct BuildResponse { struct BuildResponse {
builds2: Vec<KBBuild>, builds3: Vec<KBBuild>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
struct KBBuild { 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, item0: KBItem,
item1: KBItem, item1: KBItem,
item2: KBItem, item2: KBItem,
item3: KBItem, item3: KBItem,
item4: KBItem, item4: KBItem,
item5: KBItem, item5: KBItem,
item6: KBItem, }
#[serde(rename = "startItem0")]
start_item0: KBItem, #[derive(Deserialize)]
#[serde(rename = "startItem1")] struct StrItemSets {
start_item1: KBItem, #[serde(rename = "strItem0")]
#[serde(rename = "startItem2")] str_item0: KBItem,
start_item2: KBItem, #[serde(rename = "strItem1")]
#[serde(rename = "startItem3")] str_item1: KBItem,
start_item3: KBItem, #[serde(rename = "strItem2")]
#[serde(rename = "startItem4")] str_item2: KBItem,
start_item4: KBItem, #[serde(rename = "strItem3")]
#[serde(rename = "startItem5")] str_item3: KBItem,
start_item5: KBItem, #[serde(rename = "strItem4")]
#[serde(rename = "skillOrder")] str_item4: KBItem,
skill_order: String, #[serde(rename = "strItem5")]
wins: u32, str_item5: KBItem,
games: u32,
kda: f32,
summoner: Summoner,
patch: Patch,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -105,15 +129,9 @@ fn get_auth_token() -> Option<String> {
}, },
Err(_) => return None, Err(_) => return None,
}; };
let auth_position = match bundle.find("Authorization") { let auth_position = bundle.find("Authorization")?;
Some(position) => position,
None => return None,
};
bundle = bundle[(auth_position + 13)..].to_string(); bundle = bundle[(auth_position + 13)..].to_string();
let q_position = match bundle.find('"') { let q_position = bundle.find('"')?;
Some(position) => position,
None => return None,
};
bundle = bundle[(q_position + 1)..].to_string(); bundle = bundle[(q_position + 1)..].to_string();
bundle bundle
.find('"') .find('"')
@ -177,86 +195,83 @@ impl KBDataSource {
fn get_build(&self, build: &KBBuild) -> (String, Vec<Value>, Stat) { fn get_build(&self, build: &KBBuild) -> (String, Vec<Value>, Stat) {
let mut starting_items: Vec<Item> = vec![]; let mut starting_items: Vec<Item> = vec![];
let mut blocks = 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 { 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, count: 1,
}) })
} }
if build.start_item1.item_id != 0 { if build.str_item_sets[0].str_item1.item_id != 0 {
starting_items.push(Item { 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, count: 1,
}) })
} }
if build.start_item2.item_id != 0 { if build.str_item_sets[0].str_item2.item_id != 0 {
starting_items.push(Item { 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, count: 1,
}) })
} }
if build.start_item3.item_id != 0 { if build.str_item_sets[0].str_item3.item_id != 0 {
starting_items.push(Item { 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, count: 1,
}) })
} }
if build.start_item4.item_id != 0 { if build.str_item_sets[0].str_item4.item_id != 0 {
starting_items.push(Item { 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, count: 1,
}) })
} }
if build.start_item5.item_id != 0 { if build.str_item_sets[0].str_item5.item_id != 0 {
starting_items.push(Item { 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, count: 1,
}) })
} }
blocks.push(json!(Build { 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 items: starting_items
})); }));
let mut final_items: Vec<Item> = vec![]; 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 { final_items.push(Item {
id: build.item0.item_id.to_string(), id: build.item_sets[0].item0.item_id.to_string(),
count: 1, count: 1,
}) })
} }
if build.item1.item_id != 0 { if build.item_sets[0].item1.item_id != 0 {
final_items.push(Item { final_items.push(Item {
id: build.item1.item_id.to_string(), id: build.item_sets[0].item1.item_id.to_string(),
count: 1, count: 1,
}) })
} }
if build.item2.item_id != 0 { if build.item_sets[0].item2.item_id != 0 {
final_items.push(Item { final_items.push(Item {
id: build.item2.item_id.to_string(), id: build.item_sets[0].item2.item_id.to_string(),
count: 1, count: 1,
}) })
} }
if build.item3.item_id != 0 { if build.item_sets[0].item3.item_id != 0 {
final_items.push(Item { final_items.push(Item {
id: build.item3.item_id.to_string(), id: build.item_sets[0].item3.item_id.to_string(),
count: 1, count: 1,
}) })
} }
if build.item4.item_id != 0 { if build.item_sets[0].item4.item_id != 0 {
final_items.push(Item { final_items.push(Item {
id: build.item4.item_id.to_string(), id: build.item_sets[0].item4.item_id.to_string(),
count: 1, count: 1,
}) })
} }
if build.item5.item_id != 0 { if build.item_sets[0].item5.item_id != 0 {
final_items.push(Item { final_items.push(Item {
id: build.item5.item_id.to_string(), id: build.item_sets[0].item5.item_id.to_string(),
count: 1,
})
}
if build.item6.item_id != 0 {
final_items.push(Item {
id: build.item6.item_id.to_string(),
count: 1, count: 1,
}) })
} }
@ -269,7 +284,7 @@ impl KBDataSource {
})); }));
( (
build.position.to_owned().to_uppercase(), build.position.position_name.to_uppercase(),
blocks, blocks,
Stat { Stat {
win_rate: (build.wins as f32 / build.games as f32) * 100f32, 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(resp) => match resp.into_json() {
Ok(val) => val, Ok(val) => val,
Err(_) => { Err(x) => {
error!("Cant json: {}", x);
return vec![]; return vec![];
} }
}, },
Err(_) => { Err(x) => {
error!("Call failed: {}", x);
return vec![]; return vec![];
} }
}; };
for pos in position { for pos in position {
for b in &data.builds2 { for b in &data.builds3 {
if b.position.to_uppercase() == *pos { if b.position.position_name.to_uppercase() == *pos {
champ_data.push(self.get_build(b)); champ_data.push(self.get_build(b));
break; break;
} }