diff --git a/client/src/components/CollectQuote.vue b/client/src/components/CollectQuote.vue index de536fb..bf61eea 100644 --- a/client/src/components/CollectQuote.vue +++ b/client/src/components/CollectQuote.vue @@ -16,9 +16,15 @@ export default { methods: { edit() { console.log('edit') + this.save() + }, + save() { + console.log('save') + this.$engine.saveQuote(this.quote.id, this.quote.quote) }, remove() { console.log('remove') + this.$engine.removeQuote(this.quote.id) }, }, } @@ -46,6 +52,7 @@ export default { line-height: 48px; font-size: 32px; color: #ffffff; + cursor: pointer; &:hover { background-color: #6040c0; diff --git a/client/src/components/CollectQuotes.vue b/client/src/components/CollectQuotes.vue index 4dc8d53..364a7b9 100644 --- a/client/src/components/CollectQuotes.vue +++ b/client/src/components/CollectQuotes.vue @@ -1,6 +1,6 @@ diff --git a/client/src/plugins/engine/index.js b/client/src/plugins/engine/index.js index 76ac61f..58ad8d5 100644 --- a/client/src/plugins/engine/index.js +++ b/client/src/plugins/engine/index.js @@ -12,6 +12,7 @@ import parseSyncData from './parseSyncData' import saveSelection from './saveSelection' import getMyQuotes from './getMyQuotes' import saveQuote from './saveQuote' +import removeQuote from './removeQuote' export default (context, inject) => { const engine = { @@ -28,6 +29,7 @@ export default (context, inject) => { collectQuotes, getMyQuotes, saveQuote, + removeQuote, startGame, resetGame, continueGame, diff --git a/client/src/plugins/engine/removeQuote.js b/client/src/plugins/engine/removeQuote.js new file mode 100644 index 0000000..c1086e7 --- /dev/null +++ b/client/src/plugins/engine/removeQuote.js @@ -0,0 +1,8 @@ +export default async function(id, quote) { + const { store } = this.context + + await this.callApi('/api/removeQuote', { + g: store.state.engine.user?.game, + id, + }) +} diff --git a/server/src/application/removeQuote.go b/server/src/application/removeQuote.go new file mode 100644 index 0000000..89c0508 --- /dev/null +++ b/server/src/application/removeQuote.go @@ -0,0 +1,31 @@ +package application + +import ( + "fmt" + "net/http" + "sirlab.de/go/knyt/user" +) + +func (app *Application) RemoveQuote(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 + } + + quoteId := r.URL.Query().Get("id") + if err := gm.RemoveQuote(usr.GetId(), quoteId); err != nil { + fmt.Printf("%s\n", err) + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "forbidden") + return + } +} diff --git a/server/src/application/saveQuote.go b/server/src/application/saveQuote.go new file mode 100644 index 0000000..fd5c382 --- /dev/null +++ b/server/src/application/saveQuote.go @@ -0,0 +1,32 @@ +package application + +import ( + "fmt" + "net/http" + "sirlab.de/go/knyt/user" +) + +func (app *Application) SaveQuote(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 + } + + quoteId := r.URL.Query().Get("id") + quoteText := r.URL.Query().Get("quote") + if err := gm.SaveQuote(usr.GetId(), quoteId, quoteText); err != nil { + fmt.Printf("%s\n", err) + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "forbidden") + return + } +} diff --git a/server/src/game/getQuote.go b/server/src/game/getQuote.go index 916807e..bccfd98 100644 --- a/server/src/game/getQuote.go +++ b/server/src/game/getQuote.go @@ -11,7 +11,7 @@ func (gm *Game) getQuoteById(id string) (*quote.Quote, error) { quote := gm.quotes[id] if quote == nil { - return nil, fmt.Errorf("player id \"%s\" not found in game\n", id) + return nil, fmt.Errorf("quote id \"%s\" not found in game\n", id) } return quote, nil diff --git a/server/src/game/removeQuote.go b/server/src/game/removeQuote.go new file mode 100644 index 0000000..db1d318 --- /dev/null +++ b/server/src/game/removeQuote.go @@ -0,0 +1,20 @@ +package game + +import ( + "fmt" +) + +func (gm *Game) RemoveQuote(usrId string, quoteId string) error { + quote, err := gm.getQuoteById(quoteId) + if err != nil { + return err + } + + if quote.GetSourceId() != usrId { + return fmt.Errorf("usrId does not match quote source id") + } + + fmt.Printf("%v\n", quote) + + return fmt.Errorf("Game::RemoveQuote() not yet implemented") +} diff --git a/server/src/game/saveQuote.go b/server/src/game/saveQuote.go new file mode 100644 index 0000000..b6a168b --- /dev/null +++ b/server/src/game/saveQuote.go @@ -0,0 +1,20 @@ +package game + +import ( + "fmt" +) + +func (gm *Game) SaveQuote(usrId string, quoteId string, quoteText string) error { + quote, err := gm.getQuoteById(quoteId) + if err != nil { + return err + } + + if quote.GetSourceId() != usrId { + return fmt.Errorf("usrId does not match quote source id") + } + + fmt.Printf("%v\n", quote) + + return fmt.Errorf("Game::SaveQuote() not yet implemented") +} diff --git a/server/src/knowyt.go b/server/src/knowyt.go index 5e6cb4c..0e77e7c 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -30,6 +30,8 @@ func main() { mux.PrivateHandleFunc("/api/finishGame", app.FinishGame) mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection) mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes) + mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote) + mux.PrivateHandleFunc("/api/removeQuote", app.RemoveQuote) // default handler fsHandler := http.FileServer(http.Dir("../../client/dist/"))