← All Workshops

MudEngine Part 11: Peer-to-Peer with iroh

Step 10 / 14

The connection screen

Part 10's App asked for a name and dropped you straight onto the game screen, connected to the fixed Axum server. With the server gone, the screen gains a role picker: Host a game, or Join a game (which needs the host's node id pasted in). The host's node id is displayed in its own net-banner after it connects — that's what the joiner pastes.

Key additions over Part 10:

  • role: Signal<Role> — defaults to Host, toggled by two buttons
  • host_id: Signal<String> — shown only for Role::Client, filled via input
  • the Enter key and the button both gate on "name non-empty AND (host OR host-id non-empty)" before setting registered
  • Game receives role, player_name, and host_id as plain props (it was only receiving the name before)
mud-engine/src/components/app.rs
use crate::components::game::Game;
use crate::game::net::Role;
use crate::game::random_name::random_name;
use dioxus::prelude::*;

#[component]
pub fn App() -> Element {
    let mut player_name = use_signal(random_name);
    let mut role = use_signal(|| Role::Host);
    let mut host_id = use_signal(String::new);
    let mut registered = use_signal(|| false);

    rsx! {
        // Dev: link the stylesheets through manganis so dx hot-reloads them from disk.
        // Release: inline them into a single-file binary — Blitz resolves asset!() from
        // the cargo/disk environment, which a bare release binary doesn't have.
        if cfg!(debug_assertions) {
            document::Stylesheet { href: asset!("/assets/main.css") }
            document::Stylesheet { href: asset!("/assets/dx-components-theme.css") }
        } else {
            style { {include_str!("../../assets/main.css")} }
            style { {include_str!("../../assets/dx-components-theme.css")} }
        }
        if !*registered.read() {
            div { class: "name-screen",
                h1 { "🧙 MudEngine" }
                p { class: "subtitle",
                    "Peer-to-peer — host a game or join a friend. No server required."
                }

                // Role picker: the host owns the authoritative state, every
                // peer dials the host's node id over iroh's QUIC mesh.
                div { class: "role-picker",
                    button {
                        class: if *role.read() == Role::Host { "role-btn on" } else { "role-btn" },
                        onclick: move |_| role.set(Role::Host),
                        "🖥️ Host a game"
                    }
                    button {
                        class: if *role.read() == Role::Client { "role-btn on" } else { "role-btn" },
                        onclick: move |_| role.set(Role::Client),
                        "🔗 Join a game"
                    }
                }

                // Joining needs the host's node id, displayed in the host's
                // window once it connects.
                if *role.read() == Role::Client {
                    input {
                        class: "host-id-input",
                        placeholder: "Paste the host's node id",
                        value: "{host_id}",
                        oninput: move |e| host_id.set(e.value()),
                    }
                }

                input {
                    class: "name-input",
                    value: "{player_name}",
                    oninput: move |e| player_name.set(e.value()),
                    onkeydown: move |e| {
                        if e.key() == Key::Enter
                            && !player_name.read().trim().is_empty()
                        {
                            registered.set(true);
                        }
                    },
                    placeholder: "Enter your name...",
                }

                button {
                    class: "join-btn",
                    onclick: move |_| {
                        let ready = !player_name.read().trim().is_empty()
                            && (*role.read() == Role::Host
                                || !host_id.read().trim().is_empty());
                        if ready {
                            registered.set(true);
                        }
                    },
                    "Enter the World"
                }
            }
        } else {
            Game {
                role: role(),
                player_name: player_name(),
                host_id: host_id(),
            }
        }
    }
}
Step 10 / 14