Compare commits

..

No commits in common. "e1eac9de0f0ee02f2732301f6a0a1c656683c9b5" and "a62de999ea821e4d3e4a9b656bf66577f3cffc5e" have entirely different histories.

3 changed files with 76 additions and 94 deletions

View File

@ -5,8 +5,9 @@ package cardsim
type Card[C StatsCollection] interface { type Card[C StatsCollection] interface {
// Title is the short name of the card displayed in the hand // Title is the short name of the card displayed in the hand
// and at the top of the card output. It receives the current // and at the top of the card output. It receives the current
// player as an argument. // player as an argument. If it returns an error that is not
Title(p *Player[C]) Message // a warning, the game crashes.
Title(p *Player[C]) (Message, error)
// Urgent reports whether the card is considered urgent. If // Urgent reports whether the card is considered urgent. If
// the player has any urgent cards in hand, they cannot choose to act // the player has any urgent cards in hand, they cannot choose to act
@ -67,8 +68,8 @@ type BasicCard[C StatsCollection] struct {
} }
// Title implements Card. // Title implements Card.
func (b *BasicCard[C]) Title(_ *Player[C]) Message { func (b *BasicCard[C]) Title(_ *Player[C]) (Message, error) {
return b.CardTitle return b.CardTitle, nil
} }
// Urgent implements Card. // Urgent implements Card.

View File

@ -11,7 +11,7 @@ import (
type InfoPanel[C StatsCollection] interface { type InfoPanel[C StatsCollection] interface {
// Title returns the title of this InfoPanel, which is also used as the // Title returns the title of this InfoPanel, which is also used as the
// label presented to the player to access this panel. // label presented to the player to access this panel.
Title(p *Player[C]) Message Title(p *Player[C]) (Message, error)
// Info returns the displayable contents of this InfoPanel. A nil Message // Info returns the displayable contents of this InfoPanel. A nil Message
// in the output is interpreted as a paragraph break. // in the output is interpreted as a paragraph break.
@ -44,8 +44,8 @@ func VisibleOrDebug[C StatsCollection](p *Player[C], s Stat) bool {
} }
// Title implements `InfoPanel[C]` by returning b's `Name`. // Title implements `InfoPanel[C]` by returning b's `Name`.
func (b *BasicStatsPanel[C]) Title(p *Player[C]) Message { func (b *BasicStatsPanel[C]) Title(p *Player[C]) (Message, error) {
return b.Name return b.Name, nil
} }
// Info implements `InfoPanel[C]` by dumpiing p.Stats, showing those items for // Info implements `InfoPanel[C]` by dumpiing p.Stats, showing those items for

View File

@ -27,8 +27,7 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
if p.DebugLevel < 1 && IsSeriousError(err) { if p.DebugLevel < 1 && IsSeriousError(err) {
return err return err
} }
display(msg) displayAndWait(msg)
wait()
} }
review(p) review(p)
@ -44,11 +43,12 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
return nil return nil
} }
func display(m Message) { func displayAndWait(m Message) {
if m == nil { if m == nil {
return return
} }
fmt.Println(m.String()) fmt.Println(m.String())
wait()
} }
func wait() { func wait() {
@ -58,18 +58,13 @@ func wait() {
} }
func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int, choiceIdx int) { func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int, choiceIdx int) {
for {
cls() cls()
needsDivider := displayMessageSection(p) needsDivider := displayMessageSection(p)
if needsDivider { if needsDivider {
divider() divider()
} }
displayOnePanel(p, p.Prompt) displayOnePanel(p, p.Prompt)
divider()
actionsOffset := displayStatsMenu(p) actionsOffset := displayStatsMenu(p)
if actionsOffset > 0 {
divider()
}
handOffset := displayPermanentActionsMenu(p, actionsOffset) handOffset := displayPermanentActionsMenu(p, actionsOffset)
max := displayHandMenu(p, handOffset) max := displayHandMenu(p, handOffset)
@ -106,8 +101,7 @@ func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int,
i -= 1 i -= 1
if i < actionsOffset { if i < actionsOffset {
cls() cls()
displayOnePanel(p, p.InfoPanels[i]) DisplayOnePanelAndWait(p, i)
wait()
} else if i < handOffset { } else if i < handOffset {
cls() cls()
i -= actionsOffset i -= actionsOffset
@ -124,7 +118,7 @@ func pickNextAction[C StatsCollection](p *Player[C]) (isCard bool, cardIdx int,
} }
} }
} }
} return pickNextAction(p)
} }
func cls() { func cls() {
@ -151,36 +145,23 @@ func confirmQuit() {
} }
func displayOnePanel[C StatsCollection](p *Player[C], panel InfoPanel[C]) error { func displayOnePanel[C StatsCollection](p *Player[C], panel InfoPanel[C]) error {
ts := panel.Title(p).String() var errs ErrorCollector
t, err := panel.Title(p)
if IsSeriousError(err) {
return err
}
errs.Add(err)
ts := t.String()
if len(ts) > 0 { if len(ts) > 0 {
fmt.Println(ts) fmt.Println(ts)
fmt.Println(strings.Repeat("-", len(ts))) fmt.Println(strings.Repeat("-", len(ts)))
fmt.Println() fmt.Println()
} }
m, err := panel.Info(p) m, err := panel.Info(p)
errs.Add(err)
if IsSeriousError(err) { if IsSeriousError(err) {
return err return errs.Emit()
} }
display(MultiMessage(m)) displayAndWait(MultiMessage(m))
return err return errs.Emit()
}
func displayMessageSection[C StatsCollection](p *Player[C]) bool {
if len(p.TemporaryMessages) == 0 {
return false
}
display(MultiMessage(p.TemporaryMessages))
return true
}
func displayStatsMenu[C StatsCollection](p *Player[C]) int {
if len(p.InfoPanels) == 0 {
return 0
}
fmt.Println("Info Panels")
fmt.Println("-----------")
for i, s := range p.InfoPanels {
fmt.Printf("[%2d]: %s", i+1, s.Title(p).String())
}
return len(p.InfoPanels)
} }