Compare commits

...

3 Commits

Author SHA1 Message Date
7ec49a9769
Improve Jeopardy cashout message
Print only a single message instead of one per winner to reduce chat
clutter.
Skip cashout to users who won less than the possible cashout value as
limited by the division value to avoid congratulating someone who only
gets 0.
Abort should a regression cause the logic to process an empty set of
finishers to prevent unexpected behavior.

Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
2024-10-03 19:43:39 +02:00
b6187e0077 Merge pull request 'Change host lookup to be optional' (#23) from dbhost into master
Reviewed-on: #23
2024-10-03 18:00:59 +02:00
accf16c92a
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 <mail@georg-pfuetzenreuter.net>
2024-10-02 22:30:06 +02:00
3 changed files with 42 additions and 6 deletions

View File

@ -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

View File

@ -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,

View File

@ -65,6 +65,23 @@ func (w *WatIntegration) Jeopardy(m *irc.Message, msgargs []string) {
// 8. The result is an array like "[nick1:1000, nick2:2000]"
finisherPrizes := strings.Split(strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Join(msgargs[2:], " "), ") (", ";", -1), ": ", ":", -1), "(", "", 1), ")", "", 1), ";")
fmt.Printf("Processing Jeopardy: %s\n", finisherPrizes)
var msg string
var many bool
fiprcount := len(finisherPrizes)
cashoutcount := 0
// only a single winner
if fiprcount == 1 {
msg = "smartass %s :) gave u %d"
many = false
// multiple winners
} else if fiprcount > 1 {
msg = "gang of smartasses :) gave %s %d"
many = true
// no winners (should never get here)
} else {
fmt.Printf("Empty finishers, aborting Jeopardy processing")
return
}
// iterate over the "$nick:$value" string elements
for _, pair := range finisherPrizes {
// turn the string element into an array, where the first entry is the nickname, and the second the value
@ -77,6 +94,10 @@ func (w *WatIntegration) Jeopardy(m *irc.Message, msgargs []string) {
name := nameCoinPair[0]
// Jeopardy prizes are quite a lot of $$$, make it a bit more sane
coins = coins / 40
if coins == 0 {
continue
}
cashoutcount += 1
// name = we assume the Jeopardy player name to match a Watbot player name
// host = we could use some WHO logic to find the host, but assuming nickname lookup to be sufficient here
// create = based on the above, maybe rather not create Watbot players based on only a nick?
@ -85,10 +106,21 @@ func (w *WatIntegration) Jeopardy(m *irc.Message, msgargs []string) {
if player.Nick == "" {
fmt.Printf("Player %s does not exist in Watbot, skipping cashout.\n", name)
continue
} else {
w.bot.reply(m, fmt.Sprintf("smartass %s, gave u %d :)", player.Nick, coins))
player.Coins += coins
w.db.Update(player)
}
// fill previous format placeholders
msg = fmt.Sprintf(msg, player.Nick, coins)
if many {
// append additional ones for filling in the next loop iteration
msg = msg + ", %s %d"
}
player.Coins += coins
w.db.Update(player)
}
if many {
// remove format placeholders from last loop iteration
msg = strings.Replace(msg, ", %s %d", ".", 1)
}
if cashoutcount > 0 {
w.bot.reply(m, msg)
}
}