sc: precalculate upgraded values
authorMischa POSLAWSKY <perl@shiar.org>
Fri, 19 Jun 2015 00:23:59 +0000 (02:23 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 19 Jun 2015 00:23:59 +0000 (02:23 +0200)
Minimal change to prepare for code cleanup.

sc.plp

diff --git a/sc.plp b/sc.plp
index 4fc00ba..45a9f7b 100644 (file)
--- a/sc.plp
+++ b/sc.plp
@@ -48,6 +48,31 @@ my $patch = shift @{$units}
 
 print "<p>Unit properties as seen or measured in $scver{name}\n$patch.\n</p>\n\n";
 
+for my $unit (@{$units}) {
+       for my $upgrade (@{ $unit->{upgrade} // $unit->{update} // [] }) {
+               while (my ($col, $increase) = each %{$upgrade}) {
+                       defined $unit->{$col} or next;
+
+                       if (ref $increase eq 'ARRAY') {
+                               for my $incrow (0 .. $#{$increase}) {
+                                       while (my ($inccol, $subinc) = each %{ $increase->[$incrow] }) {
+                                               $unit->{upgraded}->{$col}->[$incrow]->{$inccol}
+                                                       //= ref $unit->{$col}->[$incrow]->{$inccol}
+                                                                 ? $unit->{$col}->[$incrow]->{$inccol}->[-1]
+                                                                 : $unit->{$col}->[$incrow]->{$inccol};
+                                               $unit->{upgraded}->{$col}->[$incrow]->{$inccol}
+                                                       += ref $subinc ? $subinc->[-1] : $subinc;
+                                       }
+                               }
+                       }
+                       elsif ($increase =~ /^-?[0-9.]+/) {
+                               $unit->{upgraded}->{$col} //= $unit->{$col};
+                               $unit->{upgraded}->{$col}  += $increase;
+                       }
+               }
+       }
+}
+
 sub coltoggle {
        my ($name, $id) = @_;
        return sprintf(
@@ -97,14 +122,10 @@ sub showval {
        }
        defined $min or return '';
 
-       if ($row->{upgrade}) {
-               for (@{ $row->{upgrade} }) {
-                       my $increase = $_ or next;
+       if (my $increase = $row->{upgraded}) {
                        $increase = ref $increase eq 'HASH'   ? $increase->{$_}
                                  : ref $increase eq 'ARRAY' && $increase->[$_] or last for @elements;
-                       $increase = $increase->[-1] if ref $increase eq 'ARRAY';
-                       $max += $increase if $increase;
-               }
+                       $max = $increase if $increase;
        }
 
        if ($elements[0] eq 'attack' and $elements[2] eq 'dps') {{