/** * Changes the ships against resources. * * @return Bengine_Game_Controller_Shipyard */ protected function changeAction() { if (!Core::getConfig()->get("SCRAP_MERCHANT_RATE")) { $this->redirect("game/" . SID . "/Shipyard"); } Core::getLanguage()->load(array("info", "buildings")); $selUnits = Core::getRequest()->getPOST("unit"); /* @var Bengine_Game_Model_Collection_Fleet $availUnits */ $availUnits = Application::getCollection("game/fleet", "game/unit"); $availUnits->addPlanetFilter(Core::getUser()->get("curplanet")); $metalCredit = 0; $siliconCredit = 0; $hydrogenCredit = 0; $totalQty = 0; $realUnits = array(); $rate = (double) Core::getConfig()->get("SCRAP_MERCHANT_RATE"); /* @var Bengine_Game_Model_Unit $unit */ foreach ($availUnits as $unit) { $unitId = $unit->getUnitid(); if (isset($selUnits[$unitId]) && $selUnits[$unitId] > 0) { $qty = _pos((int) $selUnits[$unitId]); $qty = min($qty, $unit->getQty()); $metalCredit += $qty * $unit->get("basic_metal"); $siliconCredit += $qty * $unit->get("basic_silicon"); $hydrogenCredit += $qty * $unit->get("basic_hydrogen"); $totalQty += $qty; $realUnits[(int) $unitId] = $qty; } } $points = ($metalCredit + $siliconCredit + $hydrogenCredit) / 1000; $metalCredit = floor($metalCredit * $rate); $siliconCredit = floor($siliconCredit * $rate); $hydrogenCredit = floor($hydrogenCredit * $rate); Core::getLang()->assign(array("metalCredit" => fNumber($metalCredit), "siliconCredit" => fNumber($siliconCredit), "hydrogenCredit" => fNumber($hydrogenCredit), "totalQty" => fNumber($totalQty))); if (Core::getRequest()->getPOST("verify") == "yes") { /* @var Bengine_Game_Model_Unit $unit */ foreach ($availUnits as $unit) { $unitId = (int) $unit->getUnitid(); if (isset($realUnits[$unitId])) { $qty = $realUnits[$unitId]; if ($unit->getQty() <= $qty) { Core::getQuery()->delete("unit2shipyard", "`unitid` = ? AND `planetid` = ?", null, null, array($unitId, Core::getUser()->get("curplanet"))); } else { $sql = "UPDATE `" . PREFIX . "unit2shipyard` SET `quantity` = `quantity` - ? WHERE `unitid` = ? AND `planetid` = ?"; Core::getDatabase()->query($sql, array($qty, $unitId, Core::getUser()->get("curplanet"))); } } } $sql = "UPDATE `" . PREFIX . "planet` SET `metal` = `metal` + ?, `silicon` = `silicon` + ?, `hydrogen` = `hydrogen` + ? WHERE `planetid` = ?"; Core::getDatabase()->query($sql, array($metalCredit, $siliconCredit, $hydrogenCredit, Core::getUser()->get("curplanet"))); $sql = "UPDATE `" . PREFIX . "user` SET `points` = `points` - ? WHERE `userid` = ?"; Core::getDatabase()->query($sql, array($points, Core::getUser()->get("userid"))); $this->redirect("game/" . SID . "/Shipyard"); } $this->setTemplate("shipyard/change"); $this->assign("units", $realUnits); return $this; }
/** * Select the ships for jump. * * @param array $ships * * @return Bengine_Game_Controller_Mission */ protected function starGateJump($ships) { $this->noAction = true; $data = array(); Core::getQuery()->delete("temp_fleet", "planetid = ?", null, null, array(Core::getUser()->get("curplanet"))); $select = array("u2s.unitid", "u2s.quantity", "d.capicity", "d.speed", "d.consume", "b.name"); $joins = "LEFT JOIN " . PREFIX . "construction b ON (b.buildingid = u2s.unitid)"; $joins .= "LEFT JOIN " . PREFIX . "ship_datasheet d ON (d.unitid = u2s.unitid)"; $result = Core::getQuery()->select("unit2shipyard u2s", $select, $joins, Core::getDB()->quoteInto("b.mode = '3' AND u2s.planetid = ?", Core::getUser()->get("curplanet"))); foreach ($result->fetchAll() as $row) { $id = $row["unitid"]; if (isset($ships[$id])) { $quantity = _pos($ships[$id]); if ($quantity > $row["quantity"]) { $quantity = $row["quantity"]; } if ($quantity > 0) { $data["ships"][$id]["quantity"] = $quantity; $data["ships"][$id]["name"] = $row["name"]; } } } $result->closeCursor(); $result = Core::getQuery()->select("stargate_jump", "time", "", Core::getDB()->quoteInto("planetid = ?", Core::getUser()->get("curplanet")), "time DESC"); $row = $result->fetchRow(); $result->closeCursor(); $requiredReloadTime = (int) Core::getConfig()->get("STAR_GATE_RELOAD_TIME") * 60; $reloadTime = $row["time"] + $requiredReloadTime; if (count($data) > 0 && $reloadTime < TIME) { $moons = array(); $select = new Recipe_Database_Select(); $select->from(array("p" => "planet"))->attributes(array("p" => array("planetid", "planetname"), "g" => array("galaxy", "system", "position"), "sj" => array("time"))); $select->join(array("b2p" => "building2planet"), array("b2p" => "planetid", "p" => "planetid"))->join(array("g" => "galaxy"), array("g" => "moonid", "p" => "planetid"))->join(array("sj" => "stargate_jump"), array("sj" => "planetid", "p" => "planetid"))->where(array("p" => "userid"), Core::getUser()->get("userid"))->where(array("p" => "ismoon"), 1)->where(array("p" => "planetid", "!="), Core::getUser()->get("curplanet"))->where(array("b2p" => "buildingid"), 56)->where("IFNULL(sj.time, 0) + ? < UNIX_TIMESTAMP()", $requiredReloadTime)->group(array("p" => "planetid"))->order(array("g" => "galaxy"))->order(array("g" => "system"))->order(array("g" => "position")); $result = $select->getStatement(); foreach ($result->fetchAll() as $row) { $moons[] = $row; $data["moons"][] = $row["planetid"]; } $result->closeCursor(); Hook::event("ShowStarGates", array(&$moons, &$data)); Core::getTPL()->addLoop("moons", $moons); $data = serialize($data); Core::getQuery()->insert("temp_fleet", array("planetid" => Core::getUser()->get("curplanet"), "data" => $data)); $this->setTemplate("mission/stargatejump"); } else { if ($reloadTime < TIME) { $reloadTime = TIME; } Core::getLanguage()->assign("reloadTime", fNumber(($reloadTime - TIME) / 60), 2); Logger::addMessage("RELOADING_STAR_GATE"); } return $this; }