Commit afc1958e authored by Joe Libipl's avatar Joe Libipl
Browse files

Refactor

parent 097ae209
Pipeline #9503137 passed with stage
in 1 minute and 34 seconds
......@@ -4,38 +4,77 @@ require 'sinatra/base'
# Sinatra-derived class for easy testing
class SMSBeholder < Sinatra::Base
player_data = {}
game_states = JSON.parse(File.open('script.json', 'r').read)
$game_states = JSON.parse(File.open('script.json', 'r').read)
get '/' do
source = params['From']
body = params['Body']
def process_input(params, player)
puts "Processing input for player #{player} params #{params}"
player_data[source] ||= {}
player_data[source]['state'] ||= 'SPLASH'
body = params['Body']
state = player_data[source]['state']
state = params['SkipToState'] if params.key?('SkipToState')
puts state
## State is the state that was last shown
state = player['state']
# Handle input
if player_data[source].key?('last_state')
last_state = player_data[source]['last_state']
if game_states[last_state].key?('textEntry')
text_entry_key = game_states[last_state]['textEntry']['key']
player_data[source][text_entry_key] = body
end
if $game_states[state].key?('textEntry')
text_entry_key = $game_states[state]['textEntry']['key']
puts "Player #{player} State #{state} adding text Key #{text_entry_key} value #{body}"
player[text_entry_key] = String.new(body)
end
## State transition
if params.key?('SkipToState')
state = params['SkipToState']
elsif $game_states[state].key?('selection')
# handle using the input to choose selection
elsif $game_states[state].key?('next')
state = $game_states[state]['next']
else
puts "Not sure where to go from state: #{$game_states[state]}"
end
next_state = game_states[state]['next']
response = String.new(game_states[state]['story'])
player_data[source]['last_state'] = state
player_data[source]['state'] = next_state
puts player_data
## Now state is the state to be shown
player['state'] = state
player
end
def process_output(player)
state = player['state']
response = String.new($game_states[state]['story'])
# Present selections
if $game_states[state].key?('selection')
$game_states[state]['selection'].keys.each_with_index do |selection, index|
response += "\n#{index + 1}) #{selection}"
end
puts response
end
# Fill in values in response
player_data[source].keys.each do |key|
response.gsub!(":#{key}:", player_data[source][key])
player.keys.each do |key|
puts "Filling in key #{key}"
response.gsub!(":#{key}:", player[key])
end
response
end
get '/' do
source = params['From']
if player_data.key?(source)
puts "Player #{source} has data #{player_data[source]}"
player_data[source] = process_input(params, player_data[source])
else
puts "Player #{source} is a new player!"
player_data[source] = {}
player_data[source]['state'] = params['SkipToState'] || 'SPLASH'
end
response = process_output(player_data[source])
response
end
......
......@@ -55,4 +55,10 @@ describe SMSBeholder do
expect(last_response.body).to include('Your brooding has distracted you')
end
it 'presents selections to player' do
get '/', From: 'Dude8', SkipToState: 'D1S108'
expect(last_response.body).to include("what is the solution?\n1) Six\n2) Sixteen\n3) I wasn't paying attention")
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment