dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 71e0202874a42f9668d8159100798fc556e1ef55
parent 64cd122b95f009b2e45922c708806cbb76bf1e2f
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu,  7 Dec 2023 16:30:42 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker/poker.go | 67++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -506,7 +506,6 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { roomTopic := "room_" + roomID roomLogsTopic := "room_" + roomID + "_logs" var winners []GameResult - var winnerHand string g.incrDealerIdx() @@ -700,32 +699,7 @@ END: mainPot := g.Ongoing.MainPot g.Ongoing.MainPotMtx.RUnlock() - winnersStr := "" - // TODO: handle all-ins - if len(winners) == 0 { - logrus.Error("winners has len 0") - } else if len(winners) == 1 && len(winners[0].Players) == 1 { - // Everyone fold but 1 player - winnerHand = "Only player alive" - winners[0].Players[0].Cash += mainPot - winnersStr += winners[0].Players[0].Username - } else if len(winners[0].Players) == 1 && winners[0].Players[0].Cash > 0 { - // Only 1 player win and is not all-in - winnerHand = poker.RankString(winners[0].HandScore) - winners[0].Players[0].Cash += mainPot - winnersStr += winners[0].Players[0].Username - //} else if len(winners[0].Players) == 1 && winners[0].Players[0].Cash == 0 { - // Only 1 player win but is all-in - } else if len(winners[0].Players) > 1 { - // Multiple winners, split pot - nb := len(winners[0].Players) - piece := mainPot / nb // TODO: fix this - for _, p := range winners[0].Players { - p.Cash += piece - winnersStr += p.Username - } - winnerHand = poker.RankString(winners[0].HandScore) - } + winnersStr, winnerHand := processPot(winners, mainPot) g.Ongoing.MainPotMtx.Lock() g.Ongoing.MainPot = 0 @@ -753,6 +727,45 @@ END: g.IsGameStarted.Store(false) } +func processPot(winners []GameResult, mainPot int) (winnersStr, winnerHand string) { + // TODO: handle all-ins + if len(winners) == 0 { + logrus.Error("winners has len 0") + } else if len(winners) == 1 && len(winners[0].Players) == 1 { + // Everyone fold but 1 player + winnerHand = "Only player alive" + winners[0].Players[0].Cash += mainPot + winnersStr += winners[0].Players[0].Username + } else { + isDone := true + for _, group := range winners { + isDone = true + if len(group.Players) == 1 && group.Players[0].Cash > 0 { + // Only 1 player win and is not all-in + winnerHand = poker.RankString(group.HandScore) + group.Players[0].Cash += mainPot + winnersStr += group.Players[0].Username + } else if len(group.Players) == 1 && group.Players[0].Cash == 0 { + // Only 1 player win but is all-in + isDone = false + } else if len(group.Players) > 1 { + // Multiple winners, split pot + nb := len(group.Players) + piece := mainPot / nb // TODO: fix this + for _, p := range group.Players { + p.Cash += piece + winnersStr += p.Username + } + winnerHand = poker.RankString(group.HandScore) + } + if isDone { + break + } + } + } + return winnersStr, winnerHand +} + func cardToPokerCard(name string) string { r := strings.NewReplacer("♠", "s", "♥", "h", "♣", "c", "♦", "d", "10", "T") return r.Replace(name)