summaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: 0fc78a817f8ee427a6a84e626bcaeb1416ec7849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  before_action :redirect_if_no_active_character, unless: :devise_controller?

  helper_method :current_char
  def current_char
    @_current_char ||= current_user&.active_character
  end

  def redirect_if_no_active_character
    redirect_to new_character_path if (current_user && current_char.nil?)
  end

  def roll(sides)
    rand(sides) + 1
  end

  private
    def start_activity(activity)
      if current_char.start_activity(activity, queued_actions: params[:queued_actions])
        redirect_to look_path
      else
        message = "You can't do that."
        message += " (requires #{activity.requirements&.join(", ")})" if activity.requirements.any?
        message += " (costs #{activity.costs&.join(", ")})" if activity.costs.any?
        flash[:alert] = message
        redirect_back(fallback_location: character_path(current_char))
      end
    end
end