Extract randomization logic for assistants to a separate function.
This commit is contained in:
parent
087671d848
commit
69d75cceb3
1 changed files with 47 additions and 72 deletions
119
stargaterando.rb
119
stargaterando.rb
|
@ -7,7 +7,50 @@ Camping.goes :StargateRando
|
|||
# Data was moved out to stargaterando-data.rb to keep this file clean
|
||||
require './stargaterando-data'
|
||||
|
||||
# This holds our (currently there's only one) controllers.
|
||||
module StargateRando
|
||||
class Randomizer
|
||||
def self.generate_assistant_text
|
||||
series_selector = 1 + rand(17)
|
||||
case series_selector
|
||||
when 1..10
|
||||
chosen_series_id = 'SG-1'
|
||||
when 11..15
|
||||
chosen_series_id = 'SGA'
|
||||
else
|
||||
chosen_series_id = 'SGU'
|
||||
end
|
||||
|
||||
@series = Data::SERIES_NAMES[chosen_series_id]
|
||||
num_seasons = Data::SEASONS_PER_SERIES[chosen_series_id]
|
||||
chosen_season = 1 + rand(num_seasons)
|
||||
|
||||
# SG-1 has a couple of seasons of length 22 and one of length 21 so we need to handle that.
|
||||
case chosen_series_id
|
||||
when 'SG-1'
|
||||
case chosen_season
|
||||
when 1
|
||||
num_episodes = 21
|
||||
when 2..7
|
||||
num_episodes = 22
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
|
||||
chosen_episode = 1 + rand(num_episodes)
|
||||
|
||||
@episode_and_season = "Season #{chosen_season} Episode #{chosen_episode}"
|
||||
@episode_name = Data::EPISODES["#{chosen_series_id}|#{chosen_season}x#{chosen_episode}"]
|
||||
|
||||
return "You should watch: #{@series}: #{@episode_and_season}: #{@episode_name}"
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Controllers live here
|
||||
module StargateRando::Controllers
|
||||
class Index < R '/'
|
||||
def get
|
||||
|
@ -54,46 +97,12 @@ module StargateRando::Controllers
|
|||
end
|
||||
|
||||
def post
|
||||
series_selector = 1 + rand(17)
|
||||
case series_selector
|
||||
when 1..10
|
||||
chosen_series_id = 'SG-1'
|
||||
when 11..15
|
||||
chosen_series_id = 'SGA'
|
||||
else
|
||||
chosen_series_id = 'SGU'
|
||||
end
|
||||
|
||||
@series = Data::SERIES_NAMES[chosen_series_id]
|
||||
num_seasons = Data::SEASONS_PER_SERIES[chosen_series_id]
|
||||
chosen_season = 1 + rand(num_seasons)
|
||||
|
||||
# SG-1 has a couple of seasons of length 22 and one of length 21 so we need to handle that.
|
||||
case chosen_series_id
|
||||
when 'SG-1'
|
||||
case chosen_season
|
||||
when 1
|
||||
num_episodes = 21
|
||||
when 2..7
|
||||
num_episodes = 22
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
|
||||
chosen_episode = 1 + rand(num_episodes)
|
||||
|
||||
@headers['Content-Type'] = "application/json"
|
||||
|
||||
@episode_and_season = "Season #{chosen_season} Episode #{chosen_episode}"
|
||||
@episode_name = Data::EPISODES["#{chosen_series_id}|#{chosen_season}x#{chosen_episode}"]
|
||||
|
||||
|
||||
result_hash = {:version => "1.0", :response => {
|
||||
:outputSpeech => {
|
||||
:type => "PlainText",
|
||||
:text => "You should watch: #{@series}: #{@episode_and_season}: #{@episode_name}"
|
||||
:text => StargateRando::Randomizer.generate_assistant_text
|
||||
},
|
||||
:shouldEndSession => true
|
||||
}
|
||||
|
@ -109,43 +118,9 @@ module StargateRando::Controllers
|
|||
end
|
||||
|
||||
def post
|
||||
series_selector = 1 + rand(17)
|
||||
case series_selector
|
||||
when 1..10
|
||||
chosen_series_id = 'SG-1'
|
||||
when 11..15
|
||||
chosen_series_id = 'SGA'
|
||||
else
|
||||
chosen_series_id = 'SGU'
|
||||
end
|
||||
|
||||
@series = Data::SERIES_NAMES[chosen_series_id]
|
||||
num_seasons = Data::SEASONS_PER_SERIES[chosen_series_id]
|
||||
chosen_season = 1 + rand(num_seasons)
|
||||
|
||||
# SG-1 has a couple of seasons of length 22 and one of length 21 so we need to handle that.
|
||||
case chosen_series_id
|
||||
when 'SG-1'
|
||||
case chosen_season
|
||||
when 1
|
||||
num_episodes = 21
|
||||
when 2..7
|
||||
num_episodes = 22
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
else
|
||||
num_episodes = 20
|
||||
end
|
||||
|
||||
chosen_episode = 1 + rand(num_episodes)
|
||||
|
||||
@headers['Content-Type'] = "application/json"
|
||||
|
||||
@episode_and_season = "Season #{chosen_season} Episode #{chosen_episode}"
|
||||
@episode_name = Data::EPISODES["#{chosen_series_id}|#{chosen_season}x#{chosen_episode}"]
|
||||
|
||||
result_hash = { :fulfillmentText => "You should watch: #{@series}: #{@episode_and_season}: #{@episode_name}" }
|
||||
result_hash = { :fulfillmentText => StargateRando::Randomizer.generate_assistant_text }
|
||||
|
||||
JSON.generate(result_hash)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue