func (m *StepMap) GetAssignee(s *game.SetupStep) *game.Player {
	for player, step := range m.stepMap {
		if s.Equal(step) {
			return player
		}
	}
	return nil
}
func (m *StepPlayerIdMap) GetAssignee(s *game.SetupStep) *game.Player {
	for playerId, step := range m.stepMap {
		if nil != step && s.Equal(step) {
			return m.playerMap[playerId]
		}
	}
	return nil
}
func (m *StepMap) IsAssigned(step *game.SetupStep) bool {
	assignees := m.GetAssignees(step.Rule)
	for _, assignee := range assignees {
		if step.CanBeOwnedBy(assignee) {
			return true
		}
	}
	return false
}
Ejemplo n.º 4
0
func TestStepHonorsDependencies(t *testing.T) {
	rule0 := game.NewSetupRule("0", "Once")
	rule0.Id = 0
	rule1 := game.NewSetupRule("1", "Once", rule0)
	rule1.Id = 1
	rule2 := game.NewSetupRule("2", "Once", rule1)
	rule2.Id = 2

	players := []*game.Player{
		&game.Player{1, "Alice"},
		&game.Player{2, "Bob"},
	}
	g := game.NewGame("war", []*game.SetupRule{rule0, rule1, rule2}, len(players), len(players))
	session, err := NewSession(g, players)
	if nil != err {
		t.Fatal("Error creating session")
	}

	states := make(map[*game.SetupRule][]stepState)
	var step *game.SetupStep

	states[rule0] = []stepState{undone, unassigned}
	states[rule1] = []stepState{undone, unassigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(1, t, session, states)

	step = session.Step(players[0])
	if step.Rule.Description != "0" {
		t.Fatal(fmt.Sprintf("Step 0 should be the next step, but is %s", step.Rule.Description))
	}
	states[rule0] = []stepState{undone, assigned}
	states[rule1] = []stepState{undone, unassigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(2, t, session, states)

	// No change
	step = session.Step(players[0])
	if step.Rule.Description != "0" {
		t.Fatal(fmt.Sprintf("Step 0 should be the next step, but is %s", step.Rule.Description))
	}
	states[rule0] = []stepState{undone, assigned}
	states[rule1] = []stepState{undone, unassigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(3, t, session, states)

	// No change. Dependency is not yet done.
	noStep := session.Step(players[1])
	if noStep != nil {
		t.Fatal("Step cannot yet be assigned")
	}
	states[rule0] = []stepState{undone, assigned}
	states[rule1] = []stepState{undone, unassigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(4, t, session, states)

	step.Finish()
	states[rule0] = []stepState{done, assigned}
	states[rule1] = []stepState{undone, unassigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(5, t, session, states)

	step = session.Step(players[0])
	if step.Rule.Description != "1" {
		t.Fatal(fmt.Sprintf("Step 1 should be the next step, but is %s", step.Rule.Description))
	}
	states[rule0] = []stepState{done, unassigned}
	states[rule1] = []stepState{undone, assigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(6, t, session, states)

	step.Finish()
	states[rule0] = []stepState{done, unassigned}
	states[rule1] = []stepState{done, assigned}
	states[rule2] = []stepState{undone, unassigned}
	verifyStates(7, t, session, states)

	step = session.Step(players[1])
	states[rule0] = []stepState{done, unassigned}
	states[rule1] = []stepState{done, assigned}
	states[rule2] = []stepState{undone, assigned}
	verifyStates(8, t, session, states)

	// No change
	session.Step(players[0])
	states[rule0] = []stepState{done, unassigned}
	states[rule1] = []stepState{done, assigned}
	states[rule2] = []stepState{undone, assigned}
	verifyStates(9, t, session, states)

	step.Finish()
	states[rule0] = []stepState{done, unassigned}
	states[rule1] = []stepState{done, assigned}
	states[rule2] = []stepState{done, assigned}
	verifyStates(10, t, session, states)
}