From 82ebb73bb4fc53ec5af428a88809ab8f11e52c0b Mon Sep 17 00:00:00 2001 From: David Gay Date: Thu, 27 May 2021 17:27:42 -0400 Subject: Display cost and requirements of any activity that fails to start --- app/controllers/activities_controller.rb | 7 +++++-- app/javascript/stylesheets/core.css | 4 ++++ app/models/activity.rb | 23 +++++++++++++++++++---- app/views/application/_flash_messages.html.erb | 6 ++++++ app/views/characters/hearth/index.html.erb | 4 ++-- app/views/layouts/application.html.erb | 3 +-- 6 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 app/views/application/_flash_messages.html.erb (limited to 'app') diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 43ee1b0..37403c7 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -8,8 +8,11 @@ class ActivitiesController < ApplicationController if current_char.start_activity(@activity, queued_actions: params[:queued_actions]) redirect_to activity_path(@activity) else - flash[:alert] = "You can't do that. Make sure you have the items and meet the requirements." - redirect_to character_path(current_char) + message = "You can't do that." + message += " (requires #{@activity.requirements&.join(", ")})" if @activity.requirements + message += " (costs #{@activity.costs&.join(", ")})" if @activity.costs + flash[:alert] = message + redirect_back(fallback_location: character_path(current_char)) end end end diff --git a/app/javascript/stylesheets/core.css b/app/javascript/stylesheets/core.css index 5829130..9bfb19b 100644 --- a/app/javascript/stylesheets/core.css +++ b/app/javascript/stylesheets/core.css @@ -1,3 +1,7 @@ +.flash-message { + @apply block rounded my-2 p-2; +} + .table-header-padded { @apply px-2 py-1; } diff --git a/app/models/activity.rb b/app/models/activity.rb index 7272a3b..562abf4 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -6,14 +6,29 @@ class Activity < ApplicationRecord attribute :innate, :boolean, default: false - def cost_string - requirements = [] + def costs + costs = [] self.whatnot[:cost].each do |cost| case cost[:type] when "item" - requirements.push "#{cost[:quantity]} #{Item.find_by_gid(cost[:gid]).name}" + costs.push "#{cost[:quantity]} #{Item.find_by_gid(cost[:gid]).name}" + end + end + costs + end + + def requirements + requirements = [] + self.whatnot[:requirements].each do |req| + case req[:type] + when "skill" + requirements.push "level #{req[:level]} #{Skill.find_by_gid(req[:gid]).name}" + when "hearth_amenity" + requirements.push "level #{req[:level]} #{HearthAmenity.find_by_gid(req[:gid]).name}" + else + raise "Invalid requirement type string (#{req[:type]})" end end - requirements.join(", ") + requirements end end diff --git a/app/views/application/_flash_messages.html.erb b/app/views/application/_flash_messages.html.erb new file mode 100644 index 0000000..a1c7467 --- /dev/null +++ b/app/views/application/_flash_messages.html.erb @@ -0,0 +1,6 @@ +<% if notice %> +
<%= notice %>
+<% end %> +<% if alert %> +
<%= alert %>
+<% end %> diff --git a/app/views/characters/hearth/index.html.erb b/app/views/characters/hearth/index.html.erb index 82dfbe8..20b6fc0 100644 --- a/app/views/characters/hearth/index.html.erb +++ b/app/views/characters/hearth/index.html.erb @@ -33,7 +33,7 @@ <%= f.hidden_field :queued_actions, value: 0 %> <%= f.submit construct_activity.name %> <% end %> -
(costs <%= construct_activity.cost_string %>)
+
(costs <%= construct_activity.costs.join(", ")%>)
<% end %> @@ -47,5 +47,5 @@ <%= f.hidden_field :queued_actions, value: 0 %> <%= f.submit construct_activity.name %> <% end %> - (costs <%= construct_activity.cost_string %>) + (costs <%= construct_activity.costs.join(", ")%>) <% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9b9b23a..7ce8860 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -21,8 +21,7 @@ <%= render "navbar" %> <% end %>
-

<%= notice %>

-

<%= alert %>

+ <%= render "flash_messages" %> <%= yield %>
<% if user_signed_in? %> -- cgit v1.2.3