feat: modify player

This commit is contained in:
Settel 2022-03-02 12:04:27 +01:00
parent 2b10149a87
commit 52fcdbd576
9 changed files with 171 additions and 7 deletions

View File

@ -2,19 +2,25 @@
<div class="admin-edit-player"> <div class="admin-edit-player">
<Infobox> <Infobox>
<div class="admin-edit-player__close" @click="$emit('close')" /> <div class="admin-edit-player__close" @click="$emit('close')" />
<h1>ooookay!</h1> <table class="admin-edit-player__table">
<table>
<tr> <tr>
<td>Name: </td> <td>Name: </td>
<td>{{ player.name }}</td> <td><input v-model="name" /></td>
</tr> </tr>
<tr> <tr>
<td>Score: </td> <td>Score: </td>
<td>{{ player.score }}</td> <td><input v-model="score" /></td>
</tr> </tr>
<tr> <tr>
<td>Authcode: </td> <td>Authcode: </td>
<td>{{ player.authcode }}</td> <td><input v-model="authcode" size="6" maxlength="6" /></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td></td>
<td><button @click="save">Submit</button></td>
</tr> </tr>
</table> </table>
</Infobox> </Infobox>
@ -24,6 +30,22 @@
<script> <script>
export default { export default {
props: ['player'], props: ['player'],
data() {
return {
...this.player,
}
},
methods: {
async save() {
await this.$engine.savePlayer({
id: this.id,
name: this.name,
score: this.score,
authcode: this.authcode,
})
this.$emit('close')
},
},
} }
</script> </script>
@ -49,5 +71,9 @@ export default {
content: 'x'; content: 'x';
} }
} }
&__table {
margin: 64px;
}
} }
</style> </style>

View File

@ -7,7 +7,7 @@
<GameControls /> <GameControls />
<div class="page-admin__body"> <div class="page-admin__body">
<div v-if="overlay.open" class="page-admin__player-overlay"> <div v-if="overlay.open" class="page-admin__player-overlay">
<AdminEditPlayer :player="overlay.player" @close="overlay.open=false"/> <AdminEditPlayer :player="overlay.player" @close="editPlayerClose"/>
</div> </div>
<div class="page-admin__tiles"> <div class="page-admin__tiles">
<AdminTileMyself :user="user" /> <AdminTileMyself :user="user" />
@ -54,6 +54,10 @@ export default {
this.overlay.open = true this.overlay.open = true
this.overlay.player = player this.overlay.player = player
}, },
async editPlayerClose() {
this.overlay.open = false
await this.$engine.fetchGameInfo({ g: this.$store.state.engine.user.game })
},
}, },
} }
</script> </script>
@ -79,7 +83,7 @@ export default {
position: absolute; position: absolute;
left: 10%; left: 10%;
top: 10%; top: 10%;
width: 80%; width: 800px;
} }
&__player-overlay-close { &__player-overlay-close {
float: right; float: right;

View File

@ -5,6 +5,7 @@ import fetchUpdate from './fetchUpdate'
import fetchUserInfo from './fetchUserInfo' import fetchUserInfo from './fetchUserInfo'
import fetchGameInfo from './fetchGameInfo' import fetchGameInfo from './fetchGameInfo'
import setGameName from './setGameName' import setGameName from './setGameName'
import savePlayer from './savePlayer'
import collectQuotes from './collectQuotes' import collectQuotes from './collectQuotes'
import startGame from './startGame' import startGame from './startGame'
import resetGame from './resetGame' import resetGame from './resetGame'
@ -31,6 +32,7 @@ export default (context, inject) => {
fetchUserInfo, fetchUserInfo,
fetchGameInfo, fetchGameInfo,
setGameName, setGameName,
savePlayer,
collectQuotes, collectQuotes,
getMyQuotes, getMyQuotes,
saveQuote, saveQuote,

View File

@ -0,0 +1,11 @@
export default async function(player) {
const { store } = this.context
await this.callApi('/api/savePlayer', {
g: store.state.engine.user?.game,
id: player.id,
name: player.name,
score: player.score,
authcode: player.authcode,
})
}

View File

@ -0,0 +1,69 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/user"
)
func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) {
gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "game not found")
return
}
if usr.GetGameId() != gameRef || !usr.IsGamemaster() {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
id := r.URL.Query().Get("id")
name := r.URL.Query().Get("name")
authcode := r.URL.Query().Get("authcode")
if name == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Printf("player name must not be empty\n")
fmt.Fprintf(w, "server error")
return
}
if id != "" {
if err := app.modifyUser(id, gm, name, authcode); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error")
return
}
} else {
if err := app.createUser(gm, name, authcode); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error")
return
}
}
fmt.Fprintf(w, "ok")
}
func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
modifyUser := app.users[id]
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {
return fmt.Errorf("couldn't find player %s in game %s", id, gm.GetId())
}
modifyUser.SetName(name)
modifyUser.SetAuthcode(authcode)
if err := modifyUser.SaveUser(); err != nil {
return err
}
gm.UpdatePlayer(modifyUser)
return nil
}
func (app *Application) createUser(gm *game.Game, name, authcode string) error {
return fmt.Errorf("Application.createUser() not yet implemented.")
}

View File

@ -68,3 +68,18 @@ func (gm *Game) AddPlayer(usr *user.User) {
score: 0, score: 0,
} }
} }
func (gm *Game) UpdatePlayer(usr *user.User) {
usrId := usr.GetId()
gm.mu.Lock()
defer gm.mu.Unlock()
player := gm.players[usrId]
if player.id != usrId {
return
}
player.name = usr.GetName()
gm.players[usrId] = player
}

View File

@ -24,6 +24,7 @@ func main() {
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo) mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo) mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
mux.PrivateHandleFunc("/api/setGameName", app.SetGameName) mux.PrivateHandleFunc("/api/setGameName", app.SetGameName)
mux.PrivateHandleFunc("/api/savePlayer", app.SavePlayer)
mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes) mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes)
mux.PrivateHandleFunc("/api/startGame", app.StartGame) mux.PrivateHandleFunc("/api/startGame", app.StartGame)

View File

@ -13,6 +13,7 @@ const (
type User struct { type User struct {
mu sync.Mutex mu sync.Mutex
id string id string
filename string
authcode string authcode string
name string name string
role string role string

View File

@ -24,6 +24,7 @@ func NewUserFromFile(fileName string) (*User, error) {
return &User{ return &User{
id: id, id: id,
name: userJson.Name, name: userJson.Name,
filename: fileName,
role: userJson.Role, role: userJson.Role,
authcode: userJson.Authcode, authcode: userJson.Authcode,
gameId: userJson.GameId, gameId: userJson.GameId,
@ -31,6 +32,26 @@ func NewUserFromFile(fileName string) (*User, error) {
} }
} }
func (usr *User) SaveUser() error {
usr.mu.Lock()
defer usr.mu.Unlock()
userJson := UserJson{
Name: usr.name,
Authcode: usr.authcode,
Role: usr.role,
GameId: usr.gameId,
}
if jsonBytes, err := json.Marshal(userJson); err != nil {
return err
} else {
if err := os.WriteFile(usr.filename, jsonBytes, 0666); err != nil {
return err
}
}
return nil
}
func (usr *User) GetId() string { func (usr *User) GetId() string {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()
@ -45,6 +66,13 @@ func (usr *User) GetAuthCode() string {
return usr.authcode return usr.authcode
} }
func (usr *User) SetAuthcode(authcode string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.authcode = authcode
}
func (usr *User) GetName() string { func (usr *User) GetName() string {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()
@ -52,6 +80,13 @@ func (usr *User) GetName() string {
return usr.name return usr.name
} }
func (usr *User) SetName(name string) {
usr.mu.Lock()
defer usr.mu.Unlock()
usr.name = name
}
func (usr *User) GetRole() string { func (usr *User) GetRole() string {
usr.mu.Lock() usr.mu.Lock()
defer usr.mu.Unlock() defer usr.mu.Unlock()