refactoring
This commit is contained in:
parent
cbc7369b8d
commit
09b61c7dc9
@ -11,21 +11,25 @@ import (
|
|||||||
func NewEngine(id string) *Engine {
|
func NewEngine(id string) *Engine {
|
||||||
engine := Engine{
|
engine := Engine{
|
||||||
id: id,
|
id: id,
|
||||||
|
versionRef: 0,
|
||||||
obs: observer.NewProperty(syncdata.SyncData{}),
|
obs: observer.NewProperty(syncdata.SyncData{}),
|
||||||
}
|
}
|
||||||
return &engine
|
return &engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Run() {
|
func (eng *Engine) Run() {
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
for {
|
for {
|
||||||
value := engine.obs.Value().(syncdata.SyncData)
|
|
||||||
fmt.Printf("game %s: %d\n", engine.id, value.VersionRef)
|
|
||||||
wait := int(1 + r.Float32()*5)
|
wait := int(1 + r.Float32()*5)
|
||||||
for i := 0; i < wait; i++ {
|
for i := 0; i < wait; i++ {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
value.VersionRef++
|
|
||||||
engine.obs.Update(value)
|
eng.versionRef++
|
||||||
|
fmt.Printf("game %s: %d\n", eng.id, eng.versionRef)
|
||||||
|
data := syncdata.SyncData{
|
||||||
|
VersionRef: eng.versionRef,
|
||||||
|
}
|
||||||
|
eng.obs.Update(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,6 @@ type HandleFunc func(http.ResponseWriter, *http.Request)
|
|||||||
|
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
id string
|
id string
|
||||||
|
versionRef int
|
||||||
obs observer.Property
|
obs observer.Property
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user