From b2e402f4bbbd0f25eded8e160852ab55161da8c2 Mon Sep 17 00:00:00 2001 From: Settel Date: Mon, 12 Dec 2022 22:50:28 +0100 Subject: [PATCH] chore: fix linter warnings --- server/src/application/getGame.go | 3 ++- server/src/application/getGameInfo.go | 4 +-- server/src/application/getGames.go | 4 +-- server/src/application/getQuotes.go | 2 +- server/src/application/getUser.go | 5 ++-- server/src/application/loadGameState.go | 3 ++- server/src/application/loadGames.go | 5 ++-- server/src/application/loadQuotes.go | 3 ++- server/src/application/loadUsers.go | 3 ++- server/src/application/removeGame.go | 34 +++++++++++++++++++++++++ server/src/knowyt.go | 1 + 11 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 server/src/application/removeGame.go diff --git a/server/src/application/getGame.go b/server/src/application/getGame.go index c7ad6f9..e92a014 100644 --- a/server/src/application/getGame.go +++ b/server/src/application/getGame.go @@ -2,10 +2,11 @@ package application import ( "fmt" + "sirlab.de/go/knowyt/game" ) -func (app Application) GetGameById(id string) (*game.Game, error) { +func (app *Application) GetGameById(id string) (*game.Game, error) { app.mu.Lock() defer app.mu.Unlock() diff --git a/server/src/application/getGameInfo.go b/server/src/application/getGameInfo.go index 7e00a8d..2420ca7 100644 --- a/server/src/application/getGameInfo.go +++ b/server/src/application/getGameInfo.go @@ -25,7 +25,7 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht } gameInfo := gm.GetGameInfo() - for i, _ := range gameInfo.Players { + for i := range gameInfo.Players { if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) w.WriteHeader(http.StatusInternalServerError) @@ -40,5 +40,5 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht w.Header().Add("Content-Type", "application/json") jsonString, _ := json.Marshal(gameInfo) - fmt.Fprintf(w, string(jsonString)) + fmt.Fprintf(w, "%s", string(jsonString)) } diff --git a/server/src/application/getGames.go b/server/src/application/getGames.go index c2e4809..e5a8bad 100644 --- a/server/src/application/getGames.go +++ b/server/src/application/getGames.go @@ -31,7 +31,7 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http. app.mu.Unlock() for k, gameInfo := range games.Games { - for i, _ := range gameInfo.Players { + for i := range gameInfo.Players { if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id) w.WriteHeader(http.StatusInternalServerError) @@ -46,5 +46,5 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http. w.Header().Add("Content-Type", "application/json") jsonString, _ := json.Marshal(games) - fmt.Fprintf(w, string(jsonString)) + fmt.Fprintf(w, "%s", string(jsonString)) } diff --git a/server/src/application/getQuotes.go b/server/src/application/getQuotes.go index fe2cc66..5c21a71 100644 --- a/server/src/application/getQuotes.go +++ b/server/src/application/getQuotes.go @@ -30,5 +30,5 @@ func (app *Application) GetQuotes(usr *user.User, w http.ResponseWriter, r *http quotesInfo := gm.GetQuotesInfoByUser(usr) w.Header().Add("Content-Type", "application/json") jsonString, _ := json.Marshal(quotesInfo) - fmt.Fprintf(w, string(jsonString)) + fmt.Fprintf(w, "%s", string(jsonString)) } diff --git a/server/src/application/getUser.go b/server/src/application/getUser.go index 2225954..5b77ee3 100644 --- a/server/src/application/getUser.go +++ b/server/src/application/getUser.go @@ -2,10 +2,11 @@ package application import ( "fmt" + "sirlab.de/go/knowyt/user" ) -func (app Application) GetUserById(id string) (*user.User, error) { +func (app *Application) GetUserById(id string) (*user.User, error) { app.mu.Lock() defer app.mu.Unlock() @@ -16,7 +17,7 @@ func (app Application) GetUserById(id string) (*user.User, error) { return usr, nil } -func (app Application) GetUserByAuthcode(authcode string) (*user.User, error) { +func (app *Application) GetUserByAuthcode(authcode string) (*user.User, error) { app.mu.Lock() defer app.mu.Unlock() diff --git a/server/src/application/loadGameState.go b/server/src/application/loadGameState.go index 55e1900..c87ea18 100644 --- a/server/src/application/loadGameState.go +++ b/server/src/application/loadGameState.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "sirlab.de/go/knowyt/game" ) @@ -15,7 +16,7 @@ func (app *Application) loadGameState(gm *game.Game, fileName string) error { var stateJson game.GameStateJson if err := json.Unmarshal(jsonBytes, &stateJson); err != nil { - return fmt.Errorf("%s: %v\n", fileName, err) + return fmt.Errorf("%s: %v", fileName, err) } else { gm.SetGameState(&stateJson) } diff --git a/server/src/application/loadGames.go b/server/src/application/loadGames.go index 78dfffd..9931028 100644 --- a/server/src/application/loadGames.go +++ b/server/src/application/loadGames.go @@ -3,10 +3,11 @@ package application import ( "os" "path" + "sirlab.de/go/knowyt/game" ) -func (app Application) loadGames() error { +func (app *Application) loadGames() error { dirName := path.Join(app.config.DataDir, "games") files, err := os.ReadDir(dirName) if err != nil { @@ -41,7 +42,7 @@ func (app Application) loadGames() error { return nil } -func (app Application) addGame(gm *game.Game) { +func (app *Application) addGame(gm *game.Game) { app.mu.Lock() defer app.mu.Unlock() diff --git a/server/src/application/loadQuotes.go b/server/src/application/loadQuotes.go index 12d1718..c24f397 100644 --- a/server/src/application/loadQuotes.go +++ b/server/src/application/loadQuotes.go @@ -3,11 +3,12 @@ package application import ( "os" "path" + "sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/quote" ) -func (app Application) loadQuotes(gm *game.Game, dirName string) error { +func (app *Application) loadQuotes(gm *game.Game, dirName string) error { quoteDirName := path.Join(dirName, "quotes") files, err := os.ReadDir(quoteDirName) if err != nil { diff --git a/server/src/application/loadUsers.go b/server/src/application/loadUsers.go index 5edb8a7..ee6269d 100644 --- a/server/src/application/loadUsers.go +++ b/server/src/application/loadUsers.go @@ -3,10 +3,11 @@ package application import ( "os" "path" + "sirlab.de/go/knowyt/user" ) -func (app Application) loadUsers() error { +func (app *Application) loadUsers() error { dirName := path.Join(app.config.DataDir, "users") files, err := os.ReadDir(dirName) if err != nil { diff --git a/server/src/application/removeGame.go b/server/src/application/removeGame.go new file mode 100644 index 0000000..b4bc444 --- /dev/null +++ b/server/src/application/removeGame.go @@ -0,0 +1,34 @@ +package application + +import ( + "fmt" + "net/http" + + "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 true { + err := fmt.Errorf("not yet implmemented (%s)", gm.GetId()) + log.ErrorLog(err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "couldn't save game state") + } + + fmt.Fprintf(w, "ok") +} diff --git a/server/src/knowyt.go b/server/src/knowyt.go index f27fe34..42fb9d9 100644 --- a/server/src/knowyt.go +++ b/server/src/knowyt.go @@ -37,6 +37,7 @@ func main() { mux.PrivateHandleFunc("/api/continueGame", app.ContinueGame) mux.PrivateHandleFunc("/api/finishGame", app.FinishGame) mux.PrivateHandleFunc("/api/disableGame", app.DisableGame) + mux.PrivateHandleFunc("/api/removeGame", app.RemoveGame) mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection) mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes) mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote)