Add min/max to stats

This commit is contained in:
Trevor Vallender 2024-06-21 12:46:12 +01:00
parent dd850813cb
commit 511f066ab0
10 changed files with 85 additions and 13 deletions

View File

@ -108,3 +108,29 @@
text-decoration: none; text-decoration: none;
} }
} }
.stat-value {
position: relative;
}
.stat-min-value:empty, .stat-max-value:empty {
display: none;
}
.stat-min-value, .stat-max-value {
font-size: .3em;
background-color: var(--background-color);
color: var(--header-text-color);
position: absolute;
bottom: 0;
padding: .5em;
}
.stat-min-value {
left: 0;
border-radius: 0 1em 0 0;
}
.stat-max-value {
right: 0;
border-radius: 1em 0 0 0;
}

View File

@ -54,6 +54,7 @@ form.stat-form {
display: block; display: block;
margin: 1em auto; margin: 1em auto;
width: fit-content; width: fit-content;
text-align: center;
} }
.feature-box form.text-field-form { .feature-box form.text-field-form {
@ -64,3 +65,14 @@ form.stat-form {
margin-bottom: 1em; margin-bottom: 1em;
} }
} }
.inline {
padding: .5em;
input {
padding: .2em;
height: auto;
min-width: 5em;
font-size: 1em;
}
}

View File

@ -23,7 +23,7 @@ class StatsController < ApplicationController
def update def update
@editable = ActiveModel::Type::Boolean.new.cast(params[:editable]) @editable = ActiveModel::Type::Boolean.new.cast(params[:editable])
@stat.update(stat_params) @stat.update!(stat_params)
end end
def destroy def destroy
@ -48,6 +48,8 @@ class StatsController < ApplicationController
params.require(:stat).permit( params.require(:stat).permit(
:name, :name,
:value, :value,
:min_allowed,
:max_allowed,
:roll_command, :roll_command,
character_sheet_feature_attributes: [ character_sheet_feature_attributes: [
:id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy, :id, :featurable_id, :featurable_type, :character_sheet_section_id, :_destroy,

View File

@ -10,10 +10,16 @@ class Stat < ApplicationRecord
validates :name, presence: true, validates :name, presence: true,
length: { maximum: 100 } length: { maximum: 100 }
validates :min_allowed, numericality: true, allow_nil: true
validates :max_allowed, numericality: true, allow_nil: true
validates :value, presence: true, validates :value, presence: true,
numericality: true numericality: true
validate :validate_roll_command validate :validate_roll_command
def rollable?
roll_command.present?
end
def roll(table) def roll(table)
roller = DiceRoller.new(roll_command, stat: self) roller = DiceRoller.new(roll_command, stat: self)
result = roller.roll result = roller.roll

View File

@ -17,7 +17,11 @@
<% end %> <% end %>
<% else %> <% else %>
<%= link_to stat, data: { turbo_frame: :modal } do %> <%= link_to stat, data: { turbo_frame: :modal } do %>
<div class="stat-value"><%= stat.value %></div> <div class="stat-value">
<%= stat.value %>
<div class="stat-min-value"><%= stat.min_allowed %></div>
<div class="stat-max-value"><%= stat.max_allowed %></div>
</div>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>

View File

@ -1,16 +1,24 @@
<%= turbo_frame_tag :modal do %> <%= turbo_frame_tag :modal do %>
<div class="feature-box stat"> <div class="feature-box stat">
<%= icon_link_to "fa-close", table_character_character_sheet_sections_path(@stat.character.table, @stat.character), <%= icon_link_to "fa-close", table_character_character_sheet_sections_path(@stat.character.table, @stat.character),
class: "icon-link" %> class: "icon-link" %>
<h2><%= @stat.name %></h2> <h2><%= @stat.name %></h2>
<%= form_with model: @stat, class: "stat-form", data: { controller: "auto-update" } do |f| %> <%= form_with model: @stat, class: "stat-form", data: { controller: "auto-update" } do |f| %>
<%= f.number_field :value %> <%= f.number_field :value, min: @stat.min_allowed, max: @stat.max_allowed %>
<div class="inline">
<%= f.label :min_allowed, t(".min_allowed") %>
<%= f.number_field :min_allowed %>
<%= f.label :max_allowed, t(".max_allowed") %>
<%= f.number_field :max_allowed %>
</div>
<% end %> <% end %>
<%= form_with model: DiceRoll.new, url: table_dice_rolls_path(@stat.character.table), class: "stat-form", <% if @stat.rollable? %>
data: { controller: "dice-roll modal-closer", action: "modal-closer#closeModal" } do |f| %> <%= form_with model: DiceRoll.new, url: table_dice_rolls_path(@stat.character.table), class: "stat-form",
<%= f.hidden_field :rollable_type, value: "Stat" %> data: { controller: "dice-roll modal-closer", action: "modal-closer#closeModal" } do |f| %>
<%= f.hidden_field :rollable_id, value: @stat.id %> <%= f.hidden_field :rollable_type, value: "Stat" %>
<%= f.submit "#{t(".roll")}".html_safe, class: "roll-button" %> <%= f.hidden_field :rollable_id, value: @stat.id %>
<%= f.submit "#{t(".roll")}".html_safe, class: "roll-button" %>
<% end %>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>

View File

@ -140,6 +140,8 @@ en:
stats: stats:
show: show:
roll: Roll! roll: Roll!
min_allowed: Min
max_allowed: Max
stat: stat:
down: Move down down: Move down
up: Move up up: Move up

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class AddMinMaxToStats < ActiveRecord::Migration[7.1]
def change
add_column :stats, :min_allowed, :integer
add_column :stats, :max_allowed, :integer
end
end

4
db/schema.rb generated
View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_06_21_091420) do ActiveRecord::Schema[7.1].define(version: 2024_06_21_102903) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -239,6 +239,8 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_21_091420) do
t.string "roll_command" t.string "roll_command"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.integer "min_allowed"
t.integer "max_allowed"
t.check_constraint "length(name::text) <= 100", name: "chk_stat_name_max_length" t.check_constraint "length(name::text) <= 100", name: "chk_stat_name_max_length"
end end

View File

@ -1,9 +1,11 @@
- request invite - request invite
- Weapons/spells: skills?
- Do this have_many :stats?
- Lists
- Easy add amount to stat
- icons on features
- auto save text fields - auto save text fields
- indicate save status on titlebar - indicate save status on titlebar
- Sheets
- Lists
- Weapons/spells
- Character avatars - Character avatars
- default avatars - default avatars
- add uuid/slug to characters and any other url-visible ids - add uuid/slug to characters and any other url-visible ids