Save individual die results
This commit is contained in:
parent
41bcdba91d
commit
2f0e1b90b4
|
@ -6,6 +6,7 @@
|
|||
--header-text-color: #fff;
|
||||
--header-height: 200px;
|
||||
|
||||
--shadow-color: #999;
|
||||
--secondary-text-color: #777;
|
||||
|
||||
--inset-bg-color: #eee;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#table-notifications {
|
||||
z-index: 1000;
|
||||
position: fixed;
|
||||
width: 60em;
|
||||
max-width: 80%;
|
||||
top: 1em; left: 0;
|
||||
transform: translate(calc(50vw - 50%));
|
||||
li {
|
||||
box-shadow: 5px 5px 15px var(--shadow-color);
|
||||
background-color: var(--notification-bg-color);
|
||||
list-style-type: none;
|
||||
padding: .5em;
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
}
|
||||
a:link.active, a:visited.active {
|
||||
display: inline-block;
|
||||
z-index: 1000;
|
||||
z-index: 500;
|
||||
border-bottom: none;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,11 @@ class DiceRollsController < ApplicationController
|
|||
rollable = dice_roll_params[:rollable_type].constantize.find(dice_roll_params[:rollable_id])
|
||||
return head :bad_request if rollable.roll_command.blank?
|
||||
|
||||
roller = DiceRoller.new(rollable.roll_command, stat: rollable)
|
||||
@table.dice_rolls.create!(
|
||||
rollable:,
|
||||
result: DiceRoller.new(rollable.roll_command, stat: rollable).roll,
|
||||
result: roller.roll,
|
||||
dice: roller.dice,
|
||||
)
|
||||
head :ok
|
||||
end
|
||||
|
|
|
@ -15,7 +15,7 @@ class SessionsController < ApplicationController
|
|||
session[:user_id] = Current.user.id
|
||||
flash[:notice] = t(".success", name: Current.user.first_name)
|
||||
redirect_to :root
|
||||
elsif !Current.user.verified?
|
||||
elsif Current.user && !Current.user.verified?
|
||||
flash[:alert] = t(".not_verified")
|
||||
render :new, status: :unprocessable_entity
|
||||
else
|
||||
|
|
|
@ -14,6 +14,15 @@ class DiceRoll < ApplicationRecord
|
|||
end
|
||||
|
||||
def display_text
|
||||
"#{rollable.character.name} rolled #{rollable.name}: <strong>#{result}</strong>".html_safe
|
||||
text = <<~TEXT
|
||||
#{rollable.character.name} rolled #{rollable.name}:
|
||||
<strong>#{result}</strong>
|
||||
(#{dice_text})
|
||||
TEXT
|
||||
text.html_safe
|
||||
end
|
||||
|
||||
def dice_text
|
||||
dice.map { |d| "D#{d[0]}: #{d[1]}" }.join(", ")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,8 +15,9 @@ class Stat < ApplicationRecord
|
|||
validate :validate_roll_command
|
||||
|
||||
def roll(table)
|
||||
result = DiceRoller.new(roll_command, stat: self).roll
|
||||
dice_rolls.create(result:, table:)
|
||||
roller = DiceRoller.new(roll_command, stat: self)
|
||||
result = roller.roll
|
||||
dice_rolls.create(result: roller.roll, table:, dice: roller.dice)
|
||||
result
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class DiceRoller
|
||||
attr_reader :dice
|
||||
attr_reader :result
|
||||
|
||||
def initialize(roll_command, stat: nil)
|
||||
@roll_command = roll_command
|
||||
@stat = stat&.value
|
||||
@dice = []
|
||||
end
|
||||
|
||||
def roll
|
||||
|
@ -59,7 +63,9 @@ class DiceRoller
|
|||
|
||||
result = 0
|
||||
dice_number.times do
|
||||
result += rand(1..die_type.to_i)
|
||||
roll = rand(1..die_type.to_i)
|
||||
result += roll
|
||||
dice << [ die_type, result ]
|
||||
end
|
||||
result
|
||||
end
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddPartsToDiceRolls < ActiveRecord::Migration[7.1]
|
||||
def change
|
||||
add_column :dice_rolls, :dice, :jsonb, null: false, default: {}
|
||||
end
|
||||
end
|
|
@ -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_17_151532) do
|
||||
ActiveRecord::Schema[7.1].define(version: 2024_06_19_190424) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
|
@ -103,6 +103,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_06_17_151532) do
|
|||
t.integer "result", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.jsonb "dice", default: {}, null: false
|
||||
t.index ["rollable_type", "rollable_id"], name: "index_dice_rolls_on_rollable"
|
||||
t.index ["table_id"], name: "index_dice_rolls_on_table_id"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue