Browse Source

Used the idg.Generator from github

Stephen McQuay 4 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 (
12 12
 	"strings"
13 13
 	"sync"
14 14
 
15
+	"github.com/smcquay/idg"
15 16
 	"golang.org/x/net/websocket"
16 17
 )
17 18
 
@@ -28,7 +29,7 @@ func (h JsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
28 29
 // Controller is the shepherd of a collection of games. The main package in
29 30
 // server simply populates one of these and starts an http server.
30 31
 type Controller struct {
31
-	Idg        *IdGenerator
32
+	Idg        *idg.Generator
32 33
 	Conf       Config
33 34
 	Games      MapLock
34 35
 	Memprofile string
@@ -40,7 +41,7 @@ type Controller struct {
40 41
 // Controller.
41 42
 func NewController(conf Config, mprof, pprof string) *http.ServeMux {
42 43
 	c := &Controller{
43
-		Idg:  NewIdGenerator(),
44
+		Idg:  idg.NewGenerator(),
44 45
 		Conf: conf,
45 46
 		Games: MapLock{
46 47
 			M: make(map[string]*Game),

+ 0
- 43
id.go View File

@@ -1,43 +0,0 @@
1
-package server
2
-
3
-import (
4
-	"crypto/md5"
5
-	"fmt"
6
-	"io"
7
-	"time"
8
-)
9
-
10
-// This thing contains a channel that when initialized (see NewIdGenerator)
11
-// will return a bunch of (as best as I can tell) unique md5 hashes.
12
-//
13
-// we use this for naming players, games, etc.
14
-//
15
-// It will consume a single goroutine
16
-type IdGenerator struct {
17
-	id chan int64
18
-}
19
-
20
-func NewIdGenerator() *IdGenerator {
21
-	return &IdGenerator{
22
-		id: make(chan int64),
23
-	}
24
-}
25
-
26
-// Run is called (typically in a gorotine) to allow for queries to be made
27
-// against IdGenerator.id throgh the Hash method.
28
-func (idg *IdGenerator) Run() {
29
-	var i int64
30
-	for i = 0; ; i++ {
31
-		idg.id <- i
32
-	}
33
-}
34
-
35
-// Hash is the method used by a properly instantiated IdGenerator that gives
36
-// fairly unique strings. They are currently truncated md5 hashes of the time
37
-// plus a unique counter
38
-func (id *IdGenerator) Hash() string {
39
-	h := md5.New()
40
-	ns := time.Now().UnixNano() + <-id.id
41
-	io.WriteString(h, fmt.Sprintf("%d", ns))
42
-	return fmt.Sprintf("%x", h.Sum(nil))[:8]
43
-}

+ 0
- 19
id_test.go View File

@@ -1,19 +0,0 @@
1
-package server
2
-
3
-import (
4
-	"testing"
5
-)
6
-
7
-func TestIDGenerator(t *testing.T) {
8
-	cache := make(map[string]bool)
9
-	var cur string
10
-	gg := NewIdGenerator()
11
-	go gg.Run()
12
-	for i := 0; i < 10000; i++ {
13
-		cur = gg.Hash()
14
-		if _, ok := cache[cur]; ok {
15
-			t.Errorf("unexpected duplicate key")
16
-		}
17
-		cache[cur] = true
18
-	}
19
-}

+ 3
- 1
robot.go View File

@@ -5,6 +5,8 @@ import (
5 5
 	"math"
6 6
 	"math/rand"
7 7
 
8
+	"github.com/smcquay/idg"
9
+
8 10
 	v "bitbucket.org/hackerbots/vector"
9 11
 )
10 12
 
@@ -34,7 +36,7 @@ type Robot struct {
34 36
 	ProbeResult    *Collision  `json:"probe_result"`
35 37
 	gameStats      *BotStats   `json:"-"`
36 38
 	Delta          float64     `json:"-"`
37
-	idg            *IdGenerator
39
+	idg            *idg.Generator
38 40
 }
39 41
 
40 42
 // Collision is basically a Point2d.

Loading…
Cancel
Save