-
Notifications
You must be signed in to change notification settings - Fork 0
/
dv
executable file
·314 lines (268 loc) · 10.1 KB
/
dv
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
#!/usr/bin/perl -w
use Getopt::Long;
use Time::Seconds;
use Data::Dumper;
use File::Basename;
use LWP::Simple;
use Cache::Memcached;
use Config::Auto;
use Pod::Usage;
use strict;
my $conf=Config::Auto->new(source => '/etc/dv.conf');
use constant CACHE_TO => ONE_DAY;
# Price of the food, and time to grow
my %data;
$data{'Berry Bauble'}={'price'=>'65000000','food'=>'1000000','tpp'=>'129600'};
$data{'Kraken Kabbage'}={'price'=>'35000000','food'=>'500000','tpp'=>'21600'};
$data{'Cacti-snack'}={'price'=>'20000000','food'=>'250000','tpp'=>'1800'};
$data{'Beanstalk'}={'price'=>'5000000','food'=>'100000','tpp'=>'3600'};
$data{'Omega Squash'}={'price'=>'1000000','food'=>'20000','tpp'=>'600'};
$data{'Sarjin Pepper'}={'price'=>'500000','food'=>'10000','tpp'=>'60'};
$data{'Dragon Fruit'}={'price'=>'75000','food'=>'3000','tpp'=>'86400'};
$data{'Pumpermelons'}={'price'=>'15000','food'=>'800','tpp'=>'21600'};
$data{'Blushrooms'}={'price'=>'5000','food'=>'300','tpp'=>'7200'};
$data{'Zazzberries'}={'price'=>'1000','food'=>'75','tpp'=>'1800'};
$data{'Dragon Root'}={'price'=>'250','food'=>'20','tpp'=>'300'};
$data{'Dragon Snaps'}={'price'=>'50','food'=>'5','tpp'=>'30'};
use constant URL => 'https://docs.google.com/spreadsheets/d/e/2PACX-1vSdpJ8PP_odJwsMAoSOSIGR__Ikff2EyDD99yneuocLsRqH1VMNgtvl3qFSFAEGYBtuiueOq53jYJfr/pub?gid=167749365&single=true&output=csv';
use constant MEMCACHE_KEY => 'dv_db';
my @db_keys=('name','income','nb','incubation','permanent','trait');
use vars qw( $score $plant $cpp $tpp $cost $rounds $time $name $quantity $income $incubation_time $combo1 $combo2 $habitat1 $habitat2 $habitat3 $habitat4 $trait);
use vars qw( $level $food $help $incubation );
use vars qw( $query $nocache );
my $farms=11;
format OUT_TOP=
+-------+---------------+---------+-------+-----------+--------+----------------+-------+
|Score | Plant | cpp | tpp | Cost | Rounds | Time | Farms |
+-------+---------------+---------+-------+-----------+--------+----------------+-------+
.
format OUT =
|@##.## | @<<<<<<<<<<<< | @>>>>>> | @>>>>>| @>>>>>>>> | @>>>>> | @>>>>>>>>>>>>> | @## |
$score,$plant,$cpp,$tpp,$cost,$rounds,$time,$farms
.
format DV_TOP =
+---------------+---+------+-------------+-------------------------------+-----------------------------------------------+-----------+
| Name | Q | Inc. | Incubation | Combo | Habitats | Trait |
+---------------+---+------+-------------+-------------------------------+-----------------------------------------------+-----------+
.
format DV =
|@<<<<<<<<<<<<< |@<<| @<<< | @<<<<<<<<<< | @>>>>>>>>>>>> + @<<<<<<<<<<<< | @|||||||| / @|||||||| / @|||||||| / @|||||||| | @<<<<<<<< |
$name, $quantity, $income, $incubation_time, $combo1, $combo2, $habitat1, $habitat2, $habitat3, $habitat4, $trait
.
my $memc=new Cache::Memcached;
my @memcache_servers;
my $config=$conf->parse;
foreach my $serv (keys %{$config}) {
push @memcache_servers,["$serv:".$config->{$serv}->[0],$config->{$serv}->[1]];
}
$memc->set_servers(\@memcache_servers);
GetOptions(
'level=s',\$level,
'food=i',\$food,
'help',\$help,
'farms=i',\$farms,
'query=s',\$query,
'nocache',\$nocache,
'fresh',\$nocache,
);
pod2usage(-verbose => 2) if ($help);
# ----
# Rounding function
sub round($) {
my $n=shift;
if ($n == sprintf('%.0f',$n)) {
return $n;
} else {
return int($n)+1;
}
}
sub grouping($) {
shift;
1 while s/^(-?\d+)(\d{3})/$1 $2/;
return $_;
}
sub s2dhms($) {
my $secs=shift(@_);
my $out;
my @unity=('s','min','h','d');
foreach my $dur (ONE_DAY,ONE_HOUR,ONE_MINUTE,1) {
if ($secs>=$dur) {
# Je fais ce que je veux, non mais
$out.=sprintf('%.0f'.$unity[int(log($dur)/log(40))],$secs/$dur);
$secs=$secs%$dur;
}
}
return $out;
}
sub compute($$) {
my $food=shift;
my $plant=shift;
# How many plants do I need for gather required food ?
my $plants=round($food/$data{$plant}{'food'});
my $cost=$plants*$data{$plant}{'price'};
# How many rounds of n farms do I need to gather food ?
my $rounds=round($plants/$farms);
# How many time does those rounds will take ?
my $time=$data{$plant}{'tpp'}*$rounds;
$time=$data{$plant}{'tpp'} if ($time < $data{$plant}{'tpp'} );
# This is the cost of the n rounds
my $score=sprintf('%5.2f',log($time*$cost)/log(10));
return {'plant'=>$plant,'tpp'=>$time,'score'=>$score,'rounds'=>$rounds,'cost'=>$cost};
}
sub n2gmk($) {
my $n=shift;
# This will give to power of 10 of the number, but I need a multiple of 3
my $pow10=log($n)/log(10);
# Rounding: if results seems to be an integer (if $n=1000_000, for example), actually it is not
$pow10="$pow10";
# Now it is
my $pow=int($pow10/3);
my $res=$n/(10**(3*$pow)).substr(' kMGTP',$pow,1);
$res=~s/ //;
return $res;
}
sub get_data() {
my $ref=$memc->get(MEMCACHE_KEY);
return $ref if ($ref);
my $c=get URL;
my %db;
foreach my $line (split(/\r\n/,$c)) {
$line=~s/\xa0/ /g;
# Air rift,15,1,0,,Oui,1,, ,48h 00m 00s,,,Non,air,rift,,,air
(my $name, my $income,my $income2,my $nbs, my $prios,my $us,my $nbg,my $priog,my $ug,my $incubation,my $combo1,my $combo2,my $permanent,my $h1,my $h2, my $h3, my $h4,my $trait)=split(/,/,$line);
next if ($name eq 'Nom' or $name eq '' or $name eq 'Nb de dragons' or $name eq 'Nb de gemstone');
$name=lc($name);
#$db{'name'}=$name;
$db{$name}{'name'}=$name;
$db{$name}{'income'}=$income;
$db{$name}{'nb'}=$nbg;
$db{$name}{'incubation'}=$incubation;
$db{$name}{'combo'}=[lc($combo1), lc($combo2)];
$db{$name}{'permanent'}=$permanent;
$db{$name}{'habitats'}=[lc($h1),lc($h2),lc($h3),lc($h4)];
$db{$name}{'trait'}=lc($trait);
}
print "Updating cache...\n";
$memc->set(MEMCACHE_KEY,\%db,CACHE_TO) or die "Cache failed: $!\n";
return \%db;
}
# ----
if(defined $level) {
our $to=$level;
our $from=1;
if ($level=~/(\d+)-(\d+)/) {
$from=$1;$to=$2;
}
my $food_end=2**($to-1)-1;
my $food_start=2**($from-1)-1;
my $food=20*($food_end-$food_start);
print "To get dragon from level $from to level $to, you should earn:\n".grouping($food)." food\n\n";
print "This will cost you:\n";
our %results;
foreach my $plant (sort keys %data) {
my $res=compute($food,$plant);
$results{$plant}=$res;
}
my @sorted=sort {$results{$a}{'score'} <=> $results{$b}{'score'}} keys %results;
select(STDOUT);
$~="OUT";
$^="OUT_TOP";
foreach $plant (@sorted) {
$score=$results{$plant}{'score'};
$cost=n2gmk($results{$plant}{'cost'});
$rounds=$results{$plant}{'rounds'};
$time=s2dhms(int($results{$plant}{'tpp'}));
$tpp=s2dhms($data{$plant}{'tpp'});
$cpp=n2gmk($data{$plant}{'price'});
write;
#print $results{$plant}{'score'},": ",grouping($results{$plant}{'cost'})," with ",$results{$plant}{'rounds'}," rounds of ",$plant," (this will take ".s2dhms(int($results{$plant}{'tpp'})),") with $farms farms) \n";
}
} elsif (defined $food) {
print "Price of the food at level $food:\n".5*2**($food-1)."\n";
} elsif (defined $nocache) {
$memc->delete(MEMCACHE_KEY);
print "Cache cleaned\n";
} elsif (defined $query) {
if ($query !~ /.*=.*/) {
die "Should be command=value. Exiting.\n";
}
(my $command, my $value)=split(/=/,$query);
if ( ! grep { /$command/ } @db_keys) {
die "'$command' is not an authorized db key\nYou may use : ".join(' ',@db_keys)."\n";
}
my @dragons;
my $habitats;
my $ref=get_data();
my %db=%{$ref};
#print Dumper(\%db);
my @res;
foreach my $dragon (keys %db) {
print $dragon if (! defined $db{$dragon}{$command});
push @res,($dragon) if ($db{$dragon}{$command} =~ /$value/i);
}
#print Dumper(@res);
# Nom,Income,,NbS,Prio,Urgence ?,NbG,Prio,Urgence ?,Incubation,Combo,,Permanent ?,Habitat,Habitat,Habitat,Habitat,Trait
select(STDOUT);
$~="DV";
$^="DV_TOP";
foreach my $dragon (sort @res) {
$trait='';
($name, $income,$quantity, $incubation_time, $combo1, $combo2, $habitat1, $habitat2, $habitat3, $habitat4, $trait)=(ucfirst($dragon),$db{$dragon}{'income'},$db{$dragon}{'nb'},$db{$dragon}{'incubation'},$db{$dragon}{'combo'}[0],$db{$dragon}{'combo'}[1],$db{$dragon}{'habitats'}[0],$db{$dragon}{'habitats'}[1],$db{$dragon}{'habitats'}[2],$db{$dragon}{'habitats'}[3],$db{$dragon}{'trait'});
$habitat2=defined $habitat2?$habitat2:'';
$habitat3=defined $habitat3?$habitat3:'';
$habitat4=defined $habitat4?$habitat4:'';
$trait=defined $trait?$trait:'';
my @habitats=($habitat1,$habitat2,$habitat3,$habitat4);
write;
}
#"$name:$income:$incubation_time:$trait";
#print "\n";
} else {
pod2usage(-verbose => 2);
}
__END__
=pod
=cut
=head1 NAME
dv
=head1 SYNOPSIS
dv --level [<start level>-]<level> [ --farms <n> ]
dv --food <level>
dv --query <command>=<value>
dv --fresh | --nocache
=head1 DESCRIPTION
Dragonvale helper.
Helps to calculate food needed to get a dragon from one level to another (rift not supported)
Query ahum ... "database" (my Google Doc), and use memcached to store data.
=head1 OPTIONS
=over
=item B<--level>
Compute food dragon needs to reach that level, from level 1 or <start level>\n";
=item B<--farms>
Number of treat farm you have
This will print something like
+-------+---------------+-------+-----------------------+--------+----------------+-------+
|Score | Plant | tpp | Cost | Rounds | Time | Farms |
+-------+---------------+-------+-----------------------+--------+----------------+-------+
| 15.83 | Dragon Snaps | 30s| 786 432 000 | 285976 | 99d7h8min | 11 |
| 16.83 | Sarjin Pepper | 1min| 7 864 320 000 000 | 143 | 2h23min | 11 |
First number is the score: log10(time*cost). This is supposed to indicate how a combination is interesting. The lower the score, the more it is.
In this case, the first combination is more interesting, as it cost you less and takes less time.
=item B<--food>
Display price of the food at that level
=item B<--query>
Query database. Command can be: name, incubation, nb, income, trait, permanent.
Value is a perl regex.
For incubation, please note that duration are, for example: 1h 01m 01s
=item B<--nocache>, B<--fresh>
Invalidate cache, forcing script to rebuild it at next run (with --db)
=back
=head1 FILES
=over
=item B</etc/dv.conf>
Configuration file for memcached servers:
server1:port:weight
server2:port:weight
...
=back
=cut