-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRakefile
286 lines (246 loc) · 9.26 KB
/
Rakefile
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
require 'rubygems'
require "bundler/setup"
require 'date'
require './lib/junethack'
require 'trophyscore'
require 'normalize_death'
require 'sync'
require 'rake/dsl_definition'
require 'rake'
$db_access = Sync.new
load File.expand_path('spec/spec.rake')
# default database is development
ENV['RACK_ENV'] = "development" unless ENV['RACK_ENV']
require 'database'
namespace :update do
i = 0
desc "recalculate scores"
task :scores do
(repository.adapter.select "select version,id,ascended from games where user_id is not null order by endtime").each {|game|
i += 1
puts "#{i} #{game.id} #{game.version}"
update_scores(Game.get(game.id))
}
end
desc "recalculate competition scores"
task :user_competition do
(repository.adapter.select "select version,id,ascended from games where user_id is not null and ascended='t' order by endtime").each {|game|
i += 1
puts "#{i} #{game.version}"
update_competition_scores_ascended(Game.get(game.id))
}
end
# only update nconducts field
task :nconducts do
i = 0
Game.all.each do |game|
game.nconducts = (Integer(game.conduct) & 4095).to_s(2).count("1")
i += 1
puts i
game.save! # only change field and don't call hooks
end
end
task :killed_medusa do
i = 0
Game.transaction do
Game.all.each do |game|
game.killed_medusa = game.defeated_medusa? ? 1 : 0
i += 1
puts i if i % 100 == 0
game.save! # only change field and don't call hooks
end
end
end
desc "re-rank clans"
task :clan_winner do
rank_clans
score_clans
end
desc "recalculate clan scores"
task :clan_scores do
Clan.all.each {|clan|
puts clan.name
game = Game.all(user_id: clan.users.map(&:id)).last
update_clan_scores(game) if game
}
end
task :normalize_deaths do
(repository.adapter.select "select version,id,ascended from games where user_id is not null order by endtime").each {|game|
i += 1
puts "#{i} #{game.version}"
local_normalize_death(Game.get(game.id))
}
end
task :all_stuff do
(repository.adapter.select "select version,id,ascended from games where user_id is not null and ascended='t' order by endtime").each {|game|
i += 1
puts "#{i} #{game.version}"
update_all_stuff(Game.get(game.id))
}
end
task :fix_gnollhack_entering_sokoban do
i = 0
variant = helper_get_variant_for 'gnollhack'
sql = "SELECT version, id, achieve, user_id FROM games WHERE version = 'gnl' AND user_id IS NOT NULL ORDER BY id"
(repository.adapter.select(sql)).each { |game|
i += 1
achievements = []
achievements << [:entered_sokoban, "entered Sokoban", 2]
achievements.each { |achievement|
icon = "#{achievement[0].to_s}.png"
if (game.achieve.to_i(16) & 0x100000)
puts "#{i} #{game.id} #{game.version} #{User.first(id: game.user_id).login}"
Trophy.first_or_create variant: variant, trophy: achievement[0], text: achievement[1], icon: icon, row: achievement[2]
end
}
update_scores(Game.get(game.id))
}
end
end
namespace :db do
desc "fetch new xlogfile entries from game servers"
task :get_games do
require 'fetch_games'
fetch_all
end
# Create registered users by using player names.
# Combine players on different servers by the common name.
task :create_users_heuristically do
(repository.adapter.select "select distinct name,server_id from games where user_id is null").each {|u|
name = u['name']
server = Server.get(u['server_id'])
puts "#{name}, #{server.name}"
user = User.first_or_create(:login => name)
account = Account.first_or_create(:user => user, :server => server, :name => name, :verified => true)
Game.all(:name => name, :server => server).update(:user_id => user.id) if account
repository.adapter.execute "UPDATE start_scummed_games set user_id = ? where name = ? and server_id = ?", user.id, name, server.id
}
end
desc "start irb with database connected"
task :irb do
require 'irb'
ARGV.clear
IRB.start
end
desc "add a server"
task :add_server, :name, :variant, :url, :xlogurl, :configfileurl do |t, args|
puts "add server got #{args.inspect}"
Server.create(:name => args[:name], :variant => args[:variant], :url => args[:url], :xlogurl => args[:xlogurl], :configfileurl => args[:configfileurl])
Trophy.check_trophies_for_variant args[:variant]
end
desc "reset a server's xlogfile modification date"
task :reset_server, :name do |t, args|
Server.all(name: args[:name]).update(xloglastmodified: "Sat Jan 01 00:00:00 UTC 2000",
xlogcurrentoffset: 0)
puts Server.all(name: args[:name]).inspect
end
desc "reset all servers' xlogfile modification date"
task :reset_all_servers do
Server.all.update(xloglastmodified: "Sat Jan 01 00:00:00 UTC 2000",
xlogcurrentoffset: 0)
end
desc "change a user's password"
task :change_password, :user, :password do |t, args|
user = User.first(:login => args[:user])
puts "User #{args[:user]} not found!" if not user
puts "No password given!" if not args[:password]
if user and args[:password] then
user.password = args[:password]
user.save
end
end
desc "post-mortem statistics"
task :statistics do
puts "Some boring statistics:"
puts
puts "#{User.count} players registered on the server,"
puts "#{User.all(:accounts.not => nil).count} linked their account with the public servers,"
puts "and #{Game.all(:fields => [:user_id]).map {|g| g.user_id}.uniq.count} actually played at least one game."
puts
puts "#{Game.all(:user_id.not => nil).count} games were played on all #{Server.count} public servers during the tournament by registered users,"
puts "#{Game.count} were played by all players including those not taking part in the tournament."
puts
games_ascended = Game.all(:conditions => [ "user_id is not null and ascended='t'" ])
puts "#{games_ascended.collect {|g| g.user_id}.uniq.count} different players ascended a total of #{games_ascended.count} games."
puts
puts "Tournament games by variant"
$variant_order.each do |v|
puts "#{$variants_mapping[v]}: #{Game.all(:conditions => [ "user_id > 0 and version = '#{v}'" ]).count}"
end
end
end
namespace :news do
desc "add a new news entry"
task :add, :html_snippet, :url, :publish_at do |t, args|
ARGV.each {|a| task a.to_sym do ; end }
news = News.new
news.html = args[:html_snippet] || ARGV[1]
news.url = args[:url] || ARGV[2]
if args[:publish_at] && !args[:publish_at].empty?
news.updated_at = news.created_at = DateTime.parse(args[:publish_at])
end
news.save
end
desc "delete a new news entry"
task :delete, :id do |t, args|
News.get(args[:id]).destroy
end
desc "update a new news entry"
task :update, :id, :html_snippet, :url do |t, args|
ARGV.each {|a| task a.to_sym do ; end }
news = News.get(args[:id])
news.html = args[:html_snippet] || ARGV[1]
news.url = args[:url] || ARGV[2]
news.save
end
desc "list all news entries"
task :list do |t, args|
news = News.all order: [ :created_at.desc ]
news.each {|n| puts n.inspect}
end
end
namespace :run do
desc "start maintenance mode"
task :maintenance do
require 'maintenance'
Sinatra::Application.run!
end
desc "start server"
task :server do
require 'sinatra_server'
# write the current process id to a file
File.open("junethack.pid", "w") {|f| f.puts(Process.pid) }
Signal.trap(0, proc { File.delete "junethack.pid" })
Sinatra::Application.run!
end
end
namespace :test do
desc 'Import a local xlogfile'
task :import, :file, :server do |t, args|
require 'parse'
server = Server.first(name: args[:server])
lines = File.open(args[:file]).readlines
lines.each {|line|
hgame = XLog.parse_xlog line.force_encoding(Encoding::UTF_8).encode("utf-8", invalid: :replace)
game = Game.create({server: server}.merge(hgame))
account = Account.first(name: hgame["name"], server_id: server.id)
game.update(user_id: account.user_id) if account
}
end
desc 'Verify all supported variants have a correct setup'
task :variants do
versions = repository.adapter.select 'SELECT DISTINCT version FROM games'
versions.each {|version|
if $variants_mapping[version].nil?
puts "Version #{version} has unknown mapping!"
end
if version != 'NH-1.3d' && !$variant_order.include?(version)
puts "Version #{version} has no known order!"
end
if Trophy.count(variant: version) == 0
puts "Version #{version} has no trophies!"
end
}
end
end
task :default => ["run:server"]