-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.rb
384 lines (311 loc) · 10.1 KB
/
models.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
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
# coding: utf-8
require 'active_record'
require 'net/http'
IGNORED_PLAYERS = [
"Kronogenics",
"BlueIsTrue",
"fiordhraoi",
"cheeseburgur101",
"Jey",
"jungletek",
"Hedgy",
"ᕈᘎᑕᒎᗩn ᙡiᗴᒪḰi",
"Venom",
"EpicGamer10075",
"Altii",
"Puςe",
"Floof The Goof",
"Prismo",
"Mishu",
]
module HighScore
def self.format_rank(rank)
"#{rank < 10 ? '0' : ''}#{rank}"
end
def self.spreads(n, type, tabs)
spreads = {}
scores = tabs.empty? ? type.all : type.where(tab: tabs)
scores.each do |elem|
spread = elem.spread(n)
if !spread.nil?
spreads[elem.name] = spread
end
end
spreads
end
def self.ties(type, tabs)
ties = {}
scores = tabs.empty? ? type.all : type.where(tab: tabs)
scores.each do |elem|
tie_count = elem.tie_count
if !tie_count.nil? && tie_count > 3
ties[elem.name] = tie_count
end
end
ties
end
def scores_uri(steam_id)
URI("https://dojo.nplusplus.ninja/prod/steam/get_scores?steam_id=#{steam_id}&steam_auth=&#{self.class.to_s.downcase}_id=#{self.id.to_s}")
end
def replay_uri(steam_id, replay_id)
URI("https://dojo.nplusplus.ninja/prod/steam/get_replay?steam_id=#{steam_id}&steam_auth=&replay_id=#{replay_id}")
end
def get_scores
initial_id = get_last_steam_id
response = Net::HTTP.get(scores_uri(initial_id))
while response == '-1337'
update_last_steam_id
break if get_last_steam_id == initial_id
response = Net::HTTP.get(scores_uri(get_last_steam_id))
end
return nil if response == '-1337'
correct_ties(JSON.parse(response)['scores'])
rescue => e
err("error getting scores: #{e}")
retry
end
def get_replay(replay_id)
initial_id = get_last_steam_id
response = Net::HTTP.get(replay_uri(initial_id, replay_id))
while response == '-1337'
update_last_steam_id
break if get_last_steam_id == initial_id
response = Net::HTTP.get(replay_uri(get_last_steam_id, replay_id))
end
return nil if response == '-1337'
response
rescue => e
err("error getting replay: #{e}")
retry
end
def save_scores(updated)
updated = updated.select { |score| !IGNORED_PLAYERS.include?(score['user_name']) }.uniq { |score| score['user_name'] }
ActiveRecord::Base.transaction do
updated.each_with_index do |score, i|
scores.find_or_create_by(rank: i)
.update(
score: score['score'] / 1000.0,
player: Player.find_or_create_by(name: score['user_name']),
tied_rank: updated.find_index { |s| s['score'] == score['score'] }
)
end
end
end
def update_scores
updated = get_scores
if updated.nil?
# TODO make this use err()
STDERR.puts "[WARNING] [#{Time.now}] failed to retrieve scores from #{scores_uri(get_last_steam_id)}"
return
end
save_scores(updated)
end
def get_replay_info(rank)
updated = get_scores
if updated.nil?
# TODO make this use err()
STDERR.puts "[WARNING] [#{Time.now}] failed to retrieve replay info from #{scores_uri(get_last_steam_id)}"
return
end
updated.select { |score| !IGNORED_PLAYERS.include?(score['user_name']) }.uniq { |score| score['user_name'] }[rank]
end
# Replay data format: Unknown (4b), replay ID (4b), level ID (4b), user ID (4b) and demo data compressed with Zlib.
# Demo data format: Unknown (1b), data length (4b), unknown (4b), frame count (4b), level ID (4b), unknown (13b) and actual demo.
# Demo format: Each byte is one frame, first bit is jump, second is right and third is left. Also, suicide is 0C.
# Note: The first frame is fictional and must be ignored.
def analyze_replay(replay_id)
replay = get_replay(replay_id)
demo = Zlib::Inflate.inflate(replay[16..-1])[30..-1]
analysis = demo.unpack('H*')[0].scan(/../).map{ |b| b.to_i }[1..-1]
end
def correct_ties(score_hash)
score_hash.sort_by{ |s| [-s['score'], s['replay_id']] }
end
def spread(n)
scores.find_by(rank: n).spread unless !scores.exists?(rank: n)
end
def tie_count
scores.take_while{ |s| s.tie }.count
end
def format_scores
scores.map(&:format).join("\n")
end
def difference(old)
scores.map do |score|
oldscore = old.find { |o| o['player']['name'] == score.player.name }
change = nil
if oldscore
change = {rank: oldscore['rank'] - score.rank, score: score.score - oldscore['score']}
end
{score: score, change: change}
end
end
def format_difference(old)
difference(old).map { |o|
c = o[:change]
diff = c ? "#{"++-"[c[:rank] <=> 0]}#{c[:rank].abs}, +#{"%.3f" % [c[:score]]}" : "new"
"#{o[:score].format} (#{diff})"
}.join("\n")
end
end
class Level < ActiveRecord::Base
include HighScore
has_many :scores, as: :highscoreable
has_many :videos, as: :highscoreable
enum tab: [:SI, :S, :SU, :SL, :SS, :SS2]
def format_name
"#{longname} (#{name})"
end
end
class Episode < ActiveRecord::Base
include HighScore
has_many :scores, as: :highscoreable
has_many :videos, as: :highscoreable
enum tab: [:SI, :S, :SU, :SL, :SS, :SS2]
def format_name
"#{name}"
end
def cleanliness
[name, Level.where("UPPER(name) LIKE ?", name.upcase + '%').map{ |l| l.scores[0].score }.sum - scores[0].score - 360]
end
def ownage
owner = scores[0].player.name
[name, Level.where("UPPER(name) LIKE ?", name.upcase + '%').map{ |l| l.scores[0].player.name == owner }.count(true) == 5, owner]
end
end
class Score < ActiveRecord::Base
belongs_to :player
belongs_to :highscoreable, polymorphic: true
belongs_to :level, -> { where(scores: {highscoreable_type: 'Level'}) }, foreign_key: 'highscoreable_id'
belongs_to :episode, -> { where(scores: {highscoreable_type: 'Episode'}) }, foreign_key: 'highscoreable_id'
def self.total_scores(type, tabs, secrets)
tabs = (tabs.empty? ? [:SI, :S, :SL, :SU, :SS, :SS2] : tabs)
tabs = (secrets ? tabs : tabs - [:SS, :SS2])
query = self.where(rank: 0, highscoreable_type: type.to_s)
result = (query.includes(:level).where(levels: {tab: tabs}) + query.includes(:episode).where(episodes: {tab: tabs})).map{ |s| s.score }
[result.sum, result.count]
end
def spread
highscoreable.scores.find_by(rank: 0).score - score
end
def tie
highscoreable.scores.find_by(rank: 0).score == score
end
def format
"#{HighScore.format_rank(rank)}: #{player.name} (#{"%.3f" % [score]})"
end
end
class Player < ActiveRecord::Base
has_many :scores
has_many :rank_histories
has_many :points_histories
has_many :total_score_histories
has_one :user
def self.rankings(&block)
players = Player.includes(:scores).all
players.map { |p| [p, yield(p)] }
.sort_by { |a| -a[1] }
end
def self.histories(type, attrs, column)
hist = type.where(attrs).includes(:player)
ret = Hash.new { |h, k| h[k] = Hash.new { |h, k| h[k] = 0 } }
hist.each do |h|
ret[h.player.name][h.timestamp] += h.send(column)
end
ret
end
def self.rank_histories(rank, type, tabs, ties)
attrs = {rank: rank, ties: ties}
attrs[:highscoreable_type] = type.to_s if type
attrs[:tab] = tabs if !tabs.empty?
self.histories(RankHistory, attrs, :count)
end
def self.score_histories(type, tabs)
attrs = {}
attrs[:highscoreable_type] = type.to_s if type
attrs[:tab] = tabs if !tabs.empty?
self.histories(TotalScoreHistory, attrs, :score)
end
def self.points_histories(type, tabs)
attrs = {}
attrs[:highscoreable_type] = type.to_s if type
attrs[:tab] = tabs if !tabs.empty?
self.histories(PointsHistory, attrs, :points)
end
def scores_by_type_and_tabs(type, tabs)
ret = type ? scores.where(highscoreable_type: type.to_s) : scores
ret = tabs.empty? ? ret : ret.includes(:level).where(levels: {tab: tabs}) + ret.includes(:episode).where(episodes: {tab: tabs})
ret
end
def top_ns(n, type, tabs, ties)
scores_by_type_and_tabs(type, tabs).select do |s|
(ties ? s.tied_rank : s.rank) < n
end
end
def top_n_count(n, type, tabs, ties)
top_ns(n, type, tabs, ties).count
end
def scores_by_rank(type, tabs)
ret = Array.new(20, [])
scores_by_type_and_tabs(type, tabs).group_by(&:rank).sort_by(&:first).each { |rank, scores| ret[rank] = scores }
ret
end
def score_counts(tabs)
{
levels: scores_by_rank(Level, tabs).map(&:length).map(&:to_i),
episodes: scores_by_rank(Episode, tabs).map(&:length).map(&:to_i)
}
end
def missing_top_ns(n, type, tabs, ties)
levels = top_ns(n, type, tabs, ties).map { |s| s.highscoreable.name }
if type
type.where(tab: tabs).where.not(name: levels).pluck(:name)
else
Level.where(tab: tabs).where.not(name: levels).pluck(:name) + Episode.where(tab: tabs).where.not(name: levels).pluck(:name)
end
end
def improvable_scores(type, tabs)
improvable = {}
scores_by_type_and_tabs(type, tabs).each { |s| improvable[s.highscoreable.name] = s.spread }
improvable
end
def points(type, tabs)
scores_by_type_and_tabs(type, tabs).pluck(:rank).map { |rank| 20 - rank }.reduce(0, :+)
end
def average_points(type, tabs)
scores = scores_by_type_and_tabs(type, tabs).pluck(:rank).map { |rank| 20 - rank }
scores.length == 0 ? 0 : scores.reduce(0, :+).to_f / scores.length
end
def total_score(type, tabs)
scores_by_type_and_tabs(type, tabs).pluck(:score).reduce(0, :+)
end
end
class User < ActiveRecord::Base
belongs_to :player
end
class GlobalProperty < ActiveRecord::Base
end
class RankHistory < ActiveRecord::Base
belongs_to :player
enum tab: [:SI, :S, :SU, :SL, :SS, :SS2]
end
class PointsHistory < ActiveRecord::Base
belongs_to :player
enum tab: [:SI, :S, :SU, :SL, :SS, :SS2]
end
class TotalScoreHistory < ActiveRecord::Base
belongs_to :player
enum tab: [:SI, :S, :SU, :SL, :SS, :SS2]
end
class Video < ActiveRecord::Base
belongs_to :highscoreable, polymorphic: true
def format_challenge
return (challenge == "G++" || challenge == "?!") ? challenge : "#{challenge} [#{challenge_code}]"
end
def format_author
return "#{author} (#{author_tag})"
end
def format_description
"#{format_challenge} by #{format_author}"
end
end