refactoring

This commit is contained in:
Settel 2021-08-05 00:52:35 +02:00
parent 406c58352b
commit d4fc7e0563
8 changed files with 32 additions and 28 deletions

View File

@ -13,12 +13,11 @@ func (app Application) loadGames() error {
return err
}
for _, file := range files {
fileName := path.Join(dirName, file.Name(), "game.json")
if gm, err := game.NewGameFromFile(fileName); err != nil {
id := file.Name()
fileName := path.Join(dirName, id, "game.json")
if gm, err := game.NewGameFromFile(id, fileName); err != nil {
return err
} else {
gm.SetId(file.Name())
app.games[gm.GetId()] = gm
}
}

View File

@ -13,12 +13,11 @@ func (app Application) loadUsers() error {
return err
}
for _, file := range files {
id := file.Name()[0:6]
fileName := path.Join(dirName, file.Name())
if usr, err := user.NewUserFromFile(fileName); err != nil {
if usr, err := user.NewUserFromFile(id, fileName); err != nil {
return err
} else {
usr.SetId(file.Name()[0:6])
app.users[usr.GetId()] = usr
}
}

View File

@ -3,16 +3,11 @@ package engine
import (
"fmt"
"github.com/imkira/go-observer"
"sirlab.de/go/knyt/handler"
"sirlab.de/go/knyt/syncdata"
"time"
)
type Engine struct {
obs observer.Property
}
func NewEngine() *Engine {
func NewEngine(id string) *Engine {
engine := Engine{
obs: observer.NewProperty(syncdata.SyncData{}),
}
@ -29,6 +24,6 @@ func (engine *Engine) Run() {
}
}
func (engine *Engine) GetHttpHandler() handler.HandlerFunc {
return handler.SyncFactory(engine.obs)
func (engine *Engine) GetHttpHandler() HandleFunc {
return SyncFactory(engine.obs)
}

View File

@ -0,0 +1,12 @@
package engine
import (
"github.com/imkira/go-observer"
"net/http"
)
type HandleFunc func(http.ResponseWriter, *http.Request)
type Engine struct {
obs observer.Property
}

View File

@ -1,4 +1,4 @@
package handler
package engine
import (
"encoding/json"
@ -9,7 +9,7 @@ import (
"strconv"
)
func SyncFactory(obs observer.Property) HandlerFunc {
func SyncFactory(obs observer.Property) HandleFunc {
return func(w http.ResponseWriter, r *http.Request) {
versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
if err != nil {

View File

@ -4,9 +4,10 @@ import (
"encoding/json"
"fmt"
"os"
"sirlab.de/go/knyt/engine"
)
func NewGameFromFile(fileName string) (*Game, error) {
func NewGameFromFile(id, fileName string) (*Game, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
@ -16,14 +17,12 @@ func NewGameFromFile(fileName string) (*Game, error) {
if err := json.Unmarshal(jsonBytes, &gm); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
gm.id = id
gm.eng = engine.NewEngine(id)
return &gm, nil
}
}
func (gm *Game) SetId(id string) {
gm.id = id
}
func (gm *Game) GetId() string {
return gm.id
}

View File

@ -1,8 +1,11 @@
package game
import ()
import (
"sirlab.de/go/knyt/engine"
)
type Game struct {
id string
Players []string `json:"players"`
eng *engine.Engine
}

View File

@ -14,7 +14,7 @@ func NewUser(id, name, role string) *User {
}
}
func NewUserFromFile(fileName string) (*User, error) {
func NewUserFromFile(id, fileName string) (*User, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
@ -24,14 +24,11 @@ func NewUserFromFile(fileName string) (*User, error) {
if err := json.Unmarshal(jsonBytes, &usr); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
usr.id = id
return &usr, nil
}
}
func (user *User) SetId(id string) {
user.id = id
}
func (user *User) GetId() string {
return user.id
}