refactor: use new Log class
This commit is contained in:
parent
190cfa5a54
commit
eb703f86b1
@ -12,7 +12,7 @@ build:
|
||||
|
||||
run:
|
||||
$(MAKE) build
|
||||
./knowyt
|
||||
./knowyt -v
|
||||
|
||||
run-loop:
|
||||
pexec -R -c -e TARGET \
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -23,7 +25,7 @@ func (app *Application) CollectQuotes(usr *user.User, w http.ResponseWriter, r *
|
||||
|
||||
gm.CollectQuotes()
|
||||
if err := app.saveGameState(gm); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
return
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -24,6 +26,6 @@ func (app *Application) ContinueGame(usr *user.User, w http.ResponseWriter, r *h
|
||||
gm.ContinueGame()
|
||||
|
||||
if err := app.saveGameState(gm); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,17 @@ package application
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
type PlayerInfo struct {
|
||||
@ -36,7 +38,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
|
||||
gm, err := app.createGame(teamname, lang)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
@ -44,7 +46,7 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
|
||||
_, err = app.createUser(gm, name, user.ROLE_GAMEMASTER, authcode)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
@ -53,11 +55,11 @@ func (app *Application) CreateGame(w http.ResponseWriter, r *http.Request) {
|
||||
Status: "ok",
|
||||
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")
|
||||
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) {
|
||||
@ -72,11 +74,12 @@ func (app *Application) createGame(gamename, lang string) (*game.Game, error) {
|
||||
if err := os.Mkdir(quotesDirName, 0777); err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gm.SaveGame(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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]
|
||||
if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() {
|
||||
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")
|
||||
return
|
||||
}
|
||||
if deleteUser.GetId() == usr.GetId() {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Printf("can't delete yourself\n")
|
||||
log.Warn("can't delete yourself\n")
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
}
|
||||
|
||||
if err := deleteUser.DeleteUser(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "internal server error")
|
||||
return
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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 {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
return
|
||||
@ -31,7 +33,7 @@ func (app *Application) FinishGame(usr *user.User, w http.ResponseWriter, r *htt
|
||||
gm.FinishGame()
|
||||
|
||||
if err := app.saveGameState(gm); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
return
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -25,7 +27,7 @@ func (app *Application) GetGameInfo(usr *user.User, w http.ResponseWriter, r *ht
|
||||
gameInfo := gm.GetGameInfo()
|
||||
for i, _ := range gameInfo.Players {
|
||||
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)
|
||||
fmt.Fprintf(w, "internal server error")
|
||||
return
|
||||
|
@ -4,7 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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 i, _ := range gameInfo.Players {
|
||||
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)
|
||||
fmt.Fprintf(w, "internal server error")
|
||||
return
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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")
|
||||
gm, err := app.GetGameById(gameRef)
|
||||
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)
|
||||
fmt.Fprintf(w, "game not found")
|
||||
return
|
||||
}
|
||||
|
||||
if usr.GetGameId() != gameRef && !usr.IsGamemaster() {
|
||||
fmt.Printf("user's game id is %s\n", usr.GetGameId())
|
||||
fmt.Printf("user not allowed to access game id %s\n", gameRef)
|
||||
log.Warn("user's game id is %s\n", usr.GetGameId())
|
||||
log.Warn("user not allowed to access game id %s\n", gameRef)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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)
|
||||
|
||||
if err := gm.RemoveQuote(quoteFileName, usr.GetId(), quoteId); err != nil {
|
||||
fmt.Printf("%s\n", err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -24,7 +26,7 @@ func (app *Application) ResetGame(usr *user.User, w http.ResponseWriter, r *http
|
||||
gm.ResetGame()
|
||||
|
||||
if err := app.saveGameState(gm); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
return
|
||||
|
@ -3,9 +3,11 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
"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) {
|
||||
@ -29,14 +31,14 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
|
||||
authcode := r.URL.Query().Get("authcode")
|
||||
if name == "" {
|
||||
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")
|
||||
return
|
||||
}
|
||||
if id != "" {
|
||||
if err := app.modifyUser(id, gm, name, authcode); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
} 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 {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -47,7 +48,7 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("%s", err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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")
|
||||
if err := gm.SetGameLang(lang); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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")
|
||||
if err := gm.SetGameName(name); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Printf("%v", err)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "server error")
|
||||
return
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package application
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -24,7 +26,7 @@ func (app *Application) StartGame(usr *user.User, w http.ResponseWriter, r *http
|
||||
gm.StartGame()
|
||||
|
||||
if err := app.saveGameState(gm); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
return
|
||||
|
@ -1,10 +1,30 @@
|
||||
package applicationConfig
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
type ApplicationConfig struct {
|
||||
DataDir string
|
||||
}
|
||||
|
||||
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{
|
||||
DataDir: "data/",
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/syncdata"
|
||||
)
|
||||
|
||||
@ -18,6 +18,6 @@ func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) {
|
||||
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)
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sirlab.de/go/knowyt/syncdata"
|
||||
"strconv"
|
||||
|
||||
"sirlab.de/go/knowyt/syncdata"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
select {
|
||||
case <-stream.Changes():
|
||||
<-stream.Changes()
|
||||
stream.Next()
|
||||
}
|
||||
}
|
||||
|
||||
jsonString, _ := json.MarshalIndent(value, "", " ")
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, string(jsonString))
|
||||
fmt.Fprintf(w, "%s", string(jsonString))
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) CollectQuotes() {
|
||||
err := gm.changeGameState(STATE_IDLE, STATE_COLLECT, PHASE_NONE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) ContinueGame() {
|
||||
state, phase := gm.GetState()
|
||||
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
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ func (gm *Game) ContinueGame() {
|
||||
case PHASE_REVEAL_SOURCE:
|
||||
gm.nextRound()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) FinishGame() {
|
||||
state, _ := gm.GetState()
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) nextRound() {
|
||||
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SOURCE, PHASE_ROUND_END); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.notifyClients()
|
||||
@ -16,7 +17,7 @@ func (gm *Game) nextRound() {
|
||||
|
||||
err := gm.changeGameState(STATE_PLAY, STATE_READY_SET, "Go!")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.notifyClients()
|
||||
@ -25,7 +26,7 @@ func (gm *Game) nextRound() {
|
||||
|
||||
err = gm.changeGameState(STATE_READY_SET, STATE_READY_SET, "")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.notifyClients()
|
||||
@ -34,7 +35,7 @@ func (gm *Game) nextRound() {
|
||||
|
||||
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.runRound()
|
||||
|
@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
|
||||
@ -24,9 +24,9 @@ func (gm *Game) EventPlayerJoins(usr *user.User) {
|
||||
gm.players[usrId] = player
|
||||
|
||||
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 {
|
||||
fmt.Printf("%s player \"%s\" joined\n", gm.id, usrName)
|
||||
log.Debug("%s player \"%s\" joined\n", gm.id, usrName)
|
||||
}
|
||||
|
||||
gm.eng.Update()
|
||||
@ -45,7 +45,7 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) {
|
||||
player.isIdle = true
|
||||
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()
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/syncdata"
|
||||
)
|
||||
|
||||
@ -12,7 +12,7 @@ func (gm *Game) populateGetRoundInfo() *syncdata.RoundInfo {
|
||||
|
||||
quote := gm.quotes[gm.round.quoteId]
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,17 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) revealShowCount() {
|
||||
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
|
||||
}
|
||||
|
||||
if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_SHOW_COUNT); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
for key, val := range gm.round.selections {
|
||||
|
@ -1,12 +1,12 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) revealSource() {
|
||||
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SHOW_COUNT, PHASE_REVEAL_SOURCE); err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) runCountdown() {
|
||||
@ -17,7 +18,7 @@ func (gm *Game) runCountdown() {
|
||||
|
||||
err := gm.changeGameState(STATE_IDLE, STATE_READY_SET, phases[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.notifyClients()
|
||||
@ -26,7 +27,7 @@ func (gm *Game) runCountdown() {
|
||||
for i := 1; i < len(phases); i++ {
|
||||
err = gm.changeGamePhase(STATE_READY_SET, phases[i-1], phases[i])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
gm.notifyClients()
|
||||
@ -35,7 +36,7 @@ func (gm *Game) runCountdown() {
|
||||
|
||||
err = gm.changeGameState(STATE_READY_SET, STATE_PLAY, PHASE_NONE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) runFinal() {
|
||||
state, _ := gm.GetState()
|
||||
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
|
||||
}
|
||||
|
||||
err := gm.changeGameState(STATE_ANY, STATE_FINAL, PHASE_NONE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,19 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) runRound() {
|
||||
state, _ := gm.GetState()
|
||||
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
|
||||
}
|
||||
|
||||
err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_SELECT_QUOTE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"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)
|
||||
}
|
||||
|
@ -3,8 +3,10 @@ package game
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sirlab.de/go/knowyt/quote"
|
||||
"time"
|
||||
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/quote"
|
||||
)
|
||||
|
||||
func (gm *Game) setupRound() error {
|
||||
@ -66,7 +68,7 @@ func (gm *Game) selectQuote() error {
|
||||
func (gm *Game) getSourceById(id string) Source {
|
||||
plInfo, err := gm.getPlayerById(id)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return Source{}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func (gm *Game) StartGame() {
|
||||
@ -11,7 +11,7 @@ func (gm *Game) StartGame() {
|
||||
func (gm *Game) ResetGame() {
|
||||
err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
log.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
module sirlab.de/go/knowyt
|
||||
|
||||
go 1.16
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
|
@ -5,7 +5,9 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"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) {
|
||||
@ -24,7 +26,7 @@ func FileHandler(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf("%s: %v\n", uri, err)
|
||||
log.Error("%s: %v\n", uri, err)
|
||||
http.NotFoundHandler().ServeHTTP(w, req)
|
||||
return
|
||||
}
|
||||
|
@ -1,18 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"sirlab.de/go/knowyt/application"
|
||||
"sirlab.de/go/knowyt/applicationConfig"
|
||||
"sirlab.de/go/knowyt/handler"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appConfig := applicationConfig.NewApplicationConfig()
|
||||
app, err := application.NewApplication(appConfig)
|
||||
if err != nil {
|
||||
fmt.Printf("%v\n", err)
|
||||
log.ErrorLog(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
mux := handler.NewAuthMux(app)
|
||||
@ -44,6 +45,6 @@ func main() {
|
||||
mux.PublicHandleFunc("/", handler.FileHandler)
|
||||
|
||||
// start listening
|
||||
fmt.Printf("http://localhost:%d/\n", mux.Port)
|
||||
log.Info("http://localhost:%d/\n", mux.Port)
|
||||
mux.ListenAndServe()
|
||||
}
|
||||
|
59
server/src/log/main.go
Normal file
59
server/src/log/main.go
Normal 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
13
server/src/log/struct.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user