refactor: use new Log class

This commit is contained in:
Settel 2022-11-06 15:41:13 +01:00
parent 190cfa5a54
commit eb703f86b1
38 changed files with 214 additions and 86 deletions

View File

@ -12,7 +12,7 @@ build:
run: run:
$(MAKE) build $(MAKE) build
./knowyt ./knowyt -v
run-loop: run-loop:
pexec -R -c -e TARGET \ pexec -R -c -e TARGET \

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -23,7 +25,7 @@ func (app *Application) CollectQuotes(usr *user.User, w http.ResponseWriter, r *
gm.CollectQuotes() gm.CollectQuotes()
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state") fmt.Fprintf(w, "couldn't save game state")
return return

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,6 +26,6 @@ func (app *Application) ContinueGame(usr *user.User, w http.ResponseWriter, r *h
gm.ContinueGame() gm.ContinueGame()
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
} }
} }

View File

@ -3,15 +3,17 @@ package application
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/google/uuid"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
"path" "path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
"strconv" "strconv"
"time" "time"
"github.com/google/uuid"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user"
) )
type PlayerInfo struct { type PlayerInfo struct {
@ -36,7 +38,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
gm, err := app.createGame(teamname, lang) gm, err := app.createGame(teamname, lang)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }
@ -44,7 +46,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
_, err = app.createUser(gm, name, user.ROLE_GAMEMASTER, authcode) _, err = app.createUser(gm, name, user.ROLE_GAMEMASTER, authcode)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }
@ -53,11 +55,11 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
Status: "ok", Status: "ok",
Authcode: authcode, Authcode: authcode,
} }
fmt.Printf("Name %s, Teamname %s, Authcode %s\n", name, teamname, authcode) log.Debug("Name %s, Teamname %s, Authcode %s\n", name, teamname, authcode)
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
jsonString, _ := json.Marshal(playerInfo) jsonString, _ := json.Marshal(playerInfo)
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, "%s", string(jsonString))
} }
func (app *Application) createGame(gamename, lang string) (*game.Game, error) { func (app *Application) createGame(gamename, lang string) (*game.Game, error) {
@ -72,11 +74,12 @@ func (app *Application) createGame(gamename, lang string) (*game.Game, error) {
if err := os.Mkdir(quotesDirName, 0777); err != nil { if err := os.Mkdir(quotesDirName, 0777); err != nil {
return nil, err return nil, err
} }
fmt.Printf("createGame(\"%s\": \"%s\" [%s])\n", gameId, gamename, lang) log.Info("createGame(\"%s\": \"%s\" [%s])\n", gameId, gamename, lang)
gm, err := game.NewGame(gameId, gameFileName, gamename, lang) gm, err := game.NewGame(gameId, gameFileName, gamename, lang)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := gm.SaveGame(); err != nil { if err := gm.SaveGame(); err != nil {
return nil, err return nil, err
} }

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -25,20 +27,20 @@ func (app *Application) DeletePlayer(usr *user.User, w http.ResponseWriter, r *h
deleteUser := app.users[id] deleteUser := app.users[id]
if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() { if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Printf("couldn't find player %s in game %s\n", id, gm.GetId()) log.Warn("couldn't find player %s in game %s\n", id, gm.GetId())
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return
} }
if deleteUser.GetId() == usr.GetId() { if deleteUser.GetId() == usr.GetId() {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Printf("can't delete yourself\n") log.Warn("can't delete yourself\n")
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return
} }
if err := deleteUser.DeleteUser(); err != nil { if err := deleteUser.DeleteUser(); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "internal server error") fmt.Fprintf(w, "internal server error")
return return
} }

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -22,7 +24,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt
} }
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state") fmt.Fprintf(w, "couldn't save game state")
return return
@ -31,7 +33,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt
gm.FinishGame() gm.FinishGame()
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state") fmt.Fprintf(w, "couldn't save game state")
return return

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -25,7 +27,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 {
fmt.Printf("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)
fmt.Fprintf(w, "internal server error") fmt.Fprintf(w, "internal server error")
return return

View File

@ -4,7 +4,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -31,7 +33,7 @@ func (app *Application) GetGames(usr *user.User, w http.ResponseWriter, r *http.
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 {
fmt.Printf("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)
fmt.Fprintf(w, "internal server error") fmt.Fprintf(w, "internal server error")
return return

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -11,15 +13,15 @@ func (app *Application) GetQuotes(usr *user.User, w http.ResponseWriter, r *http
gameRef := r.URL.Query().Get("g") gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef) gm, err := app.GetGameById(gameRef)
if err != nil { if err != nil {
fmt.Printf("attempt to get quotes for invalid game id %s\n", gameRef) log.Warn("attempt to get quotes for invalid game id %s\n", gameRef)
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "game not found") fmt.Fprintf(w, "game not found")
return return
} }
if usr.GetGameId() != gameRef && !usr.IsGamemaster() { if usr.GetGameId() != gameRef && !usr.IsGamemaster() {
fmt.Printf("user's game id is %s\n", usr.GetGameId()) log.Warn("user's game id is %s\n", usr.GetGameId())
fmt.Printf("user not allowed to access game id %s\n", gameRef) log.Warn("user not allowed to access game id %s\n", gameRef)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"path" "path"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -28,7 +30,7 @@ func (app *Application) RemoveQuote(usr *user.User, w http.ResponseWriter, r *ht
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort) quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); err != nil { if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); err != nil {
fmt.Printf("%s\n", err) log.ErrorLog(err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,7 +26,7 @@ func (app *Application) ResetGame(usr *user.User, w http.ResponseWriter, r *http
gm.ResetGame() gm.ResetGame()
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state") fmt.Fprintf(w, "couldn't save game state")
return return

View File

@ -3,9 +3,11 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
"strconv" "strconv"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user"
) )
func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) { func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) {
@ -29,14 +31,14 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
authcode := r.URL.Query().Get("authcode") authcode := r.URL.Query().Get("authcode")
if name == "" { if name == "" {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
fmt.Printf("player name must not be empty\n") log.Warn("player name must not be empty\n")
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }
if id != "" { if id != "" {
if err := app.modifyUser(id, gm, name, authcode); err != nil { if err := app.modifyUser(id, gm, name, authcode); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }
@ -44,7 +46,7 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
newPlayerId, err := app.createUser(gm, name, user.ROLE_PLAYER, authcode) newPlayerId, err := app.createUser(gm, name, user.ROLE_PLAYER, authcode)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} else { } else {
@ -54,7 +56,7 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
if err := app.updateScore(gm, id, score); err != nil { if err := app.updateScore(gm, id, score); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -47,7 +48,7 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
} }
if err != nil { if err != nil {
fmt.Printf("%s", err) log.ErrorLog(err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,7 +26,7 @@ func (app *Application) SetGameLang(usr *user.User, w http.ResponseWriter, r *ht
lang := r.URL.Query().Get("lang") lang := r.URL.Query().Get("lang")
if err := gm.SetGameLang(lang); err != nil { if err := gm.SetGameLang(lang); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,7 +26,7 @@ func (app *Application) SetGameName(usr *user.User, w http.ResponseWriter, r *ht
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
if err := gm.SetGameName(name); err != nil { if err := gm.SetGameName(name); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v", err) log.ErrorLog(err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} }

View File

@ -3,6 +3,8 @@ package application
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,7 +26,7 @@ func (app *Application) StartGame(usr *user.User, w http.ResponseWriter, r *http
gm.StartGame() gm.StartGame()
if err := app.saveGameState(gm); err != nil { if err := app.saveGameState(gm); err != nil {
fmt.Println(err) log.ErrorLog(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "couldn't save game state") fmt.Fprintf(w, "couldn't save game state")
return return

View File

@ -1,10 +1,30 @@
package applicationConfig package applicationConfig
import (
"flag"
"sirlab.de/go/knowyt/log"
)
type ApplicationConfig struct { type ApplicationConfig struct {
DataDir string DataDir string
} }
func NewApplicationConfig() ApplicationConfig { func NewApplicationConfig() ApplicationConfig {
flagVerbose := flag.Bool("v", false, "log debug messages, too")
flagQuiet := flag.Bool("q", false, "be quiet; warning and error messages only")
flag.Parse()
log.SetLoglevel(log.LOG_INFO)
if *flagVerbose {
log.SetLoglevel(log.LOG_DEBUG)
}
if *flagQuiet {
log.SetLoglevel(log.LOG_WARN)
}
return ApplicationConfig{ return ApplicationConfig{
DataDir: "data/", DataDir: "data/",
} }

View File

@ -1,7 +1,7 @@
package engine package engine
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/syncdata" "sirlab.de/go/knowyt/syncdata"
) )
@ -18,6 +18,6 @@ func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) {
populateSyncDataCb(&data) populateSyncDataCb(&data)
} }
fmt.Printf("%s engine versionRef %d\n", data.GameInfo.GameId, eng.versionRef) log.Debug("%s engine versionRef %d\n", data.GameInfo.GameId, eng.versionRef)
eng.obs.Update(data) eng.obs.Update(data)
} }

View File

@ -4,8 +4,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"sirlab.de/go/knowyt/syncdata"
"strconv" "strconv"
"sirlab.de/go/knowyt/syncdata"
) )
func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) { func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) {
@ -25,13 +26,11 @@ func (eng *Engine) SyncHandler(w http.ResponseWriter, r *http.Request) {
break break
} }
select { <-stream.Changes()
case <-stream.Changes(): stream.Next()
stream.Next()
}
} }
jsonString, _ := json.MarshalIndent(value, "", " ") jsonString, _ := json.MarshalIndent(value, "", " ")
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, string(jsonString)) fmt.Fprintf(w, "%s", string(jsonString))
} }

View File

@ -1,13 +1,13 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) CollectQuotes() { func (gm *Game) CollectQuotes() {
err := gm.changeGameState(STATE_IDLE, STATE_COLLECT, PHASE_NONE) err := gm.changeGameState(STATE_IDLE, STATE_COLLECT, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,13 +1,13 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) ContinueGame() { func (gm *Game) ContinueGame() {
state, phase := gm.GetState() state, phase := gm.GetState()
if state != STATE_PLAY { if state != STATE_PLAY {
fmt.Printf("invalid state, can't continue game in %s state\n", state) log.Warn("invalid state, can't continue game in %s state\n", state)
return return
} }
@ -19,7 +19,7 @@ func (gm *Game) ContinueGame() {
case PHASE_REVEAL_SOURCE: case PHASE_REVEAL_SOURCE:
gm.nextRound() gm.nextRound()
default: default:
fmt.Printf("invalid state, can't continue game in %s state, %s phase\n", state, phase) log.Warn("invalid state, can't continue game in %s state, %s phase\n", state, phase)
return return
} }
} }

View File

@ -1,13 +1,13 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) FinishGame() { func (gm *Game) FinishGame() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY { if state != STATE_IDLE && state != STATE_PLAY {
fmt.Printf("%s invalid state, can't finish game in %s state\n", gm.id, state) log.Warn("%s invalid state, can't finish game in %s state\n", gm.id, state)
return return
} }

View File

@ -1,13 +1,14 @@
package game package game
import ( import (
"fmt"
"time" "time"
"sirlab.de/go/knowyt/log"
) )
func (gm *Game) nextRound() { func (gm *Game) nextRound() {
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SOURCE, PHASE_ROUND_END); err != nil { if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SOURCE, PHASE_ROUND_END); err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.notifyClients() gm.notifyClients()
@ -16,7 +17,7 @@ func (gm *Game) nextRound() {
err := gm.changeGameState(STATE_PLAY, STATE_READY_SET, "Go!") err := gm.changeGameState(STATE_PLAY, STATE_READY_SET, "Go!")
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.notifyClients() gm.notifyClients()
@ -25,7 +26,7 @@ func (gm *Game) nextRound() {
err = gm.changeGameState(STATE_READY_SET, STATE_READY_SET, "") err = gm.changeGameState(STATE_READY_SET, STATE_READY_SET, "")
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.notifyClients() gm.notifyClients()
@ -34,7 +35,7 @@ func (gm *Game) nextRound() {
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE) err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.runRound() gm.runRound()

View File

@ -1,7 +1,7 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -24,9 +24,9 @@ func (gm *Game) EventPlayerJoins(usr *user.User) {
gm.players[usrId] = player gm.players[usrId] = player
if prevPlayer.id != "" { if prevPlayer.id != "" {
fmt.Printf("%s player \"%s\" re-joined\n", gm.id, gm.players[usrId].name) log.Debug("%s player \"%s\" re-joined\n", gm.id, gm.players[usrId].name)
} else { } else {
fmt.Printf("%s player \"%s\" joined\n", gm.id, usrName) log.Debug("%s player \"%s\" joined\n", gm.id, usrName)
} }
gm.eng.Update() gm.eng.Update()
@ -45,7 +45,7 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) {
player.isIdle = true player.isIdle = true
gm.players[usrId] = player gm.players[usrId] = player
fmt.Printf("%s player \"%s\" is idle\n", gm.id, usr.GetName()) log.Debug("%s player \"%s\" is idle\n", gm.id, usr.GetName())
gm.eng.Update() gm.eng.Update()
} }

View File

@ -1,7 +1,7 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/syncdata" "sirlab.de/go/knowyt/syncdata"
) )
@ -12,7 +12,7 @@ func (gm *Game) populateGetRoundInfo() *syncdata.RoundInfo {
quote := gm.quotes[gm.round.quoteId] quote := gm.quotes[gm.round.quoteId]
if quote == nil { if quote == nil {
fmt.Printf("%s fatal error: quote id \"%s\" not found\n", gm.id, gm.round.quoteId) log.Error("%s fatal error: quote id \"%s\" not found\n", gm.id, gm.round.quoteId)
return nil return nil
} }

View File

@ -1,17 +1,17 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) revealShowCount() { func (gm *Game) revealShowCount() {
if !gm.allPlayersHaveSelectedAQuote() { if !gm.allPlayersHaveSelectedAQuote() {
fmt.Printf("%s not all players have selected a quote yet\n", gm.GetId()) log.Warn("%s not all players have selected a quote yet\n", gm.GetId())
return return
} }
if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_SHOW_COUNT); err != nil { if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_SHOW_COUNT); err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
for key, val := range gm.round.selections { for key, val := range gm.round.selections {

View File

@ -1,12 +1,12 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) revealSource() { func (gm *Game) revealSource() {
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SHOW_COUNT, PHASE_REVEAL_SOURCE); err != nil { if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SHOW_COUNT, PHASE_REVEAL_SOURCE); err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,8 +1,9 @@
package game package game
import ( import (
"fmt"
"time" "time"
"sirlab.de/go/knowyt/log"
) )
func (gm *Game) runCountdown() { func (gm *Game) runCountdown() {
@ -17,7 +18,7 @@ func (gm *Game) runCountdown() {
err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0]) err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0])
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.notifyClients() gm.notifyClients()
@ -26,7 +27,7 @@ func (gm *Game) runCountdown() {
for i := 1; i < len(phases); i++ { for i := 1; i < len(phases); i++ {
err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i]) err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i])
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }
gm.notifyClients() gm.notifyClients()
@ -35,7 +36,7 @@ func (gm *Game) runCountdown() {
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE) err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,19 +1,19 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) runFinal() { func (gm *Game) runFinal() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY { if state != STATE_IDLE && state != STATE_PLAY {
fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" != \"%s\"", gm.GetId(), state) log.Warn("%s expected state \"IDLE\" | \"PLAY\" != \"%s\"", gm.GetId(), state)
return return
} }
err := gm.changeGameState(STATE_ANY, STATE_FINAL, PHASE_NONE) err := gm.changeGameState(STATE_ANY, STATE_FINAL, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,19 +1,19 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) runRound() { func (gm *Game) runRound() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY && state != STATE_COLLECT { if state != STATE_IDLE && state != STATE_PLAY && state != STATE_COLLECT {
fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", gm.GetId(), state) log.Warn("%s expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", gm.GetId(), state)
return return
} }
err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_SELECT_QUOTE) err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_SELECT_QUOTE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,7 +1,7 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -26,5 +26,5 @@ func (gm *Game) updateSelection(usr *user.User, selection string) {
} }
} }
fmt.Printf("%s invalid selection id \"%s\"\n", gm.id, selection) log.Warn("%s invalid selection id \"%s\"\n", gm.id, selection)
} }

View File

@ -3,8 +3,10 @@ package game
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"sirlab.de/go/knowyt/quote"
"time" "time"
"sirlab.de/go/knowyt/log"
"sirlab.de/go/knowyt/quote"
) )
func (gm *Game) setupRound() error { func (gm *Game) setupRound() error {
@ -66,7 +68,7 @@ func (gm *Game) selectQuote() error {
func (gm *Game) getSourceById(id string) Source { func (gm *Game) getSourceById(id string) Source {
plInfo, err := gm.getPlayerById(id) plInfo, err := gm.getPlayerById(id)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return Source{} return Source{}
} }

View File

@ -1,7 +1,7 @@
package game package game
import ( import (
"fmt" "sirlab.de/go/knowyt/log"
) )
func (gm *Game) StartGame() { func (gm *Game) StartGame() {
@ -11,7 +11,7 @@ func (gm *Game) StartGame() {
func (gm *Game) ResetGame() { func (gm *Game) ResetGame() {
err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE) err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE)
if err != nil { if err != nil {
fmt.Println(err) log.ErrorLog(err)
return return
} }

View File

@ -1,6 +1,6 @@
module sirlab.de/go/knowyt module sirlab.de/go/knowyt
go 1.16 go 1.18
require ( require (
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0

View File

@ -5,7 +5,9 @@ import (
"mime" "mime"
"net/http" "net/http"
"path" "path"
"sirlab.de/go/knowyt/resources"
"sirlab.de/go/knowyt/log"
Resources "sirlab.de/go/knowyt/resources"
) )
func FileHandler(w http.ResponseWriter, req *http.Request) { func FileHandler(w http.ResponseWriter, req *http.Request) {
@ -24,7 +26,7 @@ func FileHandler(w http.ResponseWriter, req *http.Request) {
} }
} }
if err != nil { if err != nil {
fmt.Printf("%s: %v\n", uri, err) log.Error("%s: %v\n", uri, err)
http.NotFoundHandler().ServeHTTP(w, req) http.NotFoundHandler().ServeHTTP(w, req)
return return
} }

View File

@ -1,18 +1,19 @@
package main package main
import ( import (
"fmt"
"os" "os"
"sirlab.de/go/knowyt/application" "sirlab.de/go/knowyt/application"
"sirlab.de/go/knowyt/applicationConfig" "sirlab.de/go/knowyt/applicationConfig"
"sirlab.de/go/knowyt/handler" "sirlab.de/go/knowyt/handler"
"sirlab.de/go/knowyt/log"
) )
func main() { func main() {
appConfig := applicationConfig.NewApplicationConfig() appConfig := applicationConfig.NewApplicationConfig()
app, err := application.NewApplication(appConfig) app, err := application.NewApplication(appConfig)
if err != nil { if err != nil {
fmt.Printf("%v\n", err) log.ErrorLog(err)
os.Exit(1) os.Exit(1)
} }
mux := handler.NewAuthMux(app) mux := handler.NewAuthMux(app)
@ -44,6 +45,6 @@ func main() {
mux.PublicHandleFunc("/", handler.FileHandler) mux.PublicHandleFunc("/", handler.FileHandler)
// start listening // start listening
fmt.Printf("http://localhost:%d/\n", mux.Port) log.Info("http://localhost:%d/\n", mux.Port)
mux.ListenAndServe() mux.ListenAndServe()
} }

59
server/src/log/main.go Normal file
View File

@ -0,0 +1,59 @@
package log
import (
"fmt"
"sync"
)
var lock = &sync.Mutex{}
var loglevel = LOG_NONE
func SetLoglevel(_loglevel int) {
lock.Lock()
defer lock.Unlock()
loglevel = _loglevel
}
func Debug(format string, v ...any) {
lock.Lock()
defer lock.Unlock()
if loglevel >= LOG_DEBUG {
fmt.Printf(format, v...)
}
}
func Info(format string, v ...any) {
lock.Lock()
defer lock.Unlock()
if loglevel >= LOG_INFO {
fmt.Printf(format, v...)
}
}
func Warn(format string, v ...any) {
lock.Lock()
defer lock.Unlock()
if loglevel >= LOG_WARN {
fmt.Printf(format, v...)
}
}
func Error(format string, v ...any) {
lock.Lock()
defer lock.Unlock()
if loglevel >= LOG_ERROR {
fmt.Printf(format, v...)
}
}
func ErrorLog(err error) {
lock.Lock()
defer lock.Unlock()
Error("%v\n", err)
}

13
server/src/log/struct.go Normal file
View File

@ -0,0 +1,13 @@
package log
const (
LOG_NONE = 1 << iota
LOG_ERROR
LOG_WARN
LOG_INFO
LOG_DEBUG
)
type Log struct {
Loglevel int
}