flatland3-gfx 0.2.19

Flatland3 Macroquad + egui graphical play client
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Tokio networking on a dedicated thread (macroquad's executor is not Tokio).

use std::sync::{Arc, RwLock};
use std::time::Duration;

use flatland_cli::characters::CharacterSummary;
use flatland_cli::game_session;
use flatland_client_lib::{
    needs_asset_sync, sync_assets, AssetSyncKind, AssetSyncOptions,
};
use flatland_client_lib::{
    connect, load_client_settings, AutoNavigator, ConnectOptions, GameClient, RemoteSession,
};
use flatland_client_ui::{InputAction, UiKeyEvent};
use flatland_gfx_engine::octant_facing_axes;
use flatland_protocol::ChatChannel;
use tokio::sync::mpsc;
use tokio::time::{self, MissedTickBehavior};
use tracing::info;

use flatland_play_loop::{dispatch_action, dispatch_menu_key, ActionCtx};
use crate::play::GfxPlayArgs;

const RECONNECT_INITIAL: Duration = Duration::from_secs(1);
const RECONNECT_MAX: Duration = Duration::from_secs(30);
/// Minimum pause after a session ends before opening a new TCP connection (avoids duplicate-character storms).
const RECONNECT_SESSION_COOLDOWN: Duration = Duration::from_millis(500);

/// Connection lifecycle shown on the gfx retry / disconnect overlay.
#[derive(Debug, Clone, PartialEq)]
pub enum ConnectionUi {
    Connected,
    /// TCP/session lost; background reconnect in progress.
    Reconnecting {
        attempt: u32,
        reason: Option<String>,
        next_secs: f32,
        last_error: Option<String>,
    },
    /// Auto-reconnect disabled or aborted — user must retry or return to login.
    Lost {
        reason: Option<String>,
        last_error: Option<String>,
    },
}

impl Default for ConnectionUi {
    fn default() -> Self {
        Self::Connected
    }
}

/// Client-only presentation mirrored from the network thread (not server state).
#[derive(Debug, Clone, Default)]
pub struct GfxPresentation {
    /// Cardinal N/S/E/W facing axes while auto-walking; `None` when idle or manual movement.
    pub auto_nav_facing_axes: Option<(f32, f32)>,
    pub connection: ConnectionUi,
}

#[derive(Debug, Clone)]
pub struct GfxSnapshot {
    pub game: flatland_client_lib::GameState,
    pub presentation: GfxPresentation,
}

pub struct GfxNet {
    state: Arc<RwLock<Option<GfxSnapshot>>>,
    cmd_tx: mpsc::UnboundedSender<NetCmd>,
}

pub enum NetCmd {
    Say(ChatChannel, String),
    MenuKey(UiKeyEvent),
    KeychainSelect(usize),
    RouteEditorClick(flatland_client_lib::RouteEditorClick),
    Action(InputAction),
    Movement {
        forward: f32,
        strafe: f32,
        vertical: f32,
        sprint: bool,
    },
    AutoNavTo {
        x: f32,
        y: f32,
    },
    /// Click-to-target: slot 1 primary, slot 2 secondary (SHIFT+click).
    SetCombatTargetSlot {
        slot_index: u8,
        target_id: flatland_protocol::EntityId,
        label: String,
    },
    CancelAutoNav,
    StopMovement,
    Esc,
    RouteEditorWaypoint {
        x: f32,
        y: f32,
        z: f32,
    },
    RouteEditorMapClick {
        x: f32,
        y: f32,
    },
    /// Force an immediate reconnect attempt (resets backoff).
    RetryConnect,
    Shutdown,
}

impl GfxNet {
    /// Start the network thread. Poll [`Self::try_ready`] each frame until `Ok(())`.
    pub fn spawn_connecting(
        args: GfxPlayArgs,
        chosen: Option<CharacterSummary>,
    ) -> anyhow::Result<(Self, std::sync::mpsc::Receiver<anyhow::Result<()>>)> {
        let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel::<anyhow::Result<()>>(1);
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let state = Arc::new(RwLock::new(None));

        std::thread::Builder::new()
            .name("flatland-gfx-net".into())
            .spawn({
                let state = Arc::clone(&state);
                move || {
                    let rt = match tokio::runtime::Runtime::new() {
                        Ok(rt) => rt,
                        Err(err) => {
                            let _ = ready_tx.send(Err(err.into()));
                            return;
                        }
                    };
                    if let Err(err) =
                        rt.block_on(net_thread(args, chosen, cmd_rx, state, ready_tx))
                    {
                        tracing::error!(error = %err, "gfx network thread exited");
                    }
                }
            })?;

        Ok((Self { state, cmd_tx }, ready_rx))
    }

    /// Blocking variant (tests / agents). Prefer [`Self::spawn_connecting`] in the gfx UI.
    #[allow(dead_code)]
    pub fn spawn(
        args: GfxPlayArgs,
        chosen: Option<CharacterSummary>,
    ) -> anyhow::Result<Self> {
        let (net, ready_rx) = Self::spawn_connecting(args, chosen)?;
        ready_rx
            .recv()
            .map_err(|_| anyhow::anyhow!("gfx network thread exited before ready"))??;
        Ok(net)
    }

    pub fn send(&self, cmd: NetCmd) {
        let _ = self.cmd_tx.send(cmd);
    }

    pub fn snapshot(&self) -> Option<GfxSnapshot> {
        self.state.read().ok().and_then(|g| g.clone())
    }
}

impl Drop for GfxNet {
    fn drop(&mut self) {
        let _ = self.cmd_tx.send(NetCmd::Shutdown);
    }
}

fn update_auto_nav_presentation(
    presentation: &mut GfxPresentation,
    auto_nav_active: bool,
    forward: f32,
    strafe: f32,
) {
    if auto_nav_active {
        if forward.abs() > f32::EPSILON || strafe.abs() > f32::EPSILON {
            presentation.auto_nav_facing_axes = Some(octant_facing_axes(forward, strafe));
        }
    } else {
        presentation.auto_nav_facing_axes = None;
    }
}

fn sync_state(
    state: &Arc<RwLock<Option<GfxSnapshot>>>,
    client: &GameClient<RemoteSession>,
    presentation: &GfxPresentation,
) {
    if let Ok(mut guard) = state.write() {
        *guard = Some(GfxSnapshot {
            game: client.state.clone(),
            presentation: presentation.clone(),
        });
    }
}

fn auto_reconnect_enabled(no_reconnect: bool) -> bool {
    if no_reconnect {
        return false;
    }
    match std::env::var("FLATLAND_AUTO_RECONNECT").as_deref() {
        Ok("0" | "false" | "FALSE" | "no" | "NO") => false,
        _ => true,
    }
}

async fn connect_client(opts: &ConnectOptions) -> anyhow::Result<GameClient<RemoteSession>> {
    let character_id = opts.character_id;
    let session = connect(opts.clone()).await?;
    let mut client = GameClient::new(session);
    client.state.character_id = character_id;
    client.wait_until_ready().await?;
    Ok(client)
}

async fn wait_for_server(
    opts: &ConnectOptions,
    state: &Arc<RwLock<Option<GfxSnapshot>>>,
    cmd_rx: &mut mpsc::UnboundedReceiver<NetCmd>,
    disconnect_reason: Option<String>,
) -> anyhow::Result<GameClient<RemoteSession>> {
    let mut backoff = RECONNECT_INITIAL;
    let mut attempt = 0u32;
    loop {
        attempt += 1;
        let next_secs = backoff.as_secs_f32();
        if let Ok(mut guard) = state.write() {
            if let Some(s) = guard.as_mut() {
                s.presentation.connection = ConnectionUi::Reconnecting {
                    attempt,
                    reason: disconnect_reason.clone(),
                    next_secs,
                    last_error: None,
                };
                s.game.connected = false;
                s.game.push_log(format!(
                    "Reconnecting to {} (attempt {attempt})…",
                    opts.server
                ));
            }
        }
        match connect_client(opts).await {
            Ok(client) => return Ok(client),
            Err(err) => {
                let err_s = err.to_string();
                if let Ok(mut guard) = state.write() {
                    if let Some(s) = guard.as_mut() {
                        s.presentation.connection = ConnectionUi::Reconnecting {
                            attempt,
                            reason: disconnect_reason.clone(),
                            next_secs,
                            last_error: Some(err_s.clone()),
                        };
                        s.game.push_log(format!(
                            "Reconnect failed: {err_s} — retry in {next_secs:.1}s"
                        ));
                    }
                }
            }
        }
        let sleep = tokio::time::sleep(backoff);
        tokio::pin!(sleep);
        let mut force_retry = false;
        tokio::select! {
            _ = &mut sleep => {}
            cmd = cmd_rx.recv() => {
                match cmd {
                    Some(NetCmd::Shutdown) | None => {
                        anyhow::bail!("quit while reconnecting");
                    }
                    Some(NetCmd::RetryConnect) => {
                        force_retry = true;
                    }
                    Some(_) => {}
                }
            }
        }
        if force_retry {
            backoff = RECONNECT_INITIAL;
        } else {
            backoff = (backoff * 2).min(RECONNECT_MAX);
        }
    }
}

async fn net_thread(
    args: GfxPlayArgs,
    chosen: Option<CharacterSummary>,
    mut cmd_rx: mpsc::UnboundedReceiver<NetCmd>,
    state: Arc<RwLock<Option<GfxSnapshot>>>,
    ready_tx: std::sync::mpsc::SyncSender<anyhow::Result<()>>,
) -> anyhow::Result<()> {
    let auto_reconnect = auto_reconnect_enabled(args.no_reconnect);

    let asset_sync_msg = if !args.skip_asset_sync {
        match sync_assets(AssetSyncOptions {
            quiet: true,
            ..AssetSyncOptions::default()
        })
        .await
        {
            Ok(result) => match result.kind {
                AssetSyncKind::Remote => {
                    info!(publish_rev = result.state.publish_rev, "gfx assets synced");
                    Some(format!("Assets synced (rev {})", result.state.publish_rev))
                }
                AssetSyncKind::LocalCacheNoRemote => {
                    info!(
                        publish_rev = result.state.publish_rev,
                        "gfx using cached sprites — remote latest.json not published"
                    );
                    Some(format!(
                        "Using cached sprites (rev {}) — remote bundle not published yet",
                        result.state.publish_rev
                    ))
                }
                AssetSyncKind::RepoDevNoRemote => {
                    info!(
                        publish_rev = result.state.publish_rev,
                        "gfx using repo sprites — remote latest.json not published"
                    );
                    Some(format!(
                        "Using repo sprites (rev {}) — run flatland-admin content publish for remote sync",
                        result.state.publish_rev
                    ))
                }
            },
            Err(err) => {
                tracing::warn!(error = %err, "gfx asset sync failed — sprites may be missing");
                Some(format!("Asset sync failed: {err}"))
            }
        }
    } else {
        None
    };

    let opts = match &chosen {
        Some(character) => game_session::prepare_remote_play_as(character, args.server).await?,
        None => game_session::prepare_remote_play(args.name.as_deref(), args.server).await?,
    };

    let mut client = match connect_client(&opts).await {
        Ok(c) => c,
        Err(err) => {
            let _ = ready_tx.send(Err(err));
            return Ok(());
        }
    };
    client
        .state
        .logs
        .retain(|line| !line.starts_with("gfx:") && !line.contains("not wired"));
    client
        .state
        .push_log(format!("Gfx client v{} ready.", env!("CARGO_PKG_VERSION")));
    if let Some(msg) = asset_sync_msg {
        client.state.push_log(msg);
    }
    if client.state.publish_rev > 0 && needs_asset_sync(client.state.publish_rev) {
        client.state.push_log(format!(
            "Server publish rev {} — run: flatland3-gfx assets sync",
            client.state.publish_rev
        ));
    }
    info!(
        entity_id = client.entity_id(),
        "gfx connected — ? help, Ctrl+C quit"
    );

    let mut presentation = GfxPresentation {
        connection: ConnectionUi::Connected,
        ..Default::default()
    };
    sync_state(&state, &client, &presentation);
    ready_tx
        .send(Ok(()))
        .map_err(|_| anyhow::anyhow!("gfx play loop not waiting for ready"))?;

    let keys = load_client_settings().keys;

    'sessions: loop {
        let mut was_moving = false;
        let mut intent = time::interval(Duration::from_millis(100));
        intent.set_missed_tick_behavior(MissedTickBehavior::Skip);
        intent.tick().await;

        let mut drain = time::interval(Duration::from_millis(33));
        drain.set_missed_tick_behavior(MissedTickBehavior::Skip);
        drain.tick().await;

        let mut move_forward = 0.0f32;
        let mut move_strafe = 0.0f32;
        let mut move_vertical = 0.0f32;
        let mut move_sprint = false;
        let mut auto_nav: Option<AutoNavigator> = None;
        let mut block_active = false;
        let mut last_move_err: Option<String> = None;
        let mut shutdown = false;
        presentation = GfxPresentation::default();

        'session: loop {
            tokio::select! {
                cmd = cmd_rx.recv() => {
                    let Some(cmd) = cmd else {
                        shutdown = true;
                        break 'session;
                    };
                    match cmd {
                        NetCmd::Shutdown => {
                            shutdown = true;
                            break 'session;
                        }
                        NetCmd::Esc => {
                            if client.state.show_npc_chat
                                || client.state.show_npc_verb_menu
                                || client.state.show_shop_menu
                                || client.state.bank_panel.is_some()
                                || client.state.storage_panel.is_some()
                                || (client.state.show_quest_offer
                                    && (client.state.show_npc_chat
                                        || client.state.npc_verb_target.is_some()))
                            {
                                let _ = client.npc_interaction_back().await;
                            } else {
                                let _ = client.back_on_esc();
                            }
                        }
                        NetCmd::Say(channel, text) => {
                            if let Err(err) = client.say(channel, &text).await {
                                client.state.push_log(format!("Chat failed: {err}"));
                            }
                        }
                        NetCmd::MenuKey(key) => {
                            let _ = dispatch_menu_key(&mut client, key).await;
                        }
                        NetCmd::KeychainSelect(idx) => {
                            let n = client.state.keychain_entries().len();
                            if n > 0 {
                                client.state.keychain_menu_index = idx.min(n - 1);
                            }
                        }
                        NetCmd::RouteEditorClick(click) => {
                            client.worker_route_editor_ui_click(click);
                        }
                        NetCmd::Action(action) => {
                            let mut ctx = ActionCtx {
                                block_active: &mut block_active,
                                auto_nav: &mut auto_nav,
                            };
                            dispatch_action(&mut client, &keys, action, &mut ctx).await;
                            if auto_nav.is_none() {
                                presentation.auto_nav_facing_axes = None;
                            }
                        }
                        NetCmd::Movement { forward, strafe, vertical, sprint } => {
                            move_forward = forward;
                            move_strafe = strafe;
                            move_vertical = vertical;
                            move_sprint = sprint;
                        }
                        NetCmd::AutoNavTo { x, y } => {
                            auto_nav = AutoNavigator::plan(&client.state, x, y);
                        }
                        NetCmd::SetCombatTargetSlot {
                            slot_index,
                            target_id,
                            label,
                        } => {
                            if let Err(err) = client
                                .set_combat_target_slot(slot_index, target_id, &label)
                                .await
                            {
                                client.state.push_log(format!("Target: {err}"));
                            }
                        }
                        NetCmd::CancelAutoNav => {
                            auto_nav = None;
                            presentation.auto_nav_facing_axes = None;
                        }
                        NetCmd::StopMovement => {
                            move_forward = 0.0;
                            move_strafe = 0.0;
                            move_vertical = 0.0;
                            auto_nav = None;
                            presentation.auto_nav_facing_axes = None;
                            if was_moving {
                                if let Err(err) = client.stop().await {
                                    client.state.push_log(format!("Stop failed: {err}"));
                                }
                                was_moving = false;
                            }
                        }
                        NetCmd::RouteEditorWaypoint { x, y, z } => {
                            client.worker_route_editor_add_waypoint(x, y, z);
                        }
                        NetCmd::RouteEditorMapClick { x, y } => {
                            client.worker_route_editor_map_click(x, y);
                        }
                        NetCmd::RetryConnect => {
                            // Only meaningful while wait_for_server is running.
                        }
                    }
                    client.drain_events();
                    sync_state(&state, &client, &presentation);
                    if !client.state.connected {
                        if let Some(reason) = &client.state.disconnect_reason {
                            info!(%reason, "gfx session ended");
                        } else {
                            info!("gfx session ended (server disconnected)");
                        }
                        break 'session;
                    }
                }
                _ = drain.tick() => {
                    client.drain_events();
                    sync_state(&state, &client, &presentation);
                    if !client.state.connected {
                        if let Some(reason) = &client.state.disconnect_reason {
                            info!(%reason, "gfx session ended");
                        } else {
                            info!("gfx session ended (server disconnected)");
                        }
                        break 'session;
                    }
                }
                _ = intent.tick() => {
                    let (px, py, pz) = client.state.player_position_with_z();
                    let (forward, strafe, vertical, sprint) = if let Some(ref mut nav) = auto_nav {
                        match nav.steer(px, py, pz, &client.state) {
                            Some(v) => v,
                            None => {
                                auto_nav = None;
                                presentation.auto_nav_facing_axes = None;
                                client.state.push_log("Arrived at map target.");
                                (0.0, 0.0, 0.0, false)
                            }
                        }
                    } else {
                        (move_forward, move_strafe, move_vertical, move_sprint)
                    };
                    update_auto_nav_presentation(
                        &mut presentation,
                        auto_nav.is_some(),
                        forward,
                        strafe,
                    );
                    let moving = forward.abs() > f32::EPSILON
                        || strafe.abs() > f32::EPSILON
                        || vertical.abs() > f32::EPSILON;
                    if moving {
                        if let Err(err) = client.move_by(forward, strafe, vertical, sprint).await {
                            let msg = format!("Move failed: {err}");
                            if last_move_err.as_deref() != Some(&msg) {
                                client.state.push_log(msg.clone());
                                last_move_err = Some(msg);
                            }
                        } else {
                            last_move_err = None;
                        }
                        was_moving = true;
                    } else if was_moving {
                        if let Err(err) = client.stop().await {
                            client.state.push_log(format!("Stop failed: {err}"));
                        }
                        was_moving = false;
                    }
                    client.drain_events();
                    sync_state(&state, &client, &presentation);
                    if !client.state.connected {
                        if let Some(reason) = &client.state.disconnect_reason {
                            info!(%reason, "gfx session ended");
                        } else {
                            info!("gfx session ended (server disconnected)");
                        }
                        break 'session;
                    }
                }
            }
        }

        let shutdown_requested = shutdown;
        let disconnect_reason = client.state.disconnect_reason.clone();
        client.disconnect();
        if shutdown_requested || !auto_reconnect {
            if let Ok(mut guard) = state.write() {
                if let Some(s) = guard.as_mut() {
                    s.game.connected = false;
                    s.presentation.connection = ConnectionUi::Lost {
                        reason: disconnect_reason.clone(),
                        last_error: None,
                    };
                    if !shutdown_requested {
                        s.game.push_log("Disconnected — reconnect disabled.");
                    }
                }
            }
            // Keep last snapshot so the play loop can show the disconnect overlay
            // until the user returns to login (Shutdown) or the GfxNet drops.
            if shutdown_requested {
                break 'sessions;
            }
            // Wait for RetryConnect / Shutdown while showing Lost overlay.
            loop {
                match cmd_rx.recv().await {
                    Some(NetCmd::Shutdown) | None => break 'sessions,
                    Some(NetCmd::RetryConnect) => {
                        info!("gfx manual reconnect requested");
                        break;
                    }
                    Some(_) => {}
                }
            }
        } else {
            info!("gfx disconnected — reconnecting");
            if let Ok(mut guard) = state.write() {
                if let Some(s) = guard.as_mut() {
                    s.game.connected = false;
                    s.presentation.connection = ConnectionUi::Reconnecting {
                        attempt: 0,
                        reason: disconnect_reason.clone(),
                        next_secs: RECONNECT_INITIAL.as_secs_f32(),
                        last_error: None,
                    };
                    s.game.push_log("Disconnected — reconnecting…");
                }
            }
        }

        // Drop the old RemoteSession before opening a new TCP connection. Otherwise the
        // worker may see two live sessions for one character, supersede the first, and
        // the gfx loop reconnects with zero backoff — spamming welcomes + entity spawns.
        drop(client);
        let reconnected = loop {
            tokio::time::sleep(RECONNECT_SESSION_COOLDOWN).await;
            match wait_for_server(&opts, &state, &mut cmd_rx, disconnect_reason.clone()).await {
                Ok(new_client) => break Some(new_client),
                Err(err) => {
                    let msg = err.to_string();
                    tracing::warn!(error = %msg, "gfx reconnect aborted");
                    if msg.contains("quit while reconnecting") {
                        break None;
                    }
                    if let Ok(mut guard) = state.write() {
                        if let Some(s) = guard.as_mut() {
                            s.presentation.connection = ConnectionUi::Lost {
                                reason: disconnect_reason.clone(),
                                last_error: Some(msg),
                            };
                        }
                    }
                    // Stay alive so Retry / Return to login still work from the overlay.
                    let mut retry = false;
                    loop {
                        match cmd_rx.recv().await {
                            Some(NetCmd::Shutdown) | None => break,
                            Some(NetCmd::RetryConnect) => {
                                retry = true;
                                break;
                            }
                            Some(_) => {}
                        }
                    }
                    if !retry {
                        break None;
                    }
                }
            }
        };
        let Some(new_client) = reconnected else {
            break 'sessions;
        };
        client = new_client;
        client.close_overlays();
        client
            .state
            .push_log(format!("Reconnected as entity {}.", client.entity_id()));
        info!(entity_id = client.entity_id(), "gfx reconnected");
        presentation = GfxPresentation {
            connection: ConnectionUi::Connected,
            ..Default::default()
        };
        sync_state(&state, &client, &presentation);
    }

    // Leave the last snapshot in place until GfxNet drop / play loop exits so the
    // disconnect overlay can still render.
    Ok(())
}