public function handleMarketplace()
 {
     $this->selectedTabIndex = isset($_GET['t']) && is_numeric($_GET['t']) && 1 <= intval($_GET['t']) && intval($_GET['t']) <= 3 ? intval($_GET['t']) : 0;
     $itemId = $this->buildings[$this->buildingIndex]['item_id'];
     $itemLevel = $this->buildings[$this->buildingIndex]['level'];
     $tribeMetadata = $this->gameMetadata['tribes'][$this->data['tribe_id']];
     $tradeOfficeLevel = $this->_getMaxBuildingLevel(28);
     $capacityFactor = $tradeOfficeLevel == 0 ? 1 : $this->gameMetadata['items'][28]['levels'][$tradeOfficeLevel - 1]['value'] / 100;
     $capacityFactor *= $this->gameMetadata['game_speed'];
     $total_merchants_num = $this->gameMetadata['items'][$itemId]['levels'][$itemLevel - 1]['value'];
     $exist_num = $total_merchants_num - $this->queueModel->tasksInQueue['out_merchants_num'] - $this->data['offer_merchants_count'];
     if ($exist_num < 0) {
         $exist_num = 0;
     }
     $this->merchantProperty = array("speed" => $tribeMetadata['merchants_velocity'] * $this->gameMetadata['game_speed'], "capacity" => floor($tribeMetadata['merchants_capacity'] * $capacityFactor), "total_num" => $total_merchants_num, "exits_num" => $exist_num, "confirm_snd" => FALSE, "same_village" => FALSE, "vRow" => NULL);
     if ($this->selectedTabIndex == 0) {
         $m = new BuildModel();
         if ($this->isPost() || isset($_GET['vid2'])) {
             $resources = array("1" => isset($_POST['r1']) ? intval($_POST['r1']) : 0, "2" => isset($_POST['r2']) ? intval($_POST['r2']) : 0, "3" => isset($_POST['r3']) ? intval($_POST['r3']) : 0, "4" => isset($_POST['r4']) ? intval($_POST['r4']) : 0);
             $this->merchantProperty['confirm_snd'] = $this->isPost() ? isset($_POST['act']) && $_POST['act'] == 1 : isset($_GET['vid2']);
             $map_size = $this->setupMetadata['map_size'];
             $doSend = FALSE;
             if ($this->merchantProperty['confirm_snd']) {
                 $vRow = NULL;
                 if (trim($_POST['y']) != "") {
                     $vid = $this->__getVillageId($map_size, $this->__getCoordInRange($map_size, intval($_POST['x'])), $this->__getCoordInRange($map_size, intval($_POST['y'])));
                     $vRow = $m->getVillageDataById($vid);
                 } else {
                     if (isset($_POST['vname']) && trim($_POST['vname']) != "") {
                         $vRow = $m->getVillageDataByName(trim($_POST['vname']));
                     } else {
                         if (isset($_GET['vid2'])) {
                             $vRow = $m->getVillageDataById(intval($_GET['vid2']));
                             if ($vRow != NULL) {
                                 $_POST['x'] = $vRow['rel_x'];
                                 $_POST['y'] = $vRow['rel_y'];
                             }
                         }
                     }
                 }
             } else {
                 $doSend = TRUE;
                 $vRow = $m->getVillageDataById(intval($_POST['vid2']));
                 $this->merchantProperty['showError'] = FALSE;
                 $_POST['r1'] = $_POST['r2'] = $_POST['r3'] = $_POST['r4'] = "";
             }
             if (0 < intval($vRow['player_id']) && $m->getPlayType(intval($vRow['player_id'])) == PLAYERTYPE_ADMIN) {
                 $this->merchantProperty['showError'] = FALSE;
                 $this->merchantProperty['confirm_snd'] = FALSE;
             } else {
                 $this->merchantProperty['vRow'] = $vRow;
                 $vid = $this->merchantProperty['to_vid'] = $vRow != NULL ? $vRow['id'] : 0;
                 $rel_x = $vRow['rel_x'];
                 $rel_y = $vRow['rel_y'];
                 $this->merchantProperty['same_village'] = $vid == $this->data['selected_village_id'];
                 $this->merchantProperty['available_res'] = $this->isResourcesAvailable($resources);
                 $this->merchantProperty['vRow_merchant_num'] = ceil(($resources[1] + $resources[2] + $resources[3] + $resources[4]) / $this->merchantProperty['capacity']);
                 $this->merchantProperty['confirm_snd'] = 0 < $vid && $this->merchantProperty['available_res'] && 0 < $this->merchantProperty['vRow_merchant_num'] && $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num'] && !$this->merchantProperty['same_village'];
                 $this->merchantProperty['showError'] = !$this->merchantProperty['confirm_snd'];
                 $distance = WebHelper::getdistance($this->data['rel_x'], $this->data['rel_y'], $rel_x, $rel_y, $this->setupMetadata['map_size'] / 2);
                 $this->merchantProperty['vRow_time'] = intval($distance / $this->merchantProperty['speed'] * 3600);
                 if (!$this->merchantProperty['showError'] && $doSend && !$this->isGameTransientStopped() && !$this->isGameOver()) {
                     $this->merchantProperty['confirm_snd'] = FALSE;
                     $this->merchantProperty -= "exits_num";
                     $newTask = new QueueTask(QS_MERCHANT_GO, $this->player->playerId, $this->merchantProperty['vRow_time']);
                     $newTask->villageId = $this->data['selected_village_id'];
                     $newTask->toPlayerId = $vRow['player_id'];
                     $newTask->toVillageId = $vid;
                     $newTask->procParams = $this->merchantProperty['vRow_merchant_num'] . "|" . ($resources[1] . " " . $resources[2] . " " . $resources[3] . " " . $resources[4]);
                     $newTask->tag = $resources;
                     $this->queueModel->addTask($newTask);
                 }
             }
         }
         $m->dispose();
     } else {
         if ($this->selectedTabIndex == 1) {
             $m = new BuildModel();
             $showOfferList = TRUE;
             if (isset($_GET['oid']) && 0 < intval($_GET['oid'])) {
                 $oRow = $m->getOffer2(intval($_GET['oid']), $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2);
                 if ($oRow != NULL) {
                     $aid = 0;
                     if ($oRow['alliance_only'] && 0 < intval($this->data['alliance_id'])) {
                         $aid = $m->getPlayerAllianceId($oRow['player_id']);
                     }
                     $res2 = explode("|", $oRow['offer']);
                     $res1 = explode("|", $oRow['offer']);
                     list($res1, $res2) = $res1;
                     $resArr1 = explode(" ", $res1);
                     $needResources = array("1" => $resArr1[0], "2" => $resArr1[1], "3" => $resArr1[2], "4" => $resArr1[3]);
                     $res1_item_id = 0;
                     $res1_value = 0;
                     $i = 0;
                     $_c = sizeof($resArr1);
                     while ($i < $_c) {
                         if (0 < $resArr1[$i]) {
                             $res1_item_id = $i + 1;
                             $res1_value = $resArr1[$i];
                             break;
                         }
                         ++$i;
                     }
                     $resArr1 = explode(" ", $res2);
                     $giveResources = array("1" => $resArr1[0], "2" => $resArr1[1], "3" => $resArr1[2], "4" => $resArr1[3]);
                     $res2_item_id = 0;
                     $res2_value = 0;
                     $i = 0;
                     $_c = sizeof($resArr1);
                     while ($i < $_c) {
                         if (0 < $resArr1[$i]) {
                             $res2_item_id = $i + 1;
                             $res2_value = $resArr1[$i];
                             break;
                         }
                         ++$i;
                     }
                     $distance = $oRow['timeInSeconds'] / 3600 * $oRow['merchants_speed'];
                     $acceptResult = $this->_canAcceptOffer($needResources, $giveResources, $oRow['village_id'], $oRow['alliance_only'], $aid, $oRow['max_time'], $distance);
                     if ($acceptResult == 5 && !$this->isGameTransientStopped() && !$this->isGameOver()) {
                         $showOfferList = FALSE;
                         $this->merchantProperty['offerProperty'] = array("player_id" => $oRow['player_id'], "player_name" => $oRow['player_name'], "res1_item_id" => $res1_item_id, "res1_value" => $res1_value, "res2_item_id" => $res2_item_id, "res2_value" => $res2_value);
                         $merchantNum = ceil(($giveResources[1] + $giveResources[2] + $giveResources[3] + $giveResources[4]) / $this->merchantProperty['capacity']);
                         $newTask = new QueueTask(QS_MERCHANT_GO, $this->player->playerId, $distance / ($this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed']) * 3600);
                         $newTask->villageId = $this->data['selected_village_id'];
                         $newTask->toPlayerId = $oRow['player_id'];
                         $newTask->toVillageId = $oRow['village_id'];
                         $newTask->procParams = $merchantNum . "|" . ($giveResources[1] . " " . $giveResources[2] . " " . $giveResources[3] . " " . $giveResources[4]);
                         $newTask->tag = $giveResources;
                         $this->queueModel->addTask($newTask);
                         $newTask = new QueueTask(QS_MERCHANT_GO, $oRow['player_id'], $oRow['timeInSeconds']);
                         $newTask->villageId = $oRow['village_id'];
                         $newTask->toPlayerId = $this->player->playerId;
                         $newTask->toVillageId = $this->data['selected_village_id'];
                         $newTask->procParams = $oRow['merchants_num'] . "|" . ($needResources[1] . " " . $needResources[2] . " " . $needResources[3] . " " . $needResources[4]);
                         $newTask->tag = array("1" => 0, "2" => 0, "3" => 0, "4" => 0);
                         $this->queueModel->addTask($newTask);
                         $m->removeMerchantOffer(intval($_GET['oid']), $oRow['player_id'], $oRow['village_id']);
                     }
                 }
             }
             $this->merchantProperty['showOfferList'] = $showOfferList;
             if ($showOfferList) {
                 $rowsCount = $m->getAllOffersCount($this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed']);
                 $this->pageCount = 0 < $rowsCount ? ceil($rowsCount / $this->pageSize) : 1;
                 $this->pageIndex = isset($_GET['p']) && is_numeric($_GET['p']) && intval($_GET['p']) < $this->pageCount ? intval($_GET['p']) : 0;
                 $this->merchantProperty['all_offers'] = $m->getAllOffers($this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->setupMetadata['map_size'] / 2, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed'], $this->pageIndex, $this->pageSize);
             }
             $m->dispose();
         } else {
             if ($this->selectedTabIndex == 2) {
                 $m = new BuildModel();
                 $this->merchantProperty['showError'] = FALSE;
                 $this->merchantProperty['showError2'] = FALSE;
                 $this->merchantProperty['showError3'] = FALSE;
                 if ($this->isPost()) {
                     if (isset($_POST['m1']) && 0 < intval($_POST['m1']) && isset($_POST['m2']) && 0 < intval($_POST['m2']) && isset($_POST['rid1']) && 0 < intval($_POST['rid1']) && isset($_POST['rid2']) && 0 < intval($_POST['rid2'])) {
                         $resources1 = array("1" => isset($_POST['rid1']) && intval($_POST['rid1']) == 1 ? intval($_POST['m1']) : 0, "2" => isset($_POST['rid1']) && intval($_POST['rid1']) == 2 ? intval($_POST['m1']) : 0, "3" => isset($_POST['rid1']) && intval($_POST['rid1']) == 3 ? intval($_POST['m1']) : 0, "4" => isset($_POST['rid1']) && intval($_POST['rid1']) == 4 ? intval($_POST['m1']) : 0);
                         $resources2 = array("1" => isset($_POST['rid2']) && intval($_POST['rid2']) == 1 ? intval($_POST['m2']) : 0, "2" => isset($_POST['rid2']) && intval($_POST['rid2']) == 2 ? intval($_POST['m2']) : 0, "3" => isset($_POST['rid2']) && intval($_POST['rid2']) == 3 ? intval($_POST['m2']) : 0, "4" => isset($_POST['rid2']) && intval($_POST['rid2']) == 4 ? intval($_POST['m2']) : 0);
                         if (intval($_POST['rid1']) == intval($_POST['rid2']) || intval($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4]) <= 0 || intval($resources2[1] + $resources2[2] + $resources2[3] + $resources2[4]) <= 0) {
                             $this->merchantProperty['showError'] = TRUE;
                         } else {
                             if (10 < ceil(($resources2[1] + $resources2[2] + $resources2[3] + $resources2[4]) / ($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4]))) {
                                 $this->merchantProperty['showError'] = TRUE;
                                 $this->merchantProperty['showError3'] = TRUE;
                             }
                         }
                         $this->merchantProperty['available_res'] = $this->isResourcesAvailable($resources1);
                         if ($this->merchantProperty['available_res'] && !$this->merchantProperty['showError']) {
                             $this->merchantProperty['vRow_merchant_num'] = ceil(($resources1[1] + $resources1[2] + $resources1[3] + $resources1[4]) / $this->merchantProperty['capacity']);
                             if (0 < $this->merchantProperty['vRow_merchant_num'] && $this->merchantProperty['vRow_merchant_num'] <= $this->merchantProperty['exits_num']) {
                                 $this->merchantProperty -= "exits_num";
                                 $this->data += "offer_merchants_count";
                                 $offer = $resources1[1] . " " . $resources1[2] . " " . $resources1[3] . " " . $resources1[4] . "|" . ($resources2[1] . " " . $resources2[2] . " " . $resources2[3] . " " . $resources2[4]);
                                 $m->addMerchantOffer($this->player->playerId, $this->data['name'], $this->data['selected_village_id'], $this->data['rel_x'], $this->data['rel_y'], $this->merchantProperty['vRow_merchant_num'], $offer, isset($_POST['ally']), 0 < intval($_POST['d2']) ? intval($_POST['d2']) : 0, $this->gameMetadata['tribes'][$this->data['tribe_id']]['merchants_velocity'] * $this->gameMetadata['game_speed']);
                                 foreach ($resources1 as $k => $v) {
                                     $this->resources[$k] -= "current_value";
                                 }
                                 $this->queueModel->_updateVillage(FALSE, FALSE);
                             } else {
                                 $this->merchantProperty['showError'] = TRUE;
                             }
                         } else {
                             $this->merchantProperty['showError'] = TRUE;
                         }
                     } else {
                         $this->merchantProperty['showError'] = TRUE;
                         $this->merchantProperty['showError2'] = TRUE;
                     }
                 } else {
                     if (isset($_GET['d']) && 0 < intval($_GET['d'])) {
                         $row = $m->getOffer(intval($_GET['d']), $this->player->playerId, $this->data['selected_village_id']);
                         if ($row != NULL) {
                             $this->merchantProperty += "exits_num";
                             $this->data -= "offer_merchants_count";
                             $resources2 = explode("|", $row['offer']);
                             $resources1 = explode("|", $row['offer']);
                             list($resources1, $resources2) = $resources1;
                             $resourcesArray1 = explode(" ", $resources1);
                             $res = array();
                             $i = 0;
                             $_c = sizeof($resourcesArray1);
                             while ($i < $_c) {
                                 $res[$i + 1] = $resourcesArray1[$i];
                                 ++$i;
                             }
                             foreach ($res as $k => $v) {
                                 $this->resources[$k] += "current_value";
                             }
                             $this->queueModel->_updateVillage(FALSE, FALSE);
                             $m->removeMerchantOffer(intval($_GET['d']), $this->player->playerId, $this->data['selected_village_id']);
                         }
                     }
                 }
                 $this->merchantProperty['offers'] = $m->getOffers($this->data['selected_village_id']);
                 $m->dispose();
             } else {
                 if ($this->selectedTabIndex == 3 && $this->isPost() && isset($_POST['m2']) && is_array($_POST['m2']) && sizeof($_POST['m2']) == 4 && $this->gameMetadata['plusTable'][6]['cost'] <= $this->data['gold_num']) {
                     $resources = array("1" => intval($_POST['m2'][0]), "2" => intval($_POST['m2'][1]), "3" => intval($_POST['m2'][2]), "4" => intval($_POST['m2'][3]));
                     $oldSum = $this->resources[1]['current_value'] + $this->resources[2]['current_value'] + $this->resources[3]['current_value'] + $this->resources[4]['current_value'];
                     $newSum = $resources[1] + $resources[2] + $resources[3] + $resources[4];
                     if ($newSum <= $oldSum) {
                         foreach ($resources as $k => $v) {
                             $this->resources[$k]['current_value'] = $v;
                         }
                         $this->queueModel->_updateVillage(FALSE, FALSE);
                         $m = new BuildModel();
                         $m->decreaseGoldNum($this->player->playerId, $this->gameMetadata['plusTable'][6]['cost']);
                         $m->dispose();
                     }
                 }
             }
         }
     }
 }