<?php
// Example: lootgo.php Treasure Chest

require "_trinity.php";

MySTART( DBnameFromArgv() );

if (substr(@$argv[1], 0, 1) == '#') { // смотреть по guid в таблице конкретных объектов
   $argv[1] = substr($argv[1], 1);
   $argv[1] = intval($argv[1]) > 0 ? "(entry IN (SELECT id FROM gameobject WHERE guid=$argv[1]))" : "(name=\"$argv[1]\")";
}

$verbose = false;
$JoinGuidPool = "";
$JoinEntryPool = "";
//
if (@$argv[count($argv)-1] == "/")  {
   $verbose = true; array_splice($argv,-1);

   if (mysql_query("SELECT guid, chance FROM pool_gameobject LIMIT 1"))
      $JoinGuidPool = "LEFT JOIN pool_gameobject USING(guid)";
}

$res_go = MyOP("SELECT * FROM gameobject_template WHERE ".EntryConditionFromArgv()." ORDER BY name, entry");
$nGO = 0;
$nLootable = 0;
$prevIsNL = false;

while($go = mysql_fetch_object($res_go)) {

++$nGO;

if ($go->type != 3 and $go->type != 25) {
    printf("%6d  %s ### GO type %d Not Lootable", $go->entry, $go->name, $go->type);
    $prevIsNL = true;
} else {
    // Chest(3) or Fishing Pool(25) -- LOOTABLE
    if ($prevIsNL) { echo "\n"; $prevIsNL = false; }
    printf("%6d  %s", $go->entry, $go->name);
}

printf("%s\n", isset($go->chance) ? "  *$go->pool_entry:$go->chance" : "");

if ($nGO <= 100) {
    $res = MyOP("SELECT * FROM gameobject $JoinGuidPool WHERE id=$go->entry ORDER BY map, position_x, position_y, position_z");
    $prevmap = -1;
    $nguids = 0;
    while($r = mysql_fetch_object($res)) {
      if ($r->map != $prevmap) {
          if ($prevmap >= 0) echo ";  ";
          if (($map = @$wowmaps[$r->map]) == "")  $map = "??? ($r->map):";
          echo "  $map ($r->map):";
          $prevmap = $r->map;
      }
      if ($verbose)
         printf("\n%10s %7s  %9.2f %9.2f %9.2f %d%s", "#".$r->guid,
            isset($r->spawntimesecsmin) ? # Nostalrius
              $r->spawntimesecsmin.($r->spawntimesecsmin!=$r->spawntimesecsmax ? "..".$r->spawntimesecsmax : "")."s"
              : $r->spawntimesecs."s",
            $r->position_x, $r->position_y, $r->position_z, $r->map,
            isset($r->chance) ? "  *$r->pool_entry:$r->chance" : ""
         );
      else
         echo " ($r->position_x $r->position_y $r->position_z)";
      ++$nguids;
    }
    if ($prevmap >= 0)  echo " {",$nguids,"}\n";
}


if ($verbose) {
    ViewRelatedData($go, &$requestPGT, array(
      "SELECT * FROM pool_gameobject_template WHERE id="),
      "",
      "Cannot access `pool_gameobject_template` table in this DB",
      "Not defined in `pool_gameobject_template`",
      "from `pool_gameobject_template`"
    );

    ViewRelatedData($go, &$requestGQS, array(
      "SELECT * FROM gameobject_queststarter WHERE id=",
      "SELECT * FROM gameobject_questrelation WHERE id="),
      "",
      "Cannot access gameobject_queststarter table in this DB",
      "Not defined in gameobject_queststarter",
      "from gameobject_queststarter"
    );

    ViewRelatedData($go, &$requestGQE, array(
      "SELECT * FROM gameobject_questender WHERE id=",
      "SELECT * FROM gameobject_involvedrelation WHERE id="),
      "",
      "Cannot access gameobject_questender table in this DB",
      "Not defined in gameobject_questender",
      "from gameobject_questender"
    );

    ViewRelatedData($go, &$requestEG, array(
      "SELECT GROUP_CONCAT(guid SEPARATOR ', ') FROM game_event_gameobject JOIN gameobject USING(guid) JOIN gameobject_template ON id=entry WHERE entry="),
      " ORDER BY guid",
      "Cannot access `game_event_gameobject` table in this DB",
      "Not defined in `game_event_gameobject`",
      "guids in `game_event_gameobject`"
    );
}


if ($go->type != 3 and $go->type != 25) {
    if ($verbose)  print_r($go);
    continue;
}

$res = MyOP("SELECT * from gameobject_loot_template WHERE entry=".(isset($go->data1) ? @$go->data1 : @$go->entry));
++$nLootable;
$loot = array();
$nl = 0;
$itemlist = "";
while($r = mysql_fetch_object($res)) {
  if (isset($r->ChanceOrRef)) { // случай SDB
      $r->mincountOrRef = $r->ChanceOrRef < 0 ? intval(- $r->ChanceOrRef) : $r->mincount;
      $r->ChanceOrQuestChance = $r->QuestChanceOrGroup > 0 ? - $r->QuestChanceOrGroup : ($r->ChanceOrRef > 0 ? $r->ChanceOrRef : 0);
      $r->groupid = $r->QuestChanceOrGroup <= 0 ? - $r->QuestChanceOrGroup : 0;
  }
  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;
}

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)
   );
}
}


if ($verbose)  print_r($go);

echo "\n";

} // for all matching game objects

if ($nGO == 0) die("Game Object(s) not found in gameobject_template!\n");
printf("=== %d game objects, %d lootable === \n", $nGO, $nLootable);

?>
