refactoring

This commit is contained in:
Settel 2021-08-05 00:40:31 +02:00
parent 8d06d702fd
commit 406c58352b
7 changed files with 26 additions and 8 deletions

View File

@ -18,8 +18,8 @@ func (app Application) loadGames() error {
if gm, err := game.NewGameFromFile(fileName); err != nil { if gm, err := game.NewGameFromFile(fileName); err != nil {
return err return err
} else { } else {
gm.Id = file.Name() gm.SetId(file.Name())
app.games[gm.Id] = gm app.games[gm.GetId()] = gm
} }
} }

View File

@ -18,8 +18,8 @@ func (app Application) loadUsers() error {
if usr, err := user.NewUserFromFile(fileName); err != nil { if usr, err := user.NewUserFromFile(fileName); err != nil {
return err return err
} else { } else {
usr.Id = file.Name()[0:6] usr.SetId(file.Name()[0:6])
app.users[usr.Id] = usr app.users[usr.GetId()] = usr
} }
} }

View File

@ -19,3 +19,11 @@ func NewGameFromFile(fileName string) (*Game, error) {
return &gm, nil return &gm, nil
} }
} }
func (gm *Game) SetId(id string) {
gm.id = id
}
func (gm *Game) GetId() string {
return gm.id
}

View File

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

View File

@ -31,7 +31,7 @@ func (authMux *AuthMux) Login(w http.ResponseWriter, r *http.Request) {
} }
cookie := authMux.createCookie() cookie := authMux.createCookie()
cookie.Value = usr.Id cookie.Value = usr.GetId()
cookie.MaxAge = 0 cookie.MaxAge = 0
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
w.Header().Add("Content-Type", "text/plain") w.Header().Add("Content-Type", "text/plain")

View File

@ -7,7 +7,7 @@ const (
) )
type User struct { type User struct {
Id string `json:"id"` id string
Name string `json:"name"` Name string `json:"name"`
Role string `json:"role"` Role string `json:"role"`
} }

View File

@ -8,7 +8,7 @@ import (
func NewUser(id, name, role string) *User { func NewUser(id, name, role string) *User {
return &User{ return &User{
Id: id, id: id,
Name: name, Name: name,
Role: role, Role: role,
} }
@ -28,6 +28,14 @@ func NewUserFromFile(fileName string) (*User, error) {
} }
} }
func (user *User) SetId(id string) {
user.id = id
}
func (user *User) GetId() string {
return user.id
}
func (user *User) IsPlayer() bool { func (user *User) IsPlayer() bool {
return true return true
} }