aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/table.go
blob: be2ea182a858703c4e58c0cf1e9a521ab0cc546f (plain)
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
package casino

import (
	"fmt"
	"math/rand"
	"sync"
)

// tableGame is a multiplayer table game implementation. Table routes every
// player interaction through this interface so new games plug in without
// changing the dispatch layer.
type tableGame interface {
	join(name string) []Event
	leave(name string) []Event
	bet(name, spot string, amount int, wallet Wallet) []Event
	tick() []Event
	action(name, action string) []Event
	rebet(name string) (spot string, amount int)
	markDisconnected(name string) []Event
	markConnected(name string)
	hasParticipant(name string) bool
}

// tableBase carries the identity every table game needs to build events.
type tableBase struct {
	RoomID int
	Config TableConfig
}

func (b tableBase) event(message string, public bool) Event {
	return Event{RoomID: b.RoomID, TableID: b.Config.ID, Type: EventResult, Message: message, Public: public, Color: "casino_action"}
}

func (b tableBase) private(name, message string) Event {
	e := b.event(message, false)
	e.PrivateTo = name
	return e
}

func (b tableBase) playerAction(name, publicMessage, privateMessage string) Event {
	e := b.event(publicMessage, true)
	e.PrivateTo = name
	e.PrivateMessage = privateMessage
	return e
}

// Table is the manager-facing handle for a multiplayer table. It only holds
// routing state; all game logic lives in the tableGame implementation.
type Table struct {
	RoomID int
	Config TableConfig
	game   tableGame
}

func newTable(roomID int, cfg TableConfig, rng *rand.Rand) *Table {
	cfg = cfg.Normalize()
	t := &Table{RoomID: roomID, Config: cfg}
	switch cfg.Game {
	case GameBlackjack:
		t.game = newBlackjackTable(roomID, cfg, rng)
	case GameBaccarat:
		t.game = newBaccaratTable(roomID, cfg, rng)
	default:
		t.game = newGenericTable(roomID, cfg, rng)
	}
	return t
}

func (t *Table) join(name string) []Event  { return t.game.join(name) }
func (t *Table) leave(name string) []Event { return t.game.leave(name) }

func (t *Table) bet(name, spot string, amount int, wallet Wallet) []Event {
	return t.game.bet(name, spot, amount, wallet)
}

func (t *Table) tick() []Event                      { return t.game.tick() }
func (t *Table) action(name, action string) []Event { return t.game.action(name, action) }
func (t *Table) rebet(name string) (string, int)    { return t.game.rebet(name) }

func (t *Table) markDisconnected(name string) []Event { return t.game.markDisconnected(name) }
func (t *Table) markConnected(name string)            { t.game.markConnected(name) }
func (t *Table) hasParticipant(name string) bool      { return t.game.hasParticipant(name) }

type tablePhase string

const (
	phaseWaiting tablePhase = "waiting"
	phaseBetting tablePhase = "betting"
)

// genericTable is the fallback table game: every bettor's wager resolves
// immediately against a simple spin once all connected players have bet.
type genericTable struct {
	mu     sync.Mutex
	RoomID int
	Config TableConfig

	participants map[string]*Participant
	order        []string
	phase        tablePhase
	timer        countdown
	rng          *rand.Rand
}

func newGenericTable(roomID int, cfg TableConfig, rng *rand.Rand) *genericTable {
	return &genericTable{
		RoomID:       roomID,
		Config:       cfg,
		phase:        phaseWaiting,
		participants: make(map[string]*Participant),
		rng:          rng,
	}
}

func (t *genericTable) seats() []*Participant {
	seats := make([]*Participant, 0, len(t.order))
	for _, name := range t.order {
		if p := t.participants[name]; p != nil {
			seats = append(seats, p)
		}
	}
	return seats
}

func (t *genericTable) join(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = true
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventJoined, Player: name, PrivateTo: name, Message: fmt.Sprintf("You resume your place at the %s table.", t.Config.ID)}}
	}
	if len(t.participants) >= t.Config.MaxPlayers {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "That table is full."}}
	}
	t.participants[name] = &Participant{Name: name, Connected: true}
	t.order = append(t.order, name)
	return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventJoined, Player: name, Message: fmt.Sprintf("%s joins the %s table.", name, t.Config.ID), Public: true}}
}

func (t *genericTable) leave(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	p := t.participants[name]
	if p == nil {
		return nil
	}
	if t.phase != phaseWaiting {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "You're in the middle of a game!"}}
	}
	delete(t.participants, name)
	for i, n := range t.order {
		if n == name {
			t.order = append(t.order[:i], t.order[i+1:]...)
			break
		}
	}
	events := []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventLeft, Player: name, Message: fmt.Sprintf("%s stops playing at the %s table.", name, t.Config.ID), Public: true}}
	if len(t.participants) == 0 {
		events = append(events, Event{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventClosed, Message: fmt.Sprintf("The %s table closes.", t.Config.ID), Public: true})
	}
	return events
}

func (t *genericTable) bet(name, spot string, amount int, wallet Wallet) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	p := t.participants[name]
	if p == nil {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "You aren't playing at that table."}}
	}
	if spot != "" {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "That table doesn't take a bet target."}}
	}
	if t.phase != phaseWaiting && t.phase != phaseBetting {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "That table isn't accepting bets."}}
	}
	if p.HasBet {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "You have already bet this round."}}
	}
	if amount < t.Config.MinBet || amount > t.Config.MaxBet {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: fmt.Sprintf("Bets must be between %d and %d.", t.Config.MinBet, t.Config.MaxBet)}}
	}
	wager, ok := wallet.Wager(amount)
	if !ok {
		return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, Player: name, PrivateTo: name, Message: "You don't have enough chips and credits for that bet."}}
	}
	p.HasBet = true
	p.Wager = wager
	p.Wallet = wallet
	if t.phase == phaseWaiting && len(t.participants) > 1 {
		t.phase = phaseBetting
		t.timer.start(t.Config.BettingTicks)
	}
	events := []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventBetPlaced, Player: name, Message: fmt.Sprintf("%s bets %d credits at the %s table.", name, amount, t.Config.ID), Public: true}}
	if t.phase == phaseBetting && t.timer.clock == 0 {
		events = append(events, Event{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventBetting, Message: fmt.Sprintf("Betting is open at the %s table for %d ticks.", t.Config.ID, t.Config.BettingTicks), Public: true})
	}
	if len(t.participants) == 1 || allBet(t.seats()) {
		events = append(events, t.resolveRound()...)
	}
	return events
}

func (t *genericTable) tick() []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.phase != phaseBetting {
		return nil
	}
	t.timer.tick()
	if allBet(t.seats()) {
		return t.resolveRound()
	}
	if !t.timer.expired() {
		return nil
	}
	var events []Event
	for _, name := range sitOutNames(t.seats()) {
		events = append(events, Event{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventTimedOut, Player: name, Message: fmt.Sprintf("%s sits out this round.", name), Public: true})
	}
	if anyBet(t.seats()) {
		events = append(events, t.resolveRound()...)
	} else {
		t.resetRound()
	}
	return events
}

func (t *genericTable) resolveRound() []Event {
	var events []Event
	for _, name := range t.order {
		p := t.participants[name]
		if p == nil || !p.HasBet {
			continue
		}
		result := slotResult(t.rng, p.Wager.Total)
		message := fmt.Sprintf("%s spins: %s", name, result.Display)
		events = append(events, Event{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventRoundStarted, Player: name, Message: message, Public: true})
		if result.Payout > 0 {
			if p.Wallet != nil {
				p.Wallet.PayCredits(result.Payout)
			}
			events = append(events, Event{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventPayout, Player: name, PrivateTo: name, Message: fmt.Sprintf("You win %d credits!", result.Payout)})
		}
	}
	t.resetRound()
	return events
}

func (t *genericTable) resetRound() {
	t.phase = phaseWaiting
	t.timer.stop()
	clearBets(t.seats())
}

func (t *genericTable) markDisconnected(name string) []Event {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = false
	}
	return nil
}

func (t *genericTable) markConnected(name string) {
	t.mu.Lock()
	defer t.mu.Unlock()
	if p := t.participants[name]; p != nil {
		p.Connected = true
	}
}

func (t *genericTable) hasParticipant(name string) bool {
	t.mu.Lock()
	defer t.mu.Unlock()
	return t.participants[name] != nil
}

func (t *genericTable) action(name, action string) []Event {
	return []Event{{RoomID: t.RoomID, TableID: t.Config.ID, Type: EventResult, PrivateTo: name, Message: "That game has no player actions."}}
}

func (t *genericTable) rebet(name string) (string, int) {
	return "", t.Config.MinBet
}