diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-12-03 15:24:37 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-01-27 16:43:54 +0100 |
| commit | 9b71fe182e8e42ea2b7d3ba42a0f2767e5e5a246 (patch) | |
| tree | 6c7fb471efa87cd4f5f2e5c00fa067c65d10ca7e /go/game/state.go | |
| parent | 771b85c660dfd03f786aaf86ff86a9416e361235 (diff) | |
| download | muhqs-game-9b71fe182e8e42ea2b7d3ba42a0f2767e5e5a246.tar.gz muhqs-game-9b71fe182e8e42ea2b7d3ba42a0f2767e5e5a246.zip | |
prompt the user to order its triggers
All TriggeredAction are passed in the wrapper action
DeclareTriggeredActionsAction to the controller of the triggers.
It is expected to receive an ordered list back from the user
again wrapped in a DeclareTriggeredActionsAction.
The triggered actions will be declared in from first to last.
Diffstat (limited to 'go/game/state.go')
| -rw-r--r-- | go/game/state.go | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/go/game/state.go b/go/game/state.go index 6bb3dbf0..36dc3e77 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -106,7 +106,7 @@ func (s *State) removeTriggers(triggers []Trigger) { } func (s *State) handleTriggers() { - var actions []Action + var actions []*TriggeredAction var triggersToRemove []Trigger for _, e := range s.events { @@ -126,9 +126,11 @@ func (s *State) handleTriggers() { s.removeTriggers(triggersToRemove) - orderedActions := s.orderTriggeredActions(actions) - for _, a := range orderedActions { - s.declareAction(a) + if len(actions) > 0 { + orderedActions := s.orderTriggeredActions(actions) + for _, a := range orderedActions { + s.declareAction(a) + } } } @@ -360,9 +362,38 @@ func (s *State) declareAction(a Action) { s.broadcastNotification(newDeclaredActionNotification(a, err)) } -func (s *State) orderTriggeredActions(actions []Action) []Action { - // TODO: implement ordering - return actions +func (s *State) orderTriggeredActions(triggeredActions []*TriggeredAction) []Action { + playerActions := make(map[*Player][]*TriggeredAction) + for _, action := range triggeredActions { + p := action.Controller() + if actions, found := playerActions[p]; found { + playerActions[p] = append(actions, action) + } else { + playerActions[p] = []*TriggeredAction{action} + } + } + + orderedActions := []Action{} + nPlayers := len(s.Players) + for i := 0; i < nPlayers; i++ { + p := s.Players[(i+s.ActivePlayer.Id-1)%nPlayers] + + actions := playerActions[p] + p.Ctrl.SendNotification(newDeclareTriggeredActionsPrompt(actions)) + + switch a := p.Ctrl.RecvAction().(type) { + case *DeclareTriggeredActionsAction: + pActions := make([]Action, 0, len(a.actions)) + for _, a := range a.actions { + pActions = append(pActions, a) + } + orderedActions = append(orderedActions, pActions...) + default: + log.Panicf("Unexpected action type %T after DeclareTriggerPrompt", a) + } + } + + return orderedActions } func (s *State) allPassing(skipFirst bool) bool { |
