/
Event.pm
732 lines (525 loc) · 14.4 KB
/
Event.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
use utf8;
package WriteOff::Schema::Result::Event;
# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE
=head1 NAME
WriteOff::Schema::Result::Event
=cut
use strict;
use warnings;
use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';
=head1 COMPONENTS LOADED
=over 4
=item * L<DBIx::Class::InflateColumn::DateTime>
=item * L<DBIx::Class::TimeStamp>
=item * L<DBIx::Class::PassphraseColumn>
=item * L<DBIx::Class::InflateColumn::Serializer>
=back
=cut
__PACKAGE__->load_components(
"InflateColumn::DateTime",
"TimeStamp",
"PassphraseColumn",
"InflateColumn::Serializer",
);
=head1 TABLE: C<events>
=cut
__PACKAGE__->table("events");
=head1 ACCESSORS
=head2 id
data_type: 'integer'
is_auto_increment: 1
is_nullable: 0
=head2 prompt
data_type: 'text'
default_value: 'TBD'
is_nullable: 0
=head2 blurb
data_type: 'text'
is_nullable: 1
=head2 wc_min
data_type: 'integer'
is_nullable: 0
=head2 wc_max
data_type: 'integer'
is_nullable: 0
=head2 rule_set
data_type: 'integer'
default_value: 1
is_nullable: 0
=head2 custom_rules
data_type: 'text'
is_nullable: 1
=head2 start
data_type: 'timestamp'
is_nullable: 0
=head2 prompt_voting
data_type: 'timestamp'
is_nullable: 0
=head2 art
data_type: 'timestamp'
is_nullable: 1
=head2 art_end
data_type: 'timestamp'
is_nullable: 1
=head2 fic
data_type: 'timestamp'
is_nullable: 0
=head2 fic_end
data_type: 'timestamp'
is_nullable: 0
=head2 prelim
data_type: 'timestamp'
is_nullable: 1
=head2 public
data_type: 'timestamp'
is_nullable: 0
=head2 private
data_type: 'timestamp'
is_nullable: 1
=head2 end
data_type: 'timestamp'
is_nullable: 0
=head2 created
data_type: 'timestamp'
is_nullable: 1
=cut
__PACKAGE__->add_columns(
"id",
{ data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
"prompt",
{ data_type => "text", default_value => "TBD", is_nullable => 0 },
"blurb",
{ data_type => "text", is_nullable => 1 },
"wc_min",
{ data_type => "integer", is_nullable => 0 },
"wc_max",
{ data_type => "integer", is_nullable => 0 },
"rule_set",
{ data_type => "integer", default_value => 1, is_nullable => 0 },
"custom_rules",
{ data_type => "text", is_nullable => 1 },
"start",
{ data_type => "timestamp", is_nullable => 0 },
"prompt_voting",
{ data_type => "timestamp", is_nullable => 0 },
"art",
{ data_type => "timestamp", is_nullable => 1 },
"art_end",
{ data_type => "timestamp", is_nullable => 1 },
"fic",
{ data_type => "timestamp", is_nullable => 0 },
"fic_end",
{ data_type => "timestamp", is_nullable => 0 },
"prelim",
{ data_type => "timestamp", is_nullable => 1 },
"public",
{ data_type => "timestamp", is_nullable => 0 },
"private",
{ data_type => "timestamp", is_nullable => 1 },
"end",
{ data_type => "timestamp", is_nullable => 0 },
"created",
{ data_type => "timestamp", is_nullable => 1 },
);
=head1 PRIMARY KEY
=over 4
=item * L</id>
=back
=cut
__PACKAGE__->set_primary_key("id");
=head1 RELATIONS
=head2 artist_awards
Type: has_many
Related object: L<WriteOff::Schema::Result::ArtistAward>
=cut
__PACKAGE__->has_many(
"artist_awards",
"WriteOff::Schema::Result::ArtistAward",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 images
Type: has_many
Related object: L<WriteOff::Schema::Result::Image>
=cut
__PACKAGE__->has_many(
"images",
"WriteOff::Schema::Result::Image",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 prompts
Type: has_many
Related object: L<WriteOff::Schema::Result::Prompt>
=cut
__PACKAGE__->has_many(
"prompts",
"WriteOff::Schema::Result::Prompt",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 scores
Type: has_many
Related object: L<WriteOff::Schema::Result::Score>
=cut
__PACKAGE__->has_many(
"scores",
"WriteOff::Schema::Result::Score",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 storys
Type: has_many
Related object: L<WriteOff::Schema::Result::Story>
=cut
__PACKAGE__->has_many(
"storys",
"WriteOff::Schema::Result::Story",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 user_events
Type: has_many
Related object: L<WriteOff::Schema::Result::UserEvent>
=cut
__PACKAGE__->has_many(
"user_events",
"WriteOff::Schema::Result::UserEvent",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
=head2 vote_records
Type: has_many
Related object: L<WriteOff::Schema::Result::VoteRecord>
=cut
__PACKAGE__->has_many(
"vote_records",
"WriteOff::Schema::Result::VoteRecord",
{ "foreign.event_id" => "self.id" },
{ cascade_copy => 0, cascade_delete => 0 },
);
# Created by DBIx::Class::Schema::Loader v0.07025 @ 2012-12-11 13:01:51
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:/e++88ZYeIweFZvRD6sWHw
__PACKAGE__->many_to_many( users => 'user_events', 'user' );
__PACKAGE__->add_columns(
created => {data_type => 'timestamp', set_on_create => 1},
);
sub leeway {
return 5; #minutes
}
sub now_dt {
return shift->result_source->resultset->now_dt;
}
my %levels = (
E => 0,
T => 1,
M => 2,
);
sub content_level {
my $self = shift;
$self->set_content_level(@_) if @_;
return 'M' if $self->rule_set & 2;
return 'T' if $self->rule_set & 1;
return 'E';
}
sub set_content_level {
my ( $self, $rating ) = @_;
$self->update({ rule_set =>
( ($self->rule_set // 0) & ~3) +
$levels{$rating} // 0
});
}
sub id_uri {
my $self = shift;
require WriteOff::Helpers;
return WriteOff::Helpers::simple_uri( $self->id, $self->prompt );
}
sub organisers {
my $self = shift;
return $self->users->search({ role => 'organiser' }, {
'+select' => [ \'role' ],
'+as' => [ 'role' ],
});
}
sub judges {
my $self = shift;
return $self->users->search({ role => 'judge' }, {
'+select' => [ \'role' ],
'+as' => [ 'role' ],
});
}
sub is_organised_by {
my $self = shift;
my $user = $self->result_source->schema->resultset('User')->resolve(shift)
or return 0;
return 1 if $self->organisers->search({ id => $user->id })->count;
return 1 if $user->is_admin;
0;
}
sub public_story_candidates {
my $self = shift;
return $self->storys->seed_order->all
if !$self->prelim || $self->prelim_votes_allowed;
# Doing the prelim_score search in the resultset doesn't work.
# ...I don't know why, but I guess this'll do.
return grep {
$_->_is_public_candidate
} $self->storys->with_prelim_stats->seed_order->all;
}
sub public_story_noncandidates {
my $self = shift;
return () if !$self->prelim || $self->prelim_votes_allowed;
return grep {
!$_->_is_public_candidate
} $self->storys->with_prelim_stats->seed_order->all;
}
sub storys_gallery_order {
my $self = shift;
return ( $self->public_story_candidates, $self->public_story_noncandidates );
}
sub prompt_subs_allowed {
my $row = shift;
return $row->check_datetimes_ascend
( $row->start, $row->now_dt, $row->prompt_voting);
}
sub prompt_votes_allowed {
my $row = shift;
return $row->check_datetimes_ascend
( $row->prompt_voting, $row->now_dt, $row->art || $row->fic );
}
sub art_subs_allowed {
my $row = shift;
return $row->check_datetimes_ascend
( $row->art, $row->now_dt, $row->art_end->add({ minutes => $row->leeway }) );
}
sub fic_subs_allowed {
my $row = shift;
return $row->check_datetimes_ascend
( $row->fic, $row->now_dt, $row->fic_end->add({ minutes => $row->leeway }) );
}
sub art_gallery_opened {
my $row = shift;
return 0 unless $row->art;
return $row->check_datetimes_ascend( $row->fic, $row->now_dt );
}
sub fic_gallery_opened {
my $row = shift;
return $row->check_datetimes_ascend
( $row->prelim || $row->public , $row->now_dt );
}
sub art_votes_allowed {
my $row = shift;
return 0 unless $row->art;
return $row->check_datetimes_ascend
( $row->art_end, $row->now_dt, $row->end );
}
sub prelim_votes_allowed {
my $row = shift;
return 0 unless $row->prelim;
return $row->check_datetimes_ascend
( $row->prelim, $row->now_dt, $row->public );
}
sub public_votes_allowed {
my $row = shift;
return $row->check_datetimes_ascend
( $row->public, $row->now_dt, $row->private || $row->end );
}
sub private_votes_allowed {
my $row = shift;
return 0 unless $row->private;
return $row->check_datetimes_ascend
( $row->private, $row->now_dt, $row->end );
}
sub is_ended {
my $row = shift;
return $row->check_datetimes_ascend( $row->end, $row->now_dt );
}
sub check_datetimes_ascend {
my $row = shift;
return 1 if join('', @_) eq join('', sort @_);
0;
}
sub new_prelim_record_for {
my $self = shift;
my $schema = $self->result_source->schema;
my $user = $schema->resultset('User')->resolve(shift) or return 0;
my $size = shift // 6;
my $voted_storys = $schema->resultset('Vote')->search({
"record.user_id" => $user->id,
"record.event_id" => $self->id,
"record.round" => 'prelim',
"record.type" => 'fic',
}, { join => 'record' })->get_column('me.story_id');
my $candidates = $self->storys->search({
id => { -not_in => $voted_storys->as_query },
user_id => { '!=' => $user->id },
});
return "You've already voted on all the stories" if !$candidates->count;
my $record = $self->create_related('vote_records', {
user_id => $user->id,
round => 'prelim',
type => 'fic',
});
for( List::Util::shuffle $candidates->get_column('id')->all ) {
$record->create_related('votes', { story_id => $_ });
last if --$size == 0;
}
0;
}
=head2 judge_distr
Distributes stories for private voting.
=cut
sub judge_distr {
my $self = shift;
my $size = shift // 5;
my @storys = $self->storys->with_scores->order_by({ -desc => 'public_score' })->all;
my $no_more_finalists = 0;
for( my $i = 0; $i <= $#storys; $i++ ) {
my ($story, $prev) = @storys[$i, $i-1];
if( $no_more_finalists ) {
$story->update({ is_finalist => 0 });
undef $storys[$i];
}
else {
if($i < $size || $story->public_score == $prev->public_score) {
$story->update({ is_finalist => 1 });
}
else {
$no_more_finalists = 1;
redo;
}
}
}
my @finalist_ids = map { $_->id } grep { defined $_ } @storys;
for my $judge ( $self->judges->all ) {
my $record = $self->create_related('vote_records', {
user_id => $judge->id,
round => 'private',
type => 'fic',
votes => [
map {
{ story_id => $_ }
} @finalist_ids
]
});
}
}
=head2 prelim_distr
Distributes stories for preliminary voting.
Criteria: users do not get their own stories, and the standard deviation of the
judges' wordcounts is minimalised.
=cut
sub prelim_distr {
my $self = shift;
my $x_len = shift // 6;
my $y_len = $self->storys->count;
require List::Util;
# Order by user_id so that initial seeding is faster
my @storys = $self->storys->search(undef, {
select => [ 'id', 'wordcount', 'user_id' ],
order_by => 'user_id',
result_class => 'DBIx::Class::ResultClass::HashRefInflator'
})->all;
my %author_count; $author_count{$_}++ for map { $_->{user_id} } @storys;
my $mode_count = List::Util::max values %author_count;
# Reduce x_len if there aren't enough stories in the pool for such a size
$x_len = List::Util::min $x_len, $y_len - $mode_count;
# No point doing prelim distr with size of 1 or fewer
return 0 if $x_len <= 1;
# System state array. First item is the judge.
my @system = map { [ $_ ] } @storys;
# Seed initial system.
for( my $col = 0; $col < $x_len; $col++ ) {
for( my $i = 0; $i < $y_len; $i++ ) {
# Shift up by $mode_count so that judges are not given their
# own stories, and by $col so that there are no dupes in any set
push $system[$i], $storys[ ($i + $mode_count + $col) % $y_len ];
}
}
my $check_system_constraints = sub {
for my $row ( @system ) {
# Judges cannot have their own stories
return 0 if $row->[0]->{user_id} ~~ [ map { $_->{user_id} } @$row[1 .. $x_len] ];
# No dupes
my %story_count; $story_count{$_}++ for map { $_->{id} } @$row;
return 0 unless List::Util::max( values %story_count ) == 1;
}
1;
};
my $system_work = sub {
my $work;
for my $row ( @system ) {
my $wc_total = List::Util::sum map { $_->{wordcount} } @$row[1 .. $x_len];
$work += $wc_total * ( 1 + $wc_total ) / 2;
}
return $work;
};
my $system_stdev = sub {
my @wcs = map {
List::Util::sum map { $_->{wordcount} } @$_[1 .. $x_len]
} @system;
my $mean = List::Util::sum( @wcs ) / $y_len;
my $variance;
$variance += ($_ - $mean) ** 2 for @wcs;
return sqrt $variance / $y_len;
};
my $cell_swap = sub {
my( $c1, $c2 ) = @_;
my $temp = $system[ $c1->{y} ][ $c1->{x} ];
$system[ $c1->{y} ][ $c1->{x} ] = $system[ $c2->{y} ][ $c2->{x} ];
$system[ $c2->{y} ][ $c2->{x} ] = $temp;
};
# Main algorithm. Take random cells and see if swapping them would decrease
# the total work in the system.
my $current_work = $system_work->();
for( my $i = 0; $i <= 1000; $i++ ) {
# Define two random cells to be swapped, with x in range (1..$x_len) so
# that judges are never moved
my $c1 = {
x => int rand($x_len) + 1,
y => int rand($y_len),
};
my $c2 = {
x => int rand($x_len) + 1,
y => int rand($y_len),
};
# No point swapping cells between the same judge
redo if $c1->{y} == $c2->{y};
my $item1 = $system[ $c1->{y} ][ $c1->{x} ];
my $item2 = $system[ $c2->{y} ][ $c2->{x} ];
# Don't give a judge their own story
redo if $system[ $c1->{y} ][0]->{user_id} == $item2->{user_id};
redo if $system[ $c2->{y} ][0]->{user_id} == $item1->{user_id};
# Don't put an item in a set if it's already there
redo if $item1->{id} ~~ [ map { $_->{id} } @{ $system[ $c2->{y} ] } ];
redo if $item2->{id} ~~ [ map { $_->{id} } @{ $system[ $c1->{y} ] } ];
$cell_swap->( $c1, $c2 );
my $new_work = $system_work->();
if( $new_work < $current_work ) {
$i = 0;
$current_work = $new_work;
}
else {
$cell_swap->( $c1, $c2 );
}
}
$self->result_source->schema->resultset('VoteRecord')->populate([ map {
{
event_id => $self->id,
user_id => $_->[0]->{user_id},
round => 'prelim',
type => 'fic',
votes => [ map {
{ story_id => $_->{id} }
} @$_[ 1..$x_len ] ],
}
} @system ]);
1;
}
# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;