diff --git a/app/controllers/dice_rolls_controller.rb b/app/controllers/dice_rolls_controller.rb new file mode 100644 index 0000000..860c5ac --- /dev/null +++ b/app/controllers/dice_rolls_controller.rb @@ -0,0 +1,33 @@ +class DiceRollsController < ApplicationController + VALID_DICE_TYPES = %w[d2 d4 d6 d8 d10 d20 d100] + + def rolls + rolls_count = 1 + + unless VALID_DICE_TYPES.include?(params[:dice_type]) + render json: {}, status: 404 + end + + dice_type = params[:dice_type] + rolls = [] + + rolls_count.times do + # on a une méthode dédiée pour lancer un dé + rolls.push(roll_dice(dice_type)) + end + + render json: { + dice: dice_type, + rolls: rolls, + } + end + + private + + def roll_dice(dice_type) + # ex: on passe de "d100" à "100" puis à l'entier 100 avec le to_i + upper_bound = dice_type[1..].to_i + # rand de 1 à la taille du dé + rand(1..upper_bound) + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 70c2da8..9e63256 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html + get '/dice-rolls/:dice_type', to: "dice_rolls#rolls" # Defines the root path route ("/") root 'home#welcome'