changeset 129:56b84ce195d9 misc

Combined csv2phpsql.pl to parsematerial.pl
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Dec 2010 15:30:28 +0000
parents c37469a384f3
children 09515ab68178
files materials/mkmaterialinfo.sh materials/parsematerial.pl
diffstat 2 files changed, 409 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/materials/mkmaterialinfo.sh	Mon Dec 20 00:35:41 2010 +0000
+++ b/materials/mkmaterialinfo.sh	Mon Dec 20 15:30:28 2010 +0000
@@ -23,17 +23,17 @@
 echo -n "Last updated: " > "$LISTSHORT"
 date >> "$LISTSHORT"
 echo "" >> "$LISTSHORT"
-perl parsematerial.pl -head >> "$LISTSHORT"
+perl -w parsematerial.pl -head >> "$LISTSHORT"
 cat $DATADIR/*.mat | perl -w parsematerial.pl >> "$LISTSHORT"
 
 echo "Generating CSV"
 cat $DATADIR/*.mat | perl -w parsematerial.pl -csv > "$LISTCSV"
 
 echo "Generating PHP ..."
-perl -w csv2phpsql.pl -php < "$LISTCSV" > "$LISTPHP"
+cat $DATADIR/*.mat | perl -w parsematerial.pl -php > "$LISTPHP"
 
 echo "Generating Perl data ..."
-perl -w csv2phpsql.pl -mat < "$LISTCSV" > "$LISTMAT"
+cat $DATADIR/*.mat | perl -w parsematerial.pl -perl > "$LISTMAT"
 
 echo "Uploading ..."
 scp -C "$LISTLONG" "$LISTSHORT" "$LISTCSV" "$LISTPHP" ccr@tnsp.org:public_html/bat/mat/
--- a/materials/parsematerial.pl	Mon Dec 20 00:35:41 2010 +0000
+++ b/materials/parsematerial.pl	Mon Dec 20 15:30:28 2010 +0000
@@ -1,7 +1,300 @@
 #!/usr/bin/perl -w
 # Superklunssi by Ggr
 use strict;
+use Data::Dumper;
 
+
+#
+# Data translation table generated with csv2transtab.pl
+#
+my %transTable = (
+  2 => {
+    "very light" => 0,
+    "light" => 1,
+    "normal" => 2,
+    "heavy" => 3,
+    "very heavy" => 4,
+  },
+  3 => {
+    "very fragile" => 0,
+    "fragile" => 1,
+    "normal" => 2,
+    "sturdy" => 3,
+    "very sturdy" => 4,
+    "incredibly sturdy" => 5,
+  },
+  4 => {
+    "notable magical" => 0,
+    "very magical" => 1,
+    "weakly magical" => 2,
+    "highly magical" => 3,
+    "not magical" => 4,
+    "magical" => 5,
+    "a bit magical" => 6,
+  },
+  5 => {
+    "unburnable" => 0,
+    "almost unburnable" => 1,
+    "quite unburnable" => 2,
+    "normal" => 3,
+    "quite flammable" => 4,
+    "very flammable" => 5,
+    "highly flammable" => 6,
+  },
+  6 => {
+    "icy" => 0,
+    "drafty" => 1,
+    "comfortable" => 2,
+    "chilly" => 3,
+    "excellent" => 4,
+    "very warm" => 5,
+  },
+  7 => {
+    "non toxic" => 0,
+    "very low toxicity" => 1,
+    "low toxicity" => 2,
+    "mediocre toxicity" => 3,
+    "toxic" => 4,
+    "very toxic" => 5,
+    "highly toxic" => 6,
+  },
+  8 => {
+    "insulator" => 0,
+    "good" => 1,
+    "weak" => 2,
+    "poor" => 3,
+    "great" => 4,
+    "lousy" => 5,
+  },
+  9 => {
+    "loose" => 0,
+    "a bit loose" => 1,
+    "quite solid" => 2,
+    "solid" => 3,
+    "very solid" => 4,
+    "incredibly solid" => 5,
+  },
+  10 => {
+    "poor mentality" => 3,
+    "low mentality" => 1,
+    "some mentality" => 0,
+    "mental" => 2,
+    "great mentality" => 4,
+    "high mentality" => 5,
+  },
+  11 => {
+    "great reactivity" => 0,
+    "not reactive" => 1,
+    "reactive" => 2,
+    "poor reactivity" => 3,
+    "low reactivity" => 4,
+    "notable reactivity" => 5,
+    "high reactivity" => 6,
+  },
+  12 => {
+    "not used" => 0,
+    "rarely used" => 1,
+    "sometimes used" => 2,
+    "frequently used" => 3,
+    "quite much used" => 4,
+    "most common" => 5,
+  },
+  13 => {
+    "not used" => 0,
+    "rarely used" => 1,
+    "sometimes used" => 2,
+    "frequently used" => 3,
+    "quite much used" => 4,
+    "most common" => 5,
+  },
+  14 => {
+    "cheap" => 0,
+    "low-priced" => 1,
+    "average" => 2,
+    "valuable" => 3,
+    "very valuable" => 4,
+    "highly valuable" => 5,
+  },
+  15 => {
+    "impossible" => 0,
+    "very rare" => 1,
+    "rare" => 2,
+    "average" => 3,
+    "common" => 4,
+    "quite common" => 5,
+    "very common" => 6,
+  },
+  16 => {
+    "Organ" => 0,
+    "Cloth" => 1,
+    "Paper" => 2,
+    "Bone" => 3,
+    "Metal" => 4,
+    "Wood" => 5,
+    "Stone" => 6,
+    "Alloy" => 7,
+    "Inorg" => 8,
+    "Glass" => 9,
+    "Gem" => 10,
+  },
+  17 => {
+    "No" => 0,
+    "Yes" => 1,
+  },
+  18 => {
+    "Hit" => 0,
+    "Protrusion" => 1,
+    "Crag" => 2,
+    "Lode" => 3,
+    "Pocket" => 4,
+    "Gloss" => 5,
+    "Shard" => 6,
+    "Deposit" => 7,
+    "Mountain" => 8,
+    "Nest" => 9,
+    "Stack" => 10,
+    "Mound" => 11,
+    "Chunky" => 12,
+    "Puddle" => 13,
+    "Slab" => 14,
+    "Network" => 15,
+    "Pool" => 16,
+    "Hill" => 17,
+    "Mass" => 18,
+    "Tangle" => 19,
+    "Graveyard" => 20,
+    "Lump" => 21,
+    "Coil" => 22,
+    "Abyss" => 23,
+    "Pile" => 24,
+    "Torrent" => 25,
+  },
+  19 => {
+    "heap" => 0,
+    "stick" => 1,
+    "pocket" => 2,
+    "hunk" => 3,
+    "shard" => 4,
+    "string" => 5,
+    "stone" => 6,
+    "slice" => 7,
+    "wisp" => 8,
+    "wad" => 9,
+    "clump" => 10,
+    "carcass" => 11,
+    "gob" => 12,
+    "strip" => 13,
+    "dough" => 14,
+    "ball" => 15,
+    "chunk" => 16,
+    "load" => 17,
+    "leaf" => 18,
+    "salt" => 19,
+    "pile" => 20,
+    "isotope" => 21,
+    "tusk" => 22,
+    "clod" => 23,
+    "lump" => 24,
+    "lode" => 25,
+    "piece" => 26,
+    "flame" => 27,
+    "stem" => 28,
+    "sphere" => 29,
+    "fragment" => 30,
+    "mineral" => 31,
+    "twig" => 32,
+    "stalk" => 33,
+    "crystal" => 34,
+    "log" => 35,
+    "reed" => 36,
+    "nugget" => 37,
+    "mix" => 38,
+    "mic" => 39,
+    "pulp" => 40,
+    "block" => 41,
+    "puddle" => 42,
+    "sliver" => 43,
+  },
+  20 => {
+    "formed" => 0,
+    "stick" => 1,
+    "bundle" => 2,
+    "ellipsoid" => 3,
+    "pocket" => 4,
+    "brick" => 5,
+    "mold" => 6,
+    "gene" => 7,
+    "shard" => 8,
+    "form" => 9,
+    "wisp" => 10,
+    "isometric" => 11,
+    "cord" => 12,
+    "billot" => 13,
+    "bar" => 14,
+    "bolt" => 15,
+    "strip" => 16,
+    "ball" => 17,
+    "billit" => 18,
+    "chunk" => 19,
+    "octagon" => 20,
+    "cube" => 21,
+    "ingot" => 22,
+    "chip" => 23,
+    "casting" => 24,
+    "dean" => 25,
+    "piece" => 26,
+    "flame" => 27,
+    "billet" => 28,
+    "crafted" => 29,
+    "cut" => 30,
+    "pelt" => 31,
+    "strand" => 32,
+    "crystal" => 33,
+    "log" => 34,
+    "reed" => 35,
+    "tetragonal" => 36,
+    "sheet" => 37,
+    "strap" => 38,
+    "reem" => 39,
+    "length" => 40,
+    "sculpted" => 41,
+    "block" => 42,
+    "hexagonal" => 43,
+    "product" => 44,
+    "shaped" => 45,
+    "puddle" => 46,
+    "fibres" => 47,
+    "slab" => 48,
+  },
+);
+
+my @fieldInfo = (
+  { "i" =>  1, "f" => "name",		"w" => 15, "l" => "Name",		"s" => "Name"		},
+  { "i" =>  2, "f" => "weight",		"w" => 10, "l" => "Weight",		"s" => "Weight"		},
+  { "i" =>  3, "f" => "sturdyness",	"w" => 18, "l" => "Sturdyness",		"s" => "Sturdyness"	},
+  { "i" =>  4, "f" => "magic",		"w" => 15, "l" => "Magic",		"s" => "Magic"		},
+  { "i" =>  5, "f" => "flammability",	"w" => 18, "l" => "Flammability",	"s" => "Flammability"	},
+  { "i" =>  6, "f" => "warmth",		"w" => 11, "l" => "Warmth",		"s" => "Warmth"		},
+  { "i" =>  7, "f" => "toxicity",	"w" => 18, "l" => "Toxicity",		"s" => "Toxicity"	},
+  { "i" =>  8, "f" => "resistivity",	"w" =>  9, "l" => "Resist",		"s" => "Resist"		},
+  { "i" =>  9, "f" => "malleability",	"w" => 18, "l" => "Malleability",	"s" => "Malleability"	},
+  { "i" => 10, "f" => "mentality",	"w" => 18, "l" => "Mentality",		"s" => "Mentality"	},
+  { "i" => 11, "f" => "reactivity",	"w" => 18, "l" => "Reactivity",		"s" => "Reactivity"	},
+  { "i" => 12, "f" => "weapon_rarity",	"w" => 15, "l" => "Weapon rarity",	"s" => "Weapon rarity"	},
+  { "i" => 13, "f" => "armour_rarity",	"w" => 15, "l" => "Armour rarity",	"s" => "Armour rarity"	},
+  { "i" => 14, "f" => "value",		"w" => 15, "l" => "Value",		"s" => "Value"		},
+  { "i" => 15, "f" => "rarity",		"w" => 12, "l" => "Rarity",		"s" => "Rarity"		},
+  { "i" => 16, "f" => "type",		"w" =>  6, "l" => "Type",		"s" => "Type"		},
+  { "i" => 17, "f" => "found_nature",	"w" =>  3, "l" => "Found in Nature",	"s" => "FN?"		},
+  { "i" => 18, "f" => "lode_name",	"w" => 10, "l" => "Lode name",		"s" => "Lode name"	},
+  { "i" => 19, "f" => "raw_names",	"w" => 35, "l" => "Raw names",		"s" => "Raw names"	},
+  { "i" => 20, "f" => "refined_names",	"w" => 35, "l" => "Refined names",	"s" => "Refined names"	}
+);
+
+
+###
+### Check options
+###
 my $opt_header = 0;
 my $opt_mode = "";
 my $opt_strip = 1;
@@ -11,44 +304,17 @@
   elsif ($arg eq "-csv") { $opt_mode = $arg; }
   elsif ($arg eq "-tf") { $opt_mode = $arg; }
   elsif ($arg eq "-names") { $opt_mode = $arg; }
+  elsif ($arg eq "-perl") { $opt_mode = $arg; }
+  elsif ($arg eq "-php") { $opt_mode = $arg; }
   elsif ($arg eq "-dump") { $opt_mode = $arg; $opt_strip = 0; }
   else {
     die("Invalid option '$arg'\n");
   }
 }
 
-
-my @titles = (
-  "Name","Weight","Sturdyness","Magic","Flammability",
-  "Warmth","Toxicity","Resist","Malleability",
-  "Mentality","Reactivity","Weapon rarity","Armour rarity",
-  "Value","Rarity","Type","FN?","Lode name","Raw names","Refined names"
-);
-
-my @names = (
-  "name",          "weight",        "sturdyness",
-  "magic",         "flammability",  "warmth",
-
-  "toxicity",      "resistivity",   "malleability",
-  "mentality",     "reactivity",    "weapon_rarity",
-
-  "armour_rarity", "value",         "rarity",
-  "type",          "found_nature",  "lode_name",
-  "raw_names",     "refined_names"
-);
-
-my @widths = (
-  15, 10, 18,
-  15, 18, 11,
-
-  18, 9,  18,
-  18, 18, 15,
-
-  15, 15, 12,
-  6,  3,  10,
-  35, 35
-);
-
+###
+### Parse input materials
+###
 my %data = ();
 
 if (!$opt_header) {
@@ -110,10 +376,14 @@
       $data{$name}{"lode_name"} = $2;
     }
     elsif (/^Raw names\: *([a-z, ]+[a-z])/) {
-      $data{$name}{"raw_names"} = $1;
+      my $tmp = $1;
+      $data{$name}{"raw_names"} = $tmp;
+      @{$data{$name}{"raw_names_arr"}} = split(/, */, $tmp);
     }
     elsif (/^Refined names\: *([a-z, ]+[a-z])/) {
-      $data{$name}{"refined_names"} = $1;
+      my $tmp = $1;
+      $data{$name}{"refined_names"} = $tmp;
+      @{$data{$name}{"refined_names_arr"}} = split(/, */, $tmp);
     }
   }
 }
@@ -121,12 +391,25 @@
 # Sort the entries by name
 my @entries = sort { $a cmp $b } keys %data;
 
+# Sort materials by type
+my %matByType = ();
+foreach my $name (@entries) {
+  push(@{$matByType{$data{$name}{"type"}}}, lc($name));
+}
 
+# Create list of known types
+my @matTypes = sort { $a cmp $b } keys %matByType;
+
+#die();
+
+###
+### Output functionality begins here
+###
 # Print header
 if ($opt_header) {
   my $str = "";
-  for (my $i = 0; $i < @names; $i++) {
-    $str .= sprintf("%-".$widths[$i]."s | ", $titles[$i]);
+  foreach my $info (@fieldInfo) {
+    $str .= sprintf("%-".$$info{"w"}."s | ", $$info{"s"});
   }
   print "$str\n";
   $str =~ s/[^|]/-/g;
@@ -144,15 +427,16 @@
 # Print CSV list
 elsif ($opt_mode eq "-csv") {
   foreach my $name (@entries) {
-    my $s = "";
-    foreach my $field (@names) {
+    my $str = "";
+    foreach my $info (@fieldInfo) {
+      my $field = $$info{"f"};
       if (defined($data{$name}{$field})) {
-        $s .= $data{$name}{$field}.";";
+        $str .= $data{$name}{$field}.";";
       } else {
         print STDERR "Field $field not defined for $name.\n";
       }
     }
-    print "$s\n";
+    print "$str\n";
   }
 }
 
@@ -204,27 +488,96 @@
   print "/set gmat_names=".join(' ', @entries)."\n";
   print "/set gmat_ntypes=".join(' ', map(lc($data{$_}{"type"}), @entries))."\n";
 
-  my %types = ();
-  foreach my $name (@entries) {
-    push(@{$types{$data{$name}{"type"}}}, lc($name));
+  print "/set gmat_types=".join(' ', map(lc($_), @matTypes))."\n";
+  
+  foreach my $type (@matTypes) {
+    print "/set gmat_type_".lc($type)."=".join('|', @{$matByType{$type}})."\n";
   }
+}
 
-  my @typet = sort { $a cmp $b } keys %types;
-  print "/set gmat_types=".join(' ', map(lc($_), @typet))."\n";
-  
-  foreach my $type (@typet) {
-    print "/set gmat_type_".lc($type)."=".join('|', @{$types{$type}})."\n";
-  }
-} else {
+# Print normal table
+elsif ($opt_mode eq "-table") {
   foreach my $name (@entries) {
     my $str = "";
-    for (my $i = 0; $i < @names; $i++) {
-      my $field = $names[$i];
+    foreach my $info (@fieldInfo) {
+      my $field = $$info{"f"};
       if (defined($data{$name}{$field})) {
-        $str .= sprintf("%-".$widths[$i]."s | ", $data{$name}{$field});
+        $str .= sprintf("%-".$$info{"w"}."s | ", $data{$name}{$field});
+      } else {
+        print STDERR "# Field $field does not exist for $name\n";
       }
     }
     print "$str\n";
   }
 }
 
+# Dump a Perl module with material data
+elsif ($opt_mode eq "-perl") {
+  $Data::Dumper::Indent = 0;
+  $Data::Dumper::Useqq  = 1; 
+  $Data::Dumper::Purity = 1;
+
+  print "package Materials;\nrequire Exporter;\n\@ISA = qw(Exporter);\n\n\@EXPORT = qw(matTable transTable);\n\n";
+  my $dumper = Data::Dumper->new([\%data], ["matTable"]);
+  print "our ".$dumper->Dump()."\n\n";
+
+  $dumper = Data::Dumper->new([\%transTable], ["transTable"]);
+  print "our ".$dumper->Dump()."\n\n1;\n";
+}
+
+# Dump PHP data
+elsif ($opt_mode eq "-php") {
+  print "<?\n".
+  "\$transNameTable = array(";
+  foreach my $info (@fieldInfo) {
+    print "\"".$$info{"l"}."\",";
+  }
+  print ");\n\n".
+  "\$transTable = array(\n";
+  foreach my $i (sort { $a <=> $b } keys %transTable) {
+    print "  array(";
+    foreach my $j (keys %{$transTable{$i}}) {
+      if (defined($transTable{$i}{$j})) {
+        print $transTable{$i}{$j};
+        $j =~ s/very /v./;
+        $j =~ s/incredibly /incr./;
+        print " => \"$j\","
+      } else {
+        print STDERR "$0: Unknown transTable type $i : $j\n";
+      }
+    }
+    print "),\n";
+  }
+
+  print ");\n\n".
+  "\$matTable = array(\n";
+  foreach my $name (sort { $a cmp $b } keys %data) {
+    print "  \"$name\" => array(";
+    foreach my $info (@fieldInfo) {
+      my $s = $$info{"i"};
+      my $f = $$info{"f"};
+      my $idx = $data{$name}{$f};
+      my $arr = $f."_arr";
+      if (defined($data{$name}{$arr})) {
+        print "array(";
+        foreach my $q (sort { $a cmp $b } @{$data{$name}{$arr}}) {
+          if (defined($transTable{$s}{$q})) {
+            print $transTable{$s}{$q}.",";
+          } else {
+            print STDERR "$0: Unknown transTable type for '$name' : $s :: $q ($idx)\n";
+          }
+        }
+        print "),";
+      } elsif (defined($transTable{$s}{$idx})) {
+        print $transTable{$s}{$idx}.", ";
+      } else {
+        if ($s != 1) {
+          print STDERR "$0: Unknown transTable type for '$name' : $s == $f ($idx)\n";
+          print "-1,";
+        }
+      }
+    }
+    print "),\n";
+  }
+  print ");\n?>\n";
+}