Browse Source

Used the idg.Generator from github

master
Stephen McQuay 5 years ago
parent
commit
f86bacbd69
4 changed files with 6 additions and 65 deletions
  1. +3
    -2
      control.go
  2. +0
    -43
      id.go
  3. +0
    -19
      id_test.go
  4. +3
    -1
      robot.go

+ 3
- 2
control.go View File

@@ -12,6 +12,7 @@ import (
"strings"
"sync"

"github.com/smcquay/idg"
"golang.org/x/net/websocket"
)

@@ -28,7 +29,7 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// Controller is the shepherd of a collection of games. The main package in
// server simply populates one of these and starts an http server.
type Controller struct {
Idg *IdGenerator
Idg *idg.Generator
Conf Config
Games MapLock
Memprofile string
@@ -40,7 +41,7 @@ type Controller struct {
// Controller.
func NewController(conf Config, mprof, pprof string) *http.ServeMux {
c := &Controller{
Idg: NewIdGenerator(),
Idg: idg.NewGenerator(),
Conf: conf,
Games: MapLock{
M: make(map[string]*Game),


+ 0
- 43
id.go View File

@@ -1,43 +0,0 @@
package server

import (
"crypto/md5"
"fmt"
"io"
"time"
)

// This thing contains a channel that when initialized (see NewIdGenerator)
// will return a bunch of (as best as I can tell) unique md5 hashes.
//
// we use this for naming players, games, etc.
//
// It will consume a single goroutine
type IdGenerator struct {
id chan int64
}

func NewIdGenerator() *IdGenerator {
return &IdGenerator{
id: make(chan int64),
}
}

// Run is called (typically in a gorotine) to allow for queries to be made
// against IdGenerator.id throgh the Hash method.
func (idg *IdGenerator) Run() {
var i int64
for i = 0; ; i++ {
idg.id <- i
}
}

// Hash is the method used by a properly instantiated IdGenerator that gives
// fairly unique strings. They are currently truncated md5 hashes of the time
// plus a unique counter
func (id *IdGenerator) Hash() string {
h := md5.New()
ns := time.Now().UnixNano() + <-id.id
io.WriteString(h, fmt.Sprintf("%d", ns))
return fmt.Sprintf("%x", h.Sum(nil))[:8]
}

+ 0
- 19
id_test.go View File

@@ -1,19 +0,0 @@
package server

import (
"testing"
)

func TestIDGenerator(t *testing.T) {
cache := make(map[string]bool)
var cur string
gg := NewIdGenerator()
go gg.Run()
for i := 0; i < 10000; i++ {
cur = gg.Hash()
if _, ok := cache[cur]; ok {
t.Errorf("unexpected duplicate key")
}
cache[cur] = true
}
}

+ 3
- 1
robot.go View File

@@ -5,6 +5,8 @@ import (
"math"
"math/rand"

"github.com/smcquay/idg"

v "bitbucket.org/hackerbots/vector"
)

@@ -34,7 +36,7 @@ type Robot struct {
ProbeResult *Collision `json:"probe_result"`
gameStats *BotStats `json:"-"`
Delta float64 `json:"-"`
idg *IdGenerator
idg *idg.Generator
}

// Collision is basically a Point2d.


Loading…
Cancel
Save