Save individual die results

This commit is contained in:
Trevor Vallender 2024-06-20 08:26:33 +01:00
parent 41bcdba91d
commit 2f0e1b90b4
11 changed files with 37 additions and 9 deletions

View File

@ -6,6 +6,7 @@
--header-text-color: #fff;
--header-height: 200px;
--shadow-color: #999;
--secondary-text-color: #777;
--inset-bg-color: #eee;

View File

@ -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;

View File

@ -23,7 +23,7 @@
}
a:link.active, a:visited.active {
display: inline-block;
z-index: 1000;
z-index: 500;
border-bottom: none;
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

3
db/schema.rb generated
View File

@ -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

View File

@ -1,4 +1,3 @@
- set up email in prod
- request invite
- don't move down/up top/bottom features
- auto save text fields