Skip to main content

plato_mud/
client.rs

1//! PLATO MUD Engine — CLI Client
2//!
3//! Terminal-based MUD client with Zork-style interface.
4
5#![cfg(feature = "client")]
6
7use std::io::{self, BufRead, Write};
8
9/// The PLATO MUD client
10pub struct PlatoClient {
11    server_url: String,
12    connected: bool,
13}
14
15impl PlatoClient {
16    pub fn new(server_url: &str) -> Self {
17        Self {
18            server_url: server_url.to_string(),
19            connected: false,
20        }
21    }
22
23    /// Run the interactive client
24    pub fn run_interactive(&mut self) -> io::Result<()> {
25        println!("╔═══════════════════════════════════════╗");
26        println!("║     PLATO MUD Client v0.1.0          ║");
27        println!("╚═══════════════════════════════════════╝");
28        println!();
29        println!("Connecting to {}...", self.server_url);
30
31        // In a real implementation, this would connect to the server
32        // via TCP/WebSocket. For now, we start a local in-process server.
33        println!("Using local in-process server (standalone mode).");
34        println!("Type QUIT to exit.\n");
35
36        // Spin up a local server
37        let mut server = crate::server::PlatoServer::new();
38
39        println!("Enter your agent name:");
40        let stdin = io::stdin();
41        let mut stdout = io::stdout();
42
43        let mut name = String::new();
44        stdin.lock().read_line(&mut name)?;
45        let name = name.trim().to_string();
46
47        let agent_id = crate::types::AgentId(name.clone());
48        server
49            .engine_mut()
50            .connect_agent(
51                agent_id.clone(),
52                crate::types::RoomId("alignment-cathedral".to_string()),
53            )
54            .expect("Starting room exists");
55
56        println!("\nWelcome, {}.\n", name);
57        if let Ok(desc) = server.engine().look(&agent_id) {
58            println!("{}", desc);
59        }
60
61        loop {
62            print!("\n> ");
63            stdout.flush()?;
64
65            let mut input = String::new();
66            stdin.lock().read_line(&mut input)?;
67            let input = input.trim();
68
69            if input.eq_ignore_ascii_case("quit") || input.eq_ignore_ascii_case("exit") {
70                println!("Goodbye, {}.", name);
71                break;
72            }
73
74            match server.process_command(&agent_id, input) {
75                Ok(response) => println!("{}", response),
76                Err(e) => println!("⚠ {}", e),
77            }
78        }
79
80        Ok(())
81    }
82}