feat: modify player
This commit is contained in:
parent
2b10149a87
commit
52fcdbd576
@ -2,19 +2,25 @@
|
||||
<div class="admin-edit-player">
|
||||
<Infobox>
|
||||
<div class="admin-edit-player__close" @click="$emit('close')" />
|
||||
<h1>ooookay!</h1>
|
||||
<table>
|
||||
<table class="admin-edit-player__table">
|
||||
<tr>
|
||||
<td>Name: </td>
|
||||
<td>{{ player.name }}</td>
|
||||
<td><input v-model="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Score: </td>
|
||||
<td>{{ player.score }}</td>
|
||||
<td><input v-model="score" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Authcode: </td>
|
||||
<td>{{ player.authcode }}</td>
|
||||
<td><input v-model="authcode" size="6" maxlength="6" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><button @click="save">Submit</button></td>
|
||||
</tr>
|
||||
</table>
|
||||
</Infobox>
|
||||
@ -24,6 +30,22 @@
|
||||
<script>
|
||||
export default {
|
||||
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>
|
||||
|
||||
@ -49,5 +71,9 @@ export default {
|
||||
content: 'x';
|
||||
}
|
||||
}
|
||||
|
||||
&__table {
|
||||
margin: 64px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
@ -7,7 +7,7 @@
|
||||
<GameControls />
|
||||
<div class="page-admin__body">
|
||||
<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 class="page-admin__tiles">
|
||||
<AdminTileMyself :user="user" />
|
||||
@ -54,6 +54,10 @@ export default {
|
||||
this.overlay.open = true
|
||||
this.overlay.player = player
|
||||
},
|
||||
async editPlayerClose() {
|
||||
this.overlay.open = false
|
||||
await this.$engine.fetchGameInfo({ g: this.$store.state.engine.user.game })
|
||||
},
|
||||
},
|
||||
}
|
||||
</script>
|
||||
@ -79,7 +83,7 @@ export default {
|
||||
position: absolute;
|
||||
left: 10%;
|
||||
top: 10%;
|
||||
width: 80%;
|
||||
width: 800px;
|
||||
}
|
||||
&__player-overlay-close {
|
||||
float: right;
|
||||
|
@ -5,6 +5,7 @@ import fetchUpdate from './fetchUpdate'
|
||||
import fetchUserInfo from './fetchUserInfo'
|
||||
import fetchGameInfo from './fetchGameInfo'
|
||||
import setGameName from './setGameName'
|
||||
import savePlayer from './savePlayer'
|
||||
import collectQuotes from './collectQuotes'
|
||||
import startGame from './startGame'
|
||||
import resetGame from './resetGame'
|
||||
@ -31,6 +32,7 @@ export default (context, inject) => {
|
||||
fetchUserInfo,
|
||||
fetchGameInfo,
|
||||
setGameName,
|
||||
savePlayer,
|
||||
collectQuotes,
|
||||
getMyQuotes,
|
||||
saveQuote,
|
||||
|
11
client/src/plugins/engine/savePlayer.js
Normal file
11
client/src/plugins/engine/savePlayer.js
Normal 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,
|
||||
})
|
||||
}
|
69
server/src/application/savePlayer.go
Normal file
69
server/src/application/savePlayer.go
Normal 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.")
|
||||
|
||||
}
|
@ -68,3 +68,18 @@ func (gm *Game) AddPlayer(usr *user.User) {
|
||||
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
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ func main() {
|
||||
mux.PrivateHandleFunc("/api/userinfo", mux.GetUserInfo)
|
||||
mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo)
|
||||
mux.PrivateHandleFunc("/api/setGameName", app.SetGameName)
|
||||
mux.PrivateHandleFunc("/api/savePlayer", app.SavePlayer)
|
||||
mux.PrivateHandleFunc("/api/sync", app.SyncHandler)
|
||||
mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes)
|
||||
mux.PrivateHandleFunc("/api/startGame", app.StartGame)
|
||||
|
@ -13,6 +13,7 @@ const (
|
||||
type User struct {
|
||||
mu sync.Mutex
|
||||
id string
|
||||
filename string
|
||||
authcode string
|
||||
name string
|
||||
role string
|
||||
|
@ -24,6 +24,7 @@ func NewUserFromFile(fileName string) (*User, error) {
|
||||
return &User{
|
||||
id: id,
|
||||
name: userJson.Name,
|
||||
filename: fileName,
|
||||
role: userJson.Role,
|
||||
authcode: userJson.Authcode,
|
||||
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 {
|
||||
usr.mu.Lock()
|
||||
defer usr.mu.Unlock()
|
||||
@ -45,6 +66,13 @@ func (usr *User) GetAuthCode() string {
|
||||
return usr.authcode
|
||||
}
|
||||
|
||||
func (usr *User) SetAuthcode(authcode string) {
|
||||
usr.mu.Lock()
|
||||
defer usr.mu.Unlock()
|
||||
|
||||
usr.authcode = authcode
|
||||
}
|
||||
|
||||
func (usr *User) GetName() string {
|
||||
usr.mu.Lock()
|
||||
defer usr.mu.Unlock()
|
||||
@ -52,6 +80,13 @@ func (usr *User) GetName() string {
|
||||
return usr.name
|
||||
}
|
||||
|
||||
func (usr *User) SetName(name string) {
|
||||
usr.mu.Lock()
|
||||
defer usr.mu.Unlock()
|
||||
|
||||
usr.name = name
|
||||
}
|
||||
|
||||
func (usr *User) GetRole() string {
|
||||
usr.mu.Lock()
|
||||
defer usr.mu.Unlock()
|
||||
|
Loading…
Reference in New Issue
Block a user