Skip to content
Snippets Groups Projects
Verified Commit 5c242c0b authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Bugfix

parent 945f0692
No related branches found
No related tags found
No related merge requests found
Pipeline #16117 passed
1.1.1 1.1.2
...@@ -17,16 +17,16 @@ sub process { ...@@ -17,16 +17,16 @@ sub process {
my @choices = (); my @choices = ();
my %count = (); my %count = ();
CHOICE: CHOICE:
foreach my $choice ( split /\W+/, $ballot->{value} ) { foreach my $choice ( split /\W+/, $ballot->{value} ) {
next CHOICE if $choice !~ /\w/; next CHOICE if $choice !~ /\w/;
if ( ! $count{ $choice }++ ) { if ( ! $count{ $choice }++ ) {
push @choices, $choice; push @choices, $choice;
} }
} }
next BALLOT if ! scalar @choices; next BALLOT if ! scalar @choices;
push @ballots, { push @ballots, {
voter => $ballot->{voter}, voter => $ballot->{voter},
...@@ -42,7 +42,7 @@ sub process { ...@@ -42,7 +42,7 @@ sub process {
$c->stash->{ballots} = \@ballots; $c->stash->{ballots} = \@ballots;
# primarni rozdeleni listku a pocitani celkoveho poctu # primarni rozdeleni listku a pocitani celkoveho poctu
BALLOT: BALLOT:
foreach my $ballot ( @ballots ) { foreach my $ballot ( @ballots ) {
...@@ -60,17 +60,17 @@ sub process { ...@@ -60,17 +60,17 @@ sub process {
CHOICE: CHOICE:
foreach my $choice ( keys %{ $r } ) { foreach my $choice ( keys %{ $r } ) {
if ( $r->{$choice}{votes} < $half ) { if ( $r->{$choice}{votes} <= $half ) {
$r->{$choice}{unsupported} = 1; $r->{$choice}{unsupported} = 1;
} }
} }
push @log, { push @log, {
title => "Počáteční rozdělení ($total listků, nadpoloviční podpora $more_than_half)", title => "Počáteční rozdělení ($total listků, nadpoloviční podpora $more_than_half)",
data => Dump $r, data => Dump $r,
}; };
# vyrazeni moznosti kteri nemaji nadpolovicni podporu # vyrazeni moznosti kteri nemaji nadpolovicni podporu
CHOICE: CHOICE:
foreach my $choice ( keys %{ $r } ) { foreach my $choice ( keys %{ $r } ) {
if ( $r->{$choice}{unsupported} ) { if ( $r->{$choice}{unsupported} ) {
...@@ -79,9 +79,9 @@ sub process { ...@@ -79,9 +79,9 @@ sub process {
} }
push @log, { push @log, {
title => 'Po vyřazení možnosti bez nadpoloviční podpory', title => 'Po vyřazení možnosti bez nadpoloviční podpory',
data => Dump $r, data => Dump $r,
}; };
my @winners = winners( $r, $half ); my @winners = winners( $r, $half );
my $round = 1; my $round = 1;
...@@ -99,15 +99,15 @@ sub process { ...@@ -99,15 +99,15 @@ sub process {
LOOSER: LOOSER:
foreach my $looser ( @loosers ) { foreach my $looser ( @loosers ) {
$r = transfer($r, $looser); $r = transfer($r, $looser);
} }
push @log, { push @log, {
title => 'Vyřazovací kolo ' . $round++, title => 'Vyřazovací kolo ' . $round++,
data => Dump $r, data => Dump $r,
loosers => \@loosers, loosers => \@loosers,
random => $random, random => $random,
}; };
@winners = winners( $r, $half ); @winners = winners( $r, $half );
} }
...@@ -116,7 +116,7 @@ sub process { ...@@ -116,7 +116,7 @@ sub process {
{ title => $_->{title}, data => Load( $_->{data}) } { title => $_->{title}, data => Load( $_->{data}) }
} @log ]; } @log ];
$c->stash->{winners} = \@winners; $c->stash->{winners} = \@winners;
$c->render( template => 'result' ); $c->render( template => 'result' );
...@@ -126,7 +126,7 @@ sub transfer { ...@@ -126,7 +126,7 @@ sub transfer {
my $r = shift; my $r = shift;
my $choice = shift; my $choice = shift;
BALLOT: BALLOT:
foreach my $ballot ( @{ $r->{$choice}{ballots} } ) { foreach my $ballot ( @{ $r->{$choice}{ballots} } ) {
HEIR: HEIR:
foreach my $heir ( @{ $ballot->{choices} } ) { foreach my $heir ( @{ $ballot->{choices} } ) {
...@@ -135,7 +135,7 @@ sub transfer { ...@@ -135,7 +135,7 @@ sub transfer {
push @{ $r->{ $heir }{ballots} }, $ballot; push @{ $r->{ $heir }{ballots} }, $ballot;
last HEIR; last HEIR;
} }
} }
} }
delete $r->{ $choice }; delete $r->{ $choice };
return $r ; return $r ;
...@@ -158,14 +158,14 @@ sub winners { ...@@ -158,14 +158,14 @@ sub winners {
foreach my $choice ( choices_by_ballots( $r ) ) { foreach my $choice ( choices_by_ballots( $r ) ) {
if ( scalar @{$r->{$choice}{ballots}} > $half ) { if ( scalar @{$r->{$choice}{ballots}} > $half ) {
push @candidates, $choice; push @candidates, $choice;
} }
} }
my $max = 0; my $max = 0;
CANDIDATE: CANDIDATE:
foreach my $choice ( sort { $r->{$b}{votes} <=> $r->{$a}{votes} } @candidates ) { foreach my $choice ( sort { $r->{$b}{votes} <=> $r->{$a}{votes} } @candidates ) {
$max ||= $r->{$choice}{votes}; $max ||= $r->{$choice}{votes};
push @winners, $choice if $r->{$choice}{votes} == $max; push @winners, $choice if $r->{$choice}{votes} == $max;
} }
...@@ -183,7 +183,7 @@ sub loosers { ...@@ -183,7 +183,7 @@ sub loosers {
CHOICE: CHOICE:
foreach my $choice ( reverse choices_by_ballots( $r ) ) { foreach my $choice ( reverse choices_by_ballots( $r ) ) {
my $ballots = scalar @{$r->{$choice}{ballots}}; my $ballots = scalar @{$r->{$choice}{ballots}};
$min ||= $ballots; $min ||= $ballots;
push @candidates, $choice if scalar $ballots == $min; push @candidates, $choice if scalar $ballots == $min;
} }
...@@ -191,7 +191,7 @@ sub loosers { ...@@ -191,7 +191,7 @@ sub loosers {
CANDIDATE: CANDIDATE:
foreach my $choice ( sort { $r->{$a}{votes} <=> $r->{$b}{votes} } @candidates ) { foreach my $choice ( sort { $r->{$a}{votes} <=> $r->{$b}{votes} } @candidates ) {
$min ||= $r->{$choice}{votes}; $min ||= $r->{$choice}{votes};
push @loosers, $choice if $r->{$choice}{votes} == $min; push @loosers, $choice if $r->{$choice}{votes} == $min;
} }
...@@ -209,39 +209,39 @@ __END__ ...@@ -209,39 +209,39 @@ __END__
while ( 1 ) { while ( 1 ) {
push @{ $c->stash->{log} }, { push @{ $c->stash->{log} }, {
title => $round++ . ". kolo" , title => $round++ . ". kolo" ,
data => { %{ $r } }, data => { %{ $r } },
}; };
my ($min, $max, @loosers, $transfers); my ($min, $max, @loosers, $transfers);
CHOICE: CHOICE:
foreach my $choice ( keys %{ $r } ) { foreach my $choice ( keys %{ $r } ) {
my $count = scalar @{ $r->{$choice}{ballots} }; my $count = scalar @{ $r->{$choice}{ballots} };
if ( ! defined $max || $count > $max ) { if ( ! defined $max || $count > $max ) {
$max = $count $max = $count
} }
if ( ! defined $min || $count < $min ) { if ( ! defined $min || $count < $min ) {
$min = $count; $min = $count;
@loosers = ($choice); @loosers = ($choice);
} }
elsif ( defined $min && $count == $min ) { elsif ( defined $min && $count == $min ) {
push @loosers, $choice; push @loosers, $choice;
} }
} }
last ROUND if $max > $half; last ROUND if $max > $half;
my $looser = (sort { $r->{$a}{votes} <=> $r->{$b}{votes} } @loosers)[0]; my $looser = (sort { $r->{$a}{votes} <=> $r->{$b}{votes} } @loosers)[0];
push @{ $c->stash->{log} }, { push @{ $c->stash->{log} }, {
title => "Vyřazuje se možnost $looser", title => "Vyřazuje se možnost $looser",
}; };
my $transfer; my $transfer;
($transfer, $r) = transfer($r, $looser); ($transfer, $r) = transfer($r, $looser);
last ROUND if ! $transfer; last ROUND if ! $transfer;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment