-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playtime.rs
101 lines (87 loc) · 3.06 KB
/
playtime.rs
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
use std::time::Duration;
use azalea::{
app::{App, Plugin, Update},
ecs::prelude::*,
};
use serde::Deserialize;
use crate::prelude::*;
/// View players play time. <https://2b2t.vc>
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlaytimeCommandPlugin;
impl ChatCmd for PlaytimeCommandPlugin {
fn aliases(&self) -> Vec<&'static str> {
vec!["playtime"]
}
}
impl Plugin for PlaytimeCommandPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
Self::handle_playtime_command_events
.ambiguous_with_all()
.before(DiscordChatPlugin::handle_whisper_events)
.before(MinecraftChatPlugin::handle_whisper_events)
.after(MinecraftChatPlugin::handle_chat_received_events),
);
}
}
impl PlaytimeCommandPlugin {
pub fn handle_playtime_command_events(
mut command_events: EventReader<CommandEvent>,
mut whisper_events: EventWriter<WhisperEvent>,
) {
if let Some(event) = command_events.read().next() {
let ChatCmds::Playtime(_plugin) = event.command else {
return;
};
let mut whisper_event = WhisperEvent {
entity: event.entity,
source: event.source,
sender: event.sender,
content: String::new(),
};
let Some(player_name) = event.args.iter().next() else {
whisper_event.content = str!("[400] Missing player name");
whisper_events.send(whisper_event);
return;
};
let response = match ureq::get("https://api.2b2t.vc/playtime")
.query("playerName", player_name)
.timeout(Duration::from_secs(25))
.call()
{
Ok(response) => response,
Err(error) => {
whisper_event.content = format!("[404] Player not found: {player_name}");
whisper_events.send(whisper_event);
error!("{error}");
return;
}
};
if response.status() == 204 {
whisper_event.content = str!("[204] Invalid player?");
whisper_events.send(whisper_event);
return;
}
let Ok(json) = response.into_json::<Json>() else {
whisper_event.content = str!("[500] Failed to parse JSON");
whisper_events.send(whisper_event);
return;
};
let duration = Duration::new(json.playtime_seconds, 0);
whisper_event.content = format!(
"{:02}:{:02}:{:02}",
duration.as_secs() / 3600 % 24,
duration.as_secs() / 60 % 60,
duration.as_secs() % 60
);
whisper_events.send(whisper_event);
}
command_events.clear();
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Json {
playtime_seconds: u64,
}