Hangman, and it plays for chips — which the plan had down as a free game, on the grounds that trivia has no euro coupling in gogobee. But a free game in a casino reads as a demo, so it stakes like everything else. The idea that makes it a casino game rather than hangman with a wager stapled on: the gallows is the payout meter. A wrong guess draws a limb *and* takes a tenth off what a win is worth, because those are the same event and showing them as one is the entire reason to bet on this. Short phrases pay 2.6x (fewer letters, less to go on), long ones 1.6x — the floor is 1x, so a win never hands back less than the stake, and the rake still comes out of winnings only. State.Pays() is the number the felt quotes and the number settle() lands on. They were briefly two sums, and the table spent an afternoon advertising a pre-rake payout it didn't honour. Two things the storage layer had already decided for us, and one it hadn't: game_live_hands is keyed on the player, so "one game at a time" holds across games for free (a live hangman 409s a blackjack deal, stake intact). But table() unmarshalled every live row as a blackjack hand, which does not fail on a hangman row — it quietly yields an empty hand. It dispatches on the game now. commit() is the settle path both games share, and casinoRoutes() the one route list, since the dev rig wires its own mux and a second copy is a copy that stops including the newest game. Driven in a browser, win and loss: 200 at 2.34x paid 455 and the bar landed on it; six wrong took the stake and no more; a reload mid-phrase brought back the board, the limbs, the multiple, the spent keys and the chips on the spot. The browser found the two bugs a Go test can't — a lives counter under the house rack, and a word wrapping early because the rack's clearance was on the whole column instead of the one row beside it.
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package hangman
|
|
|
|
import (
|
|
"bufio"
|
|
_ "embed"
|
|
"errors"
|
|
"math/rand/v2"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// The bank. gogobee kept its phrases in a file it read at boot out of a path in
|
|
// an env var, which meant the game was one missing file away from not existing.
|
|
// Embedding it means the casino cannot start without its phrases, which is the
|
|
// correct relationship between a game and the thing it is about.
|
|
//
|
|
//go:embed phrases.txt
|
|
var phrasesTxt string
|
|
|
|
// ErrNoPhrases means the bank has nothing at this length. It can only happen if
|
|
// someone edits phrases.txt down past a tier, and it is a programming error
|
|
// rather than anything a player did — but it's an error, not a panic, because a
|
|
// casino that won't boot is worse than one game being shut.
|
|
var ErrNoPhrases = errors.New("hangman: no phrases in that tier")
|
|
|
|
var (
|
|
shelvesOnce sync.Once
|
|
shelves map[string][]string // tier slug -> the phrases that fit it
|
|
)
|
|
|
|
// load sorts the bank onto one shelf per tier, once. Comments and blank lines
|
|
// are dropped, and so is anything too short to be a game — the tiers' own Min
|
|
// is the floor.
|
|
func load() {
|
|
shelves = make(map[string][]string, len(Tiers))
|
|
sc := bufio.NewScanner(strings.NewReader(phrasesTxt))
|
|
for sc.Scan() {
|
|
line := strings.TrimSpace(sc.Text())
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
n := len([]rune(line))
|
|
for _, t := range Tiers {
|
|
if n >= t.Min && n <= t.Max {
|
|
shelves[t.Slug] = append(shelves[t.Slug], line)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Shelf is how many phrases a tier has. Exists so a test can assert the bank
|
|
// hasn't been edited out from under a tier.
|
|
func Shelf(slug string) int {
|
|
shelvesOnce.Do(load)
|
|
return len(shelves[slug])
|
|
}
|
|
|
|
// drawPhrase picks one phrase from a tier's shelf. The rng is threaded, never
|
|
// the package global, so a game replays exactly from the seed in its audit row.
|
|
func drawPhrase(t Tier, rng *rand.Rand) (string, error) {
|
|
shelvesOnce.Do(load)
|
|
shelf := shelves[t.Slug]
|
|
if len(shelf) == 0 {
|
|
return "", ErrNoPhrases
|
|
}
|
|
return shelf[rng.IntN(len(shelf))], nil
|
|
}
|