Restructure loop, basic display functions

This commit is contained in:
Kistaro Windrider 2023-04-02 00:26:34 -07:00
parent a62de999ea
commit aecd8683b2
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -27,7 +27,8 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
if p.DebugLevel < 1 && IsSeriousError(err) { if p.DebugLevel < 1 && IsSeriousError(err) {
return err return err
} }
displayAndWait(msg) display(msg)
wait()
} }
review(p) review(p)
@ -43,12 +44,11 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
return nil return nil
} }
func displayAndWait(m Message) { func display(m Message) {
if m == nil { if m == nil {
return return
} }
fmt.Println(m.String()) fmt.Println(m.String())
wait()
} }
func wait() { func wait() {
@ -58,67 +58,69 @@ 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) {
cls() for {
needsDivider := displayMessageSection(p)
if needsDivider {
divider()
}
displayOnePanel(p, p.Prompt)
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() cls()
displayMessageSection(p) needsDivider := displayMessageSection(p)
wait() if needsDivider {
case "s": divider()
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 { displayOnePanel(p, p.Prompt)
fmt.Println("That's not a valid action.") 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() wait()
return pickNextAction(p) case "s":
} statsMode(p)
i -= 1 case "a":
if i < actionsOffset { actionsMode(p)
cls() case "q":
DisplayOnePanelAndWait(p, i) confirmQuit()
} else if i < handOffset { default:
cls() i, err := strconv.Atoi(input)
i -= actionsOffset if err != nil {
option, ok := promptPermanentAction(p, i) fmt.Println("Sorry, I don't understand.")
if ok { wait()
return false, i, option return pickNextAction(p)
} }
} else { if i > max {
cls() fmt.Println("That's not a valid action.")
i -= handOffset wait()
option, ok := promptCard(p, i) return pickNextAction(p)
if ok { }
return true, i, option i -= 1
if i < actionsOffset {
cls()
displayOnePanel(p, p.InfoPanels[i])
wait()
} 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() { func cls() {
@ -162,6 +164,14 @@ func displayOnePanel[C StatsCollection](p *Player[C], panel InfoPanel[C]) error
if IsSeriousError(err) { if IsSeriousError(err) {
return errs.Emit() return errs.Emit()
} }
displayAndWait(MultiMessage(m)) display(MultiMessage(m))
return errs.Emit() return errs.Emit()
} }
func displayMessageSection[C StatsCollection](p *Player[C]) bool {
if len(p.TemporaryMessages) == 0 {
return false
}
display(MultiMessage(p.TemporaryMessages))
return true
}