Skip to content

Instantly share code, notes, and snippets.

@entombedvirus
Forked from jconstable/gist:215597
Created October 22, 2009 00:18
Show Gist options
  • Select an option

  • Save entombedvirus/215599 to your computer and use it in GitHub Desktop.

Select an option

Save entombedvirus/215599 to your computer and use it in GitHub Desktop.
module UserMods
module Tutorials
TUTORIALS = {
# define the flows, and an array of symbols for the steps of each flow here
:intro_flow => [
:welcome,
:buy_pet,
:pet_chore
]
}
TUTORIAL_COMPLETE = :completed
MAX_TUTORIAL_STEPS = 31
def progress_for(tutorial)
flow = get_flow_for(tutorial)
# find the index of the first step that has not been completed
binary = "%032b" % flow.bitfield.to_i
idx = binary.reverse.index('0')
# don't wanna go over the limit
idx = [MAX_TUTORIAL_STEPS, idx].min
return TUTORIALS[tutorial][idx] || TUTORIAL_COMPLETE
end
def update_progress_for(tutorial, step)
flow = get_flow_for(tutorial)
index = TUTORIALS[tutorial].index(step)
unless (0..TUTORIALS[tutorial].size-1).include?(index)
flow.bitfield = flow.bitfield | (2**index)
flow.save!
end
end
private
def get_flow_for(tutorial)
self.tutorial_flow.first || begin
TutorialFlow.create!(:user_id => self.id, :name => tutorial.to_s, :bitfield => 0)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment