ms: improve
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nyyu 2021-10-31 10:12:53 +01:00
parent 0982c30f7a
commit d854da599f
4 changed files with 54 additions and 34 deletions

4
Cargo.lock generated
View file

@ -210,9 +210,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.105"
version = "0.2.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013"
checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673"
[[package]]
name = "log"

View file

@ -3,7 +3,7 @@ use crate::Champion;
use indexmap::IndexMap;
use log::{error, info};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{json, Value};
use std::fs;
use std::path::Path;
@ -19,14 +19,14 @@ struct ItemSet {
blocks: Vec<Value>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
pub struct Build {
#[serde(rename = "type")]
pub type_: String,
pub items: Vec<Item>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize)]
pub struct Item {
pub id: String,
pub count: u8,
@ -50,6 +50,13 @@ pub trait DataSource {
champion: &Champion,
) -> IndexMap<u32, Vec<String>>;
fn make_item_set(&self, items: Vec<&str>, label: String) -> Value {
json!({
"items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": label
})
}
fn get_champ_data_with_win_pourcentage(
&self,
champ: &ChampInfo,

View file

@ -1,5 +1,5 @@
use indexmap::IndexMap;
use serde_json::{json, Value};
use serde_json::Value;
use crate::data_source::{DataSource, Stat};
use crate::ChampInfo;
@ -44,7 +44,7 @@ impl DataSource for MSDataSource {
let id = champion.data.get(key).unwrap().key.parse::<u32>().unwrap();
champs.insert(id, vec![role.to_string()]);
champs.insert(id, vec![role.to_uppercase()]);
let next = page[p..].find(CHAMP_PATTERN);
if let Some(n) = next {
@ -65,41 +65,56 @@ impl DataSource for MSDataSource {
) -> Vec<(String, Vec<Value>, Stat)> {
let mut builds = vec![];
let page = client
let rep = client
.get(
format!(
"https://www.metasrc.com/5v5/champion/{}/{}",
champ.id.to_lowercase(),
positions[0]
positions[0].to_lowercase()
)
.as_str(),
)
.call()
.unwrap()
.into_string()
.unwrap();
.call();
if let Ok(p) = rep {
let page = p.into_string().unwrap();
let mut items = vec![];
let mut pos = page.find("Patch ").unwrap();
let patch = &page[pos + 6..pos + 11];
let mut pos: Option<usize> = page.find("/item/");
while let Some(mut p) = pos {
p += 6;
let i = &page[p..p + page[p..].find('.').unwrap()];
pos = page.find("Win Rate:").unwrap();
let win_rate: f64 = (&page[pos + 26..pos + 31]).parse().unwrap();
items.push(i);
pos = page.find("KDA:").unwrap();
let kda: f64 = (&page[pos + 21..pos + 25]).parse().unwrap();
let next = page[p..].find("/item/");
if let Some(n) = next {
pos = Some(p + n);
} else {
pos = None;
let mut items = vec![];
let mut pos: Option<usize> = page.find("/item/");
while let Some(mut p) = pos {
p += 6;
let i = &page[p..p + page[p..].find('.').unwrap()];
items.push(i);
let next = page[p..].find("/item/");
if let Some(n) = next {
pos = Some(p + n);
} else {
pos = None;
}
}
}
builds.push((positions[0].to_owned(), vec![json!({
"items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": "Set"
})], Stat{win_rate:0.0,games:1,kda:0.0,patch:"".to_string()}));
builds.push((
positions[0].to_owned(),
vec![self.make_item_set(items, "Set".to_owned())],
Stat {
win_rate,
games: 1,
kda,
patch: patch.to_owned(),
},
));
}
builds
}

View file

@ -1,6 +1,6 @@
use indexmap::IndexMap;
use serde_derive::Deserialize;
use serde_json::{json, Value};
use serde_json::Value;
use crate::data_source::{DataSource, Stat};
use crate::ChampInfo;
@ -73,7 +73,7 @@ impl DataSource for PBDataSource {
for build in rep {
let pos = build.find("/players").unwrap();
let tmp = &build[pos..];
let name = &tmp[tmp.find(">").unwrap() + 1..tmp.find("<").unwrap()];
let name = &tmp[tmp.find('>').unwrap() + 1..tmp.find('<').unwrap()];
let mut items = vec![];
let mut build = &build[build.find("'items'").unwrap()..];
@ -85,9 +85,7 @@ impl DataSource for PBDataSource {
build = &build[end + 20..];
}
blocks.push(json!({
"items": items.iter().map(|x| json!({"id": x.to_string(), "count": 1})).collect::<Vec<Value>>(),
"type": format!("{}", name)}));
blocks.push(self.make_item_set(items, name.to_string()));
}
builds.push((
positions[0].to_owned(),