Created
April 13, 2011 01:21
-
-
Save rnmp/916788 to your computer and use it in GitHub Desktop.
Dice roller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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