OverrideDefaultMsg and support in VerbosePolicy

For when you need a partially-functional message but don't want to repeatedly write the same Enact func.
This commit is contained in:
Kistaro Windrider 2023-04-03 19:57:09 -07:00
parent 2c1fc73ef5
commit 5af762474c
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -16,7 +16,7 @@ var (
// If these are returned in a context that does not know how to respond // If these are returned in a context that does not know how to respond
// to them, then they're just errors. // to them, then they're just errors.
ErrUnimplemented = errors.New("unimplemented policy element") 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 { type Policy interface {
@ -270,7 +270,9 @@ func (v *VerbosePolicy) OptionText(p *Player) (cardsim.Message, error) {
} else { } else {
msg, err = v.Variants[v.lastIdx].OptionText(p) 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) msg, err = v.Default.OptionText(p)
} }
return msg, err return msg, err
@ -284,7 +286,9 @@ func (v *VerbosePolicy) Enact(p *Player) (cardsim.Message, error) {
} else { } else {
msg, err = v.Variants[v.lastIdx].Enact(p) 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) msg, err = v.Default.Enact(p)
} }
return msg, err return msg, err
@ -399,3 +403,14 @@ func ShuffleIntoBottomHalf(c Card, p *Player, _ CardOption) error {
p.Deck.InsertRandomBottom(0.5, c) p.Deck.InsertRandomBottom(0.5, c)
return nil 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
}
}