Compare commits

..

No commits in common. "main" and "debug-actions" have entirely different histories.

3 changed files with 36 additions and 43 deletions

View File

@ -35,19 +35,6 @@ func Msgf(f string, args ...any) Message {
return stringMessage(fmt.Sprintf(f, args...)) return stringMessage(fmt.Sprintf(f, args...))
} }
// ErrorMessage returns a Message representing an Error.
// This is preferred over Msgf for errors, since future versions of the library
// may perform special message formatting for errors.
func ErrorMessage(e error) Message {
if e == nil {
return nil
}
if IsSeriousError(e) {
return MultiMessage{MsgStr("SERIOUS ERROR:"), Msgf("%v", e)}
}
return MultiMessage{MsgStr("Warning:"), Msgf("%v", e)}
}
// A SpecialMessage is a specific, uniquely identifiable message. // A SpecialMessage is a specific, uniquely identifiable message.
type SpecialMessage struct { type SpecialMessage struct {
msg Message msg Message

View File

@ -8,14 +8,14 @@ import (
) )
var ( var (
ErrUncooperativeCards = errors.New("a milion cards refused to join the hand")
ErrInvalidCard = errors.New("invalid card specified") ErrInvalidCard = errors.New("invalid card specified")
ErrInvalidChoice = errors.New("invalid choice specified") ErrInvalidChoice = errors.New("invalid choice specified")
ErrNotUrgent = errors.New("action not urgent when urgent card is available") ErrNotUrgent = errors.New("action not urgent when urgent card is available")
ErrNoActions = errors.New("no actions remaining") ErrNoActions = errors.New("no actions remaining")
ErrNotDebugging = errors.New("this is a debug-only feature and you're not in debug mode") ErrNotDebugging = errors.New("this is a debug-only feature and you're not in debug mode")
WarningStalemate = &Warning{errors.New("no actions can be taken")} WarningStalemate = errors.New("no actions can be taken")
WarningUncoperativeCards = &Warning{errors.New("a milion cards refused to join the hand")}
) )
// Player stores all gameplay state for one player at a specific point in time. // Player stores all gameplay state for one player at a specific point in time.
@ -272,9 +272,9 @@ func (p *Player[C]) StartNextTurn() error {
} }
// Draw draws a card into the hand, informing the card that it has been drawn. // Draw draws a card into the hand, informing the card that it has been drawn.
// If more than a million cards refuse to enter the hand, this gives up and // If more than a million cards refuse to enter the hand, this crashes with
// returns WarningUncooperativeCards. If the deck does not have enough cards, // ErrUncooperativeCards. If the deck does not have enough cards, this
// this returns WarningTooFewCards. // returns WarningTooFewCards.
func (p *Player[C]) Draw() error { func (p *Player[C]) Draw() error {
for attempts := 0; attempts < 1000000; attempts++ { for attempts := 0; attempts < 1000000; attempts++ {
if p.Deck.Len() == 0 { if p.Deck.Len() == 0 {
@ -286,13 +286,13 @@ func (p *Player[C]) Draw() error {
return nil return nil
} }
} }
return WarningUncoperativeCards return ErrUncooperativeCards
} }
// FillHand draws up to the hand limit, informing cards that they have been // FillHand draws up to the hand limit, informing cards that they have been
// drawn. If more than a million cards refuse to enter the hand, this gives up // drawn. If more than a million cards refuse to enter the hand, this crashes
// and returns WarningUncooperativeCards. If the deck does not have enough // with ErrUncooperativeCards. If the deck does not have enough cards, this
// cards, this returns WarningTooFewCards. // returns WarningTooFewCards.
func (p *Player[C]) FillHand() error { func (p *Player[C]) FillHand() error {
var lastErr error var lastErr error
for p.Deck.Len() > 0 && len(p.Hand) < p.HandLimit { for p.Deck.Len() > 0 && len(p.Hand) < p.HandLimit {
@ -386,14 +386,17 @@ func (p *Player[C]) EnactCardUnchecked(cardIdx, choiceIdx int) (Message, error)
ret, err := options[choiceIdx].Enact(p) ret, err := options[choiceIdx].Enact(p)
errs.Add(err) errs.Add(err)
if IsSeriousError(err) {
p.State = GameCrashed
return ret, errs.Emit()
}
err = card.Then(p, options[choiceIdx]) err = card.Then(p, options[choiceIdx])
errs.Add(err) errs.Add(err)
err = errs.Emit()
if IsSeriousError(err) { if IsSeriousError(err) {
p.State = GameCrashed p.State = GameCrashed
} }
return ret, err return ret, errs.Emit()
} }
// EnactCard executes a card choice, removes it from the hand, and decrements // EnactCard executes a card choice, removes it from the hand, and decrements
@ -467,13 +470,17 @@ func (p *Player[C]) enactActionUnchecked(actionSource []Card[C], actionIdx, choi
ret, err := chosen.Enact(p) ret, err := chosen.Enact(p)
errs.Add(err) errs.Add(err)
if IsSeriousError(err) {
p.State = GameCrashed
return ret, errs.Emit()
}
err = card.Then(p, chosen) err = card.Then(p, chosen)
errs.Add(err) errs.Add(err)
retErr := errs.Emit() if IsSeriousError(err) {
if IsSeriousError(retErr) {
p.State = GameCrashed p.State = GameCrashed
} }
return ret, retErr return ret, errs.Emit()
} }
// EnactPermanentAction executes a permanently-available card and decrements // EnactPermanentAction executes a permanently-available card and decrements
@ -505,11 +512,15 @@ func (p *Player[C]) ReportError(e error) {
if e == nil || p.DebugLevel < -1 { if e == nil || p.DebugLevel < -1 {
return return
} }
minLvl := NotDebugging if p.DebugLevel < 0 && !IsSeriousError(e) {
if IsSeriousError(e) { return
minLvl = HideWarnings
} }
p.Debug(minLvl, ErrorMessage(e)) 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. // CanAct returns whether the player has actions theoretically available.
@ -523,7 +534,6 @@ func (p *Player[C]) Debug(minLevel int, msg Message) {
if p.DebugLevel < minLevel || msg == nil { if p.DebugLevel < minLevel || msg == nil {
return return
} }
p.ChapterBreak()
p.TemporaryMessages = append(p.TemporaryMessages, msg) p.TemporaryMessages = append(p.TemporaryMessages, msg)
} }

View File

@ -46,10 +46,6 @@ func RunSimpleTerminalUI[C StatsCollection](p *Player[C]) error {
} }
continue continue
} }
if err != nil {
display(ErrorMessage(err))
display(MsgStr(""))
}
display(msg) display(msg)
wait() wait()
} }
@ -200,7 +196,7 @@ func lightDivider() {
func confirmQuit() { func confirmQuit() {
divider() divider()
fmt.Printf("Are you sure you want to quit? (Y/N) > ") fmt.Println("Are you sure you want to quit? (Y/N) > ")
s := getResponse() s := getResponse()
if s == "y" || s == "yes" { if s == "y" || s == "yes" {
fmt.Println("Bye!") fmt.Println("Bye!")