manabrew-server 0.9.2

Matchmaking and lobby relay server for manabrew multiplayer
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
use std::time::Instant;

use crate::protocol::{
    DraftConfig, EngineKind, GameFormat, PlayerDeckInfo, RoomInfo, RoomPlayerInfo, RoomStatus,
    SealedConfig,
};
use crate::replay::GameReplayCache;
use manabrew_protocol::deck_dto::Deck;

#[derive(Debug, Clone)]
pub struct RoomSlot {
    pub player_id: String,
    pub username: String,
    pub ready: bool,
    pub connected: bool,
    pub is_bot: bool,
    pub selected_deck_name: Option<String>,
    pub selected_deck: Option<Deck>,
    pub published_deck_id: Option<String>,
    pub selected_commander_name: Option<String>,
    pub avatar: Option<String>,
}

#[derive(Debug, Clone)]
pub struct RoomObserver {
    pub player_id: String,
    pub connected: bool,
}

#[derive(Debug)]
pub struct Room {
    pub room_id: String,
    pub room_name: String,
    pub protocol_version: u32,
    pub host_player_id: String,
    pub host_username: String,
    pub hosted: bool,
    pub official: bool,
    pub password: Option<String>,
    pub max_players: u8,
    pub format: GameFormat,
    pub engine: EngineKind,
    pub status: RoomStatus,
    pub players: Vec<RoomSlot>,
    pub observers: Vec<RoomObserver>,
    pub draft_config: Option<DraftConfig>,
    pub sealed_config: Option<SealedConfig>,
    pub reconnect_timeout_s: u32,
    pub replay: Option<GameReplayCache>,
    pub resume_token: String,
    pub humanless_since: Option<Instant>,
}

impl Room {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        room_id: String,
        room_name: String,
        protocol_version: u32,
        host_player_id: String,
        host_username: String,
        max_players: u8,
        format: GameFormat,
        engine: EngineKind,
        host_plays: bool,
        draft_config: Option<DraftConfig>,
        sealed_config: Option<SealedConfig>,
        official: bool,
        password: Option<String>,
        reconnect_timeout_s: u32,
    ) -> Self {
        let max_players = max_players.clamp(2, 8);
        let (players, observers) = if host_plays {
            (
                vec![RoomSlot {
                    player_id: host_player_id.clone(),
                    username: host_username.clone(),
                    ready: false,
                    connected: true,
                    is_bot: false,
                    selected_deck_name: None,
                    selected_deck: None,
                    published_deck_id: None,
                    selected_commander_name: None,
                    avatar: None,
                }],
                vec![],
            )
        } else {
            (
                vec![],
                vec![RoomObserver {
                    player_id: host_player_id.clone(),
                    connected: true,
                }],
            )
        };
        Room {
            room_id,
            room_name,
            protocol_version,
            host_player_id: host_player_id.clone(),
            host_username,
            hosted: !host_plays,
            official,
            password,
            max_players,
            format,
            engine,
            status: RoomStatus::Lobby,
            players,
            observers,
            draft_config,
            sealed_config,
            reconnect_timeout_s,
            replay: None,
            resume_token: String::new(),
            humanless_since: None,
        }
    }

    pub fn is_full(&self) -> bool {
        self.players.len() >= self.max_players as usize
    }

    pub fn is_empty(&self) -> bool {
        self.players.is_empty() && self.observers.is_empty()
    }

    pub fn is_limited_session(&self) -> bool {
        matches!(
            self.format,
            GameFormat::Any | GameFormat::Draft | GameFormat::Sealed
        ) && (self.draft_config.is_some() || self.sealed_config.is_some())
    }

    pub fn reset_lobby_settings(&mut self) {
        if self.draft_config.is_none() && self.sealed_config.is_none() {
            self.format = GameFormat::Any;
            self.max_players = 4;
        }
    }

    pub fn all_ready(&self) -> bool {
        let min_players = if self.is_limited_session() { 1 } else { 2 };
        let controller_id = self.controller_id();
        self.players.len() >= min_players
            && self
                .players
                .iter()
                .filter(|p| Some(p.player_id.as_str()) != controller_id)
                .all(|p| p.ready)
    }

    pub fn add_player(
        &mut self,
        player_id: String,
        username: String,
        is_bot: bool,
    ) -> Result<(), String> {
        if self.is_full() {
            return Err("Room is full".into());
        }
        if self.status != RoomStatus::Lobby {
            return Err("Game already started".into());
        }
        if self.players.iter().any(|p| p.player_id == player_id) {
            return Err("Already in this room".into());
        }
        self.players.push(RoomSlot {
            player_id,
            username,
            ready: false,
            connected: true,
            is_bot,
            selected_deck_name: None,
            selected_deck: None,
            published_deck_id: None,
            selected_commander_name: None,
            avatar: None,
        });
        Ok(())
    }

    pub fn add_observer(&mut self, player_id: String, _username: String) -> Result<(), String> {
        if self.players.iter().any(|p| p.player_id == player_id)
            || self.observers.iter().any(|p| p.player_id == player_id)
        {
            return Err("Already in this room".into());
        }
        self.observers.push(RoomObserver {
            player_id,
            connected: true,
        });
        Ok(())
    }

    pub fn remove_player(&mut self, player_id: &str) -> Option<RoomSlot> {
        if let Some(idx) = self.players.iter().position(|p| p.player_id == player_id) {
            let slot = self.players.remove(idx);
            // If the host left, promote the first remaining player
            if self.host_player_id == player_id {
                if let Some(new_host) = self.players.first() {
                    self.host_player_id = new_host.player_id.clone();
                    self.host_username = new_host.username.clone();
                }
            }
            Some(slot)
        } else {
            None
        }
    }

    pub fn remove_observer(&mut self, player_id: &str) -> Option<RoomObserver> {
        self.observers
            .iter()
            .position(|p| p.player_id == player_id)
            .map(|idx| self.observers.remove(idx))
    }

    pub fn remove_participant(&mut self, player_id: &str) -> bool {
        self.remove_player(player_id).is_some() || self.remove_observer(player_id).is_some()
    }

    pub fn set_connected(&mut self, player_id: &str, connected: bool) {
        if let Some(slot) = self.players.iter_mut().find(|p| p.player_id == player_id) {
            slot.connected = connected;
            return;
        }
        if let Some(observer) = self.observers.iter_mut().find(|p| p.player_id == player_id) {
            observer.connected = connected;
        }
    }

    /// Forward-ported for future use when room cleanup logic is implemented.
    #[allow(dead_code)]
    pub fn all_disconnected(&self) -> bool {
        self.players.iter().all(|p| !p.connected) && self.observers.iter().all(|p| !p.connected)
    }

    pub fn has_connected_human(&self) -> bool {
        self.players.iter().any(|p| p.connected && !p.is_bot)
            || self
                .observers
                .iter()
                .any(|o| o.connected && !(self.hosted && o.player_id == self.host_player_id))
    }

    pub fn set_ready(&mut self, player_id: &str, ready: bool) -> Result<(), String> {
        if let Some(slot) = self.players.iter_mut().find(|p| p.player_id == player_id) {
            slot.ready = ready;
            Ok(())
        } else {
            Err("Player not in room".into())
        }
    }

    pub fn set_deck_selection(
        &mut self,
        player_id: &str,
        deck_name: String,
        deck: Deck,
        published_deck_id: Option<String>,
        commander_name: Option<String>,
        avatar: Option<String>,
    ) -> Result<(), String> {
        if let Some(slot) = self.players.iter_mut().find(|p| p.player_id == player_id) {
            slot.selected_deck_name = Some(deck_name);
            slot.selected_deck = Some(deck);
            slot.published_deck_id = published_deck_id;
            slot.selected_commander_name = commander_name;
            slot.avatar = avatar;
            slot.ready = false;
            Ok(())
        } else {
            Err("Player not in room".into())
        }
    }

    pub fn has_selected_deck(&self, player_id: &str) -> bool {
        self.players
            .iter()
            .find(|p| p.player_id == player_id)
            .map(|p| p.selected_deck.is_some() && p.selected_deck_name.is_some())
            .unwrap_or(false)
    }

    pub fn player_decks(&self) -> Vec<PlayerDeckInfo> {
        self.players
            .iter()
            .filter_map(|p| {
                p.selected_deck.clone().map(|deck| PlayerDeckInfo {
                    username: p.username.clone(),
                    deck_name: p
                        .selected_deck_name
                        .clone()
                        .unwrap_or_else(|| "Unknown Deck".to_string()),
                    deck,
                    published_deck_id: p.published_deck_id.clone(),
                    commander_name: p.selected_commander_name.clone(),
                    avatar: p.avatar.clone(),
                })
            })
            .collect()
    }

    pub fn is_host(&self, player_id: &str) -> bool {
        self.host_player_id == player_id
    }

    /// The room controller is the first human (non-bot) player to take a seat.
    /// They drive the lobby (format, seats, bots, start) regardless of who holds
    /// the engine (`host`). Computed dynamically from current seats, so if the
    /// controlling human leaves, control passes to the next human — never to a
    /// bot. Falls back to the first seat only if every player is a bot.
    pub fn controller_id(&self) -> Option<&str> {
        self.players
            .iter()
            .find(|p| !p.is_bot)
            .or_else(|| self.players.first())
            .map(|p| p.player_id.as_str())
    }

    pub fn is_controller(&self, player_id: &str) -> bool {
        self.controller_id() == Some(player_id)
    }

    pub fn host_username(&self) -> String {
        self.host_username.clone()
    }

    pub fn host_is_player(&self) -> bool {
        self.players
            .iter()
            .any(|p| p.player_id == self.host_player_id)
    }

    pub fn host_connected(&self) -> bool {
        self.players
            .iter()
            .any(|p| p.player_id == self.host_player_id && p.connected)
            || self
                .observers
                .iter()
                .any(|p| p.player_id == self.host_player_id && p.connected)
    }

    pub fn player_usernames(&self) -> Vec<String> {
        self.players.iter().map(|p| p.username.clone()).collect()
    }

    /// Forward-ported for future use when full player ID tracking is needed.
    #[allow(dead_code)]
    pub fn player_ids(&self) -> Vec<String> {
        self.players.iter().map(|p| p.player_id.clone()).collect()
    }

    pub fn connected_player_ids(&self) -> Vec<String> {
        self.players
            .iter()
            .filter(|p| p.connected)
            .map(|p| p.player_id.clone())
            .chain(
                self.observers
                    .iter()
                    .filter(|p| p.connected)
                    .map(|p| p.player_id.clone()),
            )
            .collect()
    }

    pub fn to_room_info(&self) -> RoomInfo {
        RoomInfo {
            room_id: self.room_id.clone(),
            room_name: self.room_name.clone(),
            protocol_version: self.protocol_version,
            host: self.host_username(),
            hosted: self.hosted,
            official: self.official,
            password_protected: self.password.is_some(),
            players: self
                .players
                .iter()
                .map(|p| RoomPlayerInfo {
                    username: p.username.clone(),
                    ready: p.ready,
                    connected: p.connected,
                    is_bot: p.is_bot,
                    selected_deck_name: p.selected_deck_name.clone(),
                })
                .collect(),
            max_players: self.max_players,
            format: self.format.clone(),
            engine: self.engine,
            reconnect_timeout_s: self.reconnect_timeout_s,
            status: self.status.clone(),
            draft_config: self.draft_config.clone(),
            sealed_config: self.sealed_config.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn room(host_plays: bool) -> Room {
        Room::new(
            "r".into(),
            "room".into(),
            manabrew_protocol::protocol::PROTOCOL_VERSION,
            "host".into(),
            "host".into(),
            4,
            GameFormat::Commander,
            EngineKind::Forge,
            host_plays,
            None,
            None,
            false,
            None,
            60,
        )
    }

    #[test]
    fn first_user_is_controller_in_hosted_room() {
        let mut r = room(false);
        assert!(r.is_host("host"));
        assert!(!r.is_controller("host"));
        r.add_player("human".into(), "human".into(), false).unwrap();
        assert!(r.is_controller("human"));
        r.add_player("bot".into(), "bot".into(), true).unwrap();
        assert!(!r.is_controller("bot"));
    }
}