~nromdotcom/gemif

ref: 077db0c242b79e9291028436c3e4d8fe8c8799d8 gemif/pkg/gamemanager/gamestate.go -rw-r--r-- 1.0 KiB
077db0c2Norm MacLennan Update ASCIInema for new version 1 year, 5 months ago
                                                                                
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
package gamemanager

// GameState contains state information about an active game in progress.
type GameState struct {
	StoryID     string   `json:"si"`
	CurrentRoom string   `json:"cr"`
	Conditions  []string `json:"cn"`
}

// MeetCondition adds a condtition to the list of met conditions if not already met.
func (gs *GameState) MeetCondition(cond string) {
	if !isInSlice(gs.Conditions, cond) {
		gs.Conditions = append(gs.Conditions, cond)
	}
}

// ConditionMet checks to see if a condition has been met.
func (gs GameState) ConditionMet(cond string) bool {
	return isInSlice(gs.Conditions, cond)
}

// UseExit applies an exit's consequences to the GameState
// to produce a new GameState.
func (gs GameState) UseExit(exit Exit) GameState {
	newState := gs
	newState.CurrentRoom = exit.Destination

	if exit.SetCondition != "" {
		newState.MeetCondition(exit.SetCondition)
	}

	return newState
}

func isInSlice(slice []string, val string) bool {
	for _, item := range slice {
		if item == val {
			return true
		}
	}

	return false
}