forked from hakusaro/the-magi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.rb
222 lines (193 loc) · 6.04 KB
/
crawl.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
require 'nokogiri'
require 'open-uri'
require 'pry'
require 'mongo_mapper'
require_relative 'Score'
MongoMapper.setup({'production' => {'uri' => ENV['MONGODB_URI']}}, 'production')
def crawl_now
begin
@document = Nokogiri::HTML(open("http://54.243.195.23/"))
rescue
puts "Failed to connect to CCS's live output system."
return nil
end
@nodeset = @document.css("tr.clickable") # @document.xpath("//tr")
@nodeset.each do |row|
if row.children[0].children[0].to_s.include? "CPOC" # CPOC is the CyberPatriot Operation Center (CPOC, CPOC_gol, CPOC_pla, CPOC_sil)
next
end
team_score = {
:id => row.children[0].children[0].to_s,
:division => row.children[1].children[0].to_s,
:state => row.children[2].children[0].to_s,
:images => row.children[4].children[0].to_s.to_i,
:time => row.children[5].children[0].to_s,
:score => row.children[6].children[0].to_s.to_i,
:warnings => row.children[7].children[0].to_s,
:tier => row.children[3].children[0].to_s
}
division = team_score[:division].downcase
if division.include?('open')
division = 'open'
elsif division.include?('service')
division = 'all-service'
elsif division.include?('middle')
division = 'ms'
end
score = Score.where({:team_id => team_score[:id]}).first
if (score == nil)
new_score = Score.new({
:team_id => team_score[:id],
:division => division,
:r1_score => 0,
:r2_score => 0,
:r3_score => team_score[:score],
:time => team_score[:time],
:warnings => team_score[:warnings],
:images => team_score[:images],
:state => team_score[:state],
:total_score => team_score[:score],
:tier => team_score[:tier]
})
if new_score.save
puts "Created a new team #{team_score[:id]} in #{team_score[:division]}."
else
puts "Failed to create new team #{team_score[:id]}"
end
else
if score.division != division
puts "Redefined #{score.team_id} from division #{score.division} to #{division}."
end
score.division = division
score.state = team_score[:state]
score.images = team_score[:images]
score.time = team_score[:time]
score.r3_score = team_score[:score]
score.warnings = team_score[:warnings]
score.total_score = team_score[:score]
score.tier = team_score[:tier]
if (score.save)
# puts "Team is #{team_score[:id]}. They're at #{team_score[:score]} in #{team_score[:state]}'s #{team_score[:division]} (#{team_score[:tier]}) division."
else
puts "Failed to save #{team_score[:id]}."
end
end
end
puts "Crawl run ok."
return true
end
def calculate_state_rank
locations = []
divisions = ['open', 'all-serivce']
tiers = ['Silver', 'Gold', 'Platinum']
File.readlines('location_list.txt').each do |line|
locations.push(line.strip!)
end
# Calculate top ranked teams.
locations.each do |location|
divisions.each do |division|
tiers.each do |tier|
score_count = Score.where({:division => division, :state => location, :tier => tier}).count
# puts "Calculating #{location} / #{division} / #{tier} (#{score_count} teams)."
scores = Score.where({:division => division, :state => location, :tier => tier}).sort(:r3_score.desc)
advancement = 3
rank = 1
scores.each do |score|
score.top3 = false
if score.r3_score == 0 || score.r3_score == nil
score.warned_multi = false
score.warned_time = false
score.top3 = false
score.wildcard = false
score.state_rank = nil
score.save
next
end
if advancement > 0
score.top3 = true
else
score.top3 = false
end
advancement -= 1
score.state_rank = rank
rank += 1
if score.warnings != nil
if score.warnings.include?('M')
score.warned_multi = true
else
score.warned_multi = false
end
if score.warnings.include?('T')
score.warned_time = true
else
score.warned_time = false
end
else
score.warned_multi = false
score.warned_time = false
end
score.save
end
end
end
# Calculate remaining wildcard teams
divisions.each do |division|
tiers.each do |tier|
wildcards = 12
scores = Score.where({:division => division, :tier => tier}).sort(:r3_score.desc)
scores.each do |score|
if wildcards == 0
break
end
if (score.r3_score == 0 || score.r3_score == nil)
score.top3 = false
score.wildcard = false
score.save
next
end
if (wildcards > 0 && score.top3 == false)
score.wildcard = true
wildcards -= 1
score.save
end
end
end
end
end
puts "Calculation of state ranks ok."
end
def calculate_platinums
divisions = ['open', 'all-service']
divisions.each do |division|
score_count = Score.where({:division => division}).count
scores = Score.where({:division => division}).sort(:total_score.desc)
plat_slots = (score_count * 0.3).round(0)
plat_slots_save = plat_slots
scores.each do |score|
if plat_slots > 0
score.platinum = true
plat_slots -= 1
else
score.platinum = false
end
if score.warnings != nil
if score.warnings.include?('M')
score.warned_multi = true
else
score.warned_multi = false
end
if score.warnings.include?('T')
score.warned_time = true
else
score.warned_time = false
end
else
score.warned_multi = false
score.warned_time = false
end
score.save
end
end
puts "Calculation of platinums ok."
end
# binding.pry