initial
This commit is contained in:
parent
d406fc321b
commit
66260ec52d
|
@ -0,0 +1,100 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type JSONRPCServer struct {
|
||||
*rpc.Server
|
||||
}
|
||||
|
||||
func NewJSONRPCServer() *JSONRPCServer {
|
||||
return &JSONRPCServer{rpc.NewServer()}
|
||||
}
|
||||
|
||||
func (s *JSONRPCServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
log.Println("rpc server got a request")
|
||||
conn, _, err := w.(http.Hijacker).Hijack()
|
||||
if err != nil {
|
||||
log.Print("rpc hijacking ", req.RemoteAddr, ": ", err.Error())
|
||||
return
|
||||
}
|
||||
io.WriteString(conn, "HTTP/1.0 200 Connected to Go JSON-RPC\n\n")
|
||||
codec := jsonrpc.NewServerCodec(conn)
|
||||
log.Println("ServeCodec")
|
||||
s.Server.ServeCodec(codec)
|
||||
log.Println("finished serving request")
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
A, B int
|
||||
}
|
||||
|
||||
type Quotient struct {
|
||||
Quo, Rem int
|
||||
}
|
||||
|
||||
type Arith int
|
||||
|
||||
func (t *Arith) Multiply(args *Args, reply *int) error {
|
||||
*reply = args.A * args.B
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Arith) Divide(args *Args, quo *Quotient) error {
|
||||
if args.B == 0 {
|
||||
return errors.New("divide by zero")
|
||||
}
|
||||
quo.Quo = args.A / args.B
|
||||
quo.Rem = args.A % args.B
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
//log.SetFormatter(&log.JSONFormatter{})
|
||||
|
||||
// Output to stdout instead of the default stderr
|
||||
// Can be any io.Writer, see below for File example
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
// Only log the warning severity or above.
|
||||
//log.SetLevel(log.WarnLevel)
|
||||
|
||||
log.Infof("starting up")
|
||||
|
||||
go runHttpServer()
|
||||
|
||||
running := true
|
||||
|
||||
for running {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func runHttpServer() {
|
||||
js := NewJSONRPCServer()
|
||||
arith := new(Arith)
|
||||
js.Register(arith)
|
||||
|
||||
port := 8080
|
||||
|
||||
listenaddr := fmt.Sprintf("0.0.0.0:%d", port)
|
||||
|
||||
s := &http.Server{
|
||||
Addr: listenaddr,
|
||||
Handler: js,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
MaxHeaderBytes: 1 << 20,
|
||||
}
|
||||
log.Infof("starting up http server %s", listenaddr)
|
||||
log.Fatal(s.ListenAndServe())
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
package pokercore
|
||||
|
||||
import "encoding/binary"
|
||||
import "fmt"
|
||||
import crand "crypto/rand"
|
||||
import . "github.com/logrusorgru/aurora"
|
||||
import log "github.com/sirupsen/logrus"
|
||||
import rand "math/rand"
|
||||
|
||||
import "strings"
|
||||
|
||||
/*
|
||||
const CLUB = "\u2663"
|
||||
const SPADE = "\u2660"
|
||||
const DIAMOND = "\u2666"
|
||||
const HEART = "\u2665"
|
||||
*/
|
||||
|
||||
const CLUB = "C"
|
||||
const SPADE = "S"
|
||||
const DIAMOND = "D"
|
||||
const HEART = "H"
|
||||
|
||||
type TestGenerationIteration struct {
|
||||
Deck *Deck
|
||||
Seed int64
|
||||
}
|
||||
|
||||
type Card struct {
|
||||
Rank string
|
||||
Suit string
|
||||
}
|
||||
|
||||
type Cards []*Card
|
||||
|
||||
type Deck struct {
|
||||
Cards Cards
|
||||
Dealt int
|
||||
ShuffleSeedVal int64
|
||||
}
|
||||
|
||||
func cryptoUint64() (v uint64) {
|
||||
err := binary.Read(crand.Reader, binary.BigEndian, &v)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Debugf("crand cryptosource is returning Uint64: %d", v)
|
||||
return v
|
||||
}
|
||||
|
||||
func NewRandom() *rand.Rand {
|
||||
|
||||
trueRandom := true
|
||||
|
||||
var rnd *rand.Rand
|
||||
|
||||
seed := int64(42)
|
||||
|
||||
if trueRandom {
|
||||
rnd = rand.New(rand.NewSource(int64(cryptoUint64())))
|
||||
} else {
|
||||
rnd = rand.New(rand.NewSource(seed))
|
||||
}
|
||||
|
||||
return rnd
|
||||
}
|
||||
|
||||
func (self *Card) String() string {
|
||||
var rank string
|
||||
var suit string
|
||||
color := Red
|
||||
switch self.Suit {
|
||||
case DIAMOND:
|
||||
color = Blue
|
||||
case HEART:
|
||||
color = Red
|
||||
case CLUB:
|
||||
color = Green
|
||||
case SPADE:
|
||||
color = Black
|
||||
}
|
||||
|
||||
rank = fmt.Sprintf("%s", BgGray(Bold(color(self.Rank))))
|
||||
suit = fmt.Sprintf("%s", BgGray(Bold(color(self.Suit))))
|
||||
return fmt.Sprintf("%s%s", rank, suit)
|
||||
}
|
||||
|
||||
func NewDeck() *Deck {
|
||||
|
||||
self := new(Deck)
|
||||
|
||||
ranks := []string{"A", "2", "3", "4", "5", "6", "7",
|
||||
"8", "9", "10", "J", "Q", "K"}
|
||||
|
||||
suits := []string{HEART, DIAMOND, CLUB, SPADE}
|
||||
|
||||
self.Cards = make([]*Card, 52)
|
||||
|
||||
// Loop over each type and suit appending to the deck
|
||||
tot := 0
|
||||
for i := 0; i < len(ranks); i++ {
|
||||
for n := 0; n < len(suits); n++ {
|
||||
self.Cards[tot] = &Card{
|
||||
Rank: ranks[i],
|
||||
Suit: suits[n],
|
||||
}
|
||||
tot++
|
||||
}
|
||||
}
|
||||
self.Dealt = 0
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *Deck) Shuffle() {
|
||||
r := NewRandom()
|
||||
r.Shuffle(len(self.Cards), func(i, j int) { self.Cards[i], self.Cards[j] = self.Cards[j], self.Cards[i] })
|
||||
}
|
||||
|
||||
func (self *Deck) Deal(n int) (output Cards) {
|
||||
|
||||
if (self.Dealt + n) > len(self.Cards) {
|
||||
return output
|
||||
}
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
card := self.Cards[self.Dealt+1]
|
||||
output = append(output, card)
|
||||
self.Dealt++
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (s Cards) String() (output string) {
|
||||
var cardstrings []string
|
||||
for i := 0; i < len(s); i++ {
|
||||
cardstrings = append(cardstrings, s[i].String())
|
||||
}
|
||||
output = strings.Join(cardstrings, ",")
|
||||
return output
|
||||
}
|
||||
|
||||
func main2() {
|
||||
|
||||
log.SetLevel(log.DebugLevel)
|
||||
|
||||
myDeck := NewDeck()
|
||||
myDeck.Shuffle()
|
||||
myHand := myDeck.Deal(2)
|
||||
//spew.Dump(myHand)
|
||||
fmt.Printf("my hand: %s\n", myHand)
|
||||
cmty := myDeck.Deal(5)
|
||||
fmt.Printf("community: %s\n", cmty)
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package pokercore
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPokerHand(t *testing.T) {
|
||||
//test stuff here
|
||||
}
|
Loading…
Reference in New Issue