From 9b71fe182e8e42ea2b7d3ba42a0f2767e5e5a246 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 3 Dec 2023 15:24:37 +0100 Subject: 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. --- go/game/state.go | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'go/game/state.go') 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 { -- cgit v1.2.3