package application import ( "fmt" "net/http" "os" "path" "sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/log" "sirlab.de/go/knowyt/user" ) func (app *Application) RemoveGame(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.IsAdminOrCameo() { w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return } if err := app.removeGame(gm); err != nil { log.ErrorLog(err) w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "failed to remove game\n") return } fmt.Fprintf(w, "ok") } func (app *Application) removeGame(gm *game.Game) error { log.Info("game %s: start removing\n", gm.GetId()) gameInfo := gm.GetGameInfo() for _, player := range gameInfo.Players { log.Info("game %s: removing player %s (%s)\n", gm.GetId(), player.Id, player.Name) if err := app.removePlayerById(player.Id, gm); err != nil { return err } } gameBaseDir := path.Join(app.config.DataDir, "games", gm.GetId()) log.Info("game %s: removing quotes dir\n", gm.GetId()) quotesDir := path.Join(gameBaseDir, "quotes") if err := os.Remove(quotesDir); err != nil { log.Error("failed to remove quotes\n") return nil } log.Info("game %s: removing state file", gm.GetId()) stateFilename := path.Join(gameBaseDir, "state.json") if err := os.Remove(stateFilename); err != nil { return err } if err := gm.RemoveGame(); err != nil { return err } log.Info("game %s: removing game dir", gm.GetId()) if err := os.Remove(gameBaseDir); err != nil { return err } app.mu.Lock() delete(app.games, gm.GetId()) app.mu.Unlock() log.Info("game %s removed\n", gm.GetId()) return nil }