2013-08-28 18:10:08 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-09-28 12:59:04 -07:00
|
|
|
// 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
|
2013-08-28 18:10:08 -07:00
|
|
|
type IdGenerator struct {
|
|
|
|
id chan int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIdGenerator() *IdGenerator {
|
|
|
|
g := IdGenerator{}
|
|
|
|
g.id = make(chan int64)
|
|
|
|
go func() {
|
2013-08-28 22:16:55 -07:00
|
|
|
var i int64
|
2013-08-28 18:10:08 -07:00
|
|
|
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))
|
2013-11-15 23:20:22 -08:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))[:8]
|
2013-08-28 18:10:08 -07:00
|
|
|
}
|