-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
288 lines (240 loc) · 6.15 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true
require "sinatra"
require "sinatra/reloader" if development?
require 'sinatra/content_for'
require "tilt/erubis"
require "bcrypt"
require "pg"
require 'json'
require_relative './lib/database'
require_relative './lib/word_ladder.rb'
configure do
enable :sessions
set :session_secret, 'secret'
# set :show_exceptions, false # UNCOMMENT TO TEST ERROR PAGE
set :erb, :escape_html => true
end
before do
session[:dark] == 1
init_test_words
session[:ladder] ||= init_ladder
session[:steps] ||= []
p session[:ladder]
end
error do
erb :error, layout: :layout
end
def init_ladder
ladder = WordLadder.new
session[:puzzle_id] = Database.add_puzzle(ladder)
return ladder
end
def init_test_words
words = File.readlines('./data/cmudict.txt').map do |line|
if data = line.match(/^[a-z]+\b/)
data.to_s
else
nil
end
end
words.select! { |word| word }
words.map! { |word| word.downcase }
words.uniq!
@test_words = words
end
def is_a_word?(word)
@test_words.include?(word)
end
def ladder_complete?
WordLadder.adjacent?(session[:steps].last, session[:ladder].last)
end
def previous_word
session[:steps].last || session[:ladder].first
end
def submit_solution
first = session[:ladder].first
last = session[:ladder].last
solution = [first] + session[:steps] + [last]
Database::Users.submit_solution(session[:user_id], session[:puzzle_id], solution)
end
get '/' do
redirect '/play'
end
get '/about' do
erb :about, layout: :layout
end
# get '/contact' do
# # erb :contact, layout: :layout
# redirect 'https://github.com/Duncan-Britt'
# end
get '/leaderboard' do
@leaderboard = Database::Users.top_100
erb :leaderboard, layout: :layout
end
get '/play' do
if params[:nojs]
@noscript = true
return erb :nojs, layout: :layout
end
if session[:complete_ladder]
redirect '/play/victory'
end
@dark = session[:dark]
@first = session[:ladder].first
@last = session[:ladder].last
@length = session[:ladder].length
erb :play, layout: :layout
end
get '/play/victory' do
session[:dark]
@first = session[:ladder].first
@last = session[:ladder].last
@length = session[:ladder].length
erb :victory, layout: :layout
end
get '/new_game' do
session[:complete_ladder] = false
session[:unlocked] = false
session[:ladder] = init_ladder
session[:steps] = []
redirect '/play'
end
get '/account/solutions' do
erb :account_solutions, layout: :layout
end
get '/account/solutions/:offset' do
WINDOW = 30
JSON.generate(
Database::Users.solutions(session[:user_id], WINDOW, params[:offset].to_i)
)
end
get '/solutions/:puzzle_id' do
session[:unlocked] = true
erb :solutions, layout: :layout
end
get '/login' do
erb :login, layout: :layout
end
post '/login' do
input_username = params[:username]
input_password = params[:password]
if (session[:user_id] = Database::Users.auth(input_username, input_password))
session[:username] = input_username
session[:success] = "You have been logged in successfully"
redirect '/'
else
session[:error] = "The username or password you enterd was incorrect"
erb :login, layout: :layout
end
end
post '/account' do
input_username = params[:username]
input_password = params[:password]
if (session[:user_id] = Database::Users.new_user(input_username, input_password))
session[:username] = input_username
session[:success] = "Account created successfully. You are logged in"
redirect '/'
else
session[:error] = "That username is taken. Try another"
redirect '/login'
end
end
get '/logout' do
session.delete(:user_id)
session.delete(:username)
redirect '/play'
end
get '/account' do
redirect '/play' unless session[:user_id]
@ldr_brd_n = Database::Users.leader_board_position(session[:user_id])
erb :account, layout: :layout
end
get '/account/edit/username' do
redirect '/play' unless session[:user_id]
erb :edit_username, layout: :layout
end
get '/account/edit/password' do
redirect '/play' unless session[:user_id]
erb :edit_password, layout: :layout
end
delete '/account' do
return unless session[:user_id]
input_username = params[:username]
input_password = params[:password]
if session[:username] == input_username &&
Database::Users.delete_account(input_username, input_password)
session.delete(:user_id)
session.delete(:username)
session[:success] = "Your account has been deleted"
status 301
JSON.generate({ path: '/play' })
else
status 401
end
end
post '/step' do
input_word = params[:step]
unless WordLadder.adjacent? input_word, previous_word
status 204
return
end
if is_a_word?(input_word)
session[:steps].push(input_word)
if ladder_complete?
submit_solution if session[:user_id] && !session[:unlocked]
session[:complete_ladder] = true
session[:success] = "Solved! Great Work!"
status 301
JSON.generate({ path: '/play/victory' })
else
@first = session[:ladder].first
@last = session[:ladder].last
status 201
erb :ladder, layout: false
end
else
status 204
end
end
delete '/step' do
session[:steps].pop
@first = session[:ladder].first
@last = session[:ladder].last
status 201
erb :ladder, layout: false
end
put '/username' do
return unless session[:user_id]
input_username = params[:new_username]
if Database::Users.account_exists?(input_username)
status 403
"Sorry, that username is taken"
else
res = Database::Users.update_username(session[:user_id], input_username)
if res.cmd_status == "UPDATE 1"
session[:username] = input_username
session[:success] = "Username updated successfully"
status 204
else
p "error"
end
end
end
put '/password' do
return unless session[:user_id]
input_password = params[:new_password]
res = Database::Users.update_password(session[:user_id], input_password)
if res.cmd_status == "UPDATE 1"
session[:success] = "Password updated successfully"
status 204
else
raise StandarError.new("Datbase not updated properly: #{res.cmd_status}")
end
end
put '/toggle_dark_mode' do
session[:dark] ^= 1
status 204
end
not_found do
redirect '/play'
end