Mercurial > hg > batmud > misc
annotate materials/parsealloys.pl @ 315:72bf6267e34f misc
Use underlines for multiword material names in plaintext format alloy list.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 30 Dec 2015 06:58:07 +0200 |
parents | 809e2a1c64eb |
children | a4dff42fc40e |
rev | line source |
---|---|
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 #!/usr/bin/perl -w |
136 | 2 # |
3 # BatMUD alloy data parser and converter | |
4 # Developed by Matti Hämäläinen (Ggr Pupunen) <ccr@tnsp.org> | |
273
2deed72277a5
Bump copyright here as well, missed it from previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents:
271
diff
changeset
|
5 # (C) Copyright 2009-2015 Tecnic Software productions (TNSP) |
136 | 6 # |
18 | 7 # Old format of alloying skill output was somewhat different, but |
8 # it has been tuned somewhere after 2005. New output format is: | |
9 # | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #You mix obsidian and crystal and create a quantity of glass |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 #You mix nullium and illumium and create a quantity of dukonium |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 #You mix nullium and illumium and create a quantity of dukonium |
18 | 13 use strict; |
269 | 14 use warnings; |
136 | 15 use materials; |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
136 | 17 my $domark = 0; |
18 my $mode = shift or die("Usage: $0 <-php|-text>\n"); | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 |
270 | 20 if ($mode eq "-php") |
21 { | |
153 | 22 $domark = 1; |
136 | 23 } |
270 | 24 elsif ($mode eq "-text") |
25 { | |
26 } | |
27 else | |
28 { | |
136 | 29 die("$0: Invalid operation mode '$mode'.\n"); |
30 } | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 |
270 | 32 |
315
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
33 sub ul($) |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
34 { |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
35 my $tmp = $_[0]; |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
36 $tmp =~ s/ /_/g; |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
37 return $tmp; |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
38 } |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
39 |
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
40 |
270 | 41 ### |
42 ### Parse data from input, ignoring fumbled alloys | |
43 ### | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 my %data = (); |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 my $fumble = 0; |
18 | 46 my $nline = 0; |
34
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
47 |
270 | 48 sub addCombo($$$) |
49 { | |
50 if (!$fumble) | |
51 { | |
256
0adaafd9d6e8
Stabler sorting for text output.
Matti Hamalainen <ccr@tnsp.org>
parents:
153
diff
changeset
|
52 push(@{$data{$_[1]}{$_[2]}}, $_[0]); |
270 | 53 } |
54 else | |
55 { | |
306 | 56 print STDERR "Ignoring fumbled combo $_[1] + $_[2] = $_[0]\n"; |
34
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
57 } |
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
58 $fumble = 0; |
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
59 } |
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
60 |
315
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
61 |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 while (<STDIN>) { |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 chomp; |
270 | 64 if (/^You mix ([a-z ]+?) and ([a-z ]+?) and create a quantity of (.+?)\.?$/) |
65 { | |
256
0adaafd9d6e8
Stabler sorting for text output.
Matti Hamalainen <ccr@tnsp.org>
parents:
153
diff
changeset
|
66 addCombo($3, $1, $2); |
270 | 67 } |
68 elsif (/^([a-zA-Z0-9]+)#([a-z ]+)\+([a-z ]+)=([a-z ]+)$/) | |
69 { | |
256
0adaafd9d6e8
Stabler sorting for text output.
Matti Hamalainen <ccr@tnsp.org>
parents:
153
diff
changeset
|
70 addCombo($4, $2, $3); |
270 | 71 } |
72 elsif (/^(You slip up and fumble your attempt)/) | |
73 { | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 $fumble = 1; |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 } |
34
6be1a542ade0
Fixes, add parsing of submission datafiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
77 |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
270 | 79 ### |
80 ### Re-format the data into single "ordered" hash, using as | |
81 ### close to alphabetical order as possible. | |
82 ### | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 my %final = (); |
270 | 84 foreach my $min1 (keys %data) |
85 { | |
86 foreach my $min2 (keys %{$data{$min1}}) | |
87 { | |
88 if (!defined($final{$min1}{$min2}) && !defined($final{$min2}{$min1})) | |
89 { | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 # Get both (a, b) and (b, a) combinations results into same array |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 my @tmp = (); |
274 | 92 if (defined($data{$min1}{$min2})) |
93 { | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 push(@tmp, @{$data{$min1}{$min2}}); |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 } |
274 | 96 if (defined($data{$min2}{$min1})) |
97 { | |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 push(@tmp, @{$data{$min2}{$min1}}); |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 # Determine if there are multiple results and which is the most likely. |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 # Some alloy combinations are inherently "unstable". |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
103 my %seen = (); |
270 | 104 $seen{$_}++ foreach (@tmp); |
105 if ($domark) | |
106 { | |
153 | 107 $final{$min1}{$min2} = $final{$min2}{$min1} = \%seen; |
270 | 108 } |
274 | 109 elsif ($min1 lt $min2) |
110 { | |
111 $final{$min1}{$min2} = \%seen; | |
112 } | |
270 | 113 else |
114 { | |
274 | 115 $final{$min2}{$min1} = \%seen; |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
117 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
119 } |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 |
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
122 ## Output the results |
270 | 123 if ($mode eq "-php") |
124 { | |
18 | 125 print "<?\n". |
126 "\$alloyTable = array(\n"; | |
127 | |
270 | 128 foreach my $min1 (sort { $a cmp $b } keys %$Materials::matTable) |
129 { | |
144 | 130 print " \"$min1\" => "; |
270 | 131 if (scalar(keys %{$final{$min1}}) > 0) |
132 { | |
144 | 133 print "array("; |
270 | 134 foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) |
135 { | |
144 | 136 print "\"$min2\" => array("; |
137 foreach my $res (sort { $final{$min1}{$min2}{$a} <=> $final{$min1}{$min2}{$b} } keys %{$final{$min1}{$min2}}) { | |
138 print "\"$res\" => ".$final{$min1}{$min2}{$res}.", "; | |
139 } | |
140 print "), "; | |
141 } | |
142 print "),\n"; | |
270 | 143 } |
144 else | |
145 { | |
144 | 146 print "null,\n"; |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 } |
18 | 148 } |
149 | |
19
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
150 print ");\n\n"; |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
151 |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
152 # Create 'reverse' alloy table |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
153 my %restab = (); |
270 | 154 foreach my $res (keys %$Materials::matTable) |
155 { | |
156 foreach my $min1 (keys %final) | |
157 { | |
158 foreach my $min2 (keys %{$final{$min1}}) | |
159 { | |
144 | 160 my $tmp = $final{$min1}{$min2}; |
270 | 161 if (scalar(keys %$tmp) > 1) |
162 { | |
144 | 163 # print STDERR "Ignoring multi $min1 + $min2 = ".join(", ", keys %$tmp)."\n"; |
270 | 164 } |
165 else | |
166 { | |
167 foreach my $key (keys %$tmp) | |
168 { | |
169 if ($key eq $res && !defined($restab{$res}{$min1}{$min2}) && !defined($restab{$res}{$min2}{$min1})) | |
170 { | |
144 | 171 $restab{$res}{$min1}{$min2} = $$tmp{$key}; |
172 } | |
173 } | |
19
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
174 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
175 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
176 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
177 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
178 |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
179 print "\$alloyRevTable = array(\n"; |
270 | 180 foreach my $res (sort { $a cmp $b } keys %restab) |
181 { | |
19
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
182 print " \"$res\" => array(\n"; |
270 | 183 foreach my $min1 (sort { $a cmp $b } keys %{$restab{$res}}) |
184 { | |
185 if (scalar(keys %{$restab{$res}{$min1}}) > 0) | |
186 { | |
19
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
187 print " \"$min1\" => array("; |
270 | 188 foreach my $min2 (sort { $a cmp $b } keys %{$restab{$res}{$min1}}) |
189 { | |
144 | 190 print "\"$min2\" => ".$restab{$res}{$min1}{$min2}.","; |
19
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
191 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
192 print "),\n"; |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
193 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
194 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
195 print " ),\n"; |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
196 } |
3f5b8222255c
Add generation of reverse alloy table.
Matti Hamalainen <ccr@tnsp.org>
parents:
18
diff
changeset
|
197 |
18 | 198 print ");\n\n". |
199 "?>"; | |
270 | 200 } |
201 elsif ($mode eq "-text") | |
202 { | |
136 | 203 my $total = 0; |
204 my %tdata = (); | |
205 | |
270 | 206 foreach my $type (sort { $a cmp $b } keys %{$Materials::matByType}) |
207 { | |
137 | 208 my $subtotal = 0; |
308
809e2a1c64eb
Improve ascii text format alloy list output.
Matti Hamalainen <ccr@tnsp.org>
parents:
306
diff
changeset
|
209 print "\n".$$Materials::revTransTable{99}{$type}." alloys\n". |
137 | 210 "=========================================================\n"; |
211 | |
271
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
212 foreach my $min1 (@{$$Materials::matByType{$type}}) |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
213 { |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
214 foreach my $min2 (sort { $a cmp $b } keys %{$final{$min1}}) |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
215 { |
315
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
216 printf "%-35s = ", ul($min1)." + ".ul($min2); |
137 | 217 my $n = 0; |
256
0adaafd9d6e8
Stabler sorting for text output.
Matti Hamalainen <ccr@tnsp.org>
parents:
153
diff
changeset
|
218 foreach my $res (sort { |
271
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
219 $final{$min1}{$min2}{$a} <=> $final{$min1}{$min2}{$b} |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
220 || |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
221 $a cmp $b |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
222 } keys %{$final{$min1}{$min2}}) |
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
223 { |
315
72bf6267e34f
Use underlines for multiword material names in plaintext format alloy list.
Matti Hamalainen <ccr@tnsp.org>
parents:
308
diff
changeset
|
224 print ul($res)."[".$final{$min1}{$min2}{$res}."] "; |
137 | 225 $n++; |
226 } | |
227 if ($n > 1) { print " (unstable)"; } | |
228 print "\n"; | |
229 $total++; | |
230 $subtotal++; | |
18 | 231 } |
232 } | |
271
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
233 print "\n".$subtotal." ".lc($$Materials::revTransTable{16}{$type})." type alloys.\n\n"; |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 } |
136 | 235 |
271
e5c0c407b019
Fix to work with the materials.pl changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
270
diff
changeset
|
236 print "\n".$total." alloys total.\n"; |
15
79c79d865218
Add a simple Perl-script for parsing alloy combinations from logfiles.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 } |