diff options
Diffstat (limited to 'app/controllers')
| -rw-r--r-- | app/controllers/wittle/puzzles_controller.rb | 85 | 
1 files changed, 67 insertions, 18 deletions
| diff --git a/app/controllers/wittle/puzzles_controller.rb b/app/controllers/wittle/puzzles_controller.rb index 8dc2ac8..9d846f9 100644 --- a/app/controllers/wittle/puzzles_controller.rb +++ b/app/controllers/wittle/puzzles_controller.rb | |||
| @@ -1,17 +1,35 @@ | |||
| 1 | module Wittle | 1 | module Wittle | 
| 2 | class PuzzlesController < ApplicationController | 2 | class PuzzlesController < ApplicationController | 
| 3 | before_action :prepare_session | ||
| 4 | after_action :commit_session | ||
| 5 | |||
| 3 | def about | 6 | def about | 
| 4 | @normal_puzzle = Puzzle.normal.order(created_at: :desc).first | 7 | @normal_puzzle = Puzzle.normal.order(created_at: :desc).first | 
| 5 | @normal_started = ((session[:started_puzzles] || []).include? @normal_puzzle.id) | 8 | if @session_puzzles["normal"]["id"] == @normal_puzzle.id | 
| 6 | @normal_solved = ((session[:played_puzzles] || []).include? @normal_puzzle.id) | 9 | @normal_started = @session_puzzles["normal"]["started"] | 
| 10 | @normal_solved = @session_puzzles["normal"]["solved"] | ||
| 11 | else | ||
| 12 | @normal_started = false | ||
| 13 | @normal_solved = false | ||
| 14 | end | ||
| 7 | 15 | ||
| 8 | @hard_puzzle = Puzzle.hard.order(created_at: :desc).first | 16 | @hard_puzzle = Puzzle.hard.order(created_at: :desc).first | 
| 9 | @hard_started = ((session[:started_puzzles] || []).include? @hard_puzzle.id) | 17 | if @session_puzzles["hard"]["id"] == @hard_puzzle.id | 
| 10 | @hard_solved = ((session[:played_puzzles] || []).include? @hard_puzzle.id) | 18 | @hard_started = @session_puzzles["hard"]["started"] | 
| 19 | @hard_solved = @session_puzzles["hard"]["solved"] | ||
| 20 | else | ||
| 21 | @hard_started = false | ||
| 22 | @hard_solved = false | ||
| 23 | end | ||
| 11 | 24 | ||
| 12 | @expert_puzzle = Puzzle.expert.order(created_at: :desc).first | 25 | @expert_puzzle = Puzzle.expert.order(created_at: :desc).first | 
| 13 | @expert_started = ((session[:started_puzzles] || []).include? @expert_puzzle.id) | 26 | if @session_puzzles["expert"]["id"] == @expert_puzzle.id | 
| 14 | @expert_solved = ((session[:played_puzzles] || []).include? @expert_puzzle.id) | 27 | @expert_started = @session_puzzles["expert"]["started"] | 
| 28 | @expert_solved = @session_puzzles["expert"]["solved"] | ||
| 29 | else | ||
| 30 | @expert_started = false | ||
| 31 | @expert_solved = false | ||
| 32 | end | ||
| 15 | end | 33 | end | 
| 16 | 34 | ||
| 17 | def index | 35 | def index | 
| @@ -20,23 +38,34 @@ module Wittle | |||
| 20 | 38 | ||
| 21 | def show | 39 | def show | 
| 22 | @puzzle = Puzzle.find(params[:id]) | 40 | @puzzle = Puzzle.find(params[:id]) | 
| 23 | @playable = @puzzle.latest? && !((session[:played_puzzles] || []).include? @puzzle.id) | ||
| 24 | @already_started = ((session[:started_puzzles] || []).include? @puzzle.id) | ||
| 25 | 41 | ||
| 26 | unless @playable | 42 | if @puzzle.latest? | 
| 27 | if (session[:puzzle_solutions] || {}).has_key? @puzzle.id.to_s | 43 | if @session_puzzles[@puzzle.category]["id"] == @puzzle.id | 
| 28 | @solution = session[:puzzle_solutions][@puzzle.id.to_s] | 44 | @playable = !(@session_puzzles[@puzzle.category]["solved"]) | 
| 45 | @already_started = @session_puzzles[@puzzle.category]["started"] | ||
| 46 | |||
| 47 | unless @playable | ||
| 48 | @solution = @session_puzzles[@puzzle.category]["path"] | ||
| 49 | end | ||
| 29 | else | 50 | else | 
| 30 | @solution = @puzzle.solved_data | 51 | @playable = true | 
| 52 | @already_started = false | ||
| 31 | end | 53 | end | 
| 54 | else | ||
| 55 | @playable = false | ||
| 56 | @already_started = false | ||
| 57 | @solution = @puzzle.solved_data | ||
| 32 | end | 58 | end | 
| 33 | end | 59 | end | 
| 34 | 60 | ||
| 35 | def start | 61 | def start | 
| 36 | @puzzle = Puzzle.find(params[:id]) | 62 | @puzzle = Puzzle.find(params[:id]) | 
| 37 | 63 | ||
| 38 | session[:started_puzzles] ||= [] | 64 | if @session_puzzles[@puzzle.category]["id"] != @puzzle.id | 
| 39 | session[:started_puzzles] << @puzzle.id | 65 | @session_puzzles[@puzzle.category] = { "id" => @puzzle.id } | 
| 66 | end | ||
| 67 | |||
| 68 | @session_puzzles[@puzzle.category]["started"] = true | ||
| 40 | end | 69 | end | 
| 41 | 70 | ||
| 42 | def solve | 71 | def solve | 
| @@ -51,11 +80,12 @@ module Wittle | |||
| 51 | 80 | ||
| 52 | @time = (params.include? :time) ? params[:time] : nil | 81 | @time = (params.include? :time) ? params[:time] : nil | 
| 53 | 82 | ||
| 54 | session[:played_puzzles] ||= [] | 83 | if @session_puzzles[@puzzle.category]["id"] != @puzzle.id | 
| 55 | session[:played_puzzles] << @puzzle.id | 84 | @session_puzzles[@puzzle.category] = { "id" => @puzzle.id } | 
| 85 | end | ||
| 56 | 86 | ||
| 57 | session[:puzzle_solutions] ||= {} | 87 | @session_puzzles[@puzzle.category]["solved"] = true | 
| 58 | session[:puzzle_solutions][@puzzle.id] = params[:solved] | 88 | @session_puzzles[@puzzle.category]["path"] = params[:solved] | 
| 59 | end | 89 | end | 
| 60 | 90 | ||
| 61 | def submit | 91 | def submit | 
| @@ -67,5 +97,24 @@ module Wittle | |||
| 67 | 97 | ||
| 68 | redirect_to @puzzle | 98 | redirect_to @puzzle | 
| 69 | end | 99 | end | 
| 100 | |||
| 101 | private | ||
| 102 | |||
| 103 | def prepare_session | ||
| 104 | if cookies.encrypted[:puzzles].nil? | ||
| 105 | @session_puzzles = { "normal" => { id: nil }, "hard" => { id: nil }, "expert" => { id: nil } } | ||
| 106 | else | ||
| 107 | @session_puzzles = JSON.parse(cookies.encrypted[:puzzles]) | ||
| 108 | end | ||
| 109 | |||
| 110 | # Temporary hack | ||
| 111 | if session.has_key? :puzzle_solutions | ||
| 112 | session[:puzzle_solutions].clear() | ||
| 113 | end | ||
| 114 | end | ||
| 115 | |||
| 116 | def commit_session | ||
| 117 | cookies.encrypted[:puzzles] = JSON.generate(@session_puzzles) | ||
| 118 | end | ||
| 70 | end | 119 | end | 
| 71 | end | 120 | end | 
