<?php
/* Examples:
item.php Whirlwind Axe
item.php (wrist)
item.php 1200
item.php -24071
item.php (class=2 AND subclass IN (2,3,18))
item.php "(class=2 and subclass=10 and (quality>=3 or requiredlevel=0))"
item.php :world_sdb (bonding IN (0,2) and requiredlevel=0 and (requiredskill=0 or requiredskillrank=0))
item.php (class=15 AND subclass=0 AND spellid_1!=0 AND spellcategory_1=330)
*/
require "_trinity.php";

MySTART( DBnameFromArgv() );

$dbcdata_use = -10000000; // показывать описания spell'ов не только для первых 100 вещей
$verbose = false;
if (@$argv[count($argv)-1] == "/")  { $verbose = true; array_splice($argv,-1); }

$nitems = 0;

if (substr(@$argv[1], 0, 1) == '-') { // показывает вещи ссылочного loot-генератора, идентифицируемого полем mincountOrRef

  $res = MyOP("SELECT * from reference_loot_template WHERE entry=".(-intval( $argv[1] )));
  $loot = array();
  $nl = 0;
  $itemlist = "";
  while($r = mysql_fetch_object($res)) {
    if (!isset($r->mincountOrRef))  continue;
    if ($r->mincountOrRef >= 0) {
        $loot[$nl++] = $r;
        $itemlist .= ($itemlist == "" ? "":",") . $r->item;
    }
    else {
       printf("  %5s%% x%-7s REF %s%s\n",
        floatfmt($r->ChanceOrQuestChance, 5),
        $r->maxcount,
        - $r->mincountOrRef,
        $r->groupid == 0 ? "" : " :".$r->groupid
       );
    }
  }

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

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

  $res_item = MyOP("SELECT * FROM item_template WHERE ".EntryConditionFromArgv()." ORDER BY ItemLevel DESC,Quality DESC,name ASC");

  while($item = mysql_fetch_object($res_item)) {
    PrintItem($item, "  ");
    if ($verbose)  print_r($item);
    ++$nitems;
  }
}

printf("  === %d items === \n", $nitems);

?>
