From 4342a3e13059ae0d89a36994f67f2a8b034e8758 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 7 Nov 2023 18:19:20 -0500 Subject: switched to separate cookie for session storage --- app/controllers/wittle/puzzles_controller.rb | 85 ++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 18 deletions(-) (limited to 'app/controllers') 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 @@ module Wittle class PuzzlesController < ApplicationController + before_action :prepare_session + after_action :commit_session + 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) + 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 - @hard_started = ((session[:started_puzzles] || []).include? @hard_puzzle.id) - @hard_solved = ((session[:played_puzzles] || []).include? @hard_puzzle.id) + 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 - @expert_started = ((session[:started_puzzles] || []).include? @expert_puzzle.id) - @expert_solved = ((session[:played_puzzles] || []).include? @expert_puzzle.id) + 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 @@ -20,23 +38,34 @@ module Wittle def show @puzzle = Puzzle.find(params[:id]) - @playable = @puzzle.latest? && !((session[:played_puzzles] || []).include? @puzzle.id) - @already_started = ((session[:started_puzzles] || []).include? @puzzle.id) - unless @playable - if (session[:puzzle_solutions] || {}).has_key? @puzzle.id.to_s - @solution = session[:puzzle_solutions][@puzzle.id.to_s] + 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 - @solution = @puzzle.solved_data + @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]) - session[:started_puzzles] ||= [] - session[:started_puzzles] << @puzzle.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 @@ -51,11 +80,12 @@ module Wittle @time = (params.include? :time) ? params[:time] : nil - session[:played_puzzles] ||= [] - session[:played_puzzles] << @puzzle.id + if @session_puzzles[@puzzle.category]["id"] != @puzzle.id + @session_puzzles[@puzzle.category] = { "id" => @puzzle.id } + end - session[:puzzle_solutions] ||= {} - session[:puzzle_solutions][@puzzle.id] = params[:solved] + @session_puzzles[@puzzle.category]["solved"] = true + @session_puzzles[@puzzle.category]["path"] = params[:solved] end def submit @@ -67,5 +97,24 @@ module Wittle 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