aboutsummaryrefslogtreecommitdiff
path: root/go/game/stack.go
blob: 1ab4ccf86194e1e29bac0d2b8cb938b94650c770 (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
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) 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)
	}

	log.Println("Resolved", a, err)
	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
}