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