From 2f0e1b90b46a4ef2c9407cf33092ff9fbea4e8a2 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 20 Jun 2024 08:26:33 +0100 Subject: [PATCH] Save individual die results --- app/assets/stylesheets/colors.css | 1 + app/assets/stylesheets/notifications.css | 2 ++ app/assets/stylesheets/tables.css | 2 +- app/controllers/dice_rolls_controller.rb | 4 +++- app/controllers/sessions_controller.rb | 2 +- app/models/dice_roll.rb | 11 ++++++++++- app/models/stat.rb | 5 +++-- app/services/dice_roller.rb | 8 +++++++- db/migrate/20240619190424_add_parts_to_dice_rolls.rb | 7 +++++++ db/schema.rb | 3 ++- todo.md | 1 - 11 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20240619190424_add_parts_to_dice_rolls.rb diff --git a/app/assets/stylesheets/colors.css b/app/assets/stylesheets/colors.css index f88a296..9021b10 100644 --- a/app/assets/stylesheets/colors.css +++ b/app/assets/stylesheets/colors.css @@ -6,6 +6,7 @@ --header-text-color: #fff; --header-height: 200px; + --shadow-color: #999; --secondary-text-color: #777; --inset-bg-color: #eee; diff --git a/app/assets/stylesheets/notifications.css b/app/assets/stylesheets/notifications.css index 05bdf38..287bc0a 100644 --- a/app/assets/stylesheets/notifications.css +++ b/app/assets/stylesheets/notifications.css @@ -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; diff --git a/app/assets/stylesheets/tables.css b/app/assets/stylesheets/tables.css index 2d4a280..3a2bf6e 100644 --- a/app/assets/stylesheets/tables.css +++ b/app/assets/stylesheets/tables.css @@ -23,7 +23,7 @@ } a:link.active, a:visited.active { display: inline-block; - z-index: 1000; + z-index: 500; border-bottom: none; } } diff --git a/app/controllers/dice_rolls_controller.rb b/app/controllers/dice_rolls_controller.rb index f0fd4d5..98a8f1d 100644 --- a/app/controllers/dice_rolls_controller.rb +++ b/app/controllers/dice_rolls_controller.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index fe1aa5c..5062a7f 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/models/dice_roll.rb b/app/models/dice_roll.rb index 1aef23a..89d72af 100644 --- a/app/models/dice_roll.rb +++ b/app/models/dice_roll.rb @@ -14,6 +14,15 @@ class DiceRoll < ApplicationRecord end def display_text - "#{rollable.character.name} rolled #{rollable.name}: #{result}".html_safe + text = <<~TEXT + #{rollable.character.name} rolled #{rollable.name}: + #{result} + (#{dice_text}) + TEXT + text.html_safe + end + + def dice_text + dice.map { |d| "D#{d[0]}: #{d[1]}" }.join(", ") end end diff --git a/app/models/stat.rb b/app/models/stat.rb index a7073b5..90a55e2 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -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 diff --git a/app/services/dice_roller.rb b/app/services/dice_roller.rb index bdbabc3..5743f7e 100644 --- a/app/services/dice_roller.rb +++ b/app/services/dice_roller.rb @@ -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 diff --git a/db/migrate/20240619190424_add_parts_to_dice_rolls.rb b/db/migrate/20240619190424_add_parts_to_dice_rolls.rb new file mode 100644 index 0000000..8a3cc46 --- /dev/null +++ b/db/migrate/20240619190424_add_parts_to_dice_rolls.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 6e6e466..9276d39 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_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 diff --git a/todo.md b/todo.md index e573fcb..e5e5e36 100644 --- a/todo.md +++ b/todo.md @@ -1,4 +1,3 @@ -- set up email in prod - request invite - don't move down/up top/bottom features - auto save text fields