chiark / gitweb /
actually work with arbitrage again
[ypp-sc-tools.web-live.git] / pctb / yppsc-commod-processor
1 #!/usr/bin/perl -w
2
3 # helper program for processing commodity output
4
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
7 #
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission.  This program is not endorsed or
25 # sponsored by Three Rings.
26
27
28 use strict (qw(vars));
29 use Data::Dumper;
30
31 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Stall}
32 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Price}
33 # $commod{'Hemp'}{Buy|Sell}{'stall'}{Qty}
34 # $commod{'Hemp'}{Hold}
35
36 our @v;
37 our ($commod,$stall,%commod);
38
39 @ARGV==1 or die "You probably don't want to run this program directly.\n";
40 our ($mode) = shift @ARGV;
41
42 # ./yppsc-commod-processor tsv <t |less -x40,80,90,100,110
43
44 sub bs_read ($$) {
45     my ($bs,$c) = @_;
46     return if @v <= $c;
47     my ($price,$qty) = @v[$c..$c+1];
48     return if !length($price) && !length($qty);
49     die "$_ ?" unless length($price) && length($qty);
50     $commod{$commod}{$bs}{$stall}= {
51         Stall => $stall,
52         Price => $price,
53         Qty => $qty,
54     };
55 }
56
57 while (<>) {
58     chomp;
59     @v= split /\t/;
60 #print STDERR "[".join("|",@v)."]\n";
61     ($commod,$stall) = @v;
62     bs_read(Buy,  2);
63     bs_read(Sell, 4);
64     $commod{$commod}{Hold}= $v[6]+0 if @v>6;
65 }
66
67 our $current;
68
69 sub bs_p ($$$) {
70     my ($commod,$bs,$sortmul) = @_;
71     my $ary= $current->{$bs};
72     my $r= [ ];
73 #print Dumper($ary);
74     foreach my $stall (sort {
75         $sortmul * ($ary->{$a}{Price} <=> $ary->{$b}{Price});
76     } keys %$ary) {
77         push @$r, $ary->{$stall};
78     }
79     return $r;
80 }
81
82 sub bs_p_bestprice ($) {
83     my ($l) = @_;
84     if (@$l) {
85         printf("| %-25.25s %4d", $l->[0]{Stall}, $l->[0]{Price}) or die $!;
86     } else {
87         printf("| %25s %4s","","") or die $!;
88     }
89 }
90
91 our $arbitrage_only= 0;
92
93 sub main__arbitrage () {
94     $arbitrage_only= 1;
95     main__bestprices();
96 }
97
98 sub main__bestprices () {
99     foreach $commod (sort keys %commod) {
100         $current= $commod{$commod};
101         my $buys=  bs_p($commod,Buy, -1);
102         my $sells= bs_p($commod,Sell,+1);
103         if ($arbitrage_only) {
104             next unless @$buys and @$sells;
105             next unless $buys->[0]{Price} > $sells->[0]{Price};
106         }
107         printf("%-15.15s", $commod) or die $!;
108         bs_p_bestprice($buys);
109         bs_p_bestprice($sells);
110         print("\n") or die $!;
111     }
112 }
113
114 sub bs_p_tsv ($) {
115     my ($bs) = @_;
116     if (exists $current->{$bs}{$stall}) {
117         my $si= $current->{$bs}{$stall};
118         printf("\t%d\t%s", $si->{Price}, $si->{Qty}) or die $!;
119     } else {
120         printf("\t\t") or die $!;
121     }
122 }
123
124 sub main__tsv () {
125     foreach $commod (sort keys %commod) {
126         $current= $commod{$commod};
127         my %stalls;
128         map { $stalls{$_}=1; } keys %{ $current->{Buy}  };
129         map { $stalls{$_}=1; } keys %{ $current->{Sell} };
130         foreach $stall (sort keys %stalls) {
131             printf("%s\t%s", $commod, $stall) or die $!;
132             bs_p_tsv(Buy);
133             bs_p_tsv(Sell);
134             print("\n") or die $!;
135         }
136     }
137 }
138
139 sub main__upload () {
140     die "\nUploading not yet implemented, sorry.\n";
141 }
142
143 $mode =~ s/\-//;
144 &{"main__$mode"};
145 close(STDOUT) or die $!;