]> iEval git - gruntmaster-data.git/blob - lib/Gruntmaster/Data/Result/Contest.pm
1da4badddb7cf880ddad4310f08eefd7c17c0ef1
[gruntmaster-data.git] / lib / Gruntmaster / Data / Result / Contest.pm
1 use utf8;
2 package 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
9 Gruntmaster::Data::Result::Contest - List of contests
10
11 =cut
12
13 use strict;
14 use warnings;
15
16 use 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
36 =head2 editorial
37
38 data_type: 'text'
39 is_nullable: 1
40
41 HTML fragment placed before the editorial
42
43 =head2 description
44
45 data_type: 'text'
46 is_nullable: 1
47
48 HTML fragment placed on contest page
49
50 =head2 start
51
52 data_type: 'integer'
53 is_nullable: 0
54
55 Unix time when contest starts
56
57 =head2 stop
58
59 data_type: 'integer'
60 is_nullable: 0
61
62 Unix time when contest ends
63
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 },
77 "editorial",
78 { data_type => "text", is_nullable => 1 },
79 "description",
80 { data_type => "text", is_nullable => 1 },
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
105 Type: has_many
106
107 Related 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
118 =head2 contest_statuses
119
120 Type: has_many
121
122 Related 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
133 =head2 jobs
134
135 Type: has_many
136
137 Related 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
150 Type: has_many
151
152 Related 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
165 Type: belongs_to
166
167 Related object: L<Gruntmaster::Data::Result::User>
168
169 =cut
170
171 __PACKAGE__->belongs_to(
172 "owner",
173 "Gruntmaster::Data::Result::User",
174 { id => "owner" },
175 { is_deferrable => 0, on_delete => "CASCADE", on_update => "NO ACTION" },
176 );
177
178 =head2 problems
179
180 Type: many_to_many
181
182 Composing rels: L</contest_problems> -> problem
183
184 =cut
185
186 __PACKAGE__->many_to_many("problems", "contest_problems", "problem");
187
188
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
191
192 use Class::Method::Modifiers qw/after/;
193 use List::Util qw/sum/;
194
195 after qw/insert update delete/ => sub {
196 my ($self) = @_;
197 Gruntmaster::Data::purge '/ct/';
198 Gruntmaster::Data::purge '/ct/' . $self->id;
199 };
200
201 sub is_pending {
202 my ($self, $time) = @_;
203 $self->start > ($time // time)
204 }
205
206 sub is_finished {
207 my ($self, $time) = @_;
208 $self->stop <= ($time // time)
209 }
210
211 sub is_running {
212 my ($self, $time) = @_;
213 !$self->is_pending($time) && !$self->is_finished($time)
214 }
215
216 sub 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
227 sub standings {
228 my ($self) = @_;
229 my $ct = $self->id;
230
231 my @problems = map { $_->rawproblem } $self->contest_problems->search({contest => $ct}, {qw/join problem order_by problem.value/});
232 my (%scores, %tries, %opens);
233 $opens{$_->rawproblem, $_->rawowner} = $_ for $self->opens->search({contest => $ct});
234 for my $job ($self->jobs->search({contest => $ct, result => {'!=', undef}}, {qw/order_by me.id prefetch/ => [qw/problem/]})) {
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
244 my %user_to_name = map { $_->id => $_->name } $self->result_source->schema->users->all;
245
246 say STDERR join "\n\n", @problems;
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
257 $st[0]->{rank} = 1 if @st;
258 $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
259 @st
260 }
261
262 1;
263
264 __END__
265
266 =head1 METHODS
267
268 =head2 is_pending(I<[$time]>)
269
270 Returns true if the contest is pending at time I<$time> (which defaults to C<time>).
271
272 =head2 is_finished(I<[$time]>)
273
274 Returns true if the contest is finished at time I<$time> (which defaults to C<time>).
275
276 =head2 is_running(I<[$time]>)
277
278 Returns true if the contest is running at time I<$time> (which defaults to C<time>).
279
280 =head1 AUTHOR
281
282 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
283
284 =head1 COPYRIGHT AND LICENSE
285
286 Copyright (C) 2014 by Marius Gavrilescu
287
288 This library is free software; you can redistribute it and/or modify
289 it under the same terms as Perl itself, either Perl version 5.18.1 or,
290 at your option, any later version of Perl 5 you may have available.
291
292
293 =cut
This page took 0.060176 seconds and 3 git commands to generate.