it'accepts a player name'doget'/',From:'Dude4'get'/',From:'Dude4',Body:'NameOfDude4'get'/',From:'Dude4'get'/',From:'Dude4'expect(last_response.body).toinclude("You're NameOfDude4, a student at Beholder High.")endit'uses the right player name'doget'/',From:'Dude5'get'/',From:'Dude5',Body:'NumberFive'get'/',From:'Dude6'get'/',From:'Dude6'get'/',From:'Dude5'get'/',From:'Dude5'expect(last_response.body).toinclude("You're NumberFive, a student at Beholder High.")end
classSMSBeholder<Sinatra::Baseplayer_data={}game_states=JSON.parse(File.open('script.json','r').read)get'/'dosource=params['From']body=params['Body']player_data[source]||={}player_data[source]['state']||='SPLASH'state=player_data[source]['state']putsstate# Handle inputifplayer_data[source].key?('last_state')last_state=player_data[source]['last_state']ifgame_states[last_state].key?('textEntry')text_entry_key=game_states[last_state]['textEntry']['key']player_data[source][text_entry_key]=bodyendendnext_state=game_states[state]['next']response=game_states[state]['story']player_data[source]['last_state']=stateplayer_data[source]['state']=next_stateputsplayer_data# Fill in values in responseforkeyinplayer_data[source].keysresponse.sub!(":#{key}:",player_data[source][key])endresponseend
So now we have a player_data hash, keyed by message source, to keep player data. We keep the
state there, defaulted to SPLASH.
Also, if the last state had a textEntry key, we set that key on the player to the test they
entered.
Finally, when sending the text to the player, we can subsitute anything if the form of :some_key:
with the value of that key in the player data.
With the end result that we can now pass those tests. Yay!
it'accepts a player name'doget'/',From:'Dude4'get'/',From:'Dude4',Body:'NameOfDude4'get'/',From:'Dude4'get'/',From:'Dude4'
and will only get worse as we progress. A purist would dictate that this should be addressed in the
test code (and I probably will at some point), but let’s use this opportunity to implement a cheat
code in the game itself (to be used purely for testing, of course).
it'lets you skip to a different state for testing'doget'/',From:'Dude7',SkipToState:'D1S107'expect(last_response.body).toinclude("Your brooding has distracted you")end