From 0929719a845897cc8567cf972e07a69a71f0fa6f Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 30 Nov 2023 13:29:08 -0500 Subject: Migrate to a full rails app --- app/controllers/application_controller.rb | 2 + app/controllers/puzzles_controller.rb | 109 ++++++++++++++++++++ app/controllers/wittle/application_controller.rb | 4 - app/controllers/wittle/puzzles_controller.rb | 120 ----------------------- 4 files changed, 111 insertions(+), 124 deletions(-) create mode 100644 app/controllers/application_controller.rb create mode 100644 app/controllers/puzzles_controller.rb delete mode 100644 app/controllers/wittle/application_controller.rb delete mode 100644 app/controllers/wittle/puzzles_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..09705d1 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::Base +end diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb new file mode 100644 index 0000000..b996022 --- /dev/null +++ b/app/controllers/puzzles_controller.rb @@ -0,0 +1,109 @@ +class PuzzlesController < ApplicationController + before_action :prepare_session + + def about + @normal_puzzle = Puzzle.normal.order(created_at: :desc).first + if session[:puzzles]["normal"]["id"] == @normal_puzzle.id + @normal_started = session[:puzzles]["normal"]["started"] + @normal_solved = session[:puzzles]["normal"]["solved"] + else + @normal_started = false + @normal_solved = false + end + + @hard_puzzle = Puzzle.hard.order(created_at: :desc).first + if session[:puzzles]["hard"]["id"] == @hard_puzzle.id + @hard_started = session[:puzzles]["hard"]["started"] + @hard_solved = session[:puzzles]["hard"]["solved"] + else + @hard_started = false + @hard_solved = false + end + + @expert_puzzle = Puzzle.expert.order(created_at: :desc).first + if session[:puzzles]["expert"]["id"] == @expert_puzzle.id + @expert_started = session[:puzzles]["expert"]["started"] + @expert_solved = session[:puzzles]["expert"]["solved"] + else + @expert_started = false + @expert_solved = false + end + 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"]) + + if @puzzle.latest? + if session[:puzzles][@puzzle.category]["id"] == @puzzle.id + @playable = !(session[:puzzles][@puzzle.category]["solved"]) + @already_started = session[:puzzles][@puzzle.category]["started"] + + unless @playable + @solution = session[:puzzles][@puzzle.category]["path"] + end + else + @playable = true + @already_started = false + end + else + @playable = false + @already_started = false + @solution = @puzzle.solved_data + end + end + + def start + @puzzle = Puzzle.find(params["id"]) + + if session[:puzzles][@puzzle.category]["id"] != @puzzle.id + session[:puzzles][@puzzle.category] = { "id" => @puzzle.id } + end + + session[:puzzles][@puzzle.category]["started"] = true + 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 + + if session[:puzzles][@puzzle.category]["id"] != @puzzle.id + session[:puzzles][@puzzle.category] = { "id" => @puzzle.id } + end + + session[:puzzles][@puzzle.category]["solved"] = true + session[:puzzles][@puzzle.category]["path"] = params["solved"] + 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 + + private + + def prepare_session + session[:puzzles] ||= { + normal: { "id" => 0 }, + hard: { "id" => 0 }, + expert: { "id" => 0 }, + } + end + +end diff --git a/app/controllers/wittle/application_controller.rb b/app/controllers/wittle/application_controller.rb deleted file mode 100644 index 252a28e..0000000 --- a/app/controllers/wittle/application_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Wittle - class ApplicationController < ActionController::Base - end -end diff --git a/app/controllers/wittle/puzzles_controller.rb b/app/controllers/wittle/puzzles_controller.rb deleted file mode 100644 index a937aa1..0000000 --- a/app/controllers/wittle/puzzles_controller.rb +++ /dev/null @@ -1,120 +0,0 @@ -module Wittle - class PuzzlesController < ApplicationController - before_action :prepare_session - after_action :commit_session - - def about - @normal_puzzle = Puzzle.normal.order(created_at: :desc).first - if @session_puzzles["normal"]["id"] == @normal_puzzle.id - @normal_started = @session_puzzles["normal"]["started"] - @normal_solved = @session_puzzles["normal"]["solved"] - else - @normal_started = false - @normal_solved = false - end - - @hard_puzzle = Puzzle.hard.order(created_at: :desc).first - if @session_puzzles["hard"]["id"] == @hard_puzzle.id - @hard_started = @session_puzzles["hard"]["started"] - @hard_solved = @session_puzzles["hard"]["solved"] - else - @hard_started = false - @hard_solved = false - end - - @expert_puzzle = Puzzle.expert.order(created_at: :desc).first - if @session_puzzles["expert"]["id"] == @expert_puzzle.id - @expert_started = @session_puzzles["expert"]["started"] - @expert_solved = @session_puzzles["expert"]["solved"] - else - @expert_started = false - @expert_solved = false - end - 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]) - - if @puzzle.latest? - if @session_puzzles[@puzzle.category]["id"] == @puzzle.id - @playable = !(@session_puzzles[@puzzle.category]["solved"]) - @already_started = @session_puzzles[@puzzle.category]["started"] - - unless @playable - @solution = @session_puzzles[@puzzle.category]["path"] - end - else - @playable = true - @already_started = false - end - else - @playable = false - @already_started = false - @solution = @puzzle.solved_data - end - end - - def start - @puzzle = Puzzle.find(params[:id]) - - if @session_puzzles[@puzzle.category]["id"] != @puzzle.id - @session_puzzles[@puzzle.category] = { "id" => @puzzle.id } - end - - @session_puzzles[@puzzle.category]["started"] = true - 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 - - if @session_puzzles[@puzzle.category]["id"] != @puzzle.id - @session_puzzles[@puzzle.category] = { "id" => @puzzle.id } - end - - @session_puzzles[@puzzle.category]["solved"] = true - @session_puzzles[@puzzle.category]["path"] = params[:solved] - 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 - - private - - def prepare_session - if cookies.encrypted[:puzzles].nil? - @session_puzzles = { "normal" => { id: nil }, "hard" => { id: nil }, "expert" => { id: nil } } - else - @session_puzzles = JSON.parse(cookies.encrypted[:puzzles]) - end - - # Temporary hack - if session.has_key? :puzzle_solutions - session[:puzzle_solutions].clear() - end - end - - def commit_session - cookies.encrypted[:puzzles] = JSON.generate(@session_puzzles) - end - end -end -- cgit 1.4.1