Skip to content

Instantly share code, notes, and snippets.

@rnmp
Created April 13, 2011 01:21
Show Gist options
  • Save rnmp/916788 to your computer and use it in GitHub Desktop.
Save rnmp/916788 to your computer and use it in GitHub Desktop.
Dice roller
def create
@roll = Roll.new(params[:roll])
@rolls = Roll.all(:order => "created_at DESC", :limit => 5)
# Creating a random id & name in case is empty
@number = (rand(900000)+100001)
@roll.id = @number
if @roll.name.empty?
@roll.name = "Roll #" + @number.to_s
end
# First I created an array from the notation
@notation = @roll.notation.split("d")
@roll.times, @roll.sides = @notation[0], @notation[1]
# Then I think a formula that simulates the dice rank
@rank = @roll.sides*@roll.times
@roll.result = rand(@rank)+(@roll.times)
if @roll.result > @rank
@roll.result = @rank
end
if @roll.save
redirect_to(root_url)
else
render :action => "index"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment