Break random.go apart

This commit is contained in:
Ryan Cavicchioni 2022-08-25 10:00:43 -05:00
parent ac6d30e085
commit c01db1abf1
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 159 additions and 131 deletions

53
bot/handler/coin.go Normal file
View File

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

View File

@ -17,9 +17,6 @@ import (
const (
MaxDice = 100
MaxSides = 100
Bullets = 1
GunFireMessage = "💀🔫"
GunClickMessage = "😌🔫"
)
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)
}

104
bot/handler/roulette.go Normal file
View File

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