"M00": {
"beholder": "mox",
"beholderPos": "center",
"beholderExp": "neutral",
"bg": "SoccerField",
"namePlate": ":name:",
"conditionalSelection": {
"condition": {
"mox-kick": "known"
},
"constants": {
"How do you like soccer?": "M08",
"Do you like dancing?": "M08"
},
"ifConditionTrue": {
"So I was wondering if you could show me that killer bicycle kick sometime...": "M01"
}
}
},
There are some selections which are always displayed (the constants), so we’ll test that first.
it 'shows constant selections from conditional selection set'do
get '/', From: 'Dude', SkipToState: 'M00'
expect(last_response.body).to include("1) How do you like soccer?\n2) Do you like dancing?")
end
it 'hides conditional selection without condition'do
get '/', From: 'Dude', SkipToState: 'M00'
expect(last_response.body).not_to include("that killer bicycle kick")
end
it 'hides conditional selection with failed condition'do
get '/', From: 'Dude', SkipToState: 'M00', SetKey: 'mox-kick', SetKeyValue: 'unknown'
expect(last_response.body).to include("1) How do you like soccer?\n2) Do you like dancing?")
expect(last_response.body).not_to include("that killer bicycle kick")
end
And get some nice false positives because the non-constant selections are being ignored. But that’ll
catch us later if we start showing selections that we shouldn’t.
it 'shows conditional selection with passed condition'do
get '/', From: 'Dude', SkipToState: 'M00', SetKey: 'mox-kick', SetKeyValue: 'known'
expect(last_response.body).to include("1) How do you like soccer?\n2) Do you like dancing?")
expect(last_response.body).to include("3) So I was wondering if you could show me that killer bicycle kick")
end
can be satisfied with this in the selections method:
conditions_met =true
@states[state]['conditionalSelection']['condition'].each_pair do| key, value |puts"Checking player #{player} for key #{key} value #{value}"if player[key]!= value
puts"nope"
conditions_met =falseelseputs"yep"endendif conditions_met
@states[state]['conditionalSelection']['ifConditionTrue'].keys.each do|selection|
response +="\n#{curr_selection}) #{selection}"
curr_selection +=1endend
Selections refactor
Having shown the conditional selection, can we choose it? Not yet. We could add a completely
separate check to see if a selection is valid according to the state of the player, but that would
be needlessly repetitive. We could save and parse through the selection string, but that seems
silly. Let’s just modify the selections method to return an array of selections.
it 'allows conditional selection with passed condition'do
get '/', From: 'Dude', SkipToState: 'M00', SetKey: 'mox-kick', SetKeyValue: 'known'
get '/', From: 'Dude', Body: '3'
expect(last_response.body).to include("Do you think you can keep up with me?")
end
With a little more refactoring of the selections method to include the actual destination state
(which I’ll leave out in the interest of brevity (too late)), we can satisy this one easily:
if params.key?('SkipToState')
state = params['SkipToState']elsifGameStates.states[state].key?('selection') ||GameStates.states[state].key?('conditionalSelection')
# handle using the input to choose selectionif (index = body.to_i) !=0
selections =GameStates.selections(state, player)
if selections.length >= index
state = selections[index -1].last
else
it 'disallows conditional selection without condition'do| example |
get '/', From: @dude, SkipToState: 'M00'
get '/', From: @dude, Body: '3'
expect(last_response.body).to include("1) How do you like soccer?")
end
it 'disallows conditional selection with failed condition'do
get '/', From: @dude, SkipToState: 'M00', SetKey: 'mox-kick', SetKeyValue: 'unknown'
get '/', From: @dude, Body: '3'
expect(last_response.body).to include("1) How do you like soccer?")
end
But what’s this @dude stuff? Oh yeah, I did a little more refactoring. I can explain. Tomorrow.