Skip to main content

mcsr_ranked_api/game/
mod.rs

1use chrono::{DateTime, Utc, serde::ts_seconds};
2use serde::Deserialize;
3#[cfg(feature = "serialize")]
4use serde::Serialize;
5use serde_repr::{Deserialize_repr, Serialize_repr};
6use uuid::Uuid;
7
8use crate::types::Time;
9use crate::types::{Elo, EloChange, MatchId, Rank, Season};
10use crate::user::UserProfile;
11#[cfg(feature = "variations")]
12use crate::variations::Variation;
13
14pub mod requests;
15#[cfg(test)]
16mod tests;
17pub mod versus;
18
19#[cfg_attr(feature = "serialize", derive(Serialize))]
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
21#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
22pub enum MatchCategory {
23	Any,
24	Custom,
25	High,
26	KillAllBosses,
27	KillWither,
28	KillElderGuardian,
29	AllAdvancements,
30	Half,
31	PoglootQuater,
32	HowDidWeGetHere,
33	HeroOfVillage,
34	Arbalistic,
35	CoverMeInDebris,
36	EnterNether,
37	EnterEnd,
38	AllSwords,
39	AllMinerals,
40	#[serde(rename = "FULL_IA_15_LVL")]
41	FullIa15Lvl,
42	AllWorkstations,
43	FullInv,
44	StackOfLimeWool,
45	AllPortals,
46	AllBlocks,
47	MineAChunk,
48}
49
50#[cfg_attr(feature = "serialize", derive(Serialize))]
51#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
52pub struct MatchSeedInfo {
53	/// Id of the seed (not the seed itself)
54	pub id: Option<Box<str>>,
55	pub overworld: Option<OverworldType>,
56	#[serde(rename = "nether")]
57	pub bastion: Option<BastionType>,
58	/// The heights of the 4 zero-cyclable end towers
59	#[serde(default)]
60	pub end_towers: Option<[u8; 4]>,
61	#[serde(default)]
62	#[cfg(feature = "variations")]
63	pub variations: Box<[Variation]>,
64	#[cfg(not(feature = "variations"))]
65	pub variations: Box<[Box<str>]>,
66}
67impl MatchSeedInfo {
68	/// Id of the seed (not the seed itself)
69	pub fn id(&self) -> Option<&str> {
70		self.id.as_ref().map(AsRef::as_ref)
71	}
72	#[cfg(not(feature = "variations"))]
73	pub fn variations(&self) -> &[Box<str>] {
74		self.variations.as_ref()
75	}
76	#[cfg(feature = "variations")]
77	pub fn variations(&self) -> &[Variation] {
78		self.variations.as_ref()
79	}
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize_repr, Serialize_repr)]
83#[repr(u8)]
84pub enum MatchType {
85	Causal = 1,
86	Ranked = 2,
87	Private = 3,
88	Event = 4,
89}
90
91#[cfg_attr(feature = "serialize", derive(Serialize))]
92#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct MatchOutcome {
95	#[serde(rename = "uuid")]
96	pub winner_uuid: Option<Uuid>,
97	pub time: Time,
98}
99
100/// Match leaderboard ranking
101#[cfg_attr(feature = "serialize", derive(Serialize))]
102#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
103#[serde(rename_all = "camelCase")]
104pub struct MatchRank {
105	pub season: Option<Rank>,
106	pub all_time: Option<Rank>,
107}
108
109/// Match contestant's elo update
110#[cfg_attr(feature = "serialize", derive(Serialize))]
111#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
112#[serde(rename_all = "camelCase")]
113pub struct MatchEloUpdate {
114	#[serde(rename = "uuid")]
115	pub player_uuid: Uuid,
116	pub change: Option<EloChange>,
117	#[serde(rename = "eloRate")]
118	pub elo: Option<Elo>,
119}
120
121/// Seed type (overworld)
122#[cfg_attr(feature = "serialize", derive(Serialize))]
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
124#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
125pub enum OverworldType {
126	Village,
127	BuriedTreasure,
128	Shipwreck,
129	RuinedPortal,
130	DesertTemple,
131}
132
133/// Bastion type
134#[cfg_attr(feature = "serialize", derive(Serialize))]
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
136#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
137pub enum BastionType {
138	Housing,
139	Treasure,
140	Bridge,
141	Stables,
142}
143
144/// Match completion info
145#[cfg_attr(feature = "serialize", derive(Serialize))]
146#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
147#[serde(rename_all = "camelCase")]
148pub struct MatchCompletion {
149	#[serde(rename = "uuid")]
150	pub player_uuid: Uuid,
151	pub time: Time,
152}
153
154/// Match timeline event
155#[cfg_attr(feature = "serialize", derive(Serialize))]
156#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
157#[serde(rename_all = "camelCase")]
158pub struct MatchTimelineEvent {
159	#[serde(rename = "uuid")]
160	pub player_uuid: Uuid,
161	pub time: Time,
162	#[serde(rename = "type")]
163	pub id: Box<str>,
164}
165impl MatchTimelineEvent {
166	pub fn id(&self) -> &str {
167		&self.id
168	}
169}
170
171/// Match info
172#[cfg_attr(feature = "serialize", derive(Serialize))]
173#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
174#[serde(rename_all = "camelCase")]
175pub struct MatchInfo {
176	pub id: MatchId,
177	#[serde(rename = "type")]
178	pub kind: MatchType,
179	pub season: Season,
180	pub category: Option<MatchCategory>,
181	#[serde(with = "ts_seconds")]
182	pub date: DateTime<Utc>,
183	pub players: Box<[UserProfile]>,
184	pub spectators: Box<[UserProfile]>,
185	pub seed: Option<MatchSeedInfo>,
186	pub result: MatchOutcome,
187	pub forfeited: bool,
188	pub decayed: bool,
189	pub rank: MatchRank,
190	#[serde(rename = "changes")]
191	pub elo_updates: Box<[MatchEloUpdate]>,
192}
193impl MatchInfo {
194	/// Users participating in the match
195	pub fn players(&self) -> &[UserProfile] {
196		&self.players
197	}
198	/// Users spectating the match
199	pub fn spectators(&self) -> &[UserProfile] {
200		&self.spectators
201	}
202	// The updates to the participants' ELOs
203	pub fn elo_updates(&self) -> &[MatchEloUpdate] {
204		&self.elo_updates
205	}
206	/// Get the overworld type of the match
207	pub fn overworld_type(&self) -> Option<OverworldType> {
208		self.seed.as_ref().and_then(|s| s.overworld)
209	}
210	/// Get the bastion type of the match
211	pub fn bastion_type(&self) -> Option<BastionType> {
212		self.seed.as_ref().and_then(|s| s.bastion)
213	}
214}
215
216/// Advanced (full) match info
217#[cfg_attr(feature = "serialize", derive(Serialize))]
218#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
219#[serde(rename_all = "camelCase")]
220pub struct AdvancedMatchInfo {
221	/// The base info about the match
222	#[serde(flatten)]
223	pub info: MatchInfo,
224	/// Which players completed the match
225	pub completions: Box<[MatchCompletion]>,
226	#[serde(rename = "timelines")]
227	/// Advancements and other events that happened during the match
228	pub timeline_events: Box<[MatchTimelineEvent]>,
229	#[serde(rename = "replayExist")]
230	pub replay_exists: bool,
231}
232impl AdvancedMatchInfo {
233	/// The completions info of the match
234	pub fn completions(&self) -> &[MatchCompletion] {
235		&self.completions
236	}
237	/// The events (achievements) timeline
238	pub fn timeline_events(&self) -> &[MatchTimelineEvent] {
239		&self.timeline_events
240	}
241}