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 index := isInSlice(gs.Conditions, cond); index == -1 {
gs.Conditions = append(gs.Conditions, cond)
}
}
// RemoveCondition removes a condtition from the list of met conditions if it exists.
func (gs *GameState) RemoveCondition(cond string) {
if index := isInSlice(gs.Conditions, cond); index > -1 {
gs.Conditions[index] = gs.Conditions[len(gs.Conditions)-1]
gs.Conditions = gs.Conditions[:len(gs.Conditions)-1]
}
}
// ConditionMet checks to see if a condition has been met.
func (gs GameState) ConditionMet(cond string) bool {
return isInSlice(gs.Conditions, cond) > -1
}
// 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)
}
if exit.UnsetCondition != "" {
newState.RemoveCondition(exit.UnsetCondition)
}
return newState
}
func isInSlice(slice []string, val string) int {
for index, item := range slice {
if item == val {
return index
}
}
return -1
}