1#[macro_use] extern crate prettytable;
2
3pub mod skill;
4pub mod hiscore;
5pub mod gamemode;
6
7use skill::Skill;
8use hiscore::Hiscore;
9use gamemode::Gamemode;
10
11
12const HISCORE_URL: &str = "https://secure.runescape.com/m=hiscore_oldschool";
13const STATS_URL: &str = "/index_lite.ws?player=";
14
15pub fn get_hiscore(username: String, gamemode: Gamemode) -> Option<Hiscore> {
25 let url_prefix = match gamemode.clone() {
26 Gamemode::Main => HISCORE_URL.to_string(),
27 _ => {
28 let mut hiscore_string: String = HISCORE_URL.to_string();
29 hiscore_string.push_str("_");
30 hiscore_string.push_str(gamemode.into());
31 hiscore_string
32 }
33 };
34
35 let url = &[url_prefix, STATS_URL.to_string(), username].concat();
36
37 let response = ureq::get(url).call();
38
39 if response.ok() {
40 let response_string = response.into_string().unwrap();
42 let mut reader = csv::ReaderBuilder::new().has_headers(false).from_reader(response_string.as_bytes());
44 let mut hiscores: [Skill; 24] = [Skill::default(); 24];
45 reader.deserialize().take(24).enumerate().for_each(|(index, skill)| {
46 let skill: Skill = skill.unwrap();
47 hiscores[index] = skill;
48 });
49
50 Some(Hiscore {
51 overall: hiscores[0],
52 attack: hiscores[1],
53 defence: hiscores[2],
54 strength: hiscores[3],
55 hitpoints: hiscores[4],
56 ranged: hiscores[5],
57 prayer: hiscores[6],
58 magic: hiscores[7],
59 cooking: hiscores[8],
60 woodcutting: hiscores[9],
61 fletching: hiscores[10],
62 fishing: hiscores[11],
63 firemaking: hiscores[12],
64 crafting: hiscores[13],
65 smithing: hiscores[14],
66 mining: hiscores[15],
67 herblore: hiscores[16],
68 agility: hiscores[17],
69 thieving: hiscores[18],
70 slayer: hiscores[19],
71 farming: hiscores[20],
72 runecraft: hiscores[21],
73 hunter: hiscores[22],
74 construction: hiscores[23]
75 })
76 } else {
77 None
79 }
80}
81
82#[cfg(test)]
83mod tests {
84 use super::*;
85
86 #[test]
87 fn test_get_hiscore_lynx_titan() {
88 assert_eq!(
89 get_hiscore("Lynx Titan".to_string(), Gamemode::Main).unwrap(),
90 Hiscore {
91 overall: (1, 2277, 4_600_000_000f64).into(),
92 attack: (15, 99, 200_000_000f64).into(),
93 defence: (28, 99, 200_000_000f64).into(),
94 strength: (18, 99, 200_000_000f64).into(),
95 hitpoints: (7, 99, 200_000_000f64).into(),
96 ranged: (8, 99, 200_000_000f64).into(),
97 prayer: (11, 99, 200_000_000f64).into(),
98 magic: (32, 99, 200_000_000f64).into(),
99 cooking: (161, 99, 200_000_000f64).into(),
100 woodcutting: (15, 99, 200_000_000f64).into(),
101 fletching: (12, 99, 200_000_000f64).into(),
102 fishing: (9, 99, 200_000_000f64).into(),
103 firemaking: (49, 99, 200_000_000f64).into(),
104 crafting: (4, 99, 200_000_000f64).into(),
105 smithing: (3, 99, 200_000_000f64).into(),
106 mining: (25, 99, 200_000_000f64).into(),
107 herblore: (5, 99, 200_000_000f64).into(),
108 agility: (24, 99, 200_000_000f64).into(),
109 thieving: (12, 99, 200_000_000f64).into(),
110 slayer: (2, 99, 200_000_000f64).into(),
111 farming: (19, 99, 200_000_000f64).into(),
112 runecraft: (7, 99, 200_000_000f64).into(),
113 hunter: (4, 99, 200_000_000f64).into(),
114 construction: (4, 99, 200_000_000f64).into()
115 }
116 );
117
118 assert_eq!(get_hiscore("Lynx Titan".to_string(), Gamemode::Main).unwrap().agility, (24, 99, 200_000_000f64).into())
119 }
120}