server/id.go
Stephen McQuay c8efd34080 ignore new binary
Create new bserv binary

Herein lie the following ideas:

- change package visibility of many things
- try to stash the globals into structs
- the code is far from correct; it merely compiles
2014-03-31 23:21:14 -07:00

38 lines
690 B
Go

package botserv
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 {
g := IdGenerator{}
g.id = make(chan int64)
go func() {
var i int64
for i = 0; ; i++ {
g.id <- i
}
}()
return &g
}
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]
}