From e629c0b6b633e0f25089d5a412d8e010245750fa Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 2 Jun 2019 21:53:52 +0000 Subject: [PATCH] get rid of crazy id scheme that was so unecessary wow --- game.go | 18 +++++++++++++++--- id.go | 55 ------------------------------------------------------- main.go | 4 +++- 3 files changed, 18 insertions(+), 59 deletions(-) delete mode 100644 id.go diff --git a/game.go b/game.go index 740412e..09e9491 100644 --- a/game.go +++ b/game.go @@ -2,11 +2,12 @@ package main import ( "fmt" + "math/rand" "time" ) type Game struct { - id Id + id string start time.Time end time.Time done chan interface{} @@ -30,9 +31,20 @@ func gamesTable() { } } +func init() { rand.Seed(time.Now().UnixNano()) } + +func newID() string { + chars := []rune("ABCDEEEEEEEEFGHJJJJJJJKMNPQQQQQQQRTUVWXXXXXYZZZZZ234677777789") + id := make([]rune, 0, 4) + for i := 0; i < cap(id); i++ { + id = append(id, chars[rand.Intn(len(chars))]) + } + return string(id) +} + func NewGame() *Game { game := &Game{ - id: NewId(), + id: newID(), start: time.Now(), done: make(chan interface{}), connections: make(map[*Connection]bool, 32), @@ -61,7 +73,7 @@ func (g *Game) Create() error { (id, start) values (?, ?) - ;`, g.id.String(), g.start) + ;`, g.id, g.start) if err != nil { return fmt.Errorf("error writing sqlite insert statement to create game: %v") } diff --git a/id.go b/id.go deleted file mode 100644 index 24cf7ae..0000000 --- a/id.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "encoding/binary" - "fmt" - "sync/atomic" - "time" -) - -// NewObjectId returns a new unique ObjectId. -// This function causes a runtime error if it fails to get the hostname -// of the current machine. -func NewId() Id { - b := make([]byte, 12) - // Timestamp, 4 bytes, big endian - binary.BigEndian.PutUint32(b, uint32(time.Now().Unix())) - b[4] = global.machineId[0] - b[5] = global.machineId[1] - b[6] = global.machineId[2] - // Pid, 2 bytes, specs don't specify endianness, but we use big endian. - b[7] = byte(global.pid >> 8) - b[8] = byte(global.pid) - // Increment, 3 bytes, big endian - i := atomic.AddUint32(&global.idCounter, 1) - b[9] = byte(i >> 16) - b[10] = byte(i >> 8) - b[11] = byte(i) - return Id(b) -} - -// Id is used for tagging each incoming http request for logging -// purposes. The actual implementation is just the ObjectId implementation -// found in launchpad.net/mgo/bson. This will most likely change and evolve -// into its own format. -type Id string - -func (id Id) String() string { - return fmt.Sprintf("%x", string(id)) -} - -// Time returns the timestamp part of the id. -// It's a runtime error to call this method with an invalid id. -func (id Id) Time() time.Time { - secs := int64(binary.BigEndian.Uint32(id.byteSlice(0, 4))) - return time.Unix(secs, 0) -} - -// byteSlice returns byte slice of id from start to end. -// Calling this function with an invalid id will cause a runtime panic. -func (id Id) byteSlice(start, end int) []byte { - if len(id) != 12 { - panic(fmt.Sprintf("Invalid Id: %q", string(id))) - } - return []byte(string(id)[start:end]) -} diff --git a/main.go b/main.go index 447c56a..c9b6c6c 100644 --- a/main.go +++ b/main.go @@ -88,10 +88,12 @@ func main() { error_log = log.New(os.Stderr, "[ERROR] ", 0) setupDb() - listener, err := net.Listen("tcp", ":9220") + addr := ":9220" + listener, err := net.Listen("tcp", addr) if err != nil { bail(E_No_Port, "unable to start server: %v", err) } + log_info("listening on %s", addr) go func() { for {