86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
package rtpconn
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"io"
|
|
"math/big"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"git.stormux.org/storm/skald/hall"
|
|
)
|
|
|
|
const maxEightBallQuestionRunes = 500
|
|
|
|
var eightBallResponses = []string{
|
|
"It is certain.",
|
|
"It is decidedly so.",
|
|
"Without a doubt.",
|
|
"Yes, definitely.",
|
|
"You may rely on it.",
|
|
"As I see it, yes.",
|
|
"Most likely.",
|
|
"Outlook good.",
|
|
"Yes.",
|
|
"Signs point to yes.",
|
|
"Reply hazy, try again.",
|
|
"Ask again later.",
|
|
"Better not tell you now.",
|
|
"Cannot predict now.",
|
|
"Concentrate and ask again.",
|
|
"Don't count on it.",
|
|
"My reply is no.",
|
|
"My sources say no.",
|
|
"Outlook not so good.",
|
|
"Very doubtful.",
|
|
}
|
|
|
|
func normalizeEightBallQuestion(value any) (string, error) {
|
|
question, ok := value.(string)
|
|
if !ok {
|
|
return "", hall.UserError("/8ball requires a question")
|
|
}
|
|
question = strings.Join(strings.Fields(question), " ")
|
|
if question == "" {
|
|
return "", hall.UserError("/8ball requires a question")
|
|
}
|
|
if utf8.RuneCountInString(question) > maxEightBallQuestionRunes {
|
|
return "", hall.UserError(fmt.Sprintf(
|
|
"Eight Ball questions may not exceed %d characters",
|
|
maxEightBallQuestionRunes,
|
|
))
|
|
}
|
|
return question, nil
|
|
}
|
|
|
|
func chooseEightBallResponse(reader io.Reader) (string, error) {
|
|
index, err := rand.Int(reader, big.NewInt(int64(len(eightBallResponses))))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return eightBallResponses[index.Int64()], nil
|
|
}
|
|
|
|
func handleEightBall(c *webClient, g *hall.Hall, value any) error {
|
|
if !member("message", c.Permissions()) {
|
|
return c.error(hall.UserError("not authorised"))
|
|
}
|
|
question, err := normalizeEightBallQuestion(value)
|
|
if err != nil {
|
|
return c.error(err)
|
|
}
|
|
response, err := chooseEightBallResponse(rand.Reader)
|
|
if err != nil {
|
|
return c.error(err)
|
|
}
|
|
announcement := fmt.Sprintf(
|
|
"%s asks: %s\nThe Eight Ball says: %s",
|
|
c.Username(), question, response,
|
|
)
|
|
if err := broadcastServerChat(g, "Eight Ball", "eightball", announcement); err != nil {
|
|
return c.error(err)
|
|
}
|
|
return nil
|
|
}
|