Compare commits

..

4 Commits

2 changed files with 173 additions and 0 deletions

View File

@ -423,3 +423,31 @@ func (p *Player[C]) EnactPermanentAction(actionIdx, choiceIdx int) (Message, err
} }
return p.EnactPermanentActionUnchecked(actionIdx, choiceIdx) return p.EnactPermanentActionUnchecked(actionIdx, choiceIdx)
} }
// ReportError adds an error to the temporary messages, depending on
// its severity and debug settings:
//
// - If the error is nil, this never does anything.
// - If the error is serious, this emits the error if the debug level is
// -1 or greater.
// - If the error is only a warning, this emits the error if the debug
// level is 0 or greater.
func (p *Player[C]) ReportError(e error) {
if e == nil || p.DebugLevel < -1 {
return
}
if p.DebugLevel < 0 && !IsSeriousError(e) {
return
}
p.ChapterBreak()
severity := "[Warning]"
if IsSeriousError(e) {
severity = "[ERROR]"
}
p.TemporaryMessages = append(p.TemporaryMessages, Msgf("%s: %v", severity, e))
}
// CanAct returns whether the player has actions theoretically available.
func (p *Player[C]) CanAct() bool {
return p.ActionsRemaining > 0 && (len(p.Hand) > 0 || len(p.PermanentActions) > 0)
}

145
cardsim/terminalui.go Normal file
View File

@ -0,0 +1,145 @@
package cardsim
import (
"fmt"
"os"
"strconv"
"strings"
)
func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
for {
err := p.StartNextTurn()
if p.DebugLevel < 1 && IsSeriousError(err) {
return err
}
p.ReportError(err)
for p.CanAct() {
isCard, cardIdx, choiceIdx := pickNextAction(p)
var msg Message
if isCard {
msg, err = p.EnactCard(cardIdx, choiceIdx)
} else {
msg, err = p.EnactPermanentAction(cardIdx, choiceIdx)
}
p.ReportError(err)
if p.DebugLevel < 1 && IsSeriousError(err) {
return err
}
displayAndWait(msg)
}
review(p)
err = p.Simulate()
if p.DebugLevel < 1 && IsSeriousError(err) {
return err
}
if p.DebugLevel < 1 && p.State.Over() {
return nil
}
}
return nil
}
func displayAndWait(m Message) {
if m == nil {
return
}
fmt.Println(m.String())
wait()
}
func wait() {
fmt.Println()
fmt.Println("<press ENTER to continue>")
fmt.Scanln()
}
func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int, choiceIdx int) {
cls()
needsDivider := displayMessageSection(p)
if needsDivider {
divider()
}
displayPrompt(p)
actionsOffset := displayStatsMenu(p)
handOffset := displayPermanentActionsMenu(p, actionsOffset)
max := displayHandMenu(p, handOffset)
divider()
var input string
fmt.Printf("Show just (M)essages, (S)tats, (A)ctions, make a choice (1-%d), or (Q)uit? >", max+1)
fmt.Scanln(&input)
input = strings.TrimSpace(input)
input = strings.ToLower(input)
switch input {
// Special cases
case "m":
cls()
displayMessageSection(p)
wait()
case "s":
statsMode(p)
case "a":
actionsMode(p)
case "q":
confirmQuit()
default:
i, err := strconv.Atoi(input)
if err != nil {
fmt.Println("Sorry, I don't understand.")
wait()
return pickNextAction(p)
}
if i > max {
fmt.Println("That's not a valid action.")
wait()
return pickNextAction(p)
}
i -= 1
if i < actionsOffset {
cls()
DisplayOnePanelAndWait(p, i)
} else if i < handOffset {
cls()
i -= actionsOffset
option, ok := promptPermanentAction(p, i)
if ok {
return false, i, option
}
} else {
cls()
i -= handOffset
option, ok := promptCard(p, i)
if ok {
return true, i, option
}
}
}
return pickNextAction(p)
}
func cls() {
fmt.Println("\033[H\033[2J")
}
func divider() {
fmt.Println()
fmt.Println(SectionBreak.String())
fmt.Println()
}
func confirmQuit() {
divider()
fmt.Println("Are you sure you want to quit? (Y/N) ")
var s string
fmt.Scanln(&s)
s = strings.TrimSpace(s)
s = strings.ToLower(s)
if strings.HasPrefix(s, "y") {
fmt.Println("Bye!")
os.Exit(0)
}
}