Commit | Line | Data |
---|---|---|
4ed3f8e7 | 1 | use utf8; |
bbf8209c | 2 | package Gruntmaster::Data; |
4ed3f8e7 MG |
3 | |
4 | # Created by DBIx::Class::Schema::Loader | |
5 | # DO NOT MODIFY THE FIRST PART OF THIS FILE | |
6 | ||
7 | use strict; | |
bbf8209c | 8 | use warnings; |
014ee8a6 | 9 | |
4ed3f8e7 MG |
10 | use base 'DBIx::Class::Schema'; |
11 | ||
12 | __PACKAGE__->load_namespaces; | |
014ee8a6 | 13 | |
f7386aab | 14 | |
4ed3f8e7 MG |
15 | # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-05 13:11:39 |
16 | # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dAEmtAexvUaNXLgYz2rNEg | |
17 | ||
47791059 | 18 | our $VERSION = '5999.000_008'; |
4ed3f8e7 MG |
19 | |
20 | use Lingua::EN::Inflect qw/PL_N/; | |
de625c9b | 21 | use JSON qw/decode_json/; |
15a021b7 | 22 | use List::Util qw/sum/; |
4ed3f8e7 | 23 | use Sub::Name qw/subname/; |
014ee8a6 | 24 | |
de625c9b MG |
25 | use constant PROBLEM_PUBLIC_COLUMNS => [qw/id author writer level name owner private statement timeout olimit value/]; |
26 | use constant USER_PUBLIC_COLUMNS => [qw/id admin name town university level/]; | |
27 | use constant JOBS_PER_PAGE => 10; | |
28 | ||
014ee8a6 | 29 | sub dynsub{ |
fb6a4e3d | 30 | our ($name, $sub) = @_; |
014ee8a6 | 31 | no strict 'refs'; |
fb6a4e3d | 32 | *$name = subname $name => $sub |
014ee8a6 MG |
33 | } |
34 | ||
35 | BEGIN { | |
4ed3f8e7 MG |
36 | for my $rs (qw/contest contest_problem job open problem user/) { |
37 | my $rsname = ucfirst $rs; | |
38 | $rsname =~ s/_([a-z])/\u$1/g; | |
39 | dynsub PL_N($rs) => sub { $_[0]->resultset($rsname) }; | |
40 | dynsub $rs => sub { $_[0]->resultset($rsname)->find($_[1]) }; | |
014ee8a6 MG |
41 | } |
42 | } | |
43 | ||
15a021b7 MG |
44 | use constant LEVEL_VALUES => { |
45 | beginner => 100, | |
46 | easy => 250, | |
47 | medium => 500, | |
48 | hard => 1000, | |
49 | }; | |
50 | ||
51 | sub calc_score{ | |
52 | my ($mxscore, $time, $tries, $totaltime) = @_; | |
53 | my $score = $mxscore; | |
54 | $time = 0 if $time < 0; | |
55 | $time = 300 if $time > $totaltime; | |
56 | $score = ($totaltime - $time) / $totaltime * $score; | |
57 | $score -= $tries / 10 * $mxscore; | |
58 | $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10; | |
59 | int $score + 0.5 | |
60 | } | |
61 | ||
62 | sub standings { | |
63 | my ($self, $ct) = @_; | |
64 | $ct &&= $self->contest($ct); | |
65 | ||
66 | my @problems = map { $_->problem } $self->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/}); | |
67 | my (%scores, %tries); | |
68 | for my $job ($self->jobs->search({contest => $ct && $ct->id}, {order_by => 'id'})) { | |
69 | if ($ct) { | |
70 | my $open = $self->opens->find($ct->id, $job->problem->id, $job->owner->id); | |
71 | my $time = $job->date - ($open ? $open->time : $ct->start); | |
72 | next if $time < 0; | |
73 | my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level}; | |
74 | my $factor = $job->result ? 0 : 1; | |
75 | $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/; | |
76 | $scores{$job->owner->id}{$job->problem->id} = int ($factor * calc_score ($value, $time, $tries{$job->owner->id}{$job->problem->id}++, $ct->stop - $ct->start)); | |
77 | } else { | |
78 | no warnings 'numeric'; | |
79 | $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100) | |
80 | } | |
81 | } | |
82 | ||
83 | my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map { | |
84 | my $user = $_; | |
85 | +{ | |
86 | user => $self->user($user), | |
87 | score => sum (values $scores{$user}), | |
88 | scores => [map { $scores{$user}{$_->id} // '-'} @problems], | |
89 | problems => $ct, | |
90 | } | |
91 | } keys %scores; | |
92 | ||
93 | $st[0]->{rank} = 1; | |
94 | $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; | |
95 | @st | |
96 | } | |
97 | ||
de625c9b MG |
98 | sub user_list { |
99 | my $rs = $_[0]->users->search(undef, {order_by => 'name', columns => USER_PUBLIC_COLUMNS}); | |
100 | [ map +{ $_->get_columns }, $rs->all ] | |
101 | } | |
102 | ||
103 | sub user_entry { | |
104 | my ($self, $id) = @_; | |
105 | +{ $self->users->find($id, {columns => USER_PUBLIC_COLUMNS})->get_columns } | |
106 | } | |
107 | ||
108 | sub problem_list { | |
109 | my ($self, %args) = @_; | |
110 | my $rs = $self->problems->search(undef, {order_by => 'me.name', columns => PROBLEM_PUBLIC_COLUMNS, prefetch => 'owner'}); | |
111 | $rs = $rs->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}, distinct => 1}) unless $args{contest}; | |
112 | $rs = $rs->search({'contest_problems.contest' => $args{contest}}, {join => 'contest_problems'}) if $args{contest}; | |
113 | $rs = $rs->search({'me.owner' => $args{owner}}) if $args{owner}; | |
114 | my %params; | |
115 | $params{contest} = $args{contest} if $args{contest}; | |
116 | for ($rs->all) { | |
117 | $params{$_->level} //= []; | |
118 | push $params{$_->level}, {$_->get_columns, owner_name => $_->owner->name} ; | |
119 | } | |
120 | \%params | |
121 | } | |
122 | ||
123 | sub problem_entry { | |
124 | my ($self, $id, $contest, $user) = @_; | |
125 | my $pb = $self->problems->find($id, {columns => PROBLEM_PUBLIC_COLUMNS, prefetch => 'owner'}); | |
126 | my $running = $contest && $self->contest($contest)->is_running; | |
127 | eval { | |
128 | $self->opens->create({ | |
129 | contest => $contest, | |
130 | problem => $id, | |
131 | owner => $user, | |
132 | time => time, | |
133 | }) | |
134 | } if $running; | |
135 | +{ $pb->get_columns, owner_name => $pb->owner->name, cansubmit => $contest ? $running : 1 } | |
136 | } | |
137 | ||
138 | sub contest_list { | |
139 | my ($self, %args) = @_; | |
140 | my $rs = $self->contests->search(undef, {order_by => {-desc => 'start'}, prefetch => 'owner'}); | |
141 | $rs = $rs->search({owner => $args{owner}}) if $args{owner}; | |
142 | my %params; | |
143 | for ($rs->all) { | |
144 | my $state = $_->is_pending ? 'pending' : $_->is_running ? 'running' : 'finished'; | |
145 | $params{$state} //= []; | |
146 | push $params{$state}, { $_->get_columns, started => !$_->is_pending, owner_name => $_->owner->name }; | |
147 | } | |
148 | \%params | |
149 | } | |
150 | ||
151 | sub contest_entry { | |
152 | my ($self, $id) = @_; | |
153 | my $ct = $self->contest($id); | |
154 | +{ $ct->get_columns, started => !$ct->is_pending, owner_name => $ct->owner->name } | |
155 | } | |
156 | ||
157 | sub job_list { | |
158 | my ($self, %args) = @_; | |
159 | $args{page} //= 1; | |
160 | my $rs = $self->jobs->search(undef, {order_by => {-desc => 'me.id'}, prefetch => ['problem', 'owner'], rows => JOBS_PER_PAGE, offset => ($args{page} - 1) * JOBS_PER_PAGE}); | |
526b9e80 MG |
161 | $rs = $rs->search({'me.owner' => $args{owner}}) if $args{owner}; |
162 | $rs = $rs->search({contest => $args{contest}}) if $args{contest}; | |
163 | $rs = $rs->search({problem => $args{problem}}) if $args{problem}; | |
de625c9b MG |
164 | [map { |
165 | my %params = $_->get_columns; | |
166 | $params{owner_name} = $_->owner->name; | |
167 | $params{problem_name} = $_->problem->name; | |
168 | $params{results} &&= decode_json $params{results}; | |
169 | $params{size} = length $params{source}; | |
170 | delete $params{source}; | |
171 | \%params | |
172 | } $rs->all] | |
173 | } | |
174 | ||
175 | sub job_entry { | |
176 | my ($self, $id) = @_; | |
177 | my $job = $self->jobs->find($id, {prefetch => ['problem', 'owner']}); | |
178 | my %params = $job->get_columns; | |
179 | $params{owner_name} = $job->owner->name; | |
180 | $params{problem_name} = $job->problem->name; | |
181 | $params{results} &&= decode_json $params{results}; | |
182 | $params{size} = length $params{source}; | |
183 | delete $params{source}; | |
184 | \%params | |
185 | } | |
186 | ||
f7386aab | 187 | 1; |
4a8747ef MG |
188 | |
189 | __END__ | |
190 | ||
191 | =encoding utf-8 | |
192 | ||
193 | =head1 NAME | |
194 | ||
195 | Gruntmaster::Data - Gruntmaster 6000 Online Judge -- database interface and tools | |
196 | ||
197 | =head1 SYNOPSIS | |
198 | ||
199 | my $db = Gruntmaster::Data->connect('dbi:Pg:'); | |
cbb36c78 MG |
200 | |
201 | my $problem = $db->problem('my_problem'); | |
202 | $problem->update({timeout => 2.5}); # Set time limit to 2.5 seconds | |
203 | $problem->rerun; # And rerun all jobs for this problem | |
204 | ||
205 | # ... | |
206 | ||
207 | my $contest = $db->contests->create({ # Create a new contest | |
208 | id => 'my_contest', | |
209 | name => 'My Awesome Contest', | |
210 | start => time + 100, | |
211 | end => time + 1900, | |
212 | }); | |
213 | $db->contest_problems->create({ # Add a problem to the contest | |
214 | contest => 'my_contest', | |
215 | problem => 'my_problem', | |
216 | }); | |
217 | ||
218 | say 'The contest has not started yet' if $contest->is_pending; | |
219 | ||
220 | # ... | |
221 | ||
222 | my @jobs = $db->jobs->search({contest => 'my_contest', owner => 'MGV'})->all; | |
223 | $_->rerun for @jobs; # Rerun all jobs sent by MGV in my_contest | |
4a8747ef MG |
224 | |
225 | =head1 DESCRIPTION | |
226 | ||
cbb36c78 MG |
227 | Gruntmaster::Data is the interface to the Gruntmaster 6000 database. Read the L<DBIx::Class> documentation for usage information. |
228 | ||
229 | In addition to the typical DBIx::Class::Schema methods, this module contains several convenience methods: | |
230 | ||
231 | =over | |
232 | ||
233 | =item contests | |
234 | ||
235 | Equivalent to C<< $schema->resultset('Contest') >> | |
236 | ||
237 | =item contest_problems | |
238 | ||
239 | Equivalent to C<< $schema->resultset('ContestProblem') >> | |
240 | ||
241 | =item jobs | |
242 | ||
243 | Equivalent to C<< $schema->resultset('Job') >> | |
244 | ||
245 | =item problems | |
246 | ||
247 | Equivalent to C<< $schema->resultset('Problem') >> | |
248 | ||
249 | =item users | |
250 | ||
251 | Equivalent to C<< $schema->resultset('User') >> | |
252 | ||
253 | =item contest($id) | |
254 | ||
255 | Equivalent to C<< $schema->resultset('Contest')->find($id) >> | |
256 | ||
257 | =item job($id) | |
258 | ||
259 | Equivalent to C<< $schema->resultset('Job')->find($id) >> | |
260 | ||
261 | =item problem($id) | |
262 | ||
263 | Equivalent to C<< $schema->resultset('Problem')->find($id) >> | |
264 | ||
265 | =item user($id) | |
266 | ||
267 | Equivalent to C<< $schema->resultset('User')->find($id) >> | |
268 | ||
de625c9b MG |
269 | =item user_list |
270 | ||
271 | Returns a list of users as an arrayref containing hashrefs. | |
272 | ||
273 | =item user_entry($id) | |
274 | ||
275 | Returns a hashref with information about the user $id. | |
276 | ||
277 | =item problem_list([%args]) | |
278 | ||
279 | Returns a list of problems grouped by level. A hashref with levels as keys. | |
280 | ||
281 | Takes the following arguments: | |
282 | ||
283 | =over | |
284 | ||
285 | =item owner | |
286 | ||
287 | Only show problems owned by this user | |
288 | ||
289 | =item contest | |
290 | ||
291 | Only show problems in this contest | |
292 | ||
293 | =back | |
294 | ||
295 | =item problem_entry($id, [$contest, $user]) | |
296 | ||
297 | Returns a hashref with information about the problem $id. If $contest and $user are present, problem open data is updated. | |
298 | ||
299 | =item contest_list([%args]) | |
300 | ||
301 | Returns a list of contests grouped by state. A hashref with the following keys: | |
302 | ||
303 | =over | |
304 | ||
305 | =item pending | |
306 | ||
307 | An arrayref of hashrefs representing pending contests | |
308 | ||
309 | =item running | |
310 | ||
311 | An arrayref of hashrefs representing running contests | |
312 | ||
313 | =item finished | |
314 | ||
315 | An arrayref of hashrefs representing finished contests | |
316 | ||
317 | =back | |
318 | ||
319 | Takes the following arguments: | |
320 | ||
321 | =over | |
322 | ||
323 | =item owner | |
324 | ||
325 | Only show contests owned by this user. | |
326 | ||
327 | =back | |
328 | ||
329 | =item contest_entry($id) | |
330 | ||
331 | Returns a hashref with information about the contest $id. | |
332 | ||
333 | =item job_list([%args]) | |
334 | ||
335 | Returns a list of jobs as an arrayref containing hashrefs. Takes the following arguments: | |
336 | ||
337 | =over | |
338 | ||
339 | =item owner | |
340 | ||
341 | Only show jobs submitted by this user. | |
342 | ||
343 | =item contest | |
344 | ||
345 | Only show jobs submitted in this contest. | |
346 | ||
347 | =item problem | |
348 | ||
349 | Only show jobs submitted for this problem. | |
350 | ||
351 | =item page | |
352 | ||
353 | Show this page of results. Defaults to 1. Pages have 10 entries, and the first page has the most recent jobs. | |
354 | ||
355 | =back | |
356 | ||
357 | =item job_entry($id) | |
358 | ||
359 | Returns a hashref with information about the job $id. | |
360 | ||
cbb36c78 | 361 | =back |
4a8747ef MG |
362 | |
363 | =head1 AUTHOR | |
364 | ||
365 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
366 | ||
367 | =head1 COPYRIGHT AND LICENSE | |
368 | ||
369 | Copyright (C) 2014 by Marius Gavrilescu | |
370 | ||
9d2e740e MG |
371 | This library is free software; you can redistribute it and/or modify |
372 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
373 | at your option, any later version of Perl 5 you may have available. | |
4a8747ef MG |
374 | |
375 | ||
376 | =cut |