-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.rb
323 lines (253 loc) · 8.1 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
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
require 'sinatra'
require 'json'
require 'base64'
require './lib/Github'
# Public: github.com API key used in order to have higher rate limit
GITHUB_API_KEY = ENV.fetch('GITHUB_API_KEY')
COOKIE_NAME = 'ach'.freeze
set :show_exceptions, false
before do
# variables used to set various achievements
@achievements = Array.new
begin
@github_client = Github.new(GITHUB_API_KEY)
rescue RateLimitError
redirect '/errors/limit'
end
@achievement_list = {
zuse: 'FOUND ZUSE',
grid: 'THE GRID',
iso: 'ISOMORPHIC',
tron: 'TRON LIVES',
faith: 'FAITH IN USERS',
mcp: 'END OF LINE',
purge: 'PURGE',
betrayal: 'BETRAYAL'
}
# if all achievements have been unlocked
# set the last achievement
all_achievements = true
@achievement_list.values.each do |item|
next if achieved?(item)
all_achievements = false
end
if all_achievements && !achieved?('THE CREATOR')
@achievements.push('THE CREATOR')
save(@achievements)
end
end
not_found do
erb :not_found, :layout => :layout2
end
error do
@error_message = env['sinatra.error'].message
erb :error
end
get '/errors/limit' do
erb :error_rate_limit
end
before %r{\/errors\/(input|reponame|ragequit)} do
unless achieved?(@achievement_list[:zuse])
@achievements.push(@achievement_list[:zuse])
save(@achievements)
end
end
get '/errors/input' do
erb :error_input
end
get '/errors/reponame' do
erb :error_program
end
get '/errors/ragequit' do
erb :error_rage_quit
end
before '/*' do
unless achieved?(@achievement_list[:grid])
@achievements.push(@achievement_list[:grid])
save(@achievements)
end
end
get '/' do
if params[:first_repo] && params[:second_repo]
first_name, first_repo = get_github(sanitize_input(params[:first_repo]))
second_name, second_repo = get_github(sanitize_input(params[:second_repo]))
if first_name && first_repo && second_name && second_repo
redirect "/#{first_name}/#{first_repo}/vs/#{second_name}/#{second_repo}"
else
redirect '/errors/input'
end
else
erb :index
end
end
get '/:first_name/:first_repo/vs/:second_name/:second_repo/?' do
@players = Array.new
# parse GET parameters
first_name, first_repo = params[:first_name], params[:first_repo]
second_name, second_repo = params[:second_name], params[:second_repo]
# check if user enters the same repo twice
if first_name == second_name && first_repo == second_repo
redirect '/errors/ragequit'
end
@players.push(name: first_name, repo: first_repo)
@players.push(name: second_name, repo: second_repo)
@players.each do |player|
begin
@github_client.set_repository(player[:name], player[:repo])
player[:score] = @github_client.score
player[:avatar], player[:disk] = @github_client.generate_avatar
player[:branches], player[:contribs] = @github_client.additional_disk_info
rescue RepoNotFoundError
redirect '/errors/reponame'
end
end
# compare two scores to choose the higher for winner
# if tie then randomly choose the winner
# the winner is identified by an index between 0 and 1
# this index is used to access to the item in @players
# @winner_index and @loser_index
# will be also used in views file fight.erb
if @players[0][:score] > @players[1][:score]
@winner_index = 0
elsif @players[0][:score] < @players[1][:score]
@winner_index = 1
else
@winner_index = Random.rand(2)
end
# check if new achievements have been unlocked
# okay, the following code block is just pure shit
# I'll update it one day, I swear!
@loser_index = (@winner_index + 1) % 2
@players.each_with_index do |player, i|
if i == @winner_index
if (player[:avatar] == 'rinzler.png' || player[:avatar] == 'tron_uprising.png') &&
!achieved?(@achievement_list[:tron])
@achievements.push(@achievement_list[:tron])
elsif player[:avatar] == 'clu2.png' &&
(@players[@loser_index][:avatar] == 'rinzler_converted.png' ||
@players[@loser_index][:avatar] == 'tron.png' ||
@players[@loser_index][:avatar] == 'tron_uprising.png') &&
!achieved?(@achievement_list[:betrayal])
@achievements.push(@achievement_list[:betrayal])
end
# checking ISO avatars (for github members repos)
if iso_avatar?(player[:avatar]) && !achieved?(@achievement_list[:iso])
@achievements.push(@achievement_list[:iso])
elsif (!iso_avatar?(player[:avatar]) &&
iso_avatar?(@players[@loser_index][:avatar])) && !achieved?(@achievement_list[:purge])
@achievements.push(@achievement_list[:purge])
end
else # check loser of the current fight
if player[:avatar] == 'mcp.gif' && !achieved?(@achievement_list[:mcp])
@achievements.push(@achievement_list[:mcp])
elsif player[:avatar] == 'jarvis.png' && !achieved?(@achievement_list[:faith])
@achievements.push(@achievement_list[:faith])
end
end
end
# if new achievements have been unlocked save
# the list inside the main cookie
if @achievements.size != 0
save(@achievements)
end
# yes, I warned you it was shit...
erb :fight
end
get '/avatar/:user/:repository' do
user, repository = params[:user], params[:repository]
begin
@github_client.set_repository(user, repository)
avatar, disk = @github_client.generate_avatar
send_file "public/img/characters/#{avatar}"
rescue RepoNotFoundError
send_file "public/img/characters-extra/castor.gif", :type => :gif
end
end
get '/avatar/disk/:user/:repository' do
user, repository = params[:user], params[:repository]
begin
@github_client.set_repository(user, repository)
avatar, disk = @github_client.generate_avatar
send_file "public/img/disk/#{disk}"
rescue RepoNotFoundError
send_file "public/img/characters-extra/castor.gif", :type => :gif
end
end
helpers do
def bake(key, content, expire = 290_304_000)
response.set_cookie(
key,
value: content,
expires: Time.now + expire,
path: '/'
)
end
def save(new_list)
# save the current unlocked achievements list
# and update the list with new achievements
current_list = fetch
current_list += new_list
# save the list inside a json array and update the cookie
json_content = current_list.to_json
bake(COOKIE_NAME, Base64.encode64(json_content))
end
def achieved?(achievement)
list = fetch
list.include?(achievement)
end
def fetch
achievements = Array.new
cookie = request.cookies[COOKIE_NAME]
json_array = Base64.decode64(cookie.to_s)
achievements = JSON.parse(json_array) if json_array != ''
achievements
end
def iso_avatar?(avatar)
isos_avatar = %w(
ophelia.png giles.png quorra.png ada.png calchas.png
young_iso_male.png young_iso_female.png old_iso_male.png
iso_female_1.png iso_male_1.png iso_male_2.png
iso_male_3.png iso_female_3.png
)
iso_avatar = false
isos_avatar.each do |iso|
next unless iso == avatar
iso_avatar = true
end
iso_avatar
end
end
# Public: Sanitize input from a github url specified via argument.
# Kinldy provided by github.com/leereilly/github-high-scores.
#
# url - url to sanitize
#
# Returns the url sanitized
def sanitize_input(url)
url = url.downcase
# Special rules for Github URLs starting with 'github.com'
if url[0..9] == 'github.com'
url = 'https://www.github.com' + url[9..url.size]
# Special rules for Github URLs starting with 'www.github.com'
elsif url[0..13] == 'www.github.com'
url = 'https://www.github.com' + url[13..url.size]
end
# Special rules for Github URLs ending in 'git'
if url[-4,4] == '.git'
url = url[0..-5]
end
url = url.gsub('http://', 'https://')
url = url.gsub('[email protected]:', 'https://www.github.com/')
url = url.gsub('git://', 'https://www.')
# If someone just passes in user/repo e.g. leereilly/leereilly.net
tokens = url.split('/')
if tokens.size == 2
url = "https://www.github.com/#{tokens[0]}/#{tokens[1]}"
end
url
end
def get_github(sanitized_github_url)
user = sanitized_github_url.split('/')[3]
repo = sanitized_github_url.split('/')[4]
return user, repo
end