chiark / gitweb /
a8b9f2aaa73a0e81126ea576fdbc1af71bc6a7a9
[ypp-sc-tools.web-live.git] / yarrg / yppedia-chart-parser
1 #!/usr/bin/perl
2
3 use strict (qw(vars));
4 use warnings;
5
6 use Graph::Undirected;
7
8 use CommodsDatabase;
9
10 my $widists= Graph::Undirected->new();
11 my $wiarchs= Graph::Undirected->new();
12 my @wiarchlabels;
13 my %wiisland2node;
14 my %winode2island;
15 my %wiisland2arch;
16 my %winode2lines;
17 my %wiccix2arch;
18
19 my $dbdists= Graph::Undirected->new();
20 my %dbisland2arch;
21
22 my %msgcount;
23 sub perr ($$) { print STDERR "$_[0]: $_[1]\n"; $msgcount{$_[0]}++; }
24 sub warning ($) { perr("warning",$_[0]); }
25 sub error   ($) { perr("error",  $_[0]); }
26 sub change  ($) { perr("change", $_[0]); }
27
28 if ($ARGV[0] eq '--debug') {
29     shift @ARGV;
30     open DEBUG, ">&STDOUT" or die $!;
31     select(DEBUG); $|=1;
32 } else {
33     open DEBUG, ">/dev/null" or die $!;
34 }
35 select(STDOUT); $|=1;
36
37 my $parity;
38 sub nn_xy ($$) {
39     my ($x,$y) = @_;
40     my $tp= (0+$x ^ 0+$y) & 1;
41     defined $parity or $parity=$tp;
42     $tp==$parity or warning("line $.: parity error $x,$y is $tp not $parity");
43     my $n= "$_[0],$_[1]";
44     $winode2lines{$n}{$.}++;
45     return $n;
46 }
47
48 sub parse_yppedia_map () {
49     # We don't even bother with tag soup; instead we do line-oriented parsing.
50
51     while (<>) {
52         s/\<--.*--\>//g;
53         s/^\s*//; chomp; s/\s+$//; s/\s+/ /g;
54         s/\<\/?(?:b|em)\>//g;
55         s/\{\{Chart\ style\|[^{}]*\}\}//g;
56         next unless m/\{\{/; # only interested in chart template stuff
57
58         my ($x,$y, $arch,$island,$solid,$dirn);
59         my $nn= sub { return nn_xy($x,$y) };
60     
61         if (($x,$y,$arch) =
62             m/^\{\{ chart\ label \|(\d+)\|(\d+)\| .*
63                     \'\[\[ [^][\']* \| (\S+)\ archipelago \]\]\'*\}\}$/xi) {
64             printf DEBUG "%d,%d arch %s\n", $x,$y,$arch;
65             push @wiarchlabels, [ $x,$y,$arch ];
66         } elsif (($x,$y,$island) =
67             m/^\{\{ chart\ island\ icon \|(\d+)\|(\d+)\|
68                     ([^| ][^|]*[^| ]) \| .*\}\}$/xi) {
69             my $n= $nn->();
70             $wiisland2node{$island}= $n;
71             $winode2island{$n}= $island;
72             $widists->add_vertex($n);
73             $wiarchs->add_vertex($n);
74 #print "\$g->add_vertex('$n');\n";
75             printf DEBUG "%d,%d island %s\n", $x,$y,$island;
76         } elsif (($solid,$x,$y,$dirn) =
77             m/^\{\{ chart\ league((?:\ solid)?) \|(\d+)\|(\d+)\|
78                     ([-\/\\o]) \| .*\}\}$/xi) {
79             next if $dirn eq 'o';
80
81             my ($bx,$by) = ($x,$y);
82             if ($dirn eq '-') { $bx+=2; }
83             elsif ($dirn eq '\\') { $bx++; $by++; }
84             elsif ($dirn eq '/') { $x++; $by++; }
85             else { die; }
86
87             $widists->add_weighted_edge($nn->(), nn_xy($bx,$by), 1);
88             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
89             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
90 #print "\$g->add_edge('".$nn->()."','".nn_xy($bx,$by)."');\n" if $solid;
91
92             printf DEBUG "%d,%d league %s %s \n", $x,$y,
93                 $solid?'solid':'dotted', $dirn;
94         } elsif (
95             m/^\{\{ chart\ head \}\}$/xi
96                  ) {
97             next;
98         } else {
99             warning("line $.: ignoring incomprehensible: $_");
100         }
101     }
102 }
103
104 sub parse_database_map () {
105     my ($row,$sth);
106     $sth= $dbh->prepare('SELECT islandname, archipelago FROM islands');
107     $sth->execute();
108     while ($row= $sth->fetchrow_hashref) {
109         print DEBUG "database-island $row->{'islandname'}".
110                      " $row->{'archipelago'}\n";
111         $dbisland2arch{$row->{'islandname'}}= $row->{'archipelago'};
112     }
113     $sth= $dbh->prepare('SELECT dist, a.islandname a, b.islandname b
114                                 FROM dists
115                                 JOIN islands AS a ON dists.aiid==a.islandid
116                                 JOIN islands AS b ON dists.biid==b.islandid');
117     $sth->execute();
118     while ($row= $sth->fetchrow_hashref) {
119         $dbdists->add_weighted_edge($row->{'a'}, $row->{'b'}, $row->{'dist'});
120     }
121 }                        
122
123 sub process_yppedia_graphs () {
124     # Prune the LP database by eliminating boring intermediate vertices
125     foreach my $delete ($widists->vertices()) {
126         next if exists $winode2island{$delete};
127         my @neigh= $widists->neighbours($delete);
128         next unless @neigh==2;
129 #       my @aneigh= $wiarchs->has_vertex($delete)
130 #           ? $wiarchs->neighbours($delete) : ();
131 #       next unless @aneigh==0 || @aneigh==2;
132         my $weight= 0;
133         map { $weight += $widists->get_edge_weight($delete, $_) } @neigh;
134         $widists->add_weighted_edge(@neigh, $weight);
135         $widists->delete_vertex($delete);
136         print DEBUG "$delete elide $weight\n";
137     }
138
139     # Check that it's connected.
140     foreach my $cc ($widists->connected_components()) {
141         next if 2*@$cc > $widists->vertices();
142         my $m= "disconnected league point(s):";
143         foreach my $n (@$cc) {
144             $m .= "\n    LP $n, def. yppedia line(s): ".
145                 join(',', sort keys %{ $winode2lines{$n} });
146         }
147         warning($m);
148     }
149
150     # Compute all-pairs-shortest-paths on dist, which is the
151     # actual distances between all LPs.
152     my $wialldists= $widists->APSP_Floyd_Warshall();
153
154     # Compute arch's
155     foreach my $label (@wiarchlabels) {
156         my ($ax,$ay,$arch) = @$label;
157         my $d2best= 9999999;
158         my $best;
159 #       print DEBUG "$ax,$ay arch-island-search $arch\n";
160         $ay += 1;  $ax += 2;  # coords are rather to the top left of label
161         foreach my $vertex ($wiarchs->vertices()) {
162             next unless exists $winode2island{$vertex};
163             my $ccix= $wiarchs->connected_component_by_vertex($vertex);
164             my @cc= $wiarchs->connected_component_by_index($ccix);
165             my ($vx,$vy) = split /,/, $vertex;
166             my $d2= ($vx-$ax)*($vx-$ax) + ($vy-$ay)*($vy-$ay);
167 #           printf DEBUG
168 #               "%d,%d arch-island-search %s d2=%d ccix=%d cc=%d %s\n",
169 #               $ax,$ay, $vertex, $d2, $ccix, scalar(@cc),
170 #               $winode2island{$vertex};
171             next unless @cc > 1;
172             next unless $d2 < $d2best;
173             $best= $vertex;
174             $d2best= $d2;
175         }
176         die 'no island vertices?!' unless defined $best;
177         printf DEBUG "%2d,%-2d arch-island-select %5s d2=%-2d %-10s %s\n",
178             $ax,$ay, $best, $d2best, $arch, $winode2island{$best};
179         my $ccix= $wiarchs->connected_component_by_vertex($best);
180         my $desc= join "\n", map {
181             my $in= $winode2island{$_};
182             "    LP $_". (defined $in ? ", $in" : "");
183         } sort $wiarchs->connected_component_by_index($ccix);
184
185         if (exists $wiccix2arch{$ccix}) {
186             error("archipelago determination failed, wrongly merged:\n".
187                   "    archipelago $arch\n".
188                   "    archipelago $wiccix2arch{$ccix}\n".
189                   $desc);
190             next;
191         }
192         $wiccix2arch{$ccix}= $arch;
193 #       print "$ccix $arch ::\n$desc\n";
194     }
195 }
196
197 sub winode2arch ($) {
198     my ($node) = @_;
199     my $ccix= $wiarchs->connected_component_by_vertex($node);
200     return $wiccix2arch{$ccix};
201 }
202 sub wiisland2arch ($) {
203     my ($island) = @_;
204     my $node= $wiisland2node{$island};
205     die "$island ?" unless defined $node;
206     return winode2arch($node);
207 }
208
209 sub compare_island_lists () {
210     foreach my $island (sort keys %dbisland2arch) {
211         my $node= $wiisland2node{$island};
212         if (!defined $node) {
213             error("would delete island: $island");
214             next;
215         }
216         my $ccix= $wiarchs->connected_component_by_vertex($node);
217         my $wiarch= $wiccix2arch{$ccix};
218         if (!defined $wiarch) {
219             error("island has no arch: $island");
220             next;
221         }
222         my $dbarch= $dbisland2arch{$island};
223         my $wiarch= winode2arch($node);
224         if ($wiarch ne $dbarch) {
225             change("change archipelago from $dbarch to $wiarch".
226                    " for island $island");
227         }
228     }
229     foreach my $island (sort keys %wiisland2node) {
230         my $dbarch= $dbisland2arch{$island};
231         if (!defined $dbarch) {
232             my $wiarch= wiisland2arch($island);
233             if (!defined $wiarch) {
234                 error("new island has no arch: $island");
235                 next;
236                 # We check arches of non-new islands above
237             }
238             change("new island in $wiarch: $island");
239         }
240     }
241 }
242
243 db_setocean('Midnight');
244 db_connect();
245 parse_yppedia_map();
246 parse_database_map();
247 process_yppedia_graphs();
248 compare_island_lists();