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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use serde::de::DeserializeOwned;
use std::{fmt, sync::Arc};
use crate::protocol::{NetworkItemFlags, NetworkPrint, NetworkText};
use crate::{Client, Error, Item, LocatedItem, Location, Player};
pub use crate::protocol::TextColor;
/// A rich-text message sent by the server, with annotations indicating how the
/// text should be formatted and what individual components refer to.
#[derive(Debug, Clone)]
pub enum Print {
/// A player received an item.
ItemSend {
data: Vec<RichText>,
item: LocatedItem,
},
/// A player used the `!getitem` command.
ItemCheat {
data: Vec<RichText>,
item: LocatedItem,
},
/// A player hinted.
Hint {
data: Vec<RichText>,
item: LocatedItem,
found: bool,
},
/// A player connected.
Join {
data: Vec<RichText>,
player: Arc<Player>,
tags: Vec<String>,
},
/// A player disconnected.
Part {
data: Vec<RichText>,
player: Arc<Player>,
},
/// A player sent a chat message.
Chat {
data: Vec<RichText>,
player: Arc<Player>,
message: String,
},
/// The server broadcasted a message.
ServerChat {
data: Vec<RichText>,
message: String,
},
/// The client has triggered a tutorial message, such as when first
/// connecting.
Tutorial { data: Vec<RichText> },
/// A player changed their tags.
TagsChanged {
data: Vec<RichText>,
player: Arc<Player>,
tags: Vec<String>,
},
/// Someone (usually the client) entered an `!` command.
CommandResult { data: Vec<RichText> },
/// The client entered an `!admin` command.
AdminCommandResult { data: Vec<RichText> },
/// A player reached their goal.
Goal {
data: Vec<RichText>,
player: Arc<Player>,
},
/// A player released the remaining items in their world.
Release {
data: Vec<RichText>,
player: Arc<Player>,
},
/// A player collected the remaining items for their world.
Collect {
data: Vec<RichText>,
player: Arc<Player>,
},
/// The current server countdown has progressed.
Countdown { data: Vec<RichText>, countdown: u64 },
/// A catch-call for unrecognized message types, plain-text messages, and
/// synthetic messages generated by [Print::message].
Unknown { data: Vec<RichText> },
}
impl Print {
/// Create this from a [NetworkPrint] using information from the client.
pub(crate) fn hydrate<S: DeserializeOwned>(
network: NetworkPrint,
client: &Client<S>,
) -> Result<Self, Error> {
Ok(match network {
NetworkPrint::ItemSend {
data,
receiving,
item,
} => {
let sender = client.teammate_arc(item.player)?;
Print::ItemSend {
data: RichText::hydrate_vec(data, client)?,
item: LocatedItem::hydrate(
item,
sender,
client.teammate_arc(receiving)?,
client,
)?,
}
}
NetworkPrint::ItemCheat {
data,
receiving,
item,
team,
} => {
let sender = client.player_arc(team, item.player)?;
Print::ItemCheat {
data: RichText::hydrate_vec(data, client)?,
item: LocatedItem::hydrate(
item,
sender,
client.player_arc(team, receiving)?,
client,
)?,
}
}
NetworkPrint::Hint {
data,
receiving,
item,
found,
} => {
let sender = client.teammate_arc(item.player)?;
Print::Hint {
data: RichText::hydrate_vec(data, client)?,
item: LocatedItem::hydrate(
item,
sender,
client.teammate_arc(receiving)?,
client,
)?,
found,
}
}
NetworkPrint::Join {
data,
team,
slot,
tags,
} => Print::Join {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
tags,
},
NetworkPrint::Part { data, team, slot } => Print::Part {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
},
NetworkPrint::Chat {
data,
team,
slot,
message,
} => Print::Chat {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
message,
},
NetworkPrint::ServerChat { data, message } => Print::ServerChat {
data: RichText::hydrate_vec(data, client)?,
message,
},
NetworkPrint::Tutorial { data } => Print::Tutorial {
data: RichText::hydrate_vec(data, client)?,
},
NetworkPrint::TagsChanged {
data,
team,
slot,
tags,
} => Print::TagsChanged {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
tags,
},
NetworkPrint::CommandResult { data } => Print::CommandResult {
data: RichText::hydrate_vec(data, client)?,
},
NetworkPrint::AdminCommandResult { data } => Print::AdminCommandResult {
data: RichText::hydrate_vec(data, client)?,
},
NetworkPrint::Goal { data, team, slot } => Print::Goal {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
},
NetworkPrint::Release { data, team, slot } => Print::Release {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
},
NetworkPrint::Collect { data, team, slot } => Print::Collect {
data: RichText::hydrate_vec(data, client)?,
player: client.player_arc(team, slot)?,
},
NetworkPrint::Countdown { data, countdown } => Print::Countdown {
data: RichText::hydrate_vec(data, client)?,
countdown,
},
NetworkPrint::Unknown { data } => Print::Unknown {
data: RichText::hydrate_vec(data, client)?,
},
})
}
/// A utility method that returns a message of an unknown type that just
/// contains the given unformatted `text`.
pub fn message(text: String) -> Print {
text.into()
}
/// Returns the data field for any Print.
pub fn data(&self) -> &[RichText] {
use Print::*;
match self {
ItemSend { data, .. } => data,
ItemCheat { data, .. } => data,
Hint { data, .. } => data,
Join { data, .. } => data,
Part { data, .. } => data,
Chat { data, .. } => data,
ServerChat { data, .. } => data,
Tutorial { data, .. } => data,
TagsChanged { data, .. } => data,
CommandResult { data, .. } => data,
AdminCommandResult { data, .. } => data,
Goal { data, .. } => data,
Release { data, .. } => data,
Collect { data, .. } => data,
Countdown { data, .. } => data,
Unknown { data, .. } => data,
}
}
}
impl From<String> for Print {
fn from(value: String) -> Print {
vec![value.into()].into()
}
}
impl From<&str> for Print {
fn from(value: &str) -> Print {
value.to_string().into()
}
}
impl From<RichText> for Print {
fn from(value: RichText) -> Print {
vec![value].into()
}
}
impl From<Vec<RichText>> for Print {
fn from(data: Vec<RichText>) -> Print {
Print::Unknown { data }
}
}
impl fmt::Display for Print {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
for part in self.data() {
part.fmt(f)?;
}
Ok(())
}
}
/// A single text component of a [Print], with additional metadata indicating
/// its formatting and semantics.
///
/// Unlike [RichText], this has not yet been hydrated with additional metadata
/// known by the client.
#[derive(Debug, Clone)]
pub enum RichText {
/// A reference to a player.
Player(Arc<Player>),
/// A reference to a player's name. This is used for messages that should be
/// rendered like player names but which don't correspond to any actual
/// [Player] objects in the game.
PlayerName(String),
/// A reference to an item for a particular player.
Item {
item: Item,
player: Arc<Player>,
progression: bool,
useful: bool,
trap: bool,
},
/// A reference to a location in a particular player's game.
Location {
location: Location,
player: Arc<Player>,
},
/// A reference to an entrance in a game.
EntranceName(String),
/// Text with a particular specified color.
Color { text: String, color: TextColor },
// We don't explicitly provide variants for `ItemName` or `LocationName`
// because they aren't ever actually sent by the server. If that changes,
// this will fall back to using [Text] for them until we add explicit
// support.
/// Plain text. This is also used as the fallback if the server sends an
/// unrecognized message type. (If that happens, please file an issue so we
/// can add support!)
Text(String),
}
impl RichText {
/// Converts [NetworkText]s in [vec] to [RichText]s.
fn hydrate_vec<S: DeserializeOwned>(
vec: Vec<NetworkText>,
client: &Client<S>,
) -> Result<Vec<RichText>, Error> {
vec.into_iter()
.map(|rt| RichText::hydrate(rt, client))
.collect()
}
/// Creates this from a [NetworkText] using information from the client.
fn hydrate<S: DeserializeOwned>(
network: NetworkText,
client: &Client<S>,
) -> Result<RichText, Error> {
Ok(match network {
NetworkText::PlayerId { id } => RichText::Player(client.teammate_arc(id)?),
NetworkText::PlayerName { text } => RichText::PlayerName(text),
NetworkText::ItemId { id, player, flags } => {
let player = client.teammate_arc(player)?;
RichText::Item {
item: client.game_or_err(player.game())?.item_or_err(id)?,
player,
progression: flags.contains(NetworkItemFlags::PROGRESSION),
useful: flags.contains(NetworkItemFlags::USEFUL),
trap: flags.contains(NetworkItemFlags::TRAP),
}
}
NetworkText::LocationId { id, player } => {
let player = client.teammate_arc(player)?;
RichText::Location {
location: client.game_or_err(player.game())?.location_or_err(id)?,
player,
}
}
NetworkText::EntranceName { text } => RichText::EntranceName(text),
NetworkText::Color { text, color } => RichText::Color { text, color },
NetworkText::Text { text } => RichText::Text(text),
})
}
}
impl From<String> for RichText {
fn from(value: String) -> RichText {
RichText::Text(value)
}
}
impl From<&str> for RichText {
fn from(value: &str) -> RichText {
value.to_string().into()
}
}
impl fmt::Display for RichText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
use RichText::*;
match self {
Player(player) => player.fmt(f),
Item { item, .. } => item.fmt(f),
Location { location, .. } => location.fmt(f),
PlayerName(text) | EntranceName(text) | Color { text, .. } | Text(text) => text.fmt(f),
}
}
}