1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
extern crate reqwest;
use reqwest::blocking::get;

extern crate serde;
extern crate serde_json;
use serde_json::Value;
#[macro_use]
extern crate serde_derive;

#[macro_use]
extern crate failure;
use failure::Error;

pub fn player(name: &str) -> Result<Player, Error> {
	let resp = get(&format!("https://api.wynncraft.com/v2/player/{}/stats", name))?
		.error_for_status()?
		.text().unwrap();

	let x: Value = serde_json::from_str(&resp)?;
	//println!("{:?}", x);
	Ok(serde_json::from_value(x["data"][0].clone())?)
}

pub fn guild(name: &str) -> Result<Option<Guild>, Error> {
	let resp = get(&format!("https://api.wynncraft.com/public_api.php?action=guildStats&command={}", name))?
		.error_for_status()?
		.text().unwrap();

	if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
		bail!("API error ({})", error.error);
	} else {
		Ok(serde_json::from_str(&resp)?)
	}
}

pub fn guild_list() -> Result<Vec<String>, Error> {
	let resp = get("https://api.wynncraft.com/public_api.php?action=guildList")?
		.error_for_status()?
		.text().unwrap();

	if let Ok(error) = serde_json::from_str::<APIError>(&resp) {
		bail!("API error ({})", error.error);
	} else {
		let list: GuildList = serde_json::from_str(&resp)?;
		Ok(list.guilds)
	}
}

pub fn guild_leaderboard() -> Result<Top100Guilds, Error> {
	let resp = get("https://api.wynncraft.com/public_api.php?action=statsLeaderboard&type=guild&timeframe=alltime")?.error_for_status()?.text()?;

	if let Ok(error) = serde_json::from_str::<APIError2>(&resp) {
		bail!("API error ({})", error.message);
	} else {
		let list: Top100Guilds = serde_json::from_str(&resp)?;
		Ok(list)
	}
}

#[derive(Deserialize)]
pub struct APIError {
	pub error: String
}

#[derive(Deserialize)]
pub struct APIError2 {
	pub code: u64,
	pub message: String
}

#[derive(Deserialize)]
pub struct GuildList {
	pub guilds: Vec<String>
}

#[derive(Deserialize)]
pub struct Top100Guilds {
	pub data: Vec<GuildEntry>
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GuildEntry {
	pub name: String,
	pub prefix: String,
  	pub xp: u64,
  	pub level: u64,
  	pub created: String,
  	pub territories: u64,
	pub members_count: u64,
	pub num: u64
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Guild {
	pub name: String,
	pub prefix: String,
  	pub members: Vec<Member>,
  	pub xp: f64,
  	pub level: u64,
  	pub created: String,
  	pub created_friendly: String,
  	pub territories: u64
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Member {
	pub name: String,
	pub rank: Option<Rank>,
	pub contributed: u64,
	pub joined: String,
	pub joined_friendly: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Rank {
	Owner,
	Chief,
	Captain,
	Recruiter,
	Recruit
}

#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
pub struct Player {
	pub username: String,
	pub uuid: String,
	pub rank: String,
	pub meta: PlayerMetadata,
	pub classes: Vec<Class>,
	pub guild: PlayerGuildInfo,
	pub global: Value,
	pub ranking: Value
}

#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
pub struct PlayerMetadata {
	pub firstJoin: String,
	pub lastJoin: String,
	pub location: Value,
	pub playtime: u64,
	pub tag: Value,
	pub veteran: bool
}

#[derive(Debug, Deserialize)]
pub struct GlobalPlayerInfo {
	pub items_identified: u64,
	pub mobs_killed: u64,
	pub pvp_kills: u64,
	pub pvp_deaths: u64,
	pub chests_found: u64,
	pub blocks_walked: u64,
	pub logins: u64,
	pub deaths: u64,
	pub total_level: u64
}

#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
pub struct Class {
	pub name: String,
	pub level: u64,
	pub dungeons: Value,
	pub quests: Value,
	pub itemsIdentified: u64,
	pub mobsKilled: u64,
	pub pvp: Value,
	pub chestsFound: u64,
	pub blocksWalked: u64,
	pub logins: u64,
	pub deaths: u64,
	pub playtime: u64,
	pub gamemode: Value,
	pub skills: Value,
	pub professions: Value,
	pub discoveries: u64,
	pub eventsWon: u64,
	pub preEconomyUpdate: bool
}

#[derive(Debug, Deserialize)]
pub struct WFPlayerInfo {
	pub kills: u64,
	pub deaths: u64,
	pub wins: u64,
	pub losses: u64
}

#[derive(Debug, Deserialize)]
pub struct PlayerRankings {
	pub pvp: Option<u64>,
	pub player: Option<u64>,
	pub guild: Option<u64>
}

#[derive(Debug, Deserialize)]
pub struct PlayerGuildInfo {
	pub name: Option<String>,
	pub rank: Option<String>
}

#[cfg(test)]
mod tests {
	#[test]
	fn player() {
		::player("dukioooo").unwrap();
		::player("FallendeWurst").unwrap();
		::player("lmays11").unwrap();
	}

	#[test]
	fn guild() {
		::guild("HackForums").unwrap();
	}

	#[test]
	fn guild_leaderboard() {
		::guild_leaderboard().unwrap();
	}

	#[test]
	fn guild_list() {
		::guild_list().unwrap();
	}
}