feat: admin interface modify user, create user

This commit is contained in:
Settel 2022-03-02 13:42:24 +01:00
parent 52fcdbd576
commit f6889725ba
5 changed files with 34 additions and 5 deletions

View File

@ -30,7 +30,7 @@ export default {
this.$emit('edit', player) this.$emit('edit', player)
}, },
newPlayer() { newPlayer() {
this.$emit('edit', {}) this.$emit('edit', { id: null, name: '', score: 0, authcode: '' })
}, },
}, },
} }

View File

@ -25,6 +25,7 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht
gameInfo := gm.GetGameInfo() gameInfo := gm.GetGameInfo()
for i, _ := range gameInfo.Players { for i, _ := range gameInfo.Players {
if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil {
fmt.Printf("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "internal server error") fmt.Fprintf(w, "internal server error")
return return

View File

@ -2,7 +2,9 @@ package application
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"net/http" "net/http"
"path"
"sirlab.de/go/knyt/game" "sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
@ -64,6 +66,17 @@ func (app *Application) modifyUser(id string, gm *game.Game, name, authcode stri
} }
func (app *Application) createUser(gm *game.Game, name, authcode string) error { func (app *Application) createUser(gm *game.Game, name, authcode string) error {
return fmt.Errorf("Application.createUser() not yet implemented.") dirName := path.Join(app.config.DataDir, "users")
userNewUuid := uuid.NewString()
fileName := path.Join(dirName, userNewUuid+".json")
newUser := user.CreateUser(fileName, gm.GetId())
newUser.SetRole(user.ROLE_PLAYER)
newUser.SetName(name)
newUser.SetAuthcode(authcode)
if err := newUser.SaveUser(); err != nil {
return err
}
app.users[newUser.GetId()] = newUser
gm.AddPlayer(newUser)
return nil
} }

View File

@ -1,7 +1,5 @@
package game package game
import ()
func (gm *Game) GetGameInfo() *GameInfoJson { func (gm *Game) GetGameInfo() *GameInfoJson {
gameInfo := gm.initGameInfoJson() gameInfo := gm.initGameInfoJson()

View File

@ -32,6 +32,16 @@ func NewUserFromFile(fileName string) (*User, error) {
} }
} }
func CreateUser(fileName, gameId string) *User {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
return &User{
id: id,
filename: fileName,
gameId: gameId,
}
}
func (usr *User) SaveUser() error { func (usr *User) SaveUser() error {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()
@ -94,6 +104,13 @@ func (usr *User) GetRole() string {
return usr.role return usr.role
} }
func (usr *User) SetRole(role string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.role = role
}
func (usr *User) GetGameId() string { func (usr *User) GetGameId() string {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()