-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
399 lines (324 loc) · 9.5 KB
/
app.rb
File metadata and controls
399 lines (324 loc) · 9.5 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
require 'sinatra'
require 'sinatra/content_for'
require 'tilt/erubis'
require 'bcrypt'
require_relative "database_persistence"
configure do
enable :sessions
set :session_secret, 'secret'
set :erb, :escape_html => true
end
configure(:development) do
require 'sinatra/reloader'
also_reload "database_persistence.rb"
end
helpers do
def admin?(user)
@data.is_admin?(user)
end
end
before do
@data = DatabasePersistence.new
@current_season = @data.get_season
end
after do
@data.disconnect
end
def valid_credentials?(user, password)
if user
bcrypt_password = BCrypt::Password.new(user[:pass])
bcrypt_password == password
else
false
end
end
def valid_new_user?(username, pass, repass, question)
valid_username?(username) && valid_password?(pass, repass) &&
valid_security?(question)
end
def valid_username?(username)
usernames = @data.get_users.map { |user| user[:username].downcase }
if usernames.include?(username.downcase)
session[:error] = "'#{username}' is already is use. Please choose another username."
false
elsif username.empty?
session[:error] = 'Username cannot be empty.'
false
else
true
end
end
def valid_password?(pass, repass)
if pass.length < 6
session[:error] = 'Password must be at least 6 characters long.'
false
elsif pass.downcase.match(/[^a-z0-9]/)
session[:error] = 'Password must only include numbers and letters.'
false
elsif pass != repass
session[:error] = 'Password fields did not match. Please try again.'
false
else
true
end
end
def valid_security?(question)
if question.empty?
session[:error] = 'Security question cannot be blank.'
false
else
true
end
end
def add_new_user(username, password, question)
bcrypt_password = BCrypt::Password.create(password)
bcrypt_question = BCrypt::Password.create(question)
@data.add_new_player(username, bcrypt_password, bcrypt_question)
end
def valid_reset_info?(user, question)
if user
bcrypt_question = BCrypt::Password.new(user[:question])
bcrypt_question == question
else
false
end
end
def update_password(username, password)
bcrypt_password = BCrypt::Password.create(password)
@data.update_password(username, bcrypt_password)
end
def valid_stats?(place, elims)
(place > 0 && place < 100) &&
(elims < 99)
end
def calc_elim_points(type, elims, players)
multiplier = {'solo' => 1, 'duo' => 2, 'squad' => 4}
points = @data.get_elim_points * multiplier[type] * elims
end
def calc_place_points(type, place, players)
handicap_multiplier = {'solo' => 1, 'duo' => 2, 'squad' => 2.4}
divisor = {'solo' => 1, 'duo' => 2, 'squad' => 4}
points = @data.get_place_points(place) / divisor[type]
handicap = if type == 'squad'
missing_players = 4 - players
points * handicap_multiplier[type] * missing_players
elsif type == 'duo'
missing_players = 2 - players
points * handicap_multiplier[type] * missing_players
else
0
end
(points + handicap).round
end
def sort_stats(stats, sort)
if sort == 'user' || sort == 'avg_place'
stats.sort_by { |player| player[@sort.to_sym] }
else
stats.sort_by { |player| player[@sort.to_sym] }.reverse
end
end
def verify_players(count, type)
if type == 'squad' && count == 0
return 4
elsif type == 'duo' && (count == 0 || count > 2)
return 2
end
count
end
get '/' do
if session[:logged_in]
@last_ten = @data.get_last_ten
end
erb :home
end
get '/players/login' do
erb :login
end
post '/players/login' do
users = @data.get_users
@username = params[:username].strip
password = params[:password]
user = users.find { |info| info[:username].downcase == @username.downcase }
if valid_credentials?(user, password)
session[:current_user] = user[:username]
session[:logged_in] = true
session[:success] = 'You have succesfully logged in!'
redirect '/'
else
session[:error] = 'Invalid login info...'
erb :login
end
end
post '/players/logout' do
session[:logged_in] = false
session.delete(:current_user)
session[:success] = "You have successfully been signed out."
redirect '/'
end
get '/players/new' do
erb :new
end
post '/players/new' do
@username = params[:username].strip
password = params[:password]
repass = params[:repass]
@question = params[:question].strip
if valid_new_user?(@username, password, repass, @question)
session[:success] = "You have succesfully created a new account! Please login."
add_new_user(@username, password, @question)
redirect '/players/login'
else
erb :new
end
end
get '/players/login/reset' do
erb :reset
end
post '/players/login/reset' do
users = @data.get_users
@username = params[:username].strip
@question = params[:question].strip
user = users.find { |info| info[:username].downcase == @username.downcase }
if valid_reset_info?(user, @question)
session[:current_user] = user[:username]
redirect '/players/login/update'
else
session[:error] = 'Did not find any matching data in system.'
erb :reset
end
end
get '/players/login/update' do
erb :update
end
post '/players/login/update' do
password = params[:password]
repass = params[:repass]
username = session[:current_user]
if valid_password?(password, repass)
session[:success] = 'You have successfully updated your password. Please log in.'
update_password(username, password)
redirect '/players/login'
else
erb :update
end
end
get '/player/stats/:type/:season/page-:page_number' do
@type = params[:type]
@season = params[:season]
@seasons = @data.get_seasons
@page = params[:page_number].to_i
offset = (@page - 1) * 10
if session[:logged_in]
@summary_stats = @data.get_summary(session[:current_user], @season, @type)
@match_stats = @data.get_match(session[:current_user], @season, @type, offset)
@page_limit = @match_stats.empty? ? 1 : (@match_stats.first[:entries] / 10.0).ceil
end
erb :my_stats
end
get '/player/stats/filter/page-:page_number' do
@type = params[:type]
@season = params[:season]
@seasons = @data.get_seasons
@page = params[:page_number].to_i
offset = (@page - 1) * 10
if session[:logged_in]
@summary_stats = @data.get_summary(session[:current_user], @season, @type)
@match_stats = @data.get_match(session[:current_user], @season, @type, offset)
@page_limit = @match_stats.empty? ? 1 : (@match_stats.first[:entries] / 10.0).ceil
end
erb :my_stats
end
get '/player/stats/add' do
@type = params[:type]
erb :add_stats
end
post '/player/stats/add' do
@type = params[:type]
@players = verify_players(params[:players].to_i, @type)
@place = params[:place].to_i
@elims = params[:elims].to_i
user = session[:current_user]
season = @current_season
if valid_stats?(@place, @elims)
elim_points = calc_elim_points(@type, @elims, @players)
place_points = calc_place_points(@type, @place, @players)
@data.add_stats(user, @type, season, place_points, elim_points, @place, @elims)
session[:success] = 'Your stats have been added!'
redirect '/player/stats/add'
else
session[:error] = 'Please enter valid values.'
erb :add_stats
end
end
get '/seasons/edit' do
@seasons = @data.get_seasons
erb :seasons
end
post '/seasons/set-active' do
active = params[:active].to_i
@data.update_active_season(active)
session[:success] = "Season #{active} has been set to active."
redirect "/player/stats/combined/#{@current_season}/page-1"
end
post '/seasons/add' do
new_season = params[:new].to_i
@data.add_new_season(new_season)
session[:success] = "Season #{new_season} has been added."
redirect "/player/stats/combined/#{@current_season}/page-1"
end
get '/players' do
@players = @data.get_all_players
erb :players
end
get '/players/:user/stats/:type/:season/page-:page_number' do
@user = params[:user]
@type = params[:type]
@season = params[:season]
@seasons = @data.get_seasons
@page = params[:page_number].to_i
offset = (@page - 1) * 10
if session[:logged_in]
@summary_stats = @data.get_summary(@user, @season, @type)
@match_stats = @data.get_match(@user, @season, @type, offset)
@page_limit = @match_stats.empty? ? 1 : (@match_stats.first[:entries] / 10.0).ceil
end
erb :player_stats
end
get '/players/:user/stats/filter/page-:page_number' do
@user = params[:user]
@type = params[:type]
@season = params[:season]
@seasons = @data.get_seasons
@page = params[:page_number].to_i
offset = (@page - 1) * 10
if session[:logged_in]
@summary_stats = @data.get_summary(@user, @season, @type)
@match_stats = @data.get_match(@user, @season, @type, offset)
@page_limit = @match_stats.empty? ? 1 : (@match_stats.first[:entries] / 10.0).ceil
end
erb :player_stats
end
get '/leaderboard/:type/:season/sort-by-:sort' do
@type = params[:type]
@season = params[:season]
@sort = params[:sort]
@seasons = @data.get_seasons
stats = @data.get_leaderboard_stats(@type, @season)
@leader_stats = sort_stats(stats, @sort)
erb :leaderboard
end
get '/leaderboard/filter' do
@type = params[:type]
@season = params[:season]
@sort = params[:sort]
@seasons = @data.get_seasons
stats = @data.get_leaderboard_stats(@type, @season)
@leader_stats = sort_stats(stats, @sort)
erb :leaderboard
end
get '/faq' do
erb :faq
end
# FIX ADD STATS PAGE. MAKE ALL FORM HIDDEN UNTIL SELECT IS PRESSED.
# ADD IF LOGGED_IN / ADMIN PROTECTION TO LOGIN / ADMIN ONLY PAGES
# ALSO ADD INVALID PAGE CATCH