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
|
package game
import (
"os"
"path/filepath"
"strings"
"testing"
"thehouseoficarus/internal/casino"
"thehouseoficarus/internal/combat"
"thehouseoficarus/internal/engine"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
)
// quitTestGame builds the minimal live state quit and stop need: a casino
// manager, a combat tracker, a tick engine for the rest countdown, and a
// durable wallet (economy journal + account store) so wagers can commit.
func quitTestGame(t *testing.T) (*Game, *net.Session) {
t.Helper()
dataDir := t.TempDir()
// Character saves (wallet persistence) need the player directory.
if err := os.MkdirAll(filepath.Join(dataDir, "players", "characters"), 0o755); err != nil {
t.Fatal(err)
}
sess := &net.Session{Player: player.New("alice"), Conn: &casinoTestConn{}}
g := &Game{
Deps: Deps{AccountStore: player.NewAccountStore(dataDir), Ticks: engine.New()},
Casino: casino.NewManager(1),
Combat: combat.NewTracker(),
Economy: newEconomyJournal(t.TempDir()),
safespot: NewSafespotManager(),
loggedInChars: map[string]*net.Session{"alice": sess},
restTimers: make(map[string]uint64),
}
sess.Player.Credits = 1000
return g, sess
}
func (g *Game) quitResting(name string) bool {
_, ok := g.restTimers[name]
return ok
}
// A wager committed to the round must block a deliberate quit: the player
// stays seated with the stake intact and the quit countdown never starts.
func TestQuitBlockedMidRoundKeepsWager(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob") // second seat opens the betting window
conn := sess.Conn.(*casinoTestConn)
if events := g.Casino.Bet(1, "blackjack", "alice", "", 10, casinoWallet{g: g, p: sess.Player}); len(events) == 0 {
t.Fatal("bet produced no events")
}
conn.data = nil
g.doQuit(sess)
if !strings.Contains(string(conn.data), "middle of a blackjack round") {
t.Fatalf("expected mid-round refusal, got %q", string(conn.data))
}
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("quit mid-round removed the player from the table")
}
if g.quitResting("alice") {
t.Fatal("blocked quit started the rest countdown")
}
// The stake is still committed to the round, not refunded.
if events := g.Casino.Leave(1, "blackjack", "alice"); len(events) == 0 || !strings.Contains(events[0].Message, "middle of a blackjack round") {
t.Fatalf("wager was not still committed after the blocked quit: %+v", events)
}
}
// Baccarat blocks a mid-round quit the same way.
func TestQuitBlockedMidRoundBaccarat(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.TableConfig{ID: "baccarat", Game: casino.GameBaccarat, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob") // second seat opens the betting window
conn := sess.Conn.(*casinoTestConn)
g.Casino.Bet(1, "baccarat", "alice", "player", 10, casinoWallet{g: g, p: sess.Player})
conn.data = nil
g.doQuit(sess)
if !strings.Contains(string(conn.data), "middle of a baccarat round") {
t.Fatalf("expected mid-round refusal, got %q", string(conn.data))
}
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("quit mid-round removed the player from the baccarat table")
}
if g.quitResting("alice") {
t.Fatal("blocked quit started the rest countdown")
}
}
// With no wager committed, quit leaves the table gracefully (the same
// stand-up the table emits for "stop") and starts the rest countdown.
func TestQuitLeavesTableBetweenRounds(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
conn := sess.Conn.(*casinoTestConn)
g.doQuit(sess)
if !strings.Contains(string(conn.data), "You stand up.") {
t.Fatalf("expected a graceful stand-up, got %q", string(conn.data))
}
if _, playing := g.Casino.Participation("alice"); playing {
t.Fatal("quit did not free the table seat")
}
if !g.quitResting("alice") {
t.Fatal("quit did not start the rest countdown")
}
}
// Quit at a slot machine stops any auto-bet (or leaves the machine) and
// starts the rest countdown; it never force-disconnects the player.
func TestQuitLeavesMachine(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.MachineConfig{ID: "slots", Game: casino.GameSlots, MinBet: 1, MaxBet: 100}
g.Casino.JoinMachine(1, cfg, "alice")
conn := sess.Conn.(*casinoTestConn)
g.doQuit(sess)
if _, playing := g.Casino.Participation("alice"); playing {
t.Fatal("quit did not free the machine")
}
if !g.quitResting("alice") {
t.Fatal("quit did not start the rest countdown")
}
if strings.Contains(string(conn.data), "WARNING") {
t.Fatalf("quit at a machine must not warn or forfeit: %q", string(conn.data))
}
}
// "stop" is an instant command, so it must cancel a pending quit countdown
// explicitly.
func TestStopCancelsQuit(t *testing.T) {
g, sess := quitTestGame(t)
g.restTimers["alice"] = 42
g.executeStop(sess, nil, "stop")
if g.quitResting("alice") {
t.Fatal("stop did not cancel the pending quit")
}
}
// Plain quit mid-round points the player at "quit now" and its forfeit.
func TestQuitMidRoundMentionsQuitNow(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob")
g.Casino.Bet(1, "blackjack", "alice", "", 10, casinoWallet{g: g, p: sess.Player})
conn := sess.Conn.(*casinoTestConn)
conn.data = nil
g.doQuit(sess)
if !strings.Contains(string(conn.data), `"quit now"`) || !strings.Contains(string(conn.data), "forfeit") {
t.Fatalf("expected a quit-now reminder with the forfeit warning, got %q", string(conn.data))
}
}
// "quit now" starts the logout countdown while the player keeps their seat
// and wager; the stake is forfeited and the seat freed only on the final
// tick, immediately before the disconnect.
func TestQuitNowForfeitsOnlyAtLogout(t *testing.T) {
g, sess := quitTestGame(t)
sess.Account = &player.Account{Name: "alice"}
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob")
g.Casino.Bet(1, "blackjack", "alice", "", 10, casinoWallet{g: g, p: sess.Player})
wallet := sess.Player
g.doQuitNow(sess)
// Still seated, still committed, countdown running.
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("quit now freed the seat before the logout tick")
}
if !g.quitResting("alice") {
t.Fatal("quit now did not start the rest countdown")
}
// Drive the countdown to its final tick (fires on the 11th engine tick).
for i := 0; i < 15 && sess.Player != nil; i++ {
g.Ticks.Tick()
}
if _, playing := g.Casino.Participation("alice"); playing {
t.Fatal("quit now did not free the seat at the logout tick")
}
// Alice's stake was debited when she bet and never refunded or paid: the
// forfeit. Bob remains seated with his round untouched.
if wallet.Credits != 990 {
t.Fatalf("alice's credits were disturbed by the forfeit: %d", wallet.Credits)
}
if _, playing := g.Casino.Participation("bob"); !playing {
t.Fatal("quit now removed the wrong player")
}
if sess.Player != nil || sess.State != net.StateMenu {
t.Fatal("quit now did not log the player out")
}
if _, stillLoggedIn := g.loggedInChars["alice"]; stillLoggedIn {
t.Fatal("quit now did not remove the session from the logged-in map")
}
if g.quitResting("alice") {
t.Fatal("countdown timer was not cleared after logout")
}
}
// Until the logout tick, "stop" cancels a pending quit-now and leaves the
// wager intact: the player keeps their seat mid-round.
func TestQuitNowCancelledByStopMidRound(t *testing.T) {
g, sess := quitTestGame(t)
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob")
g.Casino.Bet(1, "blackjack", "alice", "", 10, casinoWallet{g: g, p: sess.Player})
g.doQuitNow(sess)
g.executeStop(sess, nil, "stop")
if g.quitResting("alice") {
t.Fatal("stop did not cancel the pending quit-now countdown")
}
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("cancelled quit-now still freed the seat")
}
// The wager is still committed after the cancellation.
if events := g.Casino.Leave(1, "blackjack", "alice"); len(events) == 0 || !strings.Contains(events[0].Message, "middle of a blackjack round") {
t.Fatalf("wager was not still committed after the cancelled quit-now: %+v", events)
}
}
// A quit-now at a machine also holds the machine until the logout tick.
func TestQuitNowAtMachineHoldsUntilLogout(t *testing.T) {
g, sess := quitTestGame(t)
sess.Account = &player.Account{Name: "alice"}
cfg := casino.MachineConfig{ID: "slots", Game: casino.GameSlots, MinBet: 1, MaxBet: 100}
g.Casino.JoinMachine(1, cfg, "alice")
g.doQuitNow(sess)
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("quit now freed the machine before the logout tick")
}
for i := 0; i < 15 && sess.Player != nil; i++ {
g.Ticks.Tick()
}
if _, playing := g.Casino.Participation("alice"); playing {
t.Fatal("quit now did not free the machine at the logout tick")
}
if sess.Player != nil || sess.State != net.StateMenu {
t.Fatal("quit now did not log the player out")
}
}
// Stop and quit share one departure path: both refuse mid-round and both
// leave gracefully between rounds.
func TestStopMatchesQuitCasinoPolicy(t *testing.T) {
cfg := casino.TableConfig{ID: "blackjack", Game: casino.GameBlackjack, MinBet: 1, MaxBet: 100}
// Mid-round: stop refuses just like quit.
g, sess := quitTestGame(t)
g.Casino.Join(1, cfg, "alice")
g.Casino.Join(1, cfg, "bob")
g.Casino.Bet(1, "blackjack", "alice", "", 10, casinoWallet{g: g, p: sess.Player})
g.restTimers["alice"] = 42
conn := sess.Conn.(*casinoTestConn)
conn.data = nil
g.executeStop(sess, nil, "stop")
if !strings.Contains(string(conn.data), "middle of a blackjack round") {
t.Fatalf("expected mid-round refusal, got %q", string(conn.data))
}
if _, playing := g.Casino.Participation("alice"); !playing {
t.Fatal("stop mid-round removed the player from the table")
}
// Between rounds: stop stands the player up.
g2, sess2 := quitTestGame(t)
g2.Casino.Join(1, cfg, "alice")
conn2 := sess2.Conn.(*casinoTestConn)
g2.executeStop(sess2, nil, "stop")
if !strings.Contains(string(conn2.data), "You stand up.") {
t.Fatalf("expected a graceful stand-up, got %q", string(conn2.data))
}
if _, playing := g2.Casino.Participation("alice"); playing {
t.Fatal("stop did not free the table seat")
}
}
|