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
}