Skip to content

Instantly share code, notes, and snippets.

@fionera
Created March 30, 2021 12:59
Show Gist options
  • Save fionera/1461a6e432566552c5776de3e6d027b6 to your computer and use it in GitHub Desktop.
Save fionera/1461a6e432566552c5776de3e6d027b6 to your computer and use it in GitHub Desktop.
Pr0gramm Blackjack Bot
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
)
const Endpoint = "https://pr0gramm.com/api/casino/blackjack"
var sessionCookie = "me=YOUR_COOKIE_HERE"
const (
ActionCard = "card"
ActionHold = "hold"
ActionStart = "start"
)
type RoundTripperFunc func(*http.Request) (*http.Response, error)
func (r RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return r(req)
}
func main() {
var nonce = "YOUR_NONCE_HERE"
c := &http.Client{
Transport: RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
req.Header.Set("Cookie", sessionCookie)
return http.DefaultClient.Do(req)
}),
}
var wins, loses int
for {
log.Printf("Starting new Round.... | Wins: %d - Loses: %d", wins, loses)
b := NewBlackJack(c, nonce, 5)
for {
log.Printf("Player: %d - Dealer %d", b.PlayerValue, b.DealerValue)
if b.DealerValue > 18 && b.PlayerValue > b.DealerValue {
break
}
if b.PlayerValue < 17 {
log.Println("Taking Card...")
b.TakeCard(c, nonce)
} else {
break
}
}
b.Hold(c, nonce)
log.Printf("Player: %d - Dealer %d", b.PlayerValue, b.DealerValue)
log.Printf("Won: %v", b.Won)
if b.Won {
wins++
} else {
loses++
}
log.Println(strings.Repeat("-", 20))
time.Sleep(1 * time.Second)
}
}
func NewBlackJack(c *http.Client, nonce string, bet int) *BlackJack {
b := BlackJack{}
b.doBlackJackRequest(c, map[string]string{
"bet": fmt.Sprintf("%d", bet),
"_nonce": nonce,
"action": ActionStart,
})
return &b
}
func (b *BlackJack) TakeCard(c *http.Client, nonce string) {
b.doBlackJackRequest(c, map[string]string{
"_nonce": nonce,
"action": ActionCard,
})
}
func (b *BlackJack) Hold(c *http.Client, nonce string) {
b.doBlackJackRequest(c, map[string]string{
"_nonce": nonce,
"action": ActionHold,
})
}
func (b *BlackJack) doBlackJackRequest(c *http.Client, vals map[string]string) {
data := make(map[string][]string)
if b.State != "" {
data = map[string][]string{
"currentDeck": {b.CurrentDeck},
"state": {b.State},
}
}
for k, v := range vals {
data[k] = []string{v}
}
resp, err := c.PostForm(Endpoint, data)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatalf("invalid response code: %d", resp.StatusCode)
}
if err := json.NewDecoder(resp.Body).Decode(b); err != nil {
log.Fatal(err)
}
}
type Card struct {
Type string
Value string
}
func (c *Card) UnmarshalJSON(p []byte) error {
var tmp []interface{}
if err := json.Unmarshal(p, &tmp); err != nil {
return err
}
c.Type = tmp[0].(string)
c.Value = fmt.Sprintf("%v", tmp[1])
return nil
}
type BlackJack struct {
State string `json:"state"`
Credits int `json:"credits"`
Options []string `json:"options"`
PlayerCards []Card `json:"playerCards"`
PlayerValue int `json:"playerValue"`
DealerCards []Card `json:"dealerCards"`
DealerValue int `json:"dealerValue"`
CurrentDeck string `json:"currentDeck"`
Busted bool `json:"busted"`
Won bool `json:"won"`
Bonus int `json:"bonus"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment