commit 45f29329e3c2aba1ff24b9f960fbc4e86458f895
parent 8fb77693c13c433159c6b6612803f8980e1e6a18
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Tue, 19 Dec 2023 01:35:10 -0500
cleanup
Diffstat:
1 file changed, 40 insertions(+), 40 deletions(-)
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -55,7 +55,7 @@ func (p *Poker) GetOrCreateGame(db *database.DkfDB, roomID RoomID, pokerTableID
tableType: TableTypeRake,
PokerTableMinBet: pokerTableMinBet,
pokerTableIsTest: pokerTableIsTest,
- playersEventCh: make(chan PlayerEvent),
+ playersEventCh: make(chan playerEvent),
Players: utils.NewRWMtx(make([]*seatedPlayer, NbPlayers)),
dealerSeatIdx: atomic.Int32{},
}
@@ -75,7 +75,7 @@ func (p *Poker) GetGame(roomID RoomID) *PokerGame {
return g
}
-type PlayerEvent struct {
+type playerEvent struct {
UserID database.UserID
Call bool
Check bool
@@ -85,7 +85,7 @@ type PlayerEvent struct {
Bet database.PokerChip
}
-func (e PlayerEvent) getAction() PlayerAction {
+func (e playerEvent) getAction() PlayerAction {
action := NoAction
if e.Fold {
action = FoldAction
@@ -104,10 +104,10 @@ func (e PlayerEvent) getAction() PlayerAction {
var PokerInstance = NewPoker()
type Ongoing struct {
+ LogEvents utils.RWMtx[[]LogEvent]
deck []string
players []*PokerPlayer
events utils.RWMtx[[]PokerEvent]
- LogEvents utils.RWMtx[[]LogEvent]
waitTurnEvent utils.RWMtx[PokerWaitTurnEvent]
autoActionEvent utils.RWMtx[AutoActionEvent]
createdAt time.Time
@@ -123,14 +123,14 @@ type seatedPlayer struct {
lastActionTS time.Time
}
-func (p *seatedPlayer) GetCash() (out database.PokerChip) {
+func (p *seatedPlayer) getCash() (out database.PokerChip) {
p.cash.RWith(func(v *database.PokerChip) { out = *v })
return
}
// Return either or not a player is eligible to play a game
func (p *seatedPlayer) isEligible(pokerTableMinBet database.PokerChip) bool {
- return p != nil && p.GetCash() >= pokerTableMinBet
+ return p != nil && p.getCash() >= pokerTableMinBet
}
type PokerPlayer struct {
@@ -155,7 +155,7 @@ func (p *PokerPlayer) canBet() bool {
}
func (p *PokerPlayer) isAllIn() bool {
- return p.GetCash() == 0
+ return p.getCash() == 0
}
func (p *PokerPlayer) refundPartialBet(db *database.DkfDB, pokerTableID int64, diff database.PokerChip) {
@@ -195,7 +195,7 @@ func (p *PokerPlayer) refundBet(db *database.DkfDB, pokerTableID int64) {
func (p *PokerPlayer) doBetAndNotif(db *database.DkfDB, pokerTableID int64, bet database.PokerChip, roomTopic string) {
p.doBet(db, pokerTableID, bet)
- PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerSeatIdx: p.seatIdx, Player: p.username, Bet: bet, TotalBet: p.GetBet(), Cash: p.GetCash()})
+ PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerSeatIdx: p.seatIdx, Player: p.username, Bet: bet, TotalBet: p.GetBet(), Cash: p.getCash()})
}
type playerCard struct {
@@ -212,7 +212,7 @@ type PokerGame struct {
tableType int
PokerTableMinBet database.PokerChip
pokerTableIsTest bool
- playersEventCh chan PlayerEvent
+ playersEventCh chan playerEvent
dealerSeatIdx atomic.Int32
isGameStarted atomic.Bool
}
@@ -223,30 +223,30 @@ type gameResult struct {
}
func (g *PokerGame) Check(userID database.UserID) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, Check: true})
+ g.sendPlayerEvent(playerEvent{UserID: userID, Check: true})
}
func (g *PokerGame) AllIn(userID database.UserID) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, AllIn: true})
+ g.sendPlayerEvent(playerEvent{UserID: userID, AllIn: true})
}
func (g *PokerGame) Bet(userID database.UserID, bet database.PokerChip) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, Bet: bet})
+ g.sendPlayerEvent(playerEvent{UserID: userID, Bet: bet})
}
func (g *PokerGame) Call(userID database.UserID) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, Call: true})
+ g.sendPlayerEvent(playerEvent{UserID: userID, Call: true})
}
func (g *PokerGame) Fold(userID database.UserID) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, Fold: true})
+ g.sendPlayerEvent(playerEvent{UserID: userID, Fold: true})
}
func (g *PokerGame) sendUnsitPlayerEvent(userID database.UserID) {
- g.sendPlayerEvent(PlayerEvent{UserID: userID, Unsit: true})
+ g.sendPlayerEvent(playerEvent{UserID: userID, Unsit: true})
}
-func (g *PokerGame) sendPlayerEvent(evt PlayerEvent) {
+func (g *PokerGame) sendPlayerEvent(evt playerEvent) {
select {
case g.playersEventCh <- evt:
default:
@@ -325,10 +325,10 @@ func (g *Ongoing) computeWinners() (winner []gameResult) {
func sortGameResults(arr []gameResult) {
for idx := range arr {
sort.Slice(arr[idx].players, func(i, j int) bool {
- if arr[idx].players[i].GetCash() == arr[idx].players[j].GetCash() {
+ if arr[idx].players[i].getCash() == arr[idx].players[j].getCash() {
return arr[idx].players[i].GameBet < arr[idx].players[j].GameBet
}
- return arr[idx].players[i].GetCash() < arr[idx].players[j].GetCash()
+ return arr[idx].players[i].getCash() < arr[idx].players[j].getCash()
})
}
sort.Slice(arr, func(i, j int) bool { return arr[i].handScore < arr[j].handScore })
@@ -636,9 +636,9 @@ const (
breakGetPlayerEventLoop
)
-type AutoAction struct {
+type autoAction struct {
action PlayerAction
- evt PlayerEvent
+ evt playerEvent
}
func foldPlayer(g *PokerGame, p *PokerPlayer) {
@@ -706,7 +706,7 @@ func doCheck(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip) int {
func doCall(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
newlyAllInPlayers *[]*PokerPlayer) int {
pUsername := p.username
- bet := utils.MinInt(*minBet-p.GetBet(), p.GetCash())
+ bet := utils.MinInt(*minBet-p.GetBet(), p.getCash())
if bet == 0 {
g.newLogEvent(fmt.Sprintf("%s check", pUsername))
} else {
@@ -723,7 +723,7 @@ func doCall(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
func doAllIn(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int) int {
- bet := p.GetCash()
+ bet := p.getCash()
if (p.GetBet() + bet) > *minBet {
*lastRaisePlayerIdx = playerToPlayIdx
}
@@ -738,7 +738,7 @@ func doAllIn(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
}
func doBet(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
- newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt PlayerEvent) int {
+ newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt playerEvent) int {
roomTopic := g.roomID.Topic()
roomUserTopic := g.roomID.UserTopic(p.userID)
pokerTableMinBet := g.PokerTableMinBet
@@ -754,8 +754,8 @@ func doBet(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg})
return continueGetPlayerEventLoop
}
- if bet > p.GetCash() {
- msg := fmt.Sprintf("Bet (%d) is too high. Must bet at most %d", bet, p.GetCash())
+ if bet > p.getCash() {
+ msg := fmt.Sprintf("Bet (%d) is too high. Must bet at most %d", bet, p.getCash())
PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg})
return continueGetPlayerEventLoop
}
@@ -773,12 +773,12 @@ func doBet(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
return doNothing
}
-func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]AutoAction, evt PlayerEvent) int {
+func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]autoAction, evt playerEvent) int {
roomUserTopic := g.roomID.UserTopic(evt.UserID)
- autoAction := autoCache[evt.UserID]
- if evt.Fold && autoAction.action == FoldAction ||
- evt.Call && autoAction.action == CallAction ||
- evt.Check && autoAction.action == CheckAction {
+ autoActionVal := autoCache[evt.UserID]
+ if evt.Fold && autoActionVal.action == FoldAction ||
+ evt.Call && autoActionVal.action == CallAction ||
+ evt.Check && autoActionVal.action == CheckAction {
delete(autoCache, evt.UserID)
setAutoAction(g, roomUserTopic, "")
return continueGetPlayerEventLoop
@@ -786,7 +786,7 @@ func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]AutoAc
action := evt.getAction()
if action != NoAction {
- autoCache[evt.UserID] = AutoAction{action: action, evt: evt}
+ autoCache[evt.UserID] = autoAction{action: action, evt: evt}
setAutoAction(g, roomUserTopic, "Will auto "+action.String())
}
return continueGetPlayerEventLoop
@@ -794,8 +794,8 @@ func handleAutoActionReceived(g *PokerGame, autoCache map[database.UserID]AutoAc
func applyAutoAction(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
newlyAllInPlayers *[]*PokerPlayer,
- lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, autoAction AutoAction,
- autoCache map[database.UserID]AutoAction) (actionResult int) {
+ lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, autoAction autoAction,
+ autoCache map[database.UserID]autoAction) (actionResult int) {
pUserID := p.userID
roomUserTopic := g.roomID.UserTopic(pUserID)
@@ -823,7 +823,7 @@ func applyAutoAction(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip,
newlyAllInPlayers *[]*PokerPlayer,
- lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, evt PlayerEvent) (actionResult int) {
+ lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, evt playerEvent) (actionResult int) {
p.lastActionTS = time.Now()
if evt.Fold {
@@ -852,7 +852,7 @@ func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool {
playerToPlayIdx := (dealerIdx + skip) % len(ongoing.players)
lastRaisePlayerIdx := -1
newlyAllInPlayers := make([]*PokerPlayer, 0)
- autoCache := make(map[database.UserID]AutoAction)
+ autoCache := make(map[database.UserID]autoAction)
playerAlive := ongoing.countAlivePlayers()
@@ -891,7 +891,7 @@ RoundIsSettledLoop:
waitCh := time.After(MaxUserCountdown * time.Second)
GetPlayerEventLoop:
for { // Repeat until we get an event from the player we're interested in
- var evt PlayerEvent
+ var evt playerEvent
actionResult := doNothing
// Check for pre-selected action
if autoAction, ok := autoCache[pUserID]; ok {
@@ -1355,7 +1355,7 @@ func processPot(winners []gameResult, mainPot, pokerTableMinBet database.PokerCh
}
handStr := poker.RankString(group.handScore)
isDone = true
- if len(group.players) == 1 && group.players[0].GetCash() > 0 {
+ if len(group.players) == 1 && group.players[0].getCash() > 0 {
// Only 1 player win and is not all-in
player := group.players[0]
piece := mainPot
@@ -1395,7 +1395,7 @@ func processPot(winners []gameResult, mainPot, pokerTableMinBet database.PokerCh
}
piece := mainPot / database.PokerChip(nbPlayersInGroup-allInCount)
for _, p := range group.players {
- if p.GetCash() > 0 {
+ if p.getCash() > 0 {
piece = utils.MinInt(piece, mainPot)
res = append(res, newPlayerGain(p, piece, groupIdx, handStr))
mainPot -= piece
@@ -1797,7 +1797,7 @@ func drawSeatsStyle(authUser *database.User, g *PokerGame) string {
html += `#seat` + itoa(i+1) + ` { border: 2px solid #0d1b8f; }`
}
html += `#seat` + itoa(i+1) + ` .inner:before { content: "` + pUsername.String() + `"; }`
- html += `#seat` + itoa(i+1) + `_cash:before { content: "` + itoa2(p.GetCash()) + `"; }`
+ html += `#seat` + itoa(i+1) + `_cash:before { content: "` + itoa2(p.getCash()) + `"; }`
if ongoing != nil {
if op := ongoing.getPlayer(pUserID); op != nil && op.GetBet() > 0 {
html += `#seat` + itoa(i+1) + `Pot:before { content: "` + itoa2(op.GetBet()) + `"; }`
@@ -1854,7 +1854,7 @@ func drawGameIsDoneHtml(g *PokerGame, evt GameIsDoneEvent) (html string) {
g.Players.RWith(func(gPlayers *[]*seatedPlayer) {
for i, p := range *gPlayers {
if p != nil {
- html += `#seat` + itoa(i+1) + `_cash:before { content: "` + itoa2(p.GetCash()) + `"; }`
+ html += `#seat` + itoa(i+1) + `_cash:before { content: "` + itoa2(p.getCash()) + `"; }`
}
}
})