From accf16c92aa8420dd12d414ab05c60358cbc23ba Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Wed, 2 Oct 2024 22:30:06 +0200 Subject: [PATCH] Change host lookup to be optional Some parts of the logic do not make use of the host column in the players table, allow the field to be empty and do not query for an empty value (which might return bogus entries) if no value is passed to the lookup function. This additionally avoids the need for the hardcoded initial host when configuring the bot player. Signed-off-by: Georg Pfuetzenreuter --- wat/db.go | 6 +++++- wat/game.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/wat/db.go b/wat/db.go index 85a598b..8971223 100644 --- a/wat/db.go +++ b/wat/db.go @@ -65,8 +65,12 @@ func NewWatDb() *WatDb { func (w *WatDb) User(nick, host string, create bool) Player { var player Player + query := "nick = ?" + if host != "" { + query = query + " or host = ?" + } // Try and get a user - if err := w.db.First(&player, "nick = ? or host = ?", nick, host).Error; err != nil && create { + if err := w.db.First(&player, query, nick, host).Error; err != nil && create { fmt.Printf("Creating user: %s\n", err.Error()) // No user, make another player.Nick = nick diff --git a/wat/game.go b/wat/game.go index 40aa6f3..8de6991 100644 --- a/wat/game.go +++ b/wat/game.go @@ -28,7 +28,7 @@ var unconscious = "wat, your hands fumble and fail you. try resting, weakling." func NewWatGame(bot *WatBot, db *WatDb) *WatGame { g := WatGame{bot, db, Player{}, nil, nil, nil, nil, map[string]int{}} - g.me = g.db.User(bot.Nick, "amia8t89xfp8y.liberta.casa", true) + g.me = g.db.User(bot.Nick, "", true) g.commands = map[string](func(*Player, []string) string){ //"wat": g.megaWat, "steroid": g.Steroid,