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 return err
} }
for _, file := range files { for _, file := range files {
fileName := path.Join(dirName, file.Name(), "game.json") id := file.Name()
fileName := path.Join(dirName, id, "game.json")
if gm, err := game.NewGameFromFile(fileName); err != nil { if gm, err := game.NewGameFromFile(id, fileName); err != nil {
return err return err
} else { } else {
gm.SetId(file.Name())
app.games[gm.GetId()] = gm app.games[gm.GetId()] = gm
} }
} }

View File

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

View File

@ -3,16 +3,11 @@ package engine
import ( import (
"fmt" "fmt"
"github.com/imkira/go-observer" "github.com/imkira/go-observer"
"sirlab.de/go/knyt/handler"
"sirlab.de/go/knyt/syncdata" "sirlab.de/go/knyt/syncdata"
"time" "time"
) )
type Engine struct { func NewEngine(id string) *Engine {
obs observer.Property
}
func NewEngine() *Engine {
engine := Engine{ engine := Engine{
obs: observer.NewProperty(syncdata.SyncData{}), obs: observer.NewProperty(syncdata.SyncData{}),
} }
@ -29,6 +24,6 @@ func (engine *Engine) Run() {
} }
} }
func (engine *Engine) GetHttpHandler() handler.HandlerFunc { func (engine *Engine) GetHttpHandler() HandleFunc {
return handler.SyncFactory(engine.obs) 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 ( import (
"encoding/json" "encoding/json"
@ -9,7 +9,7 @@ import (
"strconv" "strconv"
) )
func SyncFactory(obs observer.Property) HandlerFunc { func SyncFactory(obs observer.Property) HandleFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
versionRef, err := strconv.Atoi(r.URL.Query().Get("v")) versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
if err != nil { if err != nil {

View File

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

View File

@ -1,8 +1,11 @@
package game package game
import () import (
"sirlab.de/go/knyt/engine"
)
type Game struct { type Game struct {
id string id string
Players []string `json:"players"` 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) jsonBytes, err := os.ReadFile(fileName)
if err != nil { if err != nil {
return nil, err return nil, err
@ -24,14 +24,11 @@ func NewUserFromFile(fileName string) (*User, error) {
if err := json.Unmarshal(jsonBytes, &usr); err != nil { if err := json.Unmarshal(jsonBytes, &usr); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err) return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else { } else {
usr.id = id
return &usr, nil return &usr, nil
} }
} }
func (user *User) SetId(id string) {
user.id = id
}
func (user *User) GetId() string { func (user *User) GetId() string {
return user.id return user.id
} }