aboutsummaryrefslogtreecommitdiff
path: root/go/game/state.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/game/state.go')
-rw-r--r--go/game/state.go45
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 {