Bump version and update Changes
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
CommitLineData
4ed3f8e7 1use utf8;
bbf8209c 2package Gruntmaster::Data;
4ed3f8e7
MG
3
4# Created by DBIx::Class::Schema::Loader
5# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7use strict;
bbf8209c 8use warnings;
014ee8a6 9
4ed3f8e7
MG
10use 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 18our $VERSION = '5999.000_008';
4ed3f8e7
MG
19
20use Lingua::EN::Inflect qw/PL_N/;
de625c9b 21use JSON qw/decode_json/;
15a021b7 22use List::Util qw/sum/;
4ed3f8e7 23use Sub::Name qw/subname/;
014ee8a6 24
de625c9b
MG
25use constant PROBLEM_PUBLIC_COLUMNS => [qw/id author writer level name owner private statement timeout olimit value/];
26use constant USER_PUBLIC_COLUMNS => [qw/id admin name town university level/];
27use constant JOBS_PER_PAGE => 10;
28
014ee8a6 29sub dynsub{
fb6a4e3d 30 our ($name, $sub) = @_;
014ee8a6 31 no strict 'refs';
fb6a4e3d 32 *$name = subname $name => $sub
014ee8a6
MG
33}
34
35BEGIN {
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
44use constant LEVEL_VALUES => {
45 beginner => 100,
46 easy => 250,
47 medium => 500,
48 hard => 1000,
49};
50
51sub 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
62sub 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
98sub user_list {
99 my $rs = $_[0]->users->search(undef, {order_by => 'name', columns => USER_PUBLIC_COLUMNS});
100 [ map +{ $_->get_columns }, $rs->all ]
101}
102
103sub user_entry {
104 my ($self, $id) = @_;
105 +{ $self->users->find($id, {columns => USER_PUBLIC_COLUMNS})->get_columns }
106}
107
108sub 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
123sub 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
138sub 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
151sub 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
157sub 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
175sub 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 1871;
4a8747ef
MG
188
189__END__
190
191=encoding utf-8
192
193=head1 NAME
194
195Gruntmaster::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
227Gruntmaster::Data is the interface to the Gruntmaster 6000 database. Read the L<DBIx::Class> documentation for usage information.
228
229In addition to the typical DBIx::Class::Schema methods, this module contains several convenience methods:
230
231=over
232
233=item contests
234
235Equivalent to C<< $schema->resultset('Contest') >>
236
237=item contest_problems
238
239Equivalent to C<< $schema->resultset('ContestProblem') >>
240
241=item jobs
242
243Equivalent to C<< $schema->resultset('Job') >>
244
245=item problems
246
247Equivalent to C<< $schema->resultset('Problem') >>
248
249=item users
250
251Equivalent to C<< $schema->resultset('User') >>
252
253=item contest($id)
254
255Equivalent to C<< $schema->resultset('Contest')->find($id) >>
256
257=item job($id)
258
259Equivalent to C<< $schema->resultset('Job')->find($id) >>
260
261=item problem($id)
262
263Equivalent to C<< $schema->resultset('Problem')->find($id) >>
264
265=item user($id)
266
267Equivalent to C<< $schema->resultset('User')->find($id) >>
268
de625c9b
MG
269=item user_list
270
271Returns a list of users as an arrayref containing hashrefs.
272
273=item user_entry($id)
274
275Returns a hashref with information about the user $id.
276
277=item problem_list([%args])
278
279Returns a list of problems grouped by level. A hashref with levels as keys.
280
281Takes the following arguments:
282
283=over
284
285=item owner
286
287Only show problems owned by this user
288
289=item contest
290
291Only show problems in this contest
292
293=back
294
295=item problem_entry($id, [$contest, $user])
296
297Returns 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
301Returns a list of contests grouped by state. A hashref with the following keys:
302
303=over
304
305=item pending
306
307An arrayref of hashrefs representing pending contests
308
309=item running
310
311An arrayref of hashrefs representing running contests
312
313=item finished
314
315An arrayref of hashrefs representing finished contests
316
317=back
318
319Takes the following arguments:
320
321=over
322
323=item owner
324
325Only show contests owned by this user.
326
327=back
328
329=item contest_entry($id)
330
331Returns a hashref with information about the contest $id.
332
333=item job_list([%args])
334
335Returns a list of jobs as an arrayref containing hashrefs. Takes the following arguments:
336
337=over
338
339=item owner
340
341Only show jobs submitted by this user.
342
343=item contest
344
345Only show jobs submitted in this contest.
346
347=item problem
348
349Only show jobs submitted for this problem.
350
351=item page
352
353Show 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
359Returns a hashref with information about the job $id.
360
cbb36c78 361=back
4a8747ef
MG
362
363=head1 AUTHOR
364
365Marius Gavrilescu E<lt>marius@ieval.roE<gt>
366
367=head1 COPYRIGHT AND LICENSE
368
369Copyright (C) 2014 by Marius Gavrilescu
370
9d2e740e
MG
371This library is free software; you can redistribute it and/or modify
372it under the same terms as Perl itself, either Perl version 5.18.1 or,
373at your option, any later version of Perl 5 you may have available.
4a8747ef
MG
374
375
376=cut
This page took 0.033361 seconds and 4 git commands to generate.