Skip to main content

go_fish_web/
lib.rs

1//! # go-fish-web
2//!
3//! `go-fish-web` provides the protocol message types used by the `go-fish` web client and server.
4
5use go_fish::Rank;
6use go_fish::{CompleteBook, Hand, HookResult};
7use serde::Deserialize;
8use serde::Serialize;
9use std::fmt::Display;
10
11/// The type of bot that can be added to a lobby.
12#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
13pub enum BotType {
14    SimpleBot,
15}
16
17impl Display for BotType {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            BotType::SimpleBot => write!(f, "SimpleBot"),
21        }
22    }
23}
24
25/// A player slot in a lobby — either a human or a bot.
26#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
27pub enum LobbyPlayer {
28    Human { name: String },
29    Bot { name: String, bot_type: BotType },
30}
31
32impl LobbyPlayer {
33    pub fn name(&self) -> &str {
34        match self {
35            LobbyPlayer::Human { name } => name,
36            LobbyPlayer::Bot { name, .. } => name,
37        }
38    }
39}
40
41/// A lobby visible in the lobby browser.
42#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
43pub struct LobbyInfo {
44    pub lobby_id: String,
45    pub player_count: usize,
46    pub max_players: usize,
47}
48
49/// Messages sent from the client to the server.
50#[derive(Debug, Serialize, Deserialize)]
51pub enum ClientMessage {
52    Hook(ClientHookRequest),
53    Identity,
54    CreateLobby,
55    JoinLobby(String),
56    LeaveLobby,
57    StartGame,
58    AddBot { bot_type: BotType },
59    RemoveBot,
60    RequestLobbies,
61}
62
63/// Messages sent from the server to the client.
64#[derive(Debug, Serialize, Deserialize, Clone)]
65pub enum ServerMessage {
66    HookAndResult(HookAndResult),
67    HookError(HookError),
68    HandState(HandState),
69    PlayerTurn(PlayerTurnValue),
70    PlayerIdentity(String),
71    GameResult(GameResult),
72    LobbyJoined {
73        lobby_id: String,
74        leader: String,
75        players: Vec<LobbyPlayer>,
76        max_players: usize,
77    },
78    LobbyUpdated {
79        leader: String,
80        players: Vec<LobbyPlayer>,
81    },
82    LobbyLeft(LobbyLeftReason),
83    GameStarted,
84    GameSnapshot(GameSnapshot),
85    GameAborted,
86    Error(String),
87    LobbyList(Vec<LobbyInfo>),
88}
89
90#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
91pub enum HookError {
92    NotYourTurn,
93    UnknownPlayer(String),
94    CannotTargetYourself,
95    YouDoNotHaveRank(Rank),
96}
97
98#[derive(Debug, Serialize, Deserialize, Clone)]
99pub enum PlayerTurnValue {
100    YourTurn,
101    OtherTurn(String)
102}
103
104#[derive(Debug, Serialize, Deserialize, Clone)]
105pub struct ClientHookRequest {
106    pub target_name: String,
107    pub rank: Rank
108}
109
110#[derive(Debug, Serialize, Deserialize, Clone)]
111pub struct FullHookRequest {
112    pub fisher_name: String,
113    pub target_name: String,
114    pub rank: Rank,
115}
116
117#[derive(Debug, Serialize, Deserialize, Clone)]
118pub struct HookAndResult {
119    pub hook_request: FullHookRequest,
120    pub hook_result: HookResult
121}
122
123#[derive(Debug, Serialize, Deserialize, Clone)]
124pub struct HandState {
125    pub hand: Hand,
126    pub completed_books: Vec<CompleteBook>,
127}
128
129#[derive(Debug, Serialize, Deserialize, Clone)]
130pub struct GameResult {
131    pub winners: Vec<String>,
132    pub losers: Vec<String>,
133}
134
135#[derive(Debug, Serialize, Deserialize, Clone)]
136pub enum LobbyLeftReason {
137    RequestedByPlayer
138}
139
140#[derive(Debug, Serialize, Deserialize, Clone)]
141pub struct HookOutcome {
142    pub fisher_name: String,
143    pub target_name: String,
144    pub rank: Rank,
145    pub result: HookResult,
146}
147
148#[derive(Debug, Serialize, Deserialize, Clone)]
149pub struct OpponentState {
150    pub name: String,
151    pub card_count: usize,
152    pub completed_books: Vec<CompleteBook>,
153}
154
155#[derive(Debug, Serialize, Deserialize, Clone)]
156pub struct GameSnapshot {
157    pub hand_state: HandState,
158    pub opponents: Vec<OpponentState>,
159    pub active_player: String,
160    pub last_hook_outcome: Option<HookOutcome>,
161    pub deck_size: usize,
162}