diff --git a/server/data/users/player-2.json b/server/data/users/547933.json similarity index 84% rename from server/data/users/player-2.json rename to server/data/users/547933.json index a8c1646..d6ec366 100644 --- a/server/data/users/player-2.json +++ b/server/data/users/547933.json @@ -1,5 +1,4 @@ { - "id": "547933", "name": "Player #2", "role": "player", "game": "067fb1b8-8303-4faa-95d2-1832770a791c" diff --git a/server/data/users/settel.json b/server/data/users/646162.json similarity index 70% rename from server/data/users/settel.json rename to server/data/users/646162.json index 63d5775..73f03a8 100644 --- a/server/data/users/settel.json +++ b/server/data/users/646162.json @@ -1,5 +1,4 @@ { - "id": "646162", "name": "Settel", "role": "admin" } diff --git a/server/data/users/player-1.json b/server/data/users/674542.json similarity index 84% rename from server/data/users/player-1.json rename to server/data/users/674542.json index fb97811..146ec26 100644 --- a/server/data/users/player-1.json +++ b/server/data/users/674542.json @@ -1,5 +1,4 @@ { - "id": "674542", "name": "Player #1", "role": "player", "game": "067fb1b8-8303-4faa-95d2-1832770a791c" diff --git a/server/src/application/application.go b/server/src/application/application.go index 17a5f46..83d2840 100644 --- a/server/src/application/application.go +++ b/server/src/application/application.go @@ -2,21 +2,27 @@ package application import ( "sirlab.de/go/knyt/applicationConfig" - "sirlab.de/go/knyt/users" + "sirlab.de/go/knyt/game" + "sirlab.de/go/knyt/user" ) type Application struct { config applicationConfig.ApplicationConfig - users users.Users + users map[string]*user.User + games map[string]*game.Game } func NewApplication(config applicationConfig.ApplicationConfig) (*Application, error) { app := Application{ 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 } @@ -26,7 +32,3 @@ func NewApplication(config applicationConfig.ApplicationConfig) (*Application, e func (app *Application) GetConfig() applicationConfig.ApplicationConfig { return app.config } - -func (app *Application) GetUsers() *users.Users { - return &app.users -} diff --git a/server/src/application/getUser.go b/server/src/application/getUser.go new file mode 100644 index 0000000..81bce4d --- /dev/null +++ b/server/src/application/getUser.go @@ -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 +} diff --git a/server/src/application/loadGames.go b/server/src/application/loadGames.go new file mode 100644 index 0000000..1c3e8ee --- /dev/null +++ b/server/src/application/loadGames.go @@ -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 +} diff --git a/server/src/application/loadUsers.go b/server/src/application/loadUsers.go new file mode 100644 index 0000000..3d112af --- /dev/null +++ b/server/src/application/loadUsers.go @@ -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 +} diff --git a/server/src/game/game.go b/server/src/game/game.go new file mode 100644 index 0000000..d504065 --- /dev/null +++ b/server/src/game/game.go @@ -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 + } +} diff --git a/server/src/game/struct.go b/server/src/game/struct.go new file mode 100644 index 0000000..9739fcf --- /dev/null +++ b/server/src/game/struct.go @@ -0,0 +1,6 @@ +package game + +type Game struct { + Id string `json:"id"` + Players []string `json:"players"` +} diff --git a/server/src/handler/login.go b/server/src/handler/login.go index 9d79273..614c56a 100644 --- a/server/src/handler/login.go +++ b/server/src/handler/login.go @@ -47,7 +47,7 @@ func (authMux *AuthMux) checkCode(r *http.Request) (*user.User, error) { return nil, fmt.Errorf("invalid code") } - usr, err := authMux.app.GetUsers().GetUserById(code) + usr, err := authMux.app.GetUserById(code) if err != nil { return nil, fmt.Errorf("invalid code") } diff --git a/server/src/handler/private.go b/server/src/handler/private.go index f9799ab..5eebef4 100644 --- a/server/src/handler/private.go +++ b/server/src/handler/private.go @@ -44,7 +44,7 @@ func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) return nil, fmt.Errorf("invalid cookie") } - usr, usrErr := authMux.app.GetUsers().GetUserById(authCookie.Value) + usr, usrErr := authMux.app.GetUserById(authCookie.Value) if usrErr != nil { return nil, fmt.Errorf("invalid cookie") } diff --git a/server/src/users/users.go b/server/src/users/users.go deleted file mode 100644 index a8b0d49..0000000 --- a/server/src/users/users.go +++ /dev/null @@ -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 -}