2022-09-25 02:43:10 +00:00
|
|
|
package command
|
2022-09-01 14:24:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-07 02:33:59 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2022-09-01 14:24:55 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Card string
|
|
|
|
|
|
|
|
Deck [52]Card
|
|
|
|
)
|
|
|
|
|
|
|
|
var deck Deck = Deck{
|
|
|
|
"2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♣", "A♣",
|
|
|
|
"2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♦", "A♦",
|
|
|
|
"2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", "A♥",
|
|
|
|
"2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", "A♠",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Deck) Deal(n int) ([]Card, error) {
|
|
|
|
var (
|
|
|
|
hand []Card
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if n < 1 {
|
|
|
|
err = errors.New("number cannot be less than 1")
|
|
|
|
return hand, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > len(d) {
|
|
|
|
err = errors.New("number is greater than cards in the deck")
|
|
|
|
return hand, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hand = deck[0:n]
|
|
|
|
|
|
|
|
return hand, err
|
|
|
|
}
|
|
|
|
|
2022-09-25 02:43:10 +00:00
|
|
|
func (h *Handlers) Deal(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
|
|
rand.Shuffle(len(deck), func(i, j int) {
|
|
|
|
deck[i], deck[j] = deck[j], deck[i]
|
|
|
|
})
|
2022-09-01 14:24:55 +00:00
|
|
|
|
2022-09-25 02:43:10 +00:00
|
|
|
log.Debugf("%+v", deck)
|
2022-09-01 14:24:55 +00:00
|
|
|
|
2022-09-25 02:43:10 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
s.ChannelMessageSend(m.ChannelID, "help: `!deal <n>`")
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-07 02:33:59 +00:00
|
|
|
|
2022-09-25 02:43:10 +00:00
|
|
|
n, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to convert string to int: %s", err)
|
|
|
|
}
|
2022-09-01 14:24:55 +00:00
|
|
|
|
2022-09-25 02:43:10 +00:00
|
|
|
hand, err := deck.Deal(n)
|
|
|
|
if err != nil {
|
|
|
|
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error: %s\n", err))
|
2022-09-06 05:00:47 +00:00
|
|
|
return nil
|
2022-09-01 14:24:55 +00:00
|
|
|
}
|
2022-09-25 02:43:10 +00:00
|
|
|
|
|
|
|
s.ChannelMessageSend(m.ChannelID, JoinCards(hand, " "))
|
|
|
|
return nil
|
2022-09-01 14:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func JoinCards(h []Card, sep string) string {
|
2022-09-07 05:54:58 +00:00
|
|
|
b := make([]string, len(h))
|
2022-09-01 14:24:55 +00:00
|
|
|
|
|
|
|
for i, v := range h {
|
|
|
|
b[i] = string(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(b, sep)
|
|
|
|
}
|