dkforest

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

commit f8315101f743188e18272ead3ed8dd104f61b4c3
parent 3f054dbfae5eb161c14389b0ffc02a0faff847b5
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  5 Dec 2023 22:31:24 -0500

actually find winning hand

Diffstat:
Mgo.mod | 1+
Mgo.sum | 4++++
Mpkg/web/handlers/poker/poker.go | 41+++++++++++++++++++++++++++++++++--------
3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/go.mod b/go.mod @@ -45,6 +45,7 @@ require ( github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c // indirect github.com/andybalholm/cascadia v1.1.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect + github.com/chehsunliu/poker v0.1.0 // indirect github.com/cloudflare/circl v1.1.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect diff --git a/go.sum b/go.sum @@ -59,6 +59,8 @@ github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chehsunliu/poker v0.1.0 h1:OeB4O+QROhA/DiXUhBBlkgbzCx0ZVWMpWgKNu+PX9vI= +github.com/chehsunliu/poker v0.1.0/go.mod h1:V6K4yyDbafp0k6lUnYbwoTS/KsHSB1EWiJdEk54uB1w= github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY= @@ -237,6 +239,7 @@ github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= +github.com/loganjspears/joker v0.0.0-20180219043703-3f2f69a75914/go.mod h1:76SAnflG7ZFhgtnaVCpP6A5Z1S/VMFzRBN7KGm5j4oc= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= @@ -289,6 +292,7 @@ github.com/nicksnyder/go-i18n/v2 v2.0.0-20190611170653-0c6ce6ac1e8c h1:MtOLrfLzA github.com/nicksnyder/go-i18n/v2 v2.0.0-20190611170653-0c6ce6ac1e8c/go.mod h1:JXS4+OKhbcwDoVTEj0sLFWL1vOwec2g/YBAxZ9owJqY= github.com/notnil/chess v1.8.0 h1:wU50vrPpH3kiT0WRC/6EN9kJpb7EBxFLCRf05TwXys8= github.com/notnil/chess v1.8.0/go.mod h1:cRuJUIBFq9Xki05TWHJxHYkC+fFpq45IWwk94DdlCrA= +github.com/notnil/joker v0.0.0-20180219043703-3f2f69a75914/go.mod h1:L0Sdr2nYdktjerdXpIn9wOCn+GebPs/nCL2qH6RTGa0= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -7,7 +7,9 @@ import ( hutils "dkforest/pkg/web/handlers/utils" "errors" "fmt" + "github.com/chehsunliu/poker" "github.com/labstack/echo" + "math" "net/http" "strconv" "strings" @@ -71,11 +73,12 @@ type PlayerEvent struct { var PokerInstance = NewPoker() type Ongoing struct { - Deck []string - Players []*PokerPlayer - Events []PokerEvent - WaitTurnEvent PokerWaitTurnEvent - MainPot int + Deck []string + Players []*PokerPlayer + Events []PokerEvent + CommunityCards []string + WaitTurnEvent PokerWaitTurnEvent + MainPot int } type PokerStandingPlayer struct { @@ -352,6 +355,7 @@ func dealerThread(g *PokerGame, roomID string) { } PokerPubSub.Pub(roomTopic, evt) g.Ongoing.Events = append(g.Ongoing.Events, evt) + g.Ongoing.CommunityCards = append(g.Ongoing.CommunityCards, card) dealCardIdx++ } @@ -455,12 +459,27 @@ END: // TODO: evaluate hands, and crown winner var winner *PokerPlayer + var minScore int32 = math.MaxInt32 for _, p := range g.Ongoing.Players { if p != nil { - winner = p - break + hand := []poker.Card{ + poker.NewCard(cardToPokerCard(g.Ongoing.CommunityCards[0])), + poker.NewCard(cardToPokerCard(g.Ongoing.CommunityCards[1])), + poker.NewCard(cardToPokerCard(g.Ongoing.CommunityCards[2])), + poker.NewCard(cardToPokerCard(g.Ongoing.CommunityCards[3])), + poker.NewCard(cardToPokerCard(g.Ongoing.CommunityCards[4])), + poker.NewCard(cardToPokerCard(p.Cards[0].Name)), + poker.NewCard(cardToPokerCard(p.Cards[1].Name)), + } + e := poker.Evaluate(hand) + fmt.Println(p.Username, e, poker.RankString(e)) + if e < minScore { + winner = p + minScore = e + } } } + fmt.Println("WINNER", winner) winner.Cash += g.Ongoing.MainPot g.Ongoing.MainPot = 0 @@ -480,6 +499,11 @@ END: fmt.Println("GAME IS OVER") } +func cardToPokerCard(name string) string { + r := strings.NewReplacer("♠", "s", "♥", "h", "♣", "c", "♦", "d") + return r.Replace(name) +} + func (g *PokerGame) Deal(roomID string) { roomTopic := "room_" + roomID @@ -1089,6 +1113,7 @@ func PokerHandler(c echo.Context) error { send(cssReset) send(pokerCss) + send(`<script>document.onclick = function(e) { console.log(e.x, e.y); };</script>`) send(buildCardsHtml()) send(buildTakeSeatHtml(authUser, g, roomID)) send(buildSeatsHtml(g)) @@ -1098,7 +1123,7 @@ func PokerHandler(c echo.Context) error { actions += `<iframe src="/poker/` + roomID + `/unsit" id="unSitBtn"></iframe>` send(actions) send(`<div id="errorMsg"></div>`) - send(`<div id="seat1Pot">BET: 0</div>`) + send(`<div id="seat1Pot"></div>`) // TODO send(buildMainPotHtml(g)) send(buildCountdownsHtml())