archipelago_rs 2.1.1

A Rust client for the archipelago.gg multiworld randomizer
Documentation
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use std::{collections::HashMap, fmt::Display, mem, time::SystemTime};

use bitflags::bitflags;
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
use serde_json::Value;
use serde_repr::{Deserialize_repr, Serialize_repr};
use serde_with::{DisplayFromStr, TimestampSeconds, serde_as};
use ustr::{Ustr, UstrMap, UstrSet};

mod bounce;
mod print;

pub(crate) use bounce::*;
pub use print::TextColor;
pub(crate) use print::*;

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "cmd")]
pub(crate) enum ClientMessage {
    Connect(Connect),
    ConnectUpdate(ConnectUpdate),
    Sync,
    LocationChecks(LocationChecks),
    LocationScouts(LocationScouts),
    CreateHints(CreateHints),
    UpdateHint(UpdateHint),
    StatusUpdate(StatusUpdate),
    Say(Say),
    GetDataPackage(GetDataPackage),
    Bounce(Bounce),
    Get(Get),
    Set(Set),
    SetNotify(SetNotify),
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "cmd")]
pub(crate) enum ServerMessage<S: DeserializeOwned + 'static> {
    RoomInfo(RoomInfo),
    ConnectionRefused(ConnectionRefused),
    #[serde(deserialize_with = "deserialize_connected_with_optional_slot_data")]
    Connected(Connected<S>),
    ReceivedItems(ReceivedItems),
    LocationInfo(LocationInfo),
    RoomUpdate(RoomUpdate),
    #[serde(rename = "Print")]
    PlainPrint(PlainPrint),
    #[serde(rename = "PrintJSON")]
    RawPrint(NetworkPrint),
    DataPackage(DataPackage),
    Bounced(Bounced),
    InvalidPacket(InvalidPacket),
    Retrieved(Retrieved),
    SetReply(SetReply),
}

impl<S: DeserializeOwned + 'static> ServerMessage<S> {
    /// Returns the name of this message's type.
    pub(crate) fn type_name(&self) -> &'static str {
        use ServerMessage::*;
        match self {
            RoomInfo(_) => "RoomInfo",
            ConnectionRefused(_) => "ConnectionRefused",
            Connected(_) => "Connected",
            ReceivedItems(_) => "ReceivedItems",
            LocationInfo(_) => "LocationInfo",
            RoomUpdate(_) => "RoomUpdate",
            PlainPrint(_) => "Print",
            RawPrint(_) => "PrintJSON",
            DataPackage(_) => "DataPackage",
            Bounced(_) => "Bounced",
            InvalidPacket(_) => "InvalidPacket",
            Retrieved(_) => "Retrieved",
            SetReply(_) => "SetReply",
        }
    }
}

#[derive(Debug, Clone, Copy, Deserialize_repr)]
#[repr(u8)]
/// Permissions for when certain actions (such as releasing all checks) may be
/// performed.
pub enum Permission {
    /// This action may never be performed.
    Disabled = 0,

    /// This action may be manually performed at any time.
    Enabled = 1,

    /// This action may be manually performed by a player after they have
    /// reached their goal.
    Goal = 2,

    /// This action is automatically performed after the player has reached
    /// their goal. This is only possible for release and collect.
    Auto = 6,

    /// This action is automatically performed after the player has reached
    /// their goal *and* may be manually performed at any time.
    AutoEnabled = 7,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct NetworkVersion {
    pub(crate) major: u16,
    pub(crate) minor: u16,
    pub(crate) build: u16,
    pub(crate) class: String,
}

impl Display for NetworkVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.build)
    }
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct NetworkPlayer {
    pub(crate) team: u32,
    pub(crate) slot: u32,
    pub(crate) alias: String,
    pub(crate) name: Ustr,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct NetworkItem {
    pub(crate) item: i64,
    pub(crate) location: i64,
    pub(crate) player: u32,
    pub(crate) flags: NetworkItemFlags,
}

bitflags! {
    #[repr(transparent)]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[serde(from = "u8")]
    #[serde(into = "u8")]
    pub(crate) struct NetworkItemFlags: u8 {
        /// The item can unlock logical advancement.
        const PROGRESSION = 0b001;

        /// The item is especially useful.
        const USEFUL = 0b010;

        /// The item is a trap.
        const TRAP = 0b100;
    }
}

impl From<u8> for NetworkItemFlags {
    fn from(value: u8) -> NetworkItemFlags {
        NetworkItemFlags::from_bits_retain(value)
    }
}

impl From<NetworkItemFlags> for u8 {
    fn from(value: NetworkItemFlags) -> Self {
        value.bits()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub(crate) enum SlotType {
    // We ignore these because they aren't currently sent by the server.
    Spectator = 0,
    Player = 1,
    Group = 2,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct NetworkSlot {
    pub(crate) name: Ustr,
    pub(crate) game: Ustr,
    pub(crate) r#type: SlotType,
    pub(crate) group_members: Vec<u32>,
}

// REQUESTS

#[derive(Debug, Clone, Serialize)]
pub(crate) struct Connect {
    pub(crate) password: Option<String>,
    pub(crate) game: Option<Ustr>,
    pub(crate) name: Ustr,
    pub(crate) uuid: String,
    pub(crate) version: NetworkVersion,
    pub(crate) items_handling: ItemsHandlingFlags,
    pub(crate) tags: UstrSet,
    pub(crate) slot_data: bool,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct ConnectUpdate {
    pub(crate) items_handling: Option<ItemsHandlingFlags>,
    pub(crate) tags: Option<Vec<Ustr>>,
}

bitflags! {
    #[derive(Debug, Clone, Copy, Serialize)]
    #[serde(into = "u8")]
    pub(crate) struct ItemsHandlingFlags: u8 {
        /// Items are sent from other worlds.
        const OTHER_WORLDS = 0b001;

        /// Items are sent from your own world. Setting this automatically sets
        /// [OTHER_WORLDS] as well.
        const OWN_WORLD = 0b011;

        /// Items are sent from your starting inventory. Setting this
        /// automatically sets [OTHER_WORLDS] as well.
        const STARTING_INVENTORY = 0b101;
    }
}

impl From<ItemsHandlingFlags> for u8 {
    fn from(value: ItemsHandlingFlags) -> Self {
        value.bits()
    }
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct LocationChecks {
    pub(crate) locations: Vec<i64>,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct LocationScouts {
    pub(crate) locations: Vec<i64>,
    pub(crate) create_as_hint: CreateAsHint,
}

/// Options for whether and how locations scouted with [Client.scout_locations]
/// should be broadcast as player-visible hints.
#[derive(Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum CreateAsHint {
    /// Don't broadcast locations as hints. Scouted locations will only be
    /// visible to the client code, not to the player.
    No = 0,

    /// Broadcast all scouted locations as hints.
    All = 1,

    /// Broadcast only locations that haven't already been hinted as hints. The
    /// return value for [scout_locations](crate::Client::scout_locations) will
    /// still contain *all* locations.
    New = 2,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct CreateHints {
    pub(crate) locations: Vec<i64>,
    pub(crate) player: u32,
    pub(crate) status: HintStatus,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct UpdateHint {
    pub(crate) player: u32,
    pub(crate) location: i64,
    pub(crate) status: HintStatus,
}

#[derive(Debug, Clone, Default, Serialize_repr)]
#[repr(u8)]
pub enum HintStatus {
    #[default]
    Unspecified = 0,
    NoPriority = 10,
    Avoid = 20,
    Priority = 30,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct StatusUpdate {
    pub(crate) status: ClientStatus,
}

/// Possible states for the client.
#[derive(Debug, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ClientStatus {
    Unknown = 0,

    /// One or more clients are connected to the server. This is automatically
    /// set when the connection is first established.
    Connected = 5,
    Ready = 10,
    Playing = 20,

    /// This player has achieved their goal.
    Goal = 30,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct Say {
    pub(crate) text: String,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct GetDataPackage {
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) games: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct Get {
    pub(crate) keys: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct Set {
    pub(crate) key: String,
    pub(crate) default: Value,
    pub(crate) want_reply: bool,
    pub(crate) operations: Vec<DataStorageOperation>,
}

/// Operations that can be applied to keys in the Archipelago server's data
/// store.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "operation", content = "value", rename_all = "snake_case")]
pub enum DataStorageOperation {
    /// Replace the value entirely with a new value.
    Replace(Value),

    /// Set the value to the default value specified in
    /// [Client::change](crate::Client::change).
    Default,

    /// Adds the given number to the current value.
    Add(f64),

    /// Appends the given values to the end of an array.
    #[serde(rename = "add")]
    Appends(Vec<Value>),

    /// Multiplies the current value by the given number.
    #[serde(rename = "mul")]
    Multiply(f64),

    /// Exponentiates the current value to the given power.
    #[serde(rename = "pow")]
    Exponentiate(f64),

    /// Sets the current value to itself modulo the given number.
    Mod(f64),

    /// Rounds the current value down to the next integer.
    Floor,

    /// Rounds the current value up to the next integer.
    Ceil,

    /// Sets the current value to the given number if it's greater than the
    /// existing value.
    Max(i64),

    /// Sets the current value to the given number if it's less than the
    /// existing value.
    Min(i64),

    /// Sets the current value to the bitwise AND of it and the given number.
    And(i64),

    /// Sets the current value to the bitwise OR of it and the given number.
    Or(i64),

    /// Sets the current value to the bitwise XOR of it and the given number.
    Xor(i64),

    /// Shifts the current value left by the given number of bits.
    LeftShift(u8),

    /// Shifts the current value right by the given number of bits.
    RightShift(u8),

    /// If the current value is an array, removes the first instance of the
    /// given value from it.
    Remove(Value),

    /// If the current value is an array, removes the element at the given
    /// index.
    #[serde(rename = "pop")]
    RemoveIndex(i64),

    /// If the current value is a map, removes the element with the given key.
    #[serde(rename = "pop")]
    RemoveKey(String),

    /// If the current value is an array, adds all elements in the given array
    /// that aren't already present.
    #[serde(rename = "update")]
    Union(Vec<Value>),

    /// If the current value is a map, sets the given keys to their associated
    /// values.
    Update(HashMap<String, Value>),
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct SetNotify {
    pub(crate) keys: Vec<String>,
}

// RESPONSES

#[serde_as]
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RoomInfo {
    pub(crate) version: NetworkVersion,
    pub(crate) generator_version: NetworkVersion,
    pub(crate) tags: UstrSet,
    #[serde(rename = "password")]
    pub(crate) password_required: bool,
    pub(crate) permissions: PermissionMap,
    pub(crate) hint_cost: u8,
    pub(crate) location_check_points: u64,
    pub(crate) games: UstrSet,
    pub(crate) datapackage_checksums: UstrMap<String>,
    pub(crate) seed_name: String,
    #[serde_as(as = "TimestampSeconds<f64>")]
    pub(crate) time: SystemTime,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct PermissionMap {
    pub(crate) release: Permission,
    pub(crate) collect: Permission,
    pub(crate) remaining: Permission,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ConnectionRefused {
    #[serde(default)]
    pub(crate) errors: Vec<String>,
}

#[serde_as]
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Connected<S> {
    pub(crate) team: u32,
    pub(crate) slot: u32,
    pub(crate) players: Vec<NetworkPlayer>,
    pub(crate) missing_locations: Vec<i64>,
    pub(crate) checked_locations: Vec<i64>,
    pub(crate) slot_data: S,
    #[serde_as(as = "HashMap<DisplayFromStr, _>")]
    pub(crate) slot_info: HashMap<u32, NetworkSlot>,
    pub(crate) hint_points: u64,
}

/// Deserializes a value of `Connected<S>`, except that if `S` is
/// [json_serde::Value], this will allow `slot_data` to be omitted entirely.
fn deserialize_connected_with_optional_slot_data<'de, S, D>(
    deserializer: D,
) -> Result<Connected<S>, D::Error>
where
    S: DeserializeOwned + 'static,
    D: Deserializer<'de>,
{
    Ok(if try_specialize::static_type_eq::<S, ()>() {
        let inner = Connected::<Option<()>>::deserialize(deserializer)?;
        let connected = Connected {
            team: inner.team,
            slot: inner.slot,
            players: inner.players,
            missing_locations: inner.missing_locations,
            checked_locations: inner.checked_locations,
            slot_data: (),
            slot_info: inner.slot_info,
            hint_points: inner.hint_points,
        };

        // Safety: We've verified that S is Value above.
        let typed_connected =
            unsafe { mem::transmute_copy::<Connected<()>, Connected<S>>(&connected) };
        mem::forget(connected);
        typed_connected
    } else {
        Connected::<S>::deserialize(deserializer)?
    })
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct ReceivedItems {
    pub(crate) index: usize,
    pub(crate) items: Vec<NetworkItem>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct LocationInfo {
    pub(crate) locations: Vec<NetworkItem>,
}

// We only include fields here that might plausibly be changed during the
// lifetime of a single connection.
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct RoomUpdate {
    // Copied from RoomInfo
    pub(crate) tags: Option<UstrSet>,
    pub(crate) permissions: Option<PermissionMap>,
    pub(crate) hint_cost: Option<u8>,
    pub(crate) location_check_points: Option<u64>,
    // Copied from Connected
    pub(crate) hint_points: Option<u64>,
    pub(crate) players: Option<Vec<NetworkPlayer>>,
    pub(crate) checked_locations: Option<Vec<i64>>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DataPackage {
    pub(crate) data: DataPackageObject,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct DataPackageObject {
    pub(crate) games: UstrMap<GameData>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct GameData {
    pub(crate) item_name_to_id: HashMap<Ustr, i64>,
    pub(crate) location_name_to_id: HashMap<Ustr, i64>,
    pub(crate) checksum: String,
}

// We could represent this as an enum of types, but there's no point when all we
// want to do is extract the error message anyway.
#[derive(Debug, Clone, Deserialize)]
pub(crate) struct InvalidPacket {
    pub(crate) text: String,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct Retrieved {
    pub(crate) keys: HashMap<String, Value>,
}

#[derive(Debug, Clone, Deserialize)]
pub(crate) struct SetReply {
    pub(crate) key: String,
    pub(crate) value: Value,
    pub(crate) original_value: Option<Value>, // Won't be there if key is prefixed with _read
    // See https://github.com/ArchipelagoMW/Archipelago/issues/5829
    pub(crate) slot: Option<u32>,
}