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;
padding: .5em;
}
input {
input, .stat-value {
text-align: center;
font-size: 3em;
width: 2.5em;
border: 1px solid var(--background-color);
}
}

View File

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

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
def update
@editable = true
@stat.update(stat_params)
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,
numericality: { only_integer: true }
after_create_commit do
broadcast_append_to table, target: "events"
end
def display_text
"#{rollable.character.name} rolled #{rollable.name}: <strong>#{result}</strong>".html_safe
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 %>
<ul>
<ul id="events">
<%= render @events %>
</ul>

View File

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

View File

@ -4,8 +4,16 @@
data: { turbo_method: :delete, turbo_confirm: t(".confirm_delete", name: stat.name) }) %>
<% end %>
<h6><%= stat.name %></h6>
<%= form_with model: stat, class: "stat-form",
data: { controller: "auto-update", auto_update_update_url_value: stat_path(stat) } do |f| %>
<%= f.number_field :value, disabled: !@editable %>
<% if @editable %>
<%= form_with model: stat, class: "stat-form", data: { controller: "auto-update" } do |f| %>
<%= 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 %>
</div>

View File

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

View File

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

View File

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

View File

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