-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontroller.go
304 lines (269 loc) · 6.52 KB
/
controller.go
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
300
301
302
303
304
package main
import (
"fmt"
"sort"
"sync"
"time"
)
type GameController struct {
Games map[guid]*Game
auth authenticator
}
type PublicPlayer struct {
GUID guid `json:"playerID"`
Handle string `json:"handle"`
State string `json:"state"`
Wealth money `json:"wealth"`
InFor money `json:"bet_so_far"`
SmallBlind bool `json:"small_blind"`
}
type PublicTable []*PublicPlayer
type Turn struct {
Player guid `json:"playerID"`
PlayerBet money `json:"bet_so_far"`
BetToPlayer money `json:"bet_to_player"`
MinRaise money `json:"minimum_raise"`
Expiry string `json:"expiry"`
}
type PublicCards struct {
Hole []string `json:"hole"`
Flop []string `json:"flop"`
Turn []string `json:"turn"`
River []string `json:"river"`
}
type PublicPots []*PublicPot
type PublicPot struct {
Size money `json:"size"`
Players []guid `json:"players"`
}
type PublicGame struct {
GameID string `json:"gameID"`
Table PublicTable `json:"table"`
Turn *Turn `json:"turn"`
Cards *PublicCards `json:"cards"`
Pots *PublicPots `json:"pots"`
LastHandWinners []Playerhand `json:"last_winners"`
}
type authenticator map[guid]guid
const TIMEOUT = 100
func (gc *GameController) getGames() []*Game {
gs := make([]*Game, 0)
for _, g := range gc.Games {
gs = append(gs, g)
}
return gs
}
func (gc *GameController) getGame(game guid) PublicGame {
return *gc.Games[game].controller.public
}
func (gc *GameController) makeGame() *PublicGame {
g := NewGame(gc)
gc.Games[g.gameID] = g
go g.run()
pg := MakePublicGame(g)
return pg
}
func MakeTurn() *Turn {
turn := new(Turn)
turn.Player = "" // ??
turn.Expiry = "" // ??
return turn
}
func MakePublicGame(g *Game) *PublicGame {
pg := new(PublicGame)
pg.GameID = string(g.gameID)
pg.Table = make(PublicTable, 0)
for _, player := range g.table {
pg.Table = append(pg.Table, MakePublicPlayer(g, player))
}
pg.Turn = MakeTurn()
pg.Turn.BetToPlayer = g.pot.totalToCall
pg.Turn.MinRaise = g.pot.minRaise
pg.Turn.Player = "" // ??
pg.Turn.Expiry = "" // ??
pg.Cards = MakePublicCards(g)
pg.Pots = MakePublicPots(g)
return pg
}
func (gc *GameController) getGamePrivate(game, player guid) (pg PublicGame) {
g := gc.Games[game]
pg = gc.getGame(game)
pg.Cards = MakePublicCards(g)
pg.Cards.Hole = g.deck.Get(string(player))
return pg
}
func MakePublicCards(g *Game) (pc *PublicCards) {
pc = new(PublicCards)
switch g.round {
case 3: // river
pc.River = g.deck.Get("RIVER")
fallthrough
case 2: // turn
pc.Turn = g.deck.Get("TURN")
fallthrough
case 1: // flop
pc.Flop = g.deck.Get("FLOP")
fallthrough
default: // pre-flop
}
return pc
}
func (deck Deck) Get(wanted string) (cards []string) {
cards = make([]string, 0)
for card, location := range deck {
if location == wanted {
cards = append(cards, card)
}
}
sort.Strings(cards)
return cards
}
func MakePublicPlayer(g *Game, p *Player) (pp *PublicPlayer) {
pp = new(PublicPlayer)
pp.Wealth = p.wealth
pp.GUID = p.guid
pp.InFor = g.pot.totalPlayerBetThisRound(p.guid)
switch p.state {
case 0:
pp.State = "active"
case 1:
pp.State = "folded"
case 2:
pp.State = "called"
default:
panic("unknown state")
}
if g.table[0] == p {
pp.SmallBlind = true
}
return pp
}
func MakePublicPots(g *Game) (pp *PublicPots) {
pp = new(PublicPots)
for _, bet := range g.pot.bets {
for int(bet.potNumber) >= len(*pp) {
*pp = append(*pp, new(PublicPot))
}
pot := (*pp)[bet.potNumber]
if !contains(pot.Players, bet.player) {
pot.Players = append(pot.Players, bet.player)
}
pot.Size += bet.value
(*pp)[bet.potNumber] = pot
}
return pp
}
func contains(haystack []guid, needle guid) bool {
for _, g := range haystack {
if g == needle {
return true
}
}
return false
}
func NewGameController() (gc *GameController) {
gc = new(GameController)
gc.Games = make(map[guid]*Game)
return gc
}
type controller struct {
toGame chan Act
public *PublicGame
waiting []*Player
sync.Mutex
}
type Playerhand struct {
PlayerID guid
Hand Hand
}
func (c *controller) recordWinners(winners []*Player) {
playerhands := make([]Playerhand, 0)
for _, player := range winners {
playerhands = append(playerhands, Playerhand{PlayerID: player.guid, Hand: player.bestHand})
}
c.public.LastHandWinners = playerhands
}
func (c *controller) getNewPlayers(g *Game, openSeats int) (players []*Player) {
c.Lock()
defer c.Unlock()
x := openSeats
if len(c.waiting) == 0 {
return players
}
if len(c.waiting) < openSeats {
x = len(c.waiting)
}
players = c.waiting[:x]
c.waiting = c.waiting[x:len(c.waiting)]
return players
}
func (c *controller) enqueuePlayer(g *Game, p *Player) error {
c.Lock()
defer c.Unlock()
for _, player := range c.waiting {
if player.guid == p.guid {
return fmt.Errorf("controller: player %v is already queued to join table")
}
}
for _, player := range g.table {
if player.guid == p.guid {
return fmt.Errorf("controller: player %v is already sitting at the table", p.guid)
}
}
c.waiting = append(c.waiting, p)
return nil
}
type Act struct {
Player guid
Action int
BetAmount money
}
func (c *controller) getPlayerBet(g *Game, wanted guid) (int, money, error) {
pg := MakePublicGame(g)
pg.LastHandWinners = c.public.LastHandWinners
c.public = pg
c.public.Turn.Player = wanted
c.public.Turn.PlayerBet = g.pot.totalPlayerBetThisRound(wanted)
c.public.Turn.Expiry = time.Now().Add(TIMEOUT * time.Second).String()
timeout := time.After(TIMEOUT * time.Second)
for {
select {
case <-timeout:
return 0, 0, fmt.Errorf("controller: timed out waiting for bet from player %v", wanted)
case a := <-c.toGame:
return a.Action, a.BetAmount, nil
}
}
panic("Unreachable")
}
func (c *controller) registerPlayerAct(a Act) error {
if a.Player != c.public.Turn.Player {
return fmt.Errorf("controller: not this player's turn: %v", a.Player)
}
c.toGame <- a
return nil
}
func NewPlayer(id guid) (p *Player) {
p = new(Player)
p.guid = id
p.wealth = BUY_IN
return p
}
func NewController(g *Game) *controller {
c := new(controller)
c.toGame = make(chan Act)
c.public = MakePublicGame(g)
c.waiting = make([]*Player, 0)
return c
}
func (c *controller) registerInvalidBet(g *Game, player guid, bet money) { return }
func (c *controller) removePlayerFromGame(g *Game, player guid) {
replacementPlayers := make([]*Player, 0)
for i := 0; i < len(g.table); i++ {
if g.table[i].guid != player {
replacementPlayers = append(replacementPlayers, g.table[i])
}
}
g.table = replacementPlayers
return
}