From 8153a7c08325ca3522dd06ffd89653d50c337af0 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Mon, 27 Mar 2023 20:21:57 -0700 Subject: [PATCH] Card.Then Add a callback to the Card interface to be invoked after an option is selected. This will most frequently be used for cards that always want to shuffle themselves back into the deck regardless of the Option selected. --- cardsim/card.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cardsim/card.go b/cardsim/card.go index 9e8f1ed..df5cc07 100644 --- a/cardsim/card.go +++ b/cardsim/card.go @@ -16,6 +16,11 @@ type Card[C StatsCollection] interface { // Options returns the possible actions the player can take for this card. // There must be at least one option. Options(p *Player[C]) ([]CardOption[C], error) + + // Then is invoked after an option is selected and executed. The selected + // option is provided as an argument. This allows cards to do certain + // cleanup for every action -- for example, returning to the deck. + Then(p *Player[C], option CardOption[C]) error } // A CardOption represents a choice a player could make for some card. @@ -35,11 +40,12 @@ type CardOption[C StatsCollection] interface { Enact(p *Player[C]) (Message, error) } -// A BasicCard is a Card with fixed title, text, and options. +// A BasicCard is a Card with fixed title, text, options, and optional post-option callback. type BasicCard[C StatsCollection] struct { CardTitle Message CardText Message CardOptions []CardOption[C] + AfterOption func(p *Player[C], option CardOption[C]) error } func (b *BasicCard[C]) Title(p *Player[C]) (Message, error) { @@ -54,6 +60,13 @@ func (b *BasicCard[C]) Options(_ *Player[C]) ([]CardOption[C], error) { return b.CardOptions, nil } +func (b *BasicCard[C]) Then(p *Player[C], option CardOption[C]) error { + if b.AfterOption == nil { + return nil + } + return b.AfterOption(p, option) +} + // A BasicOption is a CardOption with fixed text, effects, and output. type BasicOption[C StatsCollection] struct { Text Message