From c01db1abf1a7e3f938bbc360d7f03aaaee5a8356 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Thu, 25 Aug 2022 10:00:43 -0500 Subject: [PATCH] Break random.go apart --- bot/handler/coin.go | 53 ++++++++++++ bot/handler/{random.go => dice.go} | 133 +---------------------------- bot/handler/roulette.go | 104 ++++++++++++++++++++++ 3 files changed, 159 insertions(+), 131 deletions(-) create mode 100644 bot/handler/coin.go rename bot/handler/{random.go => dice.go} (51%) create mode 100644 bot/handler/roulette.go diff --git a/bot/handler/coin.go b/bot/handler/coin.go new file mode 100644 index 0000000..439e375 --- /dev/null +++ b/bot/handler/coin.go @@ -0,0 +1,53 @@ +package handler + +import ( + "strings" + + "git.kill0.net/chill9/beepboop/bot" + "git.kill0.net/chill9/beepboop/lib" + "github.com/bwmarrin/discordgo" +) + +type ( + Coin bool + + CoinHandler struct { + config bot.Config + } +) + +func (c *Coin) Flip() bool { + *c = Coin(lib.Itob(lib.RandInt(0, 1))) + return bool(*c) +} + +func NewCoinHandler() *CoinHandler { + return new(CoinHandler) +} + +func (h *CoinHandler) SetConfig(config bot.Config) { + h.config = config +} + +func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { + var ( + c Coin + msg string + ) + + if m.Author.ID == s.State.User.ID { + return + } + + if !strings.HasPrefix(m.Content, "!coin") { + return + } + + if c.Flip() { + msg = "heads" + } else { + msg = "tails" + } + + s.ChannelMessageSend(m.ChannelID, msg) +} diff --git a/bot/handler/random.go b/bot/handler/dice.go similarity index 51% rename from bot/handler/random.go rename to bot/handler/dice.go index 5beaed0..2e37198 100644 --- a/bot/handler/random.go +++ b/bot/handler/dice.go @@ -15,11 +15,8 @@ import ( ) const ( - MaxDice = 100 - MaxSides = 100 - Bullets = 1 - GunFireMessage = "💀🔫" - GunClickMessage = "😌🔫" + MaxDice = 100 + MaxSides = 100 ) type ( @@ -29,33 +26,11 @@ type ( S string } - Coin bool - - Gun struct { - C [6]bool - N int - } - - CoinHandler struct { - config bot.Config - } RollHandler struct { config bot.Config } - - RouletteHandler struct { - config bot.Config - } ) -var ( - gun *Gun -) - -func init() { - gun = NewGun() -} - func NewRoll(n, d int) *Roll { r := new(Roll) r.N = n @@ -108,49 +83,6 @@ func (r *Roll) RollDice() { } } -func (c *Coin) Flip() bool { - *c = Coin(lib.Itob(lib.RandInt(0, 1))) - return bool(*c) -} - -func NewGun() *Gun { - return new(Gun) -} - -func (g *Gun) Load(n int) { - g.N = 0 - for i := 1; i <= n; { - x := lib.RandInt(0, len(g.C)-1) - if g.C[x] == false { - g.C[x] = true - i++ - } else { - continue - } - } -} - -func (g *Gun) Fire() bool { - if g.C[g.N] { - g.C[g.N] = false - g.N++ - return true - } - - g.N++ - return false -} - -func (g *Gun) IsEmpty() bool { - for _, v := range g.C { - if v == true { - return false - } - } - - return true -} - func NewRollHandler() *RollHandler { return new(RollHandler) } @@ -196,64 +128,3 @@ func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSend(m.ChannelID, msg) } - -func NewRouletteHandler() *RouletteHandler { - return new(RouletteHandler) -} - -func (h *RouletteHandler) SetConfig(config bot.Config) { - h.config = config -} - -func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { - if m.Author.ID == s.State.User.ID { - return - } - - if !strings.HasPrefix(m.Content, "!roulette") { - return - } - - if gun.IsEmpty() { - gun.Load(Bullets) - log.Debugf("reloading gun: %+v\n", gun) - } - - log.Debugf("firing gun: %+v\n", gun) - if gun.Fire() { - s.ChannelMessageSend(m.ChannelID, GunFireMessage) - } else { - s.ChannelMessageSend(m.ChannelID, GunClickMessage) - } -} - -func NewCoinHandler() *CoinHandler { - return new(CoinHandler) -} - -func (h *CoinHandler) SetConfig(config bot.Config) { - h.config = config -} - -func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { - var ( - c Coin - msg string - ) - - if m.Author.ID == s.State.User.ID { - return - } - - if !strings.HasPrefix(m.Content, "!coin") { - return - } - - if c.Flip() { - msg = "heads" - } else { - msg = "tails" - } - - s.ChannelMessageSend(m.ChannelID, msg) -} diff --git a/bot/handler/roulette.go b/bot/handler/roulette.go new file mode 100644 index 0000000..4015462 --- /dev/null +++ b/bot/handler/roulette.go @@ -0,0 +1,104 @@ +package handler + +import ( + "strings" + + "git.kill0.net/chill9/beepboop/bot" + "git.kill0.net/chill9/beepboop/lib" + "github.com/bwmarrin/discordgo" + + log "github.com/sirupsen/logrus" +) + +const ( + Bullets = 1 + GunFireMessage = "💀🔫" + GunClickMessage = "😌🔫" +) + +type ( + Gun struct { + C [6]bool + N int + } + + RouletteHandler struct { + config bot.Config + } +) + +var ( + gun *Gun +) + +func init() { + gun = NewGun() +} + +func NewGun() *Gun { + return new(Gun) +} + +func (g *Gun) Load(n int) { + g.N = 0 + for i := 1; i <= n; { + x := lib.RandInt(0, len(g.C)-1) + if g.C[x] == false { + g.C[x] = true + i++ + } else { + continue + } + } +} + +func (g *Gun) Fire() bool { + if g.C[g.N] { + g.C[g.N] = false + g.N++ + return true + } + + g.N++ + return false +} + +func (g *Gun) IsEmpty() bool { + for _, v := range g.C { + if v == true { + return false + } + } + + return true +} + +func NewRouletteHandler() *RouletteHandler { + return new(RouletteHandler) +} + +func (h *RouletteHandler) SetConfig(config bot.Config) { + h.config = config +} + +func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == s.State.User.ID { + return + } + + if !strings.HasPrefix(m.Content, "!roulette") { + return + } + + if gun.IsEmpty() { + gun.Load(Bullets) + log.Debugf("reloading gun: %+v\n", gun) + } + + log.Debugf("firing gun: %+v\n", gun) + if gun.Fire() { + s.ChannelMessageSend(m.ChannelID, GunFireMessage) + } else { + s.ChannelMessageSend(m.ChannelID, GunClickMessage) + } +}