From efaf81dfcb988f75998597436e36104aa879170e Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Fri, 21 Jun 2024 15:47:07 +0100 Subject: [PATCH] Add multiple dice roll types to stats --- app/controllers/dice_rolls_controller.rb | 1 + app/models/dice_roll_type.rb | 20 +++++++++++++++++++ app/models/stat.rb | 1 + app/views/stats/show.html.erb | 8 ++++++++ config/locales/en.yml | 1 + .../20240621141219_create_dice_roll_types.rb | 16 +++++++++++++++ db/schema.rb | 14 ++++++++++++- test/fixtures/dice_roll_types.yml | 4 ++++ todo.md | 3 +++ 9 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 app/models/dice_roll_type.rb create mode 100644 db/migrate/20240621141219_create_dice_roll_types.rb create mode 100644 test/fixtures/dice_roll_types.yml diff --git a/app/controllers/dice_rolls_controller.rb b/app/controllers/dice_rolls_controller.rb index 98a8f1d..acdd316 100644 --- a/app/controllers/dice_rolls_controller.rb +++ b/app/controllers/dice_rolls_controller.rb @@ -26,6 +26,7 @@ class DiceRollsController < ApplicationController params.require(:dice_roll).permit( :rollable_type, :rollable_id, + :dice_roll_type_id, ) end end diff --git a/app/models/dice_roll_type.rb b/app/models/dice_roll_type.rb new file mode 100644 index 0000000..1d80ec3 --- /dev/null +++ b/app/models/dice_roll_type.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class DiceRollType < ApplicationRecord + belongs_to :rollable, polymorphic: true + + validates :name, length: { maximum: 100 } + validates :roll_command, length: { maximum: 100 } + + def roll(table) + roller = DiceRoller.new(roll_command, stat: rollable) + end + + def value + rollable.value + end + + def character + rollable.character + end +end diff --git a/app/models/stat.rb b/app/models/stat.rb index 139dc9d..914b0b9 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -7,6 +7,7 @@ class Stat < ApplicationRecord has_one :character, through: :character_sheet_section has_many :dice_rolls, as: :rollable, dependent: :destroy + has_many :dice_roll_types, as: :rollable, dependent: :destroy validates :name, presence: true, length: { maximum: 100 } diff --git a/app/views/stats/show.html.erb b/app/views/stats/show.html.erb index 7330dc7..a98f7ef 100644 --- a/app/views/stats/show.html.erb +++ b/app/views/stats/show.html.erb @@ -19,6 +19,14 @@ <%= f.hidden_field :rollable_id, value: @stat.id %> <%= f.submit "#{t(".roll")}".html_safe, class: "roll-button" %> <% end %> + <% @stat.dice_roll_types.each do |dice_roll_type| %> + <%= form_with model: DiceRoll.new, url: table_dice_rolls_path(@stat.character.table), class: "stat-form", + data: { controller: "dice-roll modal-closer", action: "modal-closer#closeModal" } do |f| %> + <%= f.hidden_field :rollable_type, value: "DiceRollType" %> + <%= f.hidden_field :rollable_id, value: dice_roll_type.id %> + <%= f.submit "#{t(".roll_type", name: dice_roll_type.name)}".html_safe, class: "roll-button" %> + <% end %> + <% end %> <% end %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 3a91625..cddf393 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -140,6 +140,7 @@ en: stats: show: roll: Roll! + roll_type: Roll %{name}! min_allowed: Min max_allowed: Max stat: diff --git a/db/migrate/20240621141219_create_dice_roll_types.rb b/db/migrate/20240621141219_create_dice_roll_types.rb new file mode 100644 index 0000000..608d210 --- /dev/null +++ b/db/migrate/20240621141219_create_dice_roll_types.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class CreateDiceRollTypes < ActiveRecord::Migration[7.1] + def change + create_table :dice_roll_types do |t| + t.string :name, null: false + t.string :roll_command, null: false + t.belongs_to :rollable, null: false, polymorphic: true + + t.timestamps + end + + add_check_constraint :dice_roll_types, "length(name) <= 100", name: "chk_name_max_length" + add_check_constraint :dice_roll_types, "length(roll_command) <= 100", name: "chk_roll_command_max_length" + end +end diff --git a/db/schema.rb b/db/schema.rb index f8655f0..9f286eb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_06_21_102903) do +ActiveRecord::Schema[7.1].define(version: 2024_06_21_141219) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -88,6 +88,18 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_21_102903) do t.check_constraint "length(name::text) <= 200", name: "chk_character_name_max_length" end + create_table "dice_roll_types", force: :cascade do |t| + t.string "name", null: false + t.string "roll_command", null: false + t.string "rollable_type", null: false + t.bigint "rollable_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["rollable_type", "rollable_id"], name: "index_dice_roll_types_on_rollable" + t.check_constraint "length(name::text) <= 100", name: "chk_name_max_length" + t.check_constraint "length(roll_command::text) <= 100", name: "chk_roll_command_max_length" + end + create_table "dice_rolls", force: :cascade do |t| t.string "rollable_type" t.bigint "rollable_id" diff --git a/test/fixtures/dice_roll_types.yml b/test/fixtures/dice_roll_types.yml new file mode 100644 index 0000000..6b7249a --- /dev/null +++ b/test/fixtures/dice_roll_types.yml @@ -0,0 +1,4 @@ +strength_advantage: + name: with advantage + roll_command: d20+self+d20 + rollable: strength (Stat) diff --git a/todo.md b/todo.md index 592e163..dba8175 100644 --- a/todo.md +++ b/todo.md @@ -1,7 +1,10 @@ +- Edit dice roll command, types - request invite - Weapons/spells: skills? - Do this have_many :stats? - Lists +- make dice rolls own model so stats can have multiple rolls (e.g. advantage) +- improve dice roll parsing to allow e.g. choose highest - Easy add amount to stat - icons on features - auto save text fields