A few gaming features added, e.g. rolling dice, flipping coin, eightball.
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user