diff --git a/client/src/components/admin/AdminEditPlayer.vue b/client/src/components/admin/AdminEditPlayer.vue index 2d4e096..009b481 100644 --- a/client/src/components/admin/AdminEditPlayer.vue +++ b/client/src/components/admin/AdminEditPlayer.vue @@ -6,6 +6,9 @@ Name: + + delete player + Score: @@ -20,7 +23,9 @@ - + + + @@ -45,6 +50,10 @@ export default { }) this.$emit('close') }, + async deletePlayer() { + await this.$engine.deletePlayer({ id: this.id }) + this.$emit('close') + }, }, } @@ -53,6 +62,7 @@ export default { @import '~/assets/css/components'; .admin-edit-player { + font-family: Dosis; &__close { float: right; margin: 16px -16px 16px 16px; @@ -75,5 +85,21 @@ export default { &__table { margin: 64px; } + &__button-save { + padding: 0.3em 2em; + } + &__table-cell-delete { + vertical-align: top; + } + &__button-delete { + margin-left: 3em; + font-size: 16px; + font-family: Dosis; + color: #e06060; + cursor: pointer; + &:hover { + color: #ffffff; + } + } } diff --git a/client/src/plugins/engine/deletePlayer.js b/client/src/plugins/engine/deletePlayer.js new file mode 100644 index 0000000..41142be --- /dev/null +++ b/client/src/plugins/engine/deletePlayer.js @@ -0,0 +1,8 @@ +export default async function(player) { + const { store } = this.context + + await this.callApi('/api/deletePlayer', { + g: store.state.engine.user?.game, + id: player.id, + }) +} diff --git a/client/src/plugins/engine/index.js b/client/src/plugins/engine/index.js index 4360e05..6eb4719 100644 --- a/client/src/plugins/engine/index.js +++ b/client/src/plugins/engine/index.js @@ -6,6 +6,7 @@ import fetchUserInfo from './fetchUserInfo' import fetchGameInfo from './fetchGameInfo' import setGameName from './setGameName' import savePlayer from './savePlayer' +import deletePlayer from './deletePlayer' import collectQuotes from './collectQuotes' import startGame from './startGame' import resetGame from './resetGame' @@ -33,6 +34,7 @@ export default (context, inject) => { fetchGameInfo, setGameName, savePlayer, + deletePlayer, collectQuotes, getMyQuotes, saveQuote, diff --git a/server/src/application/deletePlayer.go b/server/src/application/deletePlayer.go new file mode 100644 index 0000000..3be46c1 --- /dev/null +++ b/server/src/application/deletePlayer.go @@ -0,0 +1,42 @@ +package application + +import ( + "fmt" + "net/http" + "sirlab.de/go/knyt/user" +) + +func (app *Application) DeletePlayer(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") + deleteUser := app.users[id] + if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() { + w.WriteHeader(http.StatusForbidden) + fmt.Printf("couldn't find player %s in game %s\n", id, gm.GetId()) + fmt.Fprintf(w, "forbidden") + return + } + if err := deleteUser.DeleteUser(); err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Printf("%v\n", err) + fmt.Fprintf(w, "internal server error") + return + } + gm.DeletePlayer(deleteUser) + delete(app.users, id) + + fmt.Fprintf(w, "ok") +} diff --git a/server/src/game/player.go b/server/src/game/player.go index 9c66552..cef4d78 100644 --- a/server/src/game/player.go +++ b/server/src/game/player.go @@ -83,3 +83,11 @@ func (gm *Game) UpdatePlayer(usr *user.User) { player.name = usr.GetName() gm.players[usrId] = player } + +func (gm *Game) DeletePlayer(usr *user.User) { + usrId := usr.GetId() + + gm.mu.Lock() + defer gm.mu.Unlock() + delete(gm.players, usrId) +} diff --git a/server/src/knowyt.go b/server/src/knowyt.go index 9dc2179..ada57d4 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -25,6 +25,7 @@ func main() { mux.PrivateHandleFunc("/api/gameinfo", app.GetGameInfo) mux.PrivateHandleFunc("/api/setGameName", app.SetGameName) mux.PrivateHandleFunc("/api/savePlayer", app.SavePlayer) + mux.PrivateHandleFunc("/api/deletePlayer", app.DeletePlayer) mux.PrivateHandleFunc("/api/sync", app.SyncHandler) mux.PrivateHandleFunc("/api/collectQuotes", app.CollectQuotes) mux.PrivateHandleFunc("/api/startGame", app.StartGame) diff --git a/server/src/user/user.go b/server/src/user/user.go index 017bcb3..d40b0ac 100644 --- a/server/src/user/user.go +++ b/server/src/user/user.go @@ -62,6 +62,10 @@ func (usr *User) SaveUser() error { return nil } +func (usr *User) DeleteUser() error { + return os.Remove(usr.filename) +} + func (usr *User) GetId() string { usr.mu.Lock() defer usr.mu.Unlock()