class GameController < ApplicationController include QuizHelperMethods skip_before_action :require_player!, only: [ :index, :start ] before_action :set_node, only: [ :stage, :answer, :stage_result ] helper_method :root_node, :stage_index, :n_stages def index @node = root_node not_found and return unless @node render action: @node.template end # POST def start player = Player.create(locale: I18n.locale.to_s) session[:player_id] = player.id redirect_to action: "facts" end def facts @node = root_node&.children&.facts&.first end def intro @node = root_node&.children&.intro&.first end def stage end # POST def answer @answer = @node.children.find_by(id: params[:value]) current_player.record_answer(stage_index, @answer.id) redirect_to action: :stage_result, id: params[:id] end def stage_result end private def root_node @root_node ||= Node.roots.viewable.first end def stages @stages ||= root_node.children.stage end def set_node @node = stages[params[:id].to_i - 1] not_found and return unless @node end def stage_index @stage_index ||= stages.index(@node) + 1 end def n_stages @n_stages ||= stages.size end def url_from_param return "" unless params[:url] # return File.join('', params[:url]) if I18n.default_locale == I18n.locale File.join("", I18n.locale.to_s, params[:url]) end end