chore: fix linter warnings

This commit is contained in:
Settel 2022-12-12 22:50:28 +01:00
parent 8313847806
commit b2e402f4bb
11 changed files with 54 additions and 13 deletions

View File

@ -2,10 +2,11 @@ package application
import ( import (
"fmt" "fmt"
"sirlab.de/go/knowyt/game" "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() app.mu.Lock()
defer app.mu.Unlock() defer app.mu.Unlock()

View File

@ -25,7 +25,7 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht
} }
gameInfo := gm.GetGameInfo() gameInfo := gm.GetGameInfo()
for i, _ := range gameInfo.Players { for i := range gameInfo.Players {
if playerUser, err := app.GetUserById(gameInfo.Players[i].Id); err != nil { 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) log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id)
w.WriteHeader(http.StatusInternalServerError) 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") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(gameInfo) jsonString, _ := json.Marshal(gameInfo)
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, "%s", string(jsonString))
} }

View File

@ -31,7 +31,7 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http.
app.mu.Unlock() app.mu.Unlock()
for k, gameInfo := range games.Games { 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 { 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) log.Warn("GetUserById() failed: couldn't find player %s\n", gameInfo.Players[i].Id)
w.WriteHeader(http.StatusInternalServerError) 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") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(games) jsonString, _ := json.Marshal(games)
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, "%s", string(jsonString))
} }

View File

@ -30,5 +30,5 @@ func (app *Application) GetQuotes(usr *user.User, w http.ResponseWriter, r *http
quotesInfo := gm.GetQuotesInfoByUser(usr) quotesInfo := gm.GetQuotesInfoByUser(usr)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(quotesInfo) jsonString, _ := json.Marshal(quotesInfo)
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, "%s", string(jsonString))
} }

View File

@ -2,10 +2,11 @@ package application
import ( import (
"fmt" "fmt"
"sirlab.de/go/knowyt/user" "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() app.mu.Lock()
defer app.mu.Unlock() defer app.mu.Unlock()
@ -16,7 +17,7 @@ func (app Application) GetUserById(id string) (*user.User, error) {
return usr, nil return usr, nil
} }
func (app Application) GetUserByAuthcode(authcode string) (*user.User, error) { func (app *Application) GetUserByAuthcode(authcode string) (*user.User, error) {
app.mu.Lock() app.mu.Lock()
defer app.mu.Unlock() defer app.mu.Unlock()

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
) )
@ -15,7 +16,7 @@ func (app *Application) loadGameState(gm *game.Game, fileName string) error {
var stateJson game.GameStateJson var stateJson game.GameStateJson
if err := json.Unmarshal(jsonBytes, &stateJson); err != nil { if err := json.Unmarshal(jsonBytes, &stateJson); err != nil {
return fmt.Errorf("%s: %v\n", fileName, err) return fmt.Errorf("%s: %v", fileName, err)
} else { } else {
gm.SetGameState(&stateJson) gm.SetGameState(&stateJson)
} }

View File

@ -3,10 +3,11 @@ package application
import ( import (
"os" "os"
"path" "path"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
) )
func (app Application) loadGames() error { func (app *Application) loadGames() error {
dirName := path.Join(app.config.DataDir, "games") dirName := path.Join(app.config.DataDir, "games")
files, err := os.ReadDir(dirName) files, err := os.ReadDir(dirName)
if err != nil { if err != nil {
@ -41,7 +42,7 @@ func (app Application) loadGames() error {
return nil return nil
} }
func (app Application) addGame(gm *game.Game) { func (app *Application) addGame(gm *game.Game) {
app.mu.Lock() app.mu.Lock()
defer app.mu.Unlock() defer app.mu.Unlock()

View File

@ -3,11 +3,12 @@ package application
import ( import (
"os" "os"
"path" "path"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/quote" "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") quoteDirName := path.Join(dirName, "quotes")
files, err := os.ReadDir(quoteDirName) files, err := os.ReadDir(quoteDirName)
if err != nil { if err != nil {

View File

@ -3,10 +3,11 @@ package application
import ( import (
"os" "os"
"path" "path"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
func (app Application) loadUsers() error { func (app *Application) loadUsers() error {
dirName := path.Join(app.config.DataDir, "users") dirName := path.Join(app.config.DataDir, "users")
files, err := os.ReadDir(dirName) files, err := os.ReadDir(dirName)
if err != nil { if err != nil {

View File

@ -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")
}

View File

@ -37,6 +37,7 @@ func main() {
mux.PrivateHandleFunc("/api/continueGame", app.ContinueGame) mux.PrivateHandleFunc("/api/continueGame", app.ContinueGame)
mux.PrivateHandleFunc("/api/finishGame", app.FinishGame) mux.PrivateHandleFunc("/api/finishGame", app.FinishGame)
mux.PrivateHandleFunc("/api/disableGame", app.DisableGame) mux.PrivateHandleFunc("/api/disableGame", app.DisableGame)
mux.PrivateHandleFunc("/api/removeGame", app.RemoveGame)
mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection) mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection)
mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes) mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes)
mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote) mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote)