Files
miti99bot/internal/modules/wordle/lookup.go
T
tiennm99 6ee8118ad9 feat(modules): port wordle module + per-subject locking
Phase 5b of go-port-cloud-run plan. Port 14855-word dictionary
(89 KB, byte-identical to JS source) and four wordle commands
(/wordle, /wordle_new, /wordle_giveup, /wordle_stats).

KV wire-format parity: GameState/Stats JSON match JS shape;
*int64 LastResultAt for null-value compatibility. Two real bugs
caught and fixed: (1) defaultRNG data race in handlers — switched
to math/rand.Intn (mutex-protected package-level); (2) Get→mutate→Put
logical race in groups — added per-subject sync.Mutex map to serialize
access. TTL deferred (Firestore has no expirationTtl equiv — Phase 11 GC).
2026-05-09 09:35:23 +07:00

55 lines
1.6 KiB
Go

package wordle
import "strings"
// normalizeWord lowercases input and strips anything outside a-z. JS parity:
// `String(input).toLowerCase().replace(/[^a-z]/g, "")`.
func normalizeWord(input string) string {
lower := strings.ToLower(input)
out := make([]byte, 0, len(lower))
for i := 0; i < len(lower); i++ {
c := lower[i]
if c >= 'a' && c <= 'z' {
out = append(out, c)
}
}
return string(out)
}
// rejectReason classifies why validateGuess returned not-ok. Values match the
// JS source's discriminated-union strings so the user-facing reply mapping
// (handlers.rejectReason) stays parallel.
type rejectReason string
const (
reasonEmpty rejectReason = "empty"
reasonLength rejectReason = "length"
reasonUnknown rejectReason = "unknown"
)
// guessResult mirrors JS's `{ok: true, word} | {ok: false, reason, word}`.
// Word is always populated (the normalized form), even on failure, so callers
// can include it in error messages if desired.
type guessResult struct {
OK bool
Word string
Reason rejectReason
}
// validateGuess normalizes input then checks length + dictionary membership.
//
// Reasons in priority order: empty (post-normalize blank) > length > unknown.
func validateGuess(dict map[string]struct{}, input string) guessResult {
w := normalizeWord(input)
if w == "" {
return guessResult{OK: false, Word: w, Reason: reasonEmpty}
}
if len(w) != WordLength {
return guessResult{OK: false, Word: w, Reason: reasonLength}
}
if _, ok := dict[w]; !ok {
return guessResult{OK: false, Word: w, Reason: reasonUnknown}
}
return guessResult{OK: true, Word: w}
}