module Wittle class PuzzlesController < ApplicationController def about @normal_puzzle = Puzzle.normal.order(created_at: :desc).first @normal_started = ((session[:started_puzzles] || []).include? @normal_puzzle.id) @normal_solved = ((session[:played_puzzles] || []).include? @normal_puzzle.id) @hard_puzzle = Puzzle.hard.order(created_at: :desc).first @hard_started = ((session[:started_puzzles] || []).include? @hard_puzzle.id) @hard_solved = ((session[:played_puzzles] || []).include? @hard_puzzle.id) @expert_puzzle = Puzzle.expert.order(created_at: :desc).first @expert_started = ((session[:started_puzzles] || []).include? @expert_puzzle.id) @expert_solved = ((session[:played_puzzles] || []).include? @expert_puzzle.id) end def index @puzzles = Puzzle.select(:id, :created_at, :category).order(created_at: :asc).all.chunk { |puzzle| puzzle.created_at.localtime.to_date }.to_h.transform_values { |by_date| by_date.sort_by(&:category).chunk { |puzzle| puzzle.category }.to_h } end def show @puzzle = Puzzle.find(params[:id]) @playable = @puzzle.latest? && !((session[:played_puzzles] || []).include? @puzzle.id) @already_started = ((session[:started_puzzles] || []).include? @puzzle.id) end def start @puzzle = Puzzle.find(params[:id]) session[:started_puzzles] ||= [] session[:started_puzzles] << @puzzle.id end def solve @puzzle = Puzzle.find(params[:id]) raise ActiveRecord::RecordNotFound unless @puzzle.latest? if @puzzle.solved_data.nil? @puzzle.solved_data = params[:solved] @puzzle.save! end @time = (params.include? :time) ? params[:time] : nil session[:played_puzzles] ||= [] session[:played_puzzles] << @puzzle.id end def submit @puzzle = Puzzle.find(params[:id]) raise ActiveRecord::RecordNotFound unless @puzzle.latest? @puzzle.scores.create!(name: params[:name], ip: request.ip, seconds_taken: (params.include? :time) ? params[:time] : nil) redirect_to @puzzle end end end