diff options
Diffstat (limited to 'app/controllers/puzzles_controller.rb')
-rw-r--r-- | app/controllers/puzzles_controller.rb | 109 |
1 files changed, 109 insertions, 0 deletions
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 | ||