1 |
205 |
ahitrov |
<%once> |
2 |
|
|
|
3 |
|
|
use JSON::XS; |
4 |
|
|
my $json = JSON::XS->new; |
5 |
|
|
|
6 |
|
|
</%once> |
7 |
|
|
<%args> |
8 |
|
|
|
9 |
|
|
$name => undef |
10 |
|
|
$SETS => undef |
11 |
|
|
|
12 |
|
|
</%args> |
13 |
|
|
<%init> |
14 |
|
|
|
15 |
|
|
return undef if (! ref($SETS)); |
16 |
|
|
return undef if (! $name); |
17 |
|
|
|
18 |
|
|
my $FIELDS = {}; |
19 |
|
|
while (my ($element,$value) = each %{$SETS}) { |
20 |
|
|
if ( $element =~ /^${name}_level_(\d+)$/ ) { |
21 |
|
|
my $index = $1; |
22 |
|
|
$value =~ s/^\s+//s; |
23 |
|
|
$value =~ s/\s+$//s; |
24 |
|
|
if ( defined $value && $value =~ /^\d+$/ ) { |
25 |
|
|
$FIELDS->{$index}{level} = $value; |
26 |
|
|
} |
27 |
|
|
} elsif ( $element =~ /^${name}_cost_(\d+)$/ ) { |
28 |
|
|
my $index = $1; |
29 |
|
|
$value =~ s/^\s+//s; |
30 |
|
|
$value =~ s/\s+$//s; |
31 |
|
|
if ( defined $value && ($value =~ /^\d+$/ || $value =~ /^\d+[\.\,]\d+$/ || $value =~ /^\d+%$/) ) { |
32 |
|
|
$value =~ s/\,/\./; |
33 |
|
|
$FIELDS->{$index}{cost} = $value; |
34 |
|
|
} |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
|
my @FIELDS = sort { int($a->{level}) <=> int($b->{level}) } grep { $_->{level} && defined $_->{cost} } values %$FIELDS; |
38 |
|
|
return $json->encode(\@FIELDS); |
39 |
|
|
|
40 |
|
|
</%init> |