]> iEval git - gruntmaster-data.git/blame - lib/Gruntmaster/Data/Result/Contest.pm
Add editorial and description columns
[gruntmaster-data.git] / lib / Gruntmaster / Data / Result / Contest.pm
CommitLineData
4ed3f8e7
MG
1use utf8;
2package Gruntmaster::Data::Result::Contest;
3
4# Created by DBIx::Class::Schema::Loader
5# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7=head1 NAME
8
be2f7678 9Gruntmaster::Data::Result::Contest - List of contests
4ed3f8e7
MG
10
11=cut
12
13use strict;
14use warnings;
15
16use base 'DBIx::Class::Core';
17
18=head1 TABLE: C<contests>
19
20=cut
21
22__PACKAGE__->table("contests");
23
24=head1 ACCESSORS
25
26=head2 id
27
28 data_type: 'text'
29 is_nullable: 0
30
31=head2 name
32
33 data_type: 'text'
34 is_nullable: 0
35
b596b2a9
MG
36=head2 editorial
37
38 data_type: 'text'
39 is_nullable: 1
40
41HTML fragment placed before the editorial
42
43=head2 description
44
45 data_type: 'text'
46 is_nullable: 1
47
48HTML fragment placed on contest page
49
4ed3f8e7
MG
50=head2 start
51
52 data_type: 'integer'
53 is_nullable: 0
54
be2f7678
MG
55Unix time when contest starts
56
4ed3f8e7
MG
57=head2 stop
58
59 data_type: 'integer'
60 is_nullable: 0
61
be2f7678
MG
62Unix time when contest ends
63
4ed3f8e7
MG
64=head2 owner
65
66 data_type: 'text'
67 is_foreign_key: 1
68 is_nullable: 0
69
70=cut
71
72__PACKAGE__->add_columns(
73 "id",
74 { data_type => "text", is_nullable => 0 },
75 "name",
76 { data_type => "text", is_nullable => 0 },
b596b2a9
MG
77 "editorial",
78 { data_type => "text", is_nullable => 1 },
79 "description",
80 { data_type => "text", is_nullable => 1 },
4ed3f8e7
MG
81 "start",
82 { data_type => "integer", is_nullable => 0 },
83 "stop",
84 { data_type => "integer", is_nullable => 0 },
85 "owner",
86 { data_type => "text", is_foreign_key => 1, is_nullable => 0 },
87);
88
89=head1 PRIMARY KEY
90
91=over 4
92
93=item * L</id>
94
95=back
96
97=cut
98
99__PACKAGE__->set_primary_key("id");
100
101=head1 RELATIONS
102
103=head2 contest_problems
104
105Type: has_many
106
107Related object: L<Gruntmaster::Data::Result::ContestProblem>
108
109=cut
110
111__PACKAGE__->has_many(
112 "contest_problems",
113 "Gruntmaster::Data::Result::ContestProblem",
114 { "foreign.contest" => "self.id" },
115 { cascade_copy => 0, cascade_delete => 0 },
116);
117
adb42d4d
MG
118=head2 contest_statuses
119
120Type: has_many
121
122Related object: L<Gruntmaster::Data::Result::ContestStatus>
123
124=cut
125
126__PACKAGE__->has_many(
127 "contest_statuses",
128 "Gruntmaster::Data::Result::ContestStatus",
129 { "foreign.contest" => "self.id" },
130 { cascade_copy => 0, cascade_delete => 0 },
131);
132
4ed3f8e7
MG
133=head2 jobs
134
135Type: has_many
136
137Related object: L<Gruntmaster::Data::Result::Job>
138
139=cut
140
141__PACKAGE__->has_many(
142 "jobs",
143 "Gruntmaster::Data::Result::Job",
144 { "foreign.contest" => "self.id" },
145 { cascade_copy => 0, cascade_delete => 0 },
146);
147
148=head2 opens
149
150Type: has_many
151
152Related object: L<Gruntmaster::Data::Result::Open>
153
154=cut
155
156__PACKAGE__->has_many(
157 "opens",
158 "Gruntmaster::Data::Result::Open",
159 { "foreign.contest" => "self.id" },
160 { cascade_copy => 0, cascade_delete => 0 },
161);
162
163=head2 owner
164
165Type: belongs_to
166
167Related object: L<Gruntmaster::Data::Result::User>
168
169=cut
170
171__PACKAGE__->belongs_to(
172 "owner",
173 "Gruntmaster::Data::Result::User",
174 { id => "owner" },
9bb39921 175 { is_deferrable => 0, on_delete => "CASCADE", on_update => "NO ACTION" },
4ed3f8e7
MG
176);
177
178=head2 problems
179
180Type: many_to_many
181
182Composing rels: L</contest_problems> -> problem
183
184=cut
185
186__PACKAGE__->many_to_many("problems", "contest_problems", "problem");
187
188
b596b2a9
MG
189# Created by DBIx::Class::Schema::Loader v0.07042 @ 2015-02-14 10:52:58
190# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UTfqBbyhmo0r1HyWtGXidA
4ed3f8e7 191
de7226ca 192use Class::Method::Modifiers qw/after/;
751c70b5
MG
193use List::Util qw/sum/;
194
de7226ca
MG
195after qw/insert update delete/ => sub {
196 my ($self) = @_;
197 Gruntmaster::Data::purge '/ct/';
198 Gruntmaster::Data::purge '/ct/' . $self->id;
199};
200
4a8747ef
MG
201sub is_pending {
202 my ($self, $time) = @_;
203 $self->start > ($time // time)
204}
205
206sub is_finished {
207 my ($self, $time) = @_;
208 $self->stop <= ($time // time)
209}
210
211sub is_running {
212 my ($self, $time) = @_;
213 !$self->is_pending($time) && !$self->is_finished($time)
214}
4ed3f8e7 215
751c70b5
MG
216sub calc_score{
217 my ($mxscore, $time, $tries, $totaltime) = @_;
218 my $score = $mxscore;
219 $time = 0 if $time < 0;
220 $time = 300 if $time > $totaltime;
221 $score = ($totaltime - $time) / $totaltime * $score;
222 $score -= $tries / 10 * $mxscore;
223 $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10;
224 int $score + 0.5
225}
226
227sub standings {
228 my ($self) = @_;
229 my $ct = $self->id;
230
ce3b056a 231 my @problems = map { $_->rawproblem } $self->contest_problems->search({contest => $ct}, {qw/join problem order_by problem.value/});
751c70b5
MG
232 my (%scores, %tries, %opens);
233 $opens{$_->rawproblem, $_->rawowner} = $_ for $self->opens->search({contest => $ct});
c84272d1 234 for my $job ($self->jobs->search({contest => $ct, result => {'!=', undef}}, {qw/order_by me.id prefetch/ => [qw/problem/]})) {
751c70b5
MG
235 my $open = $opens{$job->rawproblem, $job->rawowner};
236 my $time = $job->date - ($open ? $open->time : $self->start);
237 next if $time < 0;
238 my $value = $job->problem->value;
239 my $factor = $job->result ? 0 : 1;
240 $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/s;
241 $scores{$job->rawowner}{$job->rawproblem} = int ($factor * calc_score ($value, $time, $tries{$job->rawowner}{$job->rawproblem}++, $self->stop - $self->start));
242 }
243
1de10923 244 my %user_to_name = map { $_->id => $_->name } $self->result_source->schema->users->all;
751c70b5 245
ce3b056a 246 say STDERR join "\n\n", @problems;
751c70b5
MG
247 my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock)
248 my $user = $_;
249 +{
250 user => $user,
251 user_name => $user_to_name{$user},
252 score => sum (values %{$scores{$user}}),
253 scores => [map { $scores{$user}{$_} // '-'} @problems],
254 }
255 } keys %scores;
256
4aa919c4 257 $st[0]->{rank} = 1 if @st;
751c70b5
MG
258 $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
259 @st
260}
261
4ed3f8e7 2621;
4a8747ef
MG
263
264__END__
265
266=head1 METHODS
267
268=head2 is_pending(I<[$time]>)
269
270Returns true if the contest is pending at time I<$time> (which defaults to C<time>).
271
272=head2 is_finished(I<[$time]>)
273
274Returns true if the contest is finished at time I<$time> (which defaults to C<time>).
275
276=head2 is_running(I<[$time]>)
277
278Returns true if the contest is running at time I<$time> (which defaults to C<time>).
279
280=head1 AUTHOR
281
282Marius Gavrilescu E<lt>marius@ieval.roE<gt>
283
284=head1 COPYRIGHT AND LICENSE
285
286Copyright (C) 2014 by Marius Gavrilescu
287
288This library is free software; you can redistribute it and/or modify
289it under the same terms as Perl itself, either Perl version 5.18.1 or,
290at your option, any later version of Perl 5 you may have available.
291
292
293=cut
This page took 0.059898 seconds and 4 git commands to generate.