"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'doget'/',From:'Dude',SkipToState:'M00'expect(last_response.body).toinclude("1) How do you like soccer?\n2) Do you like dancing?")end
it'hides conditional selection without condition'doget'/',From:'Dude',SkipToState:'M00'expect(last_response.body).not_toinclude("that killer bicycle kick")endit'hides conditional selection with failed condition'doget'/',From:'Dude',SkipToState:'M00',SetKey:'mox-kick',SetKeyValue:'unknown'expect(last_response.body).toinclude("1) How do you like soccer?\n2) Do you like dancing?")expect(last_response.body).not_toinclude("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'doget'/',From:'Dude',SkipToState:'M00',SetKey:'mox-kick',SetKeyValue:'known'expect(last_response.body).toinclude("1) How do you like soccer?\n2) Do you like dancing?")expect(last_response.body).toinclude("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_pairdo|key,value|puts"Checking player #{player} for key #{key} value #{value}"ifplayer[key]!=valueputs"nope"conditions_met=falseelseputs"yep"endendifconditions_met@states[state]['conditionalSelection']['ifConditionTrue'].keys.eachdo|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.
defself.selections(state,player)selections=[]if@states[state]&&@states[state].key?('selection')@states[state]['selection'].keys.eachdo|selection|selections<<selectionendendif@states[state]&&@states[state].key?('conditionalSelection')@states[state]['conditionalSelection']['constants'].keys.eachdo|selection|selections<<selectionendconditions_met=true@states[state]['conditionalSelection']['condition'].each_pairdo|key,value|puts"Checking player #{player} for key #{key} value #{value}"ifplayer[key]!=valueputs"nope"conditions_met=falseelseputs"yep"endendifconditions_met@states[state]['conditionalSelection']['ifConditionTrue'].keys.eachdo|selection|selections<<selectionendendendselectionsendend
Just a little mod to the way we consume the result, and we’re good.
it'allows conditional selection with passed condition'doget'/',From:'Dude',SkipToState:'M00',SetKey:'mox-kick',SetKeyValue:'known'get'/',From:'Dude',Body:'3'expect(last_response.body).toinclude("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:
ifparams.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)!=0selections=GameStates.selections(state,player)ifselections.length>=indexstate=selections[index-1].lastelse
it'disallows conditional selection without condition'do|example|get'/',From:@dude,SkipToState:'M00'get'/',From:@dude,Body:'3'expect(last_response.body).toinclude("1) How do you like soccer?")endit'disallows conditional selection with failed condition'doget'/',From:@dude,SkipToState:'M00',SetKey:'mox-kick',SetKeyValue:'unknown'get'/',From:@dude,Body:'3'expect(last_response.body).toinclude("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.