summaryrefslogtreecommitdiffstats
path: root/wat/integration.go
diff options
context:
space:
mode:
authorGeorg Pfuetzenreuter2024-09-29 14:35:25 +0200
committerGeorg Pfuetzenreuter2024-10-02 20:03:56 +0200
commitd06e724f067deb55b8009b62be8e300fe79d1961 (patch)
tree79e06a740a09f2df6dd1b04093509547c2835af7 /wat/integration.go
parent97e9d7d0c2312385bfd8dcbb85a1bc4bd7d263cc (diff)
downloadwatbot-d06e724f067deb55b8009b62be8e300fe79d1961.tar.gz
watbot-d06e724f067deb55b8009b62be8e300fe79d1961.tar.bz2
watbot-d06e724f067deb55b8009b62be8e300fe79d1961.zip
Refactor integrations
Move to a separate file for better code structure and to avoid huge branching inside Msg(). Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
Diffstat (limited to 'wat/integration.go')
-rw-r--r--wat/integration.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/wat/integration.go b/wat/integration.go
new file mode 100644
index 0000000..833b9a3
--- /dev/null
+++ b/wat/integration.go
@@ -0,0 +1,83 @@
+package wat
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+
+ "github.com/go-irc/irc"
+)
+
+type BotGameConfig map[string][]string
+
+type WatIntegrationConfig struct {
+ BotHosts []string
+ BotGames BotGameConfig
+}
+
+type WatIntegration struct {
+ bot *WatBot
+ db *WatDb
+ c *WatIntegrationConfig
+}
+
+func NewWatIntegration(bot *WatBot, db *WatDb, c *WatIntegrationConfig) *WatIntegration {
+ return &WatIntegration{bot, db, c}
+}
+
+func (w *WatIntegration) Bot(m *irc.Message) (bool, []string) {
+ isBot := w.bot.Allowed(m.Prefix.Host, w.c.BotHosts)
+ var games []string
+ if isBot {
+ for b, g := range w.c.BotGames {
+ if b == m.Prefix.Name {
+ games = g
+ break
+ }
+ }
+ }
+ return isBot, games
+}
+
+func (w *WatIntegration) HandleIntegration(m *irc.Message, msgargs []string) bool {
+ isBot, games := w.Bot(m)
+ if isBot {
+ // handles a message "Top finishers: (nick1: 1300) (nick2: 1200)" from an authorized Jeopardy game bot
+ if msgargs[0] == "Top" && msgargs[1] == "finishers:" && w.bot.Allowed("jeopardy", games) {
+ w.Jeopardy(m, msgargs)
+ return true
+ }
+ }
+ // not an authorized bot or no integration matched the given message
+ return false
+}
+
+func (w *WatIntegration) Jeopardy(m *irc.Message, msgargs []string) {
+ // hey, I avoided regex!
+ 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)
+ for _, pair := range finisherPrizes {
+ nameCoinPair := strings.Split(pair, ":")
+ coins, err := strconv.ParseUint(nameCoinPair[1], 10, 64)
+ if err != nil {
+ fmt.Printf("Invalid coins, cannot process pair for cashout: %s.\n", nameCoinPair)
+ continue
+ }
+ name := nameCoinPair[0]
+ // Jeopardy prizes are quite a lot of $$$, make it a bit more sane
+ coins = coins / 40
+ // 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?
+ // but it expects someone to have played with Watbot before to be eligible for Jeopardy cashout ..
+ player := w.db.User(name, "", false)
+ 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)
+ }
+ }
+}