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