]>
Commit | Line | Data |
---|---|---|
4ed3f8e7 MG |
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 | ||
be2f7678 | 9 | Gruntmaster::Data::Result::Contest - List of contests |
4ed3f8e7 MG |
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 | ||
b596b2a9 MG |
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 | ||
4ed3f8e7 MG |
50 | =head2 start |
51 | ||
52 | data_type: 'integer' | |
53 | is_nullable: 0 | |
54 | ||
be2f7678 MG |
55 | Unix time when contest starts |
56 | ||
4ed3f8e7 MG |
57 | =head2 stop |
58 | ||
59 | data_type: 'integer' | |
60 | is_nullable: 0 | |
61 | ||
be2f7678 MG |
62 | Unix 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 | ||
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 | ||
adb42d4d MG |
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 | ||
4ed3f8e7 MG |
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" }, | |
9bb39921 | 175 | { is_deferrable => 0, on_delete => "CASCADE", on_update => "NO ACTION" }, |
4ed3f8e7 MG |
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 | ||
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 | 192 | use Class::Method::Modifiers qw/after/; |
751c70b5 MG |
193 | use List::Util qw/sum/; |
194 | ||
de7226ca MG |
195 | after qw/insert update delete/ => sub { |
196 | my ($self) = @_; | |
197 | Gruntmaster::Data::purge '/ct/'; | |
198 | Gruntmaster::Data::purge '/ct/' . $self->id; | |
199 | }; | |
200 | ||
4a8747ef MG |
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 | } | |
4ed3f8e7 | 215 | |
751c70b5 MG |
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 | ||
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 MG |
245 | |
246 | my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock) | |
247 | my $user = $_; | |
248 | +{ | |
249 | user => $user, | |
250 | user_name => $user_to_name{$user}, | |
251 | score => sum (values %{$scores{$user}}), | |
252 | scores => [map { $scores{$user}{$_} // '-'} @problems], | |
253 | } | |
254 | } keys %scores; | |
255 | ||
4aa919c4 | 256 | $st[0]->{rank} = 1 if @st; |
751c70b5 MG |
257 | $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; |
258 | @st | |
259 | } | |
260 | ||
4ed3f8e7 | 261 | 1; |
4a8747ef MG |
262 | |
263 | __END__ | |
264 | ||
265 | =head1 METHODS | |
266 | ||
267 | =head2 is_pending(I<[$time]>) | |
268 | ||
269 | Returns true if the contest is pending at time I<$time> (which defaults to C<time>). | |
270 | ||
271 | =head2 is_finished(I<[$time]>) | |
272 | ||
273 | Returns true if the contest is finished at time I<$time> (which defaults to C<time>). | |
274 | ||
275 | =head2 is_running(I<[$time]>) | |
276 | ||
277 | Returns true if the contest is running at time I<$time> (which defaults to C<time>). | |
278 | ||
279 | =head1 AUTHOR | |
280 | ||
281 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
282 | ||
283 | =head1 COPYRIGHT AND LICENSE | |
284 | ||
285 | Copyright (C) 2014 by Marius Gavrilescu | |
286 | ||
287 | This library is free software; you can redistribute it and/or modify | |
288 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
289 | at your option, any later version of Perl 5 you may have available. | |
290 | ||
291 | ||
292 | =cut |