aboutsummaryrefslogtreecommitdiff
path: root/go/game/stack.go
blob: bb93f40c6a48c37a9136eea1529abcb4cf2c5614 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package game

import (
	"log"
)

type Stack struct {
	gameState *LocalState
	Actions   []Action
}

func NewStack(s *LocalState) *Stack {
	return &Stack{s, []Action{}}
}

func (s *Stack) IsEmpty() bool {
	return len(s.Actions) == 0
}

func (s *Stack) push(a Action) {
	s.Actions = append(s.Actions, a)
}

func (s *Stack) remove(a Action) {
	for i, o := range s.Actions {
		if o != a {
			continue
		}
		s.Actions = append(s.Actions[:i], s.Actions[i+1:]...)
		return
	}
}

func (s *Stack) pop() {
	l := len(s.Actions)
	if l == 0 {
		log.Panicf("Can not pop from empty stack")
	}

	a := s.Actions[l-1]
	s.Actions = s.Actions[:l-1]

	err := s.gameState.ValidateAction(a)
	if err == nil {
		s.gameState.ResolveAction(a)
	}

	resolvedActionEvent := newResolvedActionEvent(a, err)
	s.gameState.fireEvent(resolvedActionEvent)
	s.gameState.broadcastNotification(newResolvedActionNotification(a, err))

	// Some action may be reused like full actions of permanents.
	// Reset their target selection to allow possible reuse.
	if t := a.Targets(); err == nil && t != nil {
		t.ClearSelections()
	}
}

func (s *Stack) resolve() []*Player {
	for !s.IsEmpty() {
		for {
			passing, w := s.gameState.allPassing(false)
			if len(w) > 0 {
				return w
			}
			if passing {
				break
			}
		}
		s.pop()
		w := s.gameState.stateBasedActions()
		if len(w) > 0 {
			return w
		}
	}
	return nil
}