Skip to main content

minesweeper_client/
lib.rs

1//! Minesweeper Client Library
2//!
3//! This library provides a Rust client for the minesweeper multiplayer server,
4//! supporting both HTTP API calls and WebSocket connections for real-time gameplay.
5//!
6//! ## Usage
7//!
8//! ### High-Level Interface (Recommended)
9//!
10//! The `MinesweeperGame` struct provides a high-level interface that manages game state
11//! locally and provides convenient methods for game actions:
12//!
13//! ```rust,no_run
14//! use minesweeper_client::{MinesweeperGame, GameParams, Pos};
15//!
16//! #[tokio::main]
17//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
18//!     let game = MinesweeperGame::new("http://localhost:8000")?;
19//!
20//!     // Start a new game
21//!     let params = GameParams { width: 8, height: 8, bombs: 10 };
22//!     game.start_game(params).await?;
23//!
24//!     // Make moves
25//!     game.reveal(Pos { x: 0, y: 0 }).await?;
26//!     game.flag(Pos { x: 1, y: 1 }).await?;
27//!     
28//!     // Check game state
29//!     if let Some(state) = game.get_state().await {
30//!         println!("Game over: {}, Won: {}", state.is_game_over(), state.is_won());
31//!     }
32//!     
33//!     game.disconnect().await?;
34//!     Ok(())
35//! }
36//! ```
37//!
38//! ### Low-Level Interface
39//!
40//! For more control, you can use the low-level `MinesweeperClient` and `MinesweeperWebSocket`
41//! directly:
42//!
43//! ```rust,no_run
44//! use minesweeper_client::{MinesweeperClient, MinesweeperWebSocket, GameParams, ClientMessage, Pos};
45//!
46//! #[tokio::main]
47//! async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
48//!     let client = MinesweeperClient::new("http://localhost:8000")?;
49//!     let game_id = client.create_game(GameParams { width: 8, height: 8, bombs: 10 }).await?;
50//!     
51//!     let ws_url = client.websocket_url(&game_id)?;
52//!     let mut ws = MinesweeperWebSocket::connect(&ws_url).await?;
53//!     
54//!     // Receive initial state
55//!     if let Some(message) = ws.receive_message().await? {
56//!         println!("Received: {:?}", message);
57//!     }
58//!     
59//!     // Send actions manually
60//!     ws.send_message(ClientMessage::Reveal { pos: Pos { x: 0, y: 0 } }).await?;
61//!     
62//!     ws.close().await?;
63//!     Ok(())
64//! }
65//! ```
66
67mod client;
68mod game;
69mod websocket;
70
71pub use client::MinesweeperClient;
72pub use game::{GameEvent, GameState, MinesweeperGame};
73pub use websocket::MinesweeperWebSocket;
74
75// Re-export common types for convenience
76pub use minesweeper_common::{models::*, protocol::*};
77
78pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;