Skip to content

Commit

Permalink
feat: support retrieving items
Browse files Browse the repository at this point in the history
  • Loading branch information
kade-robertson committed Jan 19, 2023
1 parent 552c27d commit 820fb87
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use url::Url;
#[cfg(test)]
use mockito;

use crate::models::champions::Champions;
use crate::models::{champions::Champions, items::Items};

pub struct DDragonClient {
agent: ureq::Agent,
Expand Down Expand Up @@ -59,6 +59,10 @@ impl DDragonClient {
pub fn champions(&self) -> anyhow::Result<Champions> {
self.get_data::<Champions>("./champion.json")
}

pub fn items(&self) -> anyhow::Result<Items> {
self.get_data::<Items>("./item.json")
}
}

#[cfg(test)]
Expand Down
17 changes: 4 additions & 13 deletions src/models/champions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::shared::Image;

#[derive(Serialize, Deserialize)]
pub struct Champions {
pub format: String,
Expand All @@ -17,23 +19,12 @@ pub struct ChampionData {
pub title: String,
pub blurb: String,
pub info: Info,
pub image: Image,
pub image: Image<ChampionSprite>,
pub tags: Vec<Tag>,
pub partype: String,
pub stats: HashMap<String, f64>,
}

#[derive(Serialize, Deserialize)]
pub struct Image {
pub full: String,
pub sprite: Sprite,
pub group: String,
pub x: i64,
pub y: i64,
pub w: i64,
pub h: i64,
}

#[derive(Serialize, Deserialize)]
pub struct Info {
pub attack: i64,
Expand All @@ -43,7 +34,7 @@ pub struct Info {
}

#[derive(Serialize, Deserialize)]
pub enum Sprite {
pub enum ChampionSprite {
#[serde(rename = "champion0.png")]
Champion0,
#[serde(rename = "champion1.png")]
Expand Down
122 changes: 122 additions & 0 deletions src/models/items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::shared::Image;

#[derive(Serialize, Deserialize)]
pub struct Items {
pub version: String,
pub data: HashMap<String, Item>,
pub groups: Vec<Group>,
pub tree: Vec<Tree>,
}

#[derive(Serialize, Deserialize)]
pub struct Gold {
pub base: i64,
pub total: i64,
pub sell: i64,
pub purchasable: bool,
}

#[derive(Serialize, Deserialize)]
pub struct Rune {
pub isrune: bool,
pub tier: i64,
#[serde(rename = "type")]
pub rune_type: String,
}

#[derive(Serialize, Deserialize)]
pub struct Item {
pub name: String,
pub description: String,
pub colloq: String,
pub plaintext: String,
pub into: Option<Vec<String>>,
pub image: Image<ItemSprite>,
pub gold: Gold,
pub tags: Vec<String>,
pub maps: HashMap<String, bool>,
pub stats: HashMap<String, f64>,
#[serde(rename = "inStore")]
pub in_store: Option<bool>,
pub from: Option<Vec<String>>,
pub effect: Option<Effect>,
pub depth: Option<i64>,
pub consumed: Option<bool>,
pub stacks: Option<i64>,
#[serde(rename = "hideFromAll")]
pub hide_from_all: Option<bool>,
#[serde(rename = "consumeOnFull")]
pub consume_on_full: Option<bool>,
#[serde(rename = "requiredChampion")]
pub required_champion: Option<String>,
#[serde(rename = "requiredAlly")]
pub required_ally: Option<String>,
#[serde(rename = "specialRecipe")]
pub special_recipe: Option<i64>,
}

#[derive(Serialize, Deserialize)]
pub struct Effect {
#[serde(rename = "Effect1Amount")]
pub effect1_amount: String,
#[serde(rename = "Effect2Amount")]
pub effect2_amount: Option<String>,
#[serde(rename = "Effect3Amount")]
pub effect3_amount: Option<String>,
#[serde(rename = "Effect4Amount")]
pub effect4_amount: Option<String>,
#[serde(rename = "Effect5Amount")]
pub effect5_amount: Option<String>,
#[serde(rename = "Effect6Amount")]
pub effect6_amount: Option<String>,
#[serde(rename = "Effect7Amount")]
pub effect7_amount: Option<String>,
#[serde(rename = "Effect8Amount")]
pub effect8_amount: Option<String>,
#[serde(rename = "Effect9Amount")]
pub effect9_amount: Option<String>,
#[serde(rename = "Effect10Amount")]
pub effect10_amount: Option<String>,
#[serde(rename = "Effect11Amount")]
pub effect11_amount: Option<String>,
#[serde(rename = "Effect12Amount")]
pub effect12_amount: Option<String>,
#[serde(rename = "Effect13Amount")]
pub effect13_amount: Option<String>,
#[serde(rename = "Effect14Amount")]
pub effect14_amount: Option<String>,
#[serde(rename = "Effect15Amount")]
pub effect15_amount: Option<String>,
#[serde(rename = "Effect16Amount")]
pub effect16_amount: Option<String>,
#[serde(rename = "Effect17Amount")]
pub effect17_amount: Option<String>,
#[serde(rename = "Effect18Amount")]
pub effect18_amount: Option<String>,
}

#[derive(Serialize, Deserialize)]
pub struct Group {
pub id: String,
#[serde(rename = "MaxGroupOwnable")]
pub max_group_ownable: String,
}

#[derive(Serialize, Deserialize)]
pub struct Tree {
pub header: String,
pub tags: Vec<String>,
}

#[derive(Serialize, Deserialize)]
pub enum ItemSprite {
#[serde(rename = "item0.png")]
Item0,
#[serde(rename = "item1.png")]
Item1,
#[serde(rename = "item2.png")]
Item2,
}
2 changes: 2 additions & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod champions;
pub mod items;
pub mod shared;
12 changes: 12 additions & 0 deletions src/models/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct Image<T> {
pub full: String,
pub sprite: T,
pub group: String,
pub x: i64,
pub y: i64,
pub w: i64,
pub h: i64,
}

0 comments on commit 820fb87

Please sign in to comment.