From 2002651e8a65556f17f4e6a577157bf6dcb6e04c Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 13 Jun 2024 17:04:25 +0100 Subject: [PATCH] Broadcast dice rolls to table --- app/assets/stylesheets/characters.css | 3 ++- app/assets/stylesheets/forms.css | 8 ++---- app/controllers/dice_rolls_controller.rb | 27 +++++++++++++++++++ app/controllers/stats_controller.rb | 1 + .../controllers/dice_roll_controller.js | 9 +++++++ app/models/dice_roll.rb | 4 +++ .../dice_rolls/_dice_roll.turbo_stream.erb | 2 ++ app/views/events/index.html.erb | 2 +- app/views/layouts/table.html.erb | 1 + app/views/stats/_stat.html.erb | 14 +++++++--- app/views/tables/show.html.erb | 6 ++--- config/cable.yml | 4 +-- config/routes.rb | 1 + todo.md | 4 +-- 14 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 app/controllers/dice_rolls_controller.rb create mode 100644 app/javascript/controllers/dice_roll_controller.js create mode 100644 app/views/dice_rolls/_dice_roll.turbo_stream.erb diff --git a/app/assets/stylesheets/characters.css b/app/assets/stylesheets/characters.css index 10dace6..dfa91dc 100644 --- a/app/assets/stylesheets/characters.css +++ b/app/assets/stylesheets/characters.css @@ -69,9 +69,10 @@ margin: 0; padding: .5em; } - input { + input, .stat-value { text-align: center; font-size: 3em; width: 2.5em; + border: 1px solid var(--background-color); } } diff --git a/app/assets/stylesheets/forms.css b/app/assets/stylesheets/forms.css index ad08a17..b6403a1 100644 --- a/app/assets/stylesheets/forms.css +++ b/app/assets/stylesheets/forms.css @@ -3,7 +3,7 @@ form, fieldset { gap: 1rem; grid-template-columns: 1fr 2fr; - input, label { + input, label, .stat-value { padding: .5em; } @@ -45,12 +45,8 @@ form.stat-form, form.counter-form { display: flex; flex-direction: column; - input:disabled { + .stat-value { color: var(--text-color); background-color: var(--input-background); } - - input[type=number]:disabled { - -moz-appearance: textfield; - } } diff --git a/app/controllers/dice_rolls_controller.rb b/app/controllers/dice_rolls_controller.rb new file mode 100644 index 0000000..712affa --- /dev/null +++ b/app/controllers/dice_rolls_controller.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class DiceRollsController < ApplicationController + before_action :set_table + + def create + rollable = dice_roll_params[:rollable_type].constantize.find(dice_roll_params[:rollable_id]) + @table.dice_rolls.create!( + rollable:, + result: DiceRoller.new(rollable.roll_command, stat: rollable).roll, + ) + head :ok + end + + private + + def set_table + @table = Current.user.owned_tables.find(params[:table_id]) + end + + def dice_roll_params + params.require(:dice_roll).permit( + :rollable_type, + :rollable_id, + ) + end +end diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 4047f42..e8fb0ff 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -17,6 +17,7 @@ class StatsController < ApplicationController end def update + @editable = true @stat.update(stat_params) end diff --git a/app/javascript/controllers/dice_roll_controller.js b/app/javascript/controllers/dice_roll_controller.js new file mode 100644 index 0000000..075f91b --- /dev/null +++ b/app/javascript/controllers/dice_roll_controller.js @@ -0,0 +1,9 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = [ "input" ] + + rollDice() { + this.element.requestSubmit() + } +} diff --git a/app/models/dice_roll.rb b/app/models/dice_roll.rb index d4b4952..12252c6 100644 --- a/app/models/dice_roll.rb +++ b/app/models/dice_roll.rb @@ -7,6 +7,10 @@ class DiceRoll < ApplicationRecord validates :result, presence: true, numericality: { only_integer: true } + after_create_commit do + broadcast_append_to table, target: "events" + end + def display_text "#{rollable.character.name} rolled #{rollable.name}: #{result}".html_safe end diff --git a/app/views/dice_rolls/_dice_roll.turbo_stream.erb b/app/views/dice_rolls/_dice_roll.turbo_stream.erb new file mode 100644 index 0000000..82169ad --- /dev/null +++ b/app/views/dice_rolls/_dice_roll.turbo_stream.erb @@ -0,0 +1,2 @@ +<%= turbo_stream.append("events", partial: "dice_rolls/dice_roll", + locals: { dice_roll: dice_roll }) %> diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index 458de76..e12b2af 100644 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -1,5 +1,5 @@ <% content_for :title, @table.name %> -