refactoring

This commit is contained in:
Settel 2021-08-05 00:12:28 +02:00
parent 9ff46c7e51
commit 8d06d702fd
12 changed files with 108 additions and 66 deletions

View File

@ -1,5 +1,4 @@
{ {
"id": "547933",
"name": "Player #2", "name": "Player #2",
"role": "player", "role": "player",
"game": "067fb1b8-8303-4faa-95d2-1832770a791c" "game": "067fb1b8-8303-4faa-95d2-1832770a791c"

View File

@ -1,5 +1,4 @@
{ {
"id": "646162",
"name": "Settel", "name": "Settel",
"role": "admin" "role": "admin"
} }

View File

@ -1,5 +1,4 @@
{ {
"id": "674542",
"name": "Player #1", "name": "Player #1",
"role": "player", "role": "player",
"game": "067fb1b8-8303-4faa-95d2-1832770a791c" "game": "067fb1b8-8303-4faa-95d2-1832770a791c"

View File

@ -2,21 +2,27 @@ package application
import ( import (
"sirlab.de/go/knyt/applicationConfig" "sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/users" "sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/user"
) )
type Application struct { type Application struct {
config applicationConfig.ApplicationConfig config applicationConfig.ApplicationConfig
users users.Users users map[string]*user.User
games map[string]*game.Game
} }
func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) {
app := Application{ app := Application{
config: config, config: config,
users: users.NewUsers(config), users: make(map[string]*user.User),
games: make(map[string]*game.Game),
} }
err := app.users.LoadUsers()
if err != nil { if err := app.loadUsers(); err != nil {
return nil, err
}
if err := app.loadGames(); err != nil {
return nil, err return nil, err
} }
@ -26,7 +32,3 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e
func (app *Application) GetConfig() applicationConfig.ApplicationConfig { func (app *Application) GetConfig() applicationConfig.ApplicationConfig {
return app.config return app.config
} }
func (app *Application) GetUsers() *users.Users {
return &app.users
}

View File

@ -0,0 +1,14 @@
package application
import (
"fmt"
"sirlab.de/go/knyt/user"
)
func (app Application) GetUserById(id string) (*user.User, error) {
usr := app.users[id]
if usr == nil {
return nil, fmt.Errorf("unknown id")
}
return usr, nil
}

View File

@ -0,0 +1,27 @@
package application
import (
"os"
"path"
"sirlab.de/go/knyt/game"
)
func (app Application) loadGames() error {
dirName := path.Join(app.config.DataDir, "games")
files, err := os.ReadDir(dirName)
if err != nil {
return err
}
for _, file := range files {
fileName := path.Join(dirName, file.Name(), "game.json")
if gm, err := game.NewGameFromFile(fileName); err != nil {
return err
} else {
gm.Id = file.Name()
app.games[gm.Id] = gm
}
}
return nil
}

View File

@ -0,0 +1,27 @@
package application
import (
"os"
"path"
"sirlab.de/go/knyt/user"
)
func (app Application) loadUsers() error {
dirName := path.Join(app.config.DataDir, "users")
files, err := os.ReadDir(dirName)
if err != nil {
return err
}
for _, file := range files {
fileName := path.Join(dirName, file.Name())
if usr, err := user.NewUserFromFile(fileName); err != nil {
return err
} else {
usr.Id = file.Name()[0:6]
app.users[usr.Id] = usr
}
}
return nil
}

21
server/src/game/game.go Normal file
View File

@ -0,0 +1,21 @@
package game
import (
"encoding/json"
"fmt"
"os"
)
func NewGameFromFile(fileName string) (*Game, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var gm Game
if err := json.Unmarshal(jsonBytes, &gm); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
return &gm, nil
}
}

View File

@ -0,0 +1,6 @@
package game
type Game struct {
Id string `json:"id"`
Players []string `json:"players"`
}

View File

@ -47,7 +47,7 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) {
return nil, fmt.Errorf("invalid code") return nil, fmt.Errorf("invalid code")
} }
usr, err := authMux.app.GetUsers().GetUserById(code) usr, err := authMux.app.GetUserById(code)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid code") return nil, fmt.Errorf("invalid code")
} }

View File

@ -44,7 +44,7 @@ func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error)
return nil, fmt.Errorf("invalid cookie") return nil, fmt.Errorf("invalid cookie")
} }
usr, usrErr := authMux.app.GetUsers().GetUserById(authCookie.Value) usr, usrErr := authMux.app.GetUserById(authCookie.Value)
if usrErr != nil { if usrErr != nil {
return nil, fmt.Errorf("invalid cookie") return nil, fmt.Errorf("invalid cookie")
} }

View File

@ -1,52 +0,0 @@
package users
import (
"fmt"
"os"
"path"
"sirlab.de/go/knyt/applicationConfig"
"sirlab.de/go/knyt/user"
)
type Users struct {
appConfig applicationConfig.ApplicationConfig
users map[string]*user.User
}
func NewUsers(appConfig applicationConfig.ApplicationConfig) Users {
return Users{
users: make(map[string]*user.User),
appConfig: appConfig,
}
}
func (users Users) LoadUsers() error {
dirName := path.Join(users.appConfig.DataDir, "users")
files, err := os.ReadDir(dirName)
if err != nil {
return err
}
for _, file := range files {
fileName := path.Join(dirName, file.Name())
if usr, err := user.NewUserFromFile(fileName); err != nil {
return err
} else {
users.AddUser(usr)
}
}
return nil
}
func (users Users) AddUser(usr *user.User) {
users.users[usr.Id] = usr
}
func (users Users) GetUserById(id string) (*user.User, error) {
usr := users.users[id]
if usr == nil {
return nil, fmt.Errorf("unknown id")
}
return usr, nil
}