A few gaming features added, e.g. rolling dice, flipping coin, eightball.

This commit is contained in:
Storm Dragon
2026-09-03 13:25:32 -04:00
parent d0f78b1295
commit 77c687b6fe
12 changed files with 871 additions and 11 deletions
+50
View File
@@ -0,0 +1,50 @@
package rtpconn
import (
"crypto/rand"
"fmt"
"io"
"math/big"
"strings"
"git.stormux.org/storm/skald/hall"
)
var coinSides = []string{"heads", "tails"}
func chooseCoinSide(reader io.Reader) (string, error) {
index, err := rand.Int(reader, big.NewInt(int64(len(coinSides))))
if err != nil {
return "", err
}
return coinSides[index.Int64()], nil
}
func validateCoinRequest(value any) error {
if value == nil {
return nil
}
arguments, ok := value.(string)
if !ok || strings.TrimSpace(arguments) != "" {
return hall.UserError("/coin does not take any arguments")
}
return nil
}
func handleCoinFlip(c *webClient, g *hall.Hall, value any) error {
if !member("message", c.Permissions()) {
return c.error(hall.UserError("not authorised"))
}
if err := validateCoinRequest(value); err != nil {
return c.error(err)
}
side, err := chooseCoinSide(rand.Reader)
if err != nil {
return c.error(err)
}
announcement := fmt.Sprintf("%s flips a coin: %s.", c.Username(), side)
if err := broadcastServerChat(g, "Coin", "coin", announcement); err != nil {
return c.error(err)
}
return nil
}