diff --git a/cardsim/player.go b/cardsim/player.go index 539a780..a5a15e9 100644 --- a/cardsim/player.go +++ b/cardsim/player.go @@ -423,3 +423,31 @@ func (p *Player[C]) EnactPermanentAction(actionIdx, choiceIdx int) (Message, err } 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) +} diff --git a/cardsim/terminalui.go b/cardsim/terminalui.go new file mode 100644 index 0000000..022a53f --- /dev/null +++ b/cardsim/terminalui.go @@ -0,0 +1,37 @@ +package cardsim + +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 +}