<?php
// Example: lootitem.php Hoard

require "_trinity.php";

MySTART( DBnameFromArgv() );

$cname = " Clam";
$res_obj = MyOP("SELECT * FROM item_template WHERE ".EntryConditionFromArgv($cname)." ORDER BY ItemLevel DESC,Quality DESC,name ASC");
$nobjs = 0;

while($obj = mysql_fetch_object($res_obj)) {

if ($nobjs++ > 0)  echo "\n";

PrintItem($obj, "");

$res = MyOP(
"(SELECT *, 'DR' AS tableID from item_loot_template WHERE entry=$obj->entry)"
." UNION ".
"(SELECT *, 'DE' AS tableID from disenchant_loot_template WHERE entry=(SELECT DisenchantID FROM item_template WHERE entry=$obj->entry))"
.(mysql_query("SELECT * FROM prospecting_loot_template LIMIT 1") ? " UNION ".
"(SELECT *, 'PR' AS tableID from prospecting_loot_template WHERE entry=$obj->entry)" : "")
.(mysql_query("SELECT * FROM milling_loot_template LIMIT 1") ? " UNION ".
"(SELECT *, 'MI' AS tableID from milling_loot_template WHERE entry=$obj->entry)" : "")
);
$loot = array();
$nl = 0;
$itemlist = "";
while($r = mysql_fetch_object($res)) {
  if ($r->mincountOrRef >= 0) {
      $loot[$nl++] = $r;
      $itemlist .= ($itemlist == "" ? "":",") . $r->item;
  }
  else {
     printf("  %5s%s x%-7s ",
      floatfmt($r->ChanceOrQuestChance, 5),
      $r->groupid > 0 ? '@' : '%',
      $r->maxcount
     );
     echo "REF ", (- $r->mincountOrRef), "\n";
  }
}

if ($itemlist == "")  continue;

$res = MyOP("SELECT * FROM item_template WHERE entry IN ($itemlist)");
$items = array();
while($r = mysql_fetch_object($res)) {
  $items[$r->entry] = $r;
}

for($i = 0; $i < $nl; $i++) {
   $l = & $loot[$i];
   PrintItem($items[ $l->item ],
     sprintf("  %5s%s x%-7s ",
      floatfmt($l->ChanceOrQuestChance, 5),
      $l->groupid > 0 ? '@':'%',
      $l->mincountOrRef == $l->maxcount ? $l->maxcount : "$l->mincountOrRef-$l->maxcount"
     )
   );
}

} // for all matching "container" items

if ($nobjs == 0) die("Item `$cname` not found in item_template!\n");
printf("=== %d items === \n", $nobjs);

?>
