From 5af762474cc69bb40a05158662d66d9e1a15d1fb Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 3 Apr 2023 19:57:09 -0700 Subject: [PATCH] OverrideDefaultMsg and support in VerbosePolicy For when you need a partially-functional message but don't want to repeatedly write the same Enact func. --- koboldsim/cardtypes.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/koboldsim/cardtypes.go b/koboldsim/cardtypes.go index a01de74..5d4c3c3 100644 --- a/koboldsim/cardtypes.go +++ b/koboldsim/cardtypes.go @@ -16,7 +16,7 @@ var ( // If these are returned in a context that does not know how to respond // to them, then they're just errors. ErrUnimplemented = errors.New("unimplemented policy element") - ErrKeepMessaage = errors.New("use the default behavior but this message") + ErrKeepMessage = errors.New("use the default behavior but this message") ) type Policy interface { @@ -270,7 +270,9 @@ func (v *VerbosePolicy) OptionText(p *Player) (cardsim.Message, error) { } else { msg, err = v.Variants[v.lastIdx].OptionText(p) } - if errors.Is(err, ErrUnimplemented) { + if errors.Is(err, ErrKeepMessage) { + _, err = v.Default.OptionText(p) + } else if errors.Is(err, ErrUnimplemented) { msg, err = v.Default.OptionText(p) } return msg, err @@ -284,7 +286,9 @@ func (v *VerbosePolicy) Enact(p *Player) (cardsim.Message, error) { } else { msg, err = v.Variants[v.lastIdx].Enact(p) } - if errors.Is(err, ErrUnimplemented) { + if errors.Is(err, ErrKeepMessage) { + _, err = v.Default.Enact(p) + } else if errors.Is(err, ErrUnimplemented) { msg, err = v.Default.Enact(p) } return msg, err @@ -399,3 +403,14 @@ func ShuffleIntoBottomHalf(c Card, p *Player, _ CardOption) error { p.Deck.InsertRandomBottom(0.5, c) return nil } + +// OverrideDefaultMsg returns a closure that returns the provided message and +// ErrKeepMessage. This can be used in a FuncPolicy to tell a VerbosePolicy +// to use its Default but keep the message written here, to avoid writing +// repetitive Enact funcs (and, for that matter, OptionText funcs, even though +// the underlying Default call should be unnecessary). +func OverrideMsg(m cardsim.Message) func(*Player) (cardsim.Message, error) { + return func(p *Player) (cardsim.Message, error) { + return m, ErrKeepMessage + } +}