diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-30 13:29:08 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-30 13:29:08 -0500 |
commit | 0929719a845897cc8567cf972e07a69a71f0fa6f (patch) | |
tree | 2b6f69c1d906abb6e0abf8a0f1d51725bc78087d /app/controllers | |
parent | 01c1947537e4e23ded0c16812a7cd9d49ad88356 (diff) | |
download | wittle-0929719a845897cc8567cf972e07a69a71f0fa6f.tar.gz wittle-0929719a845897cc8567cf972e07a69a71f0fa6f.tar.bz2 wittle-0929719a845897cc8567cf972e07a69a71f0fa6f.zip |
Migrate to a full rails app
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/application_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/puzzles_controller.rb | 109 | ||||
-rw-r--r-- | app/controllers/wittle/application_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/wittle/puzzles_controller.rb | 120 |
4 files changed, 111 insertions, 124 deletions
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 @@ | |||
1 | class ApplicationController < ActionController::Base | ||
2 | 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 @@ | |||
1 | class PuzzlesController < ApplicationController | ||
2 | before_action :prepare_session | ||
3 | |||
4 | def about | ||
5 | @normal_puzzle = Puzzle.normal.order(created_at: :desc).first | ||
6 | if session[:puzzles]["normal"]["id"] == @normal_puzzle.id | ||
7 | @normal_started = session[:puzzles]["normal"]["started"] | ||
8 | @normal_solved = session[:puzzles]["normal"]["solved"] | ||
9 | else | ||
10 | @normal_started = false | ||
11 | @normal_solved = false | ||
12 | end | ||
13 | |||
14 | @hard_puzzle = Puzzle.hard.order(created_at: :desc).first | ||
15 | if session[:puzzles]["hard"]["id"] == @hard_puzzle.id | ||
16 | @hard_started = session[:puzzles]["hard"]["started"] | ||
17 | @hard_solved = session[:puzzles]["hard"]["solved"] | ||
18 | else | ||
19 | @hard_started = false | ||
20 | @hard_solved = false | ||
21 | end | ||
22 | |||
23 | @expert_puzzle = Puzzle.expert.order(created_at: :desc).first | ||
24 | if session[:puzzles]["expert"]["id"] == @expert_puzzle.id | ||
25 | @expert_started = session[:puzzles]["expert"]["started"] | ||
26 | @expert_solved = session[:puzzles]["expert"]["solved"] | ||
27 | else | ||
28 | @expert_started = false | ||
29 | @expert_solved = false | ||
30 | end | ||
31 | end | ||
32 | |||
33 | def index | ||
34 | @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 } | ||
35 | end | ||
36 | |||
37 | def show | ||
38 | @puzzle = Puzzle.find(params["id"]) | ||
39 | |||
40 | if @puzzle.latest? | ||
41 | if session[:puzzles][@puzzle.category]["id"] == @puzzle.id | ||
42 | @playable = !(session[:puzzles][@puzzle.category]["solved"]) | ||
43 | @already_started = session[:puzzles][@puzzle.category]["started"] | ||
44 | |||
45 | unless @playable | ||
46 | @solution = session[:puzzles][@puzzle.category]["path"] | ||
47 | end | ||
48 | else | ||
49 | @playable = true | ||
50 | @already_started = false | ||
51 | end | ||
52 | else | ||
53 | @playable = false | ||
54 | @already_started = false | ||
55 | @solution = @puzzle.solved_data | ||
56 | end | ||
57 | end | ||
58 | |||
59 | def start | ||
60 | @puzzle = Puzzle.find(params["id"]) | ||
61 | |||
62 | if session[:puzzles][@puzzle.category]["id"] != @puzzle.id | ||
63 | session[:puzzles][@puzzle.category] = { "id" => @puzzle.id } | ||
64 | end | ||
65 | |||
66 | session[:puzzles][@puzzle.category]["started"] = true | ||
67 | end | ||
68 | |||
69 | def solve | ||
70 | @puzzle = Puzzle.find(params["id"]) | ||
71 | |||
72 | raise ActiveRecord::RecordNotFound unless @puzzle.latest? | ||
73 | |||
74 | if @puzzle.solved_data.nil? | ||
75 | @puzzle.solved_data = params["solved"] | ||
76 | @puzzle.save! | ||
77 | end | ||
78 | |||
79 | @time = (params.include? :time) ? params[:time] : nil | ||
80 | |||
81 | if session[:puzzles][@puzzle.category]["id"] != @puzzle.id | ||
82 | session[:puzzles][@puzzle.category] = { "id" => @puzzle.id } | ||
83 | end | ||
84 | |||
85 | session[:puzzles][@puzzle.category]["solved"] = true | ||
86 | session[:puzzles][@puzzle.category]["path"] = params["solved"] | ||
87 | end | ||
88 | |||
89 | def submit | ||
90 | @puzzle = Puzzle.find(params["id"]) | ||
91 | |||
92 | raise ActiveRecord::RecordNotFound unless @puzzle.latest? | ||
93 | |||
94 | @puzzle.scores.create(name: params[:name], ip: request.ip, seconds_taken: (params.include? :time) ? params[:time] : nil) | ||
95 | |||
96 | redirect_to @puzzle | ||
97 | end | ||
98 | |||
99 | private | ||
100 | |||
101 | def prepare_session | ||
102 | session[:puzzles] ||= { | ||
103 | normal: { "id" => 0 }, | ||
104 | hard: { "id" => 0 }, | ||
105 | expert: { "id" => 0 }, | ||
106 | } | ||
107 | end | ||
108 | |||
109 | 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 @@ | |||
1 | module Wittle | ||
2 | class ApplicationController < ActionController::Base | ||
3 | end | ||
4 | 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 @@ | |||
1 | module 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 | ||
120 | end | ||