Broadcast dice rolls to table

This commit is contained in:
Trevor Vallender 2024-06-13 17:04:25 +01:00
parent dd49c79edf
commit 2002651e8a
14 changed files with 67 additions and 19 deletions

View File

@ -69,9 +69,10 @@
margin: 0; margin: 0;
padding: .5em; padding: .5em;
} }
input { input, .stat-value {
text-align: center; text-align: center;
font-size: 3em; font-size: 3em;
width: 2.5em; width: 2.5em;
border: 1px solid var(--background-color);
} }
} }

View File

@ -3,7 +3,7 @@ form, fieldset {
gap: 1rem; gap: 1rem;
grid-template-columns: 1fr 2fr; grid-template-columns: 1fr 2fr;
input, label { input, label, .stat-value {
padding: .5em; padding: .5em;
} }
@ -45,12 +45,8 @@ form.stat-form, form.counter-form {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
input:disabled { .stat-value {
color: var(--text-color); color: var(--text-color);
background-color: var(--input-background); background-color: var(--input-background);
} }
input[type=number]:disabled {
-moz-appearance: textfield;
}
} }

View File

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

View File

@ -17,6 +17,7 @@ class StatsController < ApplicationController
end end
def update def update
@editable = true
@stat.update(stat_params) @stat.update(stat_params)
end end

View File

@ -0,0 +1,9 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "input" ]
rollDice() {
this.element.requestSubmit()
}
}

View File

@ -7,6 +7,10 @@ class DiceRoll < ApplicationRecord
validates :result, presence: true, validates :result, presence: true,
numericality: { only_integer: true } numericality: { only_integer: true }
after_create_commit do
broadcast_append_to table, target: "events"
end
def display_text def display_text
"#{rollable.character.name} rolled #{rollable.name}: <strong>#{result}</strong>".html_safe "#{rollable.character.name} rolled #{rollable.name}: <strong>#{result}</strong>".html_safe
end end

View File

@ -0,0 +1,2 @@
<%= turbo_stream.append("events", partial: "dice_rolls/dice_roll",
locals: { dice_roll: dice_roll }) %>

View File

@ -1,5 +1,5 @@
<% content_for :title, @table.name %> <% content_for :title, @table.name %>
<ul> <ul id="events">
<%= render @events %> <%= render @events %>
</ul> </ul>

View File

@ -1,4 +1,5 @@
<% content_for :submenu do %> <% content_for :submenu do %>
<%= turbo_stream_from @table %>
<h2><%= @table.name %></h2> <h2><%= @table.name %></h2>
<nav class="table-tabs"> <nav class="table-tabs">
<ul> <ul>

View File

@ -4,8 +4,16 @@
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %> data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %>
<% end %> <% end %>
<h6><%= stat.name %></h6> <h6><%= stat.name %></h6>
<%= form_with model: stat, class: "stat-form", <% if @editable %>
data: { controller: "auto-update", auto_update_update_url_value: stat_path(stat) } do |f| %> <%= form_with model: stat, class: "stat-form", data: { controller: "auto-update" } do |f| %>
<%= f.number_field :value, disabled: !@editable %> <%= f.number_field :value %>
<% end %>
<% else %>
<%= form_with model: DiceRoll.new, url: table_dice_rolls_path(stat.character.table), class: "stat-form",
data: { controller: "dice-roll" } do |f| %>
<%= f.hidden_field :rollable_type, value: "Stat" %>
<%= f.hidden_field :rollable_id, value: stat.id %>
<div class="stat-value" data-action="click->dice-roll#rollDice"><%= stat.value %></div>
<% end %>
<% end %> <% end %>
</div> </div>

View File

@ -1,7 +1,7 @@
<% content_for :title, @table.name %> <% content_for :title, @table.name %>
<%= link_to t(".invite_user"), new_table_table_invite_path(@table) %> <%= link_to t(".invite_user"), new_table_table_invite_path(@table), data: { turbo_frame: "_top" } %>
<%= link_to t(".edit_table"), edit_table_path(@table) %> <%= link_to t(".edit_table"), edit_table_path(@table), data: { turbo_frame: "_top" } %>
<dl> <dl>
<dt><%= t(".game_system") %>:</dt> <dt><%= t(".game_system") %>:</dt>
@ -21,7 +21,7 @@
<% if @table.users.any? %> <% if @table.users.any? %>
<ul> <ul>
<% @table.users.each do |user| %> <% @table.users.each do |user| %>
<li><%= link_to user.username, user %></li> <li><%= link_to user.username, user, data: { turbo_frame: "_top" } %></li>
<% end %> <% end %>
</ul> </ul>
<% else %> <% else %>

View File

@ -5,6 +5,4 @@ test:
adapter: test adapter: test
production: production:
adapter: redis adapter: postgresql
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: tabletop_companion_production

View File

@ -29,6 +29,7 @@ Rails.application.routes.draw do
resources :characters, only: [] do resources :characters, only: [] do
resources :character_sheet_sections, only: [ :index ] resources :character_sheet_sections, only: [ :index ]
end end
resources :dice_rolls
resources :events, only: [ :index ] resources :events, only: [ :index ]
resources :table_invites, only: [ :new, :create ] resources :table_invites, only: [ :new, :create ]
end end

View File

@ -1,5 +1,5 @@
- character sheet on table - test for rolls controller & system tests
- table tabs - table notifications for rolls
- roll on click - roll on click
- Sheets - Sheets
- Lists - Lists