about summary refs log tree commit diff stats
path: root/app/controllers/wittle
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2023-11-30 13:29:08 -0500
committerStar Rauchenberger <fefferburbia@gmail.com>2023-11-30 13:29:08 -0500
commit0929719a845897cc8567cf972e07a69a71f0fa6f (patch)
tree2b6f69c1d906abb6e0abf8a0f1d51725bc78087d /app/controllers/wittle
parent01c1947537e4e23ded0c16812a7cd9d49ad88356 (diff)
downloadwittle-0929719a845897cc8567cf972e07a69a71f0fa6f.tar.gz
wittle-0929719a845897cc8567cf972e07a69a71f0fa6f.tar.bz2
wittle-0929719a845897cc8567cf972e07a69a71f0fa6f.zip
Migrate to a full rails app
Diffstat (limited to 'app/controllers/wittle')
-rw-r--r--app/controllers/wittle/application_controller.rb4
-rw-r--r--app/controllers/wittle/puzzles_controller.rb120
2 files changed, 0 insertions, 124 deletions
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 @@
1module Wittle
2 class ApplicationController < ActionController::Base
3 end
4end
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 @@
1module Wittle
2 class PuzzlesController < ApplicationController
3 before_action :prepare_session
4 after_action :commit_session
5
6 def about
7 @normal_puzzle = Puzzle.normal.order(created_at: :desc).first
8 if @session_puzzles["normal"]["id"] == @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
15
16 @hard_puzzle = Puzzle.hard.order(created_at: :desc).first
17 if @session_puzzles["hard"]["id"] == @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
24
25 @expert_puzzle = Puzzle.expert.order(created_at: :desc).first
26 if @session_puzzles["expert"]["id"] == @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
33 end
34
35 def index
36 @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 }
37 end
38
39 def show
40 @puzzle = Puzzle.find(params[:id])
41
42 if @puzzle.latest?
43 if @session_puzzles[@puzzle.category]["id"] == @puzzle.id
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
50 else
51 @playable = true
52 @already_started = false
53 end
54 else
55 @playable = false
56 @already_started = false
57 @solution = @puzzle.solved_data
58 end
59 end
60
61 def start
62 @puzzle = Puzzle.find(params[:id])
63
64 if @session_puzzles[@puzzle.category]["id"] != @puzzle.id
65 @session_puzzles[@puzzle.category] = { "id" => @puzzle.id }
66 end
67
68 @session_puzzles[@puzzle.category]["started"] = true
69 end
70
71 def solve
72 @puzzle = Puzzle.find(params[:id])
73
74 raise ActiveRecord::RecordNotFound unless @puzzle.latest?
75
76 if @puzzle.solved_data.nil?
77 @puzzle.solved_data = params[:solved]
78 @puzzle.save!
79 end
80
81 @time = (params.include? :time) ? params[:time] : nil
82
83 if @session_puzzles[@puzzle.category]["id"] != @puzzle.id
84 @session_puzzles[@puzzle.category] = { "id" => @puzzle.id }
85 end
86
87 @session_puzzles[@puzzle.category]["solved"] = true
88 @session_puzzles[@puzzle.category]["path"] = params[:solved]
89 end
90
91 def submit
92 @puzzle = Puzzle.find(params[:id])
93
94 raise ActiveRecord::RecordNotFound unless @puzzle.latest?
95
96 @puzzle.scores.create(name: params[:name], ip: request.ip, seconds_taken: (params.include? :time) ? params[:time] : nil)
97
98 redirect_to @puzzle
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
119 end
120end