refactoring

This commit is contained in:
Settel 2021-08-09 09:47:52 +02:00
parent cbc7369b8d
commit 09b61c7dc9
2 changed files with 14 additions and 9 deletions

View File

@ -10,22 +10,26 @@ import (
func NewEngine(id string) *Engine { func NewEngine(id string) *Engine {
engine := Engine{ engine := Engine{
id: id, id: id,
obs: observer.NewProperty(syncdata.SyncData{}), versionRef: 0,
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)
} }
} }

View File

@ -8,6 +8,7 @@ import (
type HandleFunc func(http.ResponseWriter, *http.Request) type HandleFunc func(http.ResponseWriter, *http.Request)
type Engine struct { type Engine struct {
id string id string
obs observer.Property versionRef int
obs observer.Property
} }