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
|
//! What a frontend draws and what it can ask the core to do.
//!
//! The core owns state and navigation; adapters map input to `Command` and
//! render `View`. Terminal text lives in [`crate::client::tui`], not here.
use crate::blackjack::BlackjackState;
use crate::client::games::roulette::{Cursor, Direction};
use crate::client::games::traits::{GamePhase, GameState};
use crate::roulette::{BetTarget, Bets};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RoulettePhase {
Placing,
Spinning,
Result,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RouletteView {
pub bet_amount: u64,
pub balance: Option<i64>,
pub chip: u64,
pub bets: Bets,
pub cursor: Cursor,
pub phase: RoulettePhase,
pub number: Option<u8>,
pub color: Option<String>,
pub winnings: Option<u64>,
pub error: Option<String>,
}
impl RouletteView {
pub fn from_state(state: &GameState) -> Self {
let bets = state
.data
.get("bets")
.and_then(|raw| Bets::from_json(raw).ok())
.unwrap_or_default();
let chip = state
.data
.get("chip")
.and_then(|value| value.parse().ok())
.unwrap_or(10);
let cursor = state
.data
.get("cursor")
.map(|raw| Cursor::parse(raw))
.unwrap_or_default();
let phase = match state.data.get("status").map(String::as_str) {
Some("spinning") => RoulettePhase::Spinning,
Some("result") => RoulettePhase::Result,
_ => RoulettePhase::Placing,
};
let number = state.data.get("number").and_then(|value| value.parse().ok());
let color = state.data.get("color").cloned();
let winnings = state
.data
.get("winnings")
.and_then(|value| value.parse().ok());
let balance = state
.data
.get("balance")
.and_then(|value| value.parse().ok());
let error = state
.data
.get("error")
.cloned()
.filter(|message| !message.is_empty());
Self {
bet_amount: state.bet_amount,
balance,
chip,
bets,
cursor,
phase,
number,
color,
winnings,
error,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotsPhase {
Ready,
Spinning,
Result,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SlotsView {
pub bet: u64,
pub balance: Option<i64>,
pub reels: Vec<char>,
pub win_cells: Vec<usize>,
pub win_lines: Vec<usize>,
pub win_type: String,
pub winnings: Option<u64>,
pub phase: SlotsPhase,
pub error: Option<String>,
}
fn parse_indices(raw: Option<&String>) -> Vec<usize> {
raw.map(|value| {
value
.split(',')
.filter_map(|part| part.trim().parse().ok())
.collect()
})
.unwrap_or_default()
}
impl SlotsView {
pub fn from_state(state: &GameState) -> Self {
let reels = state
.data
.get("reels")
.map(|raw| raw.chars().collect())
.unwrap_or_default();
let phase = match state.phase {
GamePhase::Playing => SlotsPhase::Spinning,
GamePhase::Finished => SlotsPhase::Result,
GamePhase::WaitingToStart => SlotsPhase::Ready,
};
Self {
bet: state.bet_amount,
balance: state
.data
.get("balance")
.and_then(|value| value.parse().ok()),
reels,
win_cells: parse_indices(state.data.get("win_cells")),
win_lines: parse_indices(state.data.get("win_lines")),
win_type: state.data.get("win_type").cloned().unwrap_or_default(),
winnings: state
.data
.get("winnings")
.and_then(|value| value.parse().ok()),
phase,
error: state
.data
.get("error")
.cloned()
.filter(|message| !message.is_empty()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlackjackView {
pub status: String,
pub balance: Option<i64>,
pub bet: u64,
pub hand: Option<BlackjackState>,
pub error: Option<String>,
}
impl BlackjackView {
pub fn from_state(state: &GameState) -> Self {
let hand = state
.data
.get("blackjack")
.and_then(|raw| serde_json::from_str(raw).ok());
Self {
status: state
.data
.get("status")
.cloned()
.unwrap_or_else(|| "ready".to_string()),
balance: state
.data
.get("balance")
.and_then(|value| value.parse().ok()),
bet: state.bet_amount,
hand,
error: state
.data
.get("error")
.cloned()
.filter(|message| !message.is_empty()),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CrashView {
pub status: String,
pub balance: Option<i64>,
pub bet: u64,
pub multiplier: f64,
pub potential: u64,
pub crash_point: Option<f64>,
pub payout: u64,
pub error: Option<String>,
}
impl CrashView {
pub fn from_state(state: &GameState) -> Self {
let bet = state.bet_amount;
Self {
status: state
.data
.get("status")
.cloned()
.unwrap_or_else(|| "ready".to_string()),
balance: state
.data
.get("balance")
.and_then(|value| value.parse().ok()),
bet,
multiplier: state
.data
.get("multiplier")
.and_then(|value| value.parse::<f64>().ok())
.unwrap_or(1.0),
potential: state
.data
.get("potential")
.and_then(|value| value.parse::<u64>().ok())
.unwrap_or(bet),
crash_point: state
.data
.get("crash_point")
.and_then(|value| value.parse::<f64>().ok()),
payout: state
.data
.get("payout")
.and_then(|value| value.parse::<u64>().ok())
.unwrap_or(0),
error: state
.data
.get("error")
.cloned()
.filter(|message| !message.is_empty()),
}
}
}
/// The lobby: who is logged in, the balance, and what can be played.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LobbyView {
pub connected: bool,
pub user: Option<String>,
pub balance: Option<i64>,
pub games: Vec<(String, String)>,
/// Why the last login or registration attempt failed.
pub login_error: Option<String>,
}
/// The play log, already parsed.
#[derive(Debug, Clone, PartialEq)]
pub struct HistoryView {
pub records: Vec<crate::history::PlayRecord>,
}
/// Everything a frontend can draw.
#[derive(Debug, Clone, PartialEq)]
pub enum View {
Lobby(LobbyView),
History(HistoryView),
Slots(SlotsView),
Roulette(RouletteView),
Blackjack(BlackjackView),
Crash(CrashView),
}
/// Semantic input. Frontends turn keys and clicks into these.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
/// The credential is already hashed by the frontend.
Login { user: String, credential: String },
Register { user: String, credential: String },
Play { game: String, bet: Option<u64> },
History { limit: u32 },
ShowLobby,
Logout,
SetBet(u64),
AdjustBet(i64),
Leave,
Spin,
Deal,
Hit,
Stand,
Double,
Start,
CashOut,
Forfeit,
MoveCursor(Direction),
PlaceBet,
PlaceBetAt(BetTarget),
ClearBets,
SetChip(u64),
AdjustChip(i64),
}
|