knowyt/server/src/engine/engine.go

32 lines
608 B
Go
Raw Normal View History

2021-07-29 19:06:25 +00:00
package engine
import (
"fmt"
"github.com/imkira/go-observer"
2021-08-04 23:42:21 +00:00
"math/rand"
2021-07-30 19:49:18 +00:00
"sirlab.de/go/knyt/syncdata"
2021-07-29 19:06:25 +00:00
"time"
)
2021-08-04 22:52:35 +00:00
func NewEngine(id string) *Engine {
2021-07-29 19:06:25 +00:00
engine := Engine{
2021-08-04 23:00:31 +00:00
id: id,
2021-07-30 19:49:18 +00:00
obs: observer.NewProperty(syncdata.SyncData{}),
2021-07-29 19:06:25 +00:00
}
return &engine
}
func (engine *Engine) Run() {
2021-08-04 23:42:21 +00:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2021-07-29 19:06:25 +00:00
for {
2021-07-30 19:49:18 +00:00
value := engine.obs.Value().(syncdata.SyncData)
2021-08-04 23:42:21 +00:00
fmt.Printf("game %s: %d\n", engine.id, value.VersionRef)
2021-08-06 19:46:00 +00:00
wait := int(1 + r.Float32()*5)
2021-08-04 23:42:21 +00:00
for i := 0; i < wait; i++ {
time.Sleep(1 * time.Second)
}
2021-07-29 19:14:54 +00:00
value.VersionRef++
2021-07-29 19:06:25 +00:00
engine.obs.Update(value)
}
}