From 653f0dbc70ad7128e1bb15f7543749cd7aa20a03 Mon Sep 17 00:00:00 2001 From: Falkirks Date: Tue, 23 Sep 2014 20:18:05 -0700 Subject: [PATCH] Version 1.0 --- plugin.yml | 2 +- resources/config.yml | 4 +++ src/buycraft/commands/BuyCommand.php | 40 ++++++++++++++++++--- src/buycraft/commands/BuyCraftCommand.php | 2 -- src/buycraft/packages/Category.php | 18 +++++++++- src/buycraft/packages/Package.php | 15 ++++++++ src/buycraft/packages/PackageManager.php | 42 +++++++++++++++++----- src/buycraft/task/ReloadCategoriesTask.php | 11 +++--- src/buycraft/task/VisitLinkTask.php | 2 +- 9 files changed, 113 insertions(+), 23 deletions(-) diff --git a/plugin.yml b/plugin.yml index 540878b..f43b4eb 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: BuyCraft main: buycraft\BuyCraft -version: 0.7 +version: 1.0 author: Falk api: [1.0.0] load: POSTWORLD diff --git a/resources/config.yml b/resources/config.yml index 1b1d41d..dd923c1 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -25,6 +25,10 @@ debug: false commandThrottleCount: 150 # Prevent setting of secret disable-secret-command: false +# Point players to directly to payment screen +directPay: false +# Amount of packages to show per page +packagePageSize: 5 # _ # | | diff --git a/src/buycraft/commands/BuyCommand.php b/src/buycraft/commands/BuyCommand.php index 89c2caa..5b34f46 100644 --- a/src/buycraft/commands/BuyCommand.php +++ b/src/buycraft/commands/BuyCommand.php @@ -2,9 +2,11 @@ namespace buycraft\commands; use buycraft\BuyCraft; +use buycraft\task\VisitLinkTask; use pocketmine\command\Command; use pocketmine\command\CommandSender; use pocketmine\command\PluginIdentifiableCommand; +use pocketmine\Player; class BuyCommand extends Command implements PluginIdentifiableCommand{ private $main; @@ -15,7 +17,7 @@ public function __construct(BuyCraft $main){ public function execute(CommandSender $sender, $label, array $args){ if(!$this->getPlugin()->getConfig()->get('disableBuyCommand')){ $pageToView = 0; - $categoryToView = 0; + $categoryToView = false; if(count($args) > 0){ if($args[0] == "page" && count($args) == 2 || count($args) == 3){ if(count($args) == 2){ @@ -28,7 +30,20 @@ public function execute(CommandSender $sender, $label, array $args){ } else{ if(count($args) == 1 && is_numeric($args[0])){ - //TODO show package + $package = $this->getPlugin()->getPackageManager()->getPackage($args[0]); + if($package !== false){ + if($this->getPlugin()->getConfig()->get('directPay')){ + $link = $this->getPlugin()->getAuthPayloadSetting('serverStore') . "/checkout/packages?popup=true&action=add&direct=true&package=" . $package->getId() . "&ign=" . $sender->getName(); + } + else{ + $link = $this->getPlugin()->getAuthPayloadSetting('serverStore') . "/checkout/packages?action=add&package=" . $package->getId() . "&ign=" . $sender->getName(); + } + $linkTask = new VisitLinkTask($this->getPlugin(), ['url' => $link], ($sender instanceof Player ? $sender->getName() : false)); + $linkTask->call(); + } + else{ + $sender->sendMessage($this->getPlugin()->getConfig()->get('packageNotFound')); + } return true; } else{ @@ -37,12 +52,27 @@ public function execute(CommandSender $sender, $label, array $args){ } } } - if(is_numeric($pageToView) && is_numeric($categoryToView)){ - //TODO show category + if(is_numeric($pageToView) && is_numeric($categoryToView) || $categoryToView === false){ + $packages = $this->getPlugin()->getPackageManager()->getPage($pageToView, $categoryToView); + if($packages !== false){ + if(count($packages) > 0){ + foreach($packages as $package){ + $sender->sendMessage($this->getPlugin()->getConfig()->get('packageId') . ": " . $package->getNiceId()); + $sender->sendMessage($this->getPlugin()->getConfig()->get('packageName') . ": " . $package->getName()); + $sender->sendMessage($this->getPlugin()->getConfig()->get('packagePrice') . ": " . $package->getPrice() . ' ' . $this->getPlugin()->getAuthPayloadSetting('serverCurrency')); + $sender->sendMessage("--------"); + } + } + else{ + $sender->sendMessage($this->getPlugin()->getConfig()->get('pageNotFound')); + } + } + else{ + $sender->sendMessage($this->getPlugin()->getConfig()->get('noPackagesForSale')); + } } else{ $sender->sendMessage($this->getPlugin()->getConfig()->get('invalidBuyCommand')); - return true; } } else{ diff --git a/src/buycraft/commands/BuyCraftCommand.php b/src/buycraft/commands/BuyCraftCommand.php index 360a930..d2f572f 100644 --- a/src/buycraft/commands/BuyCraftCommand.php +++ b/src/buycraft/commands/BuyCraftCommand.php @@ -26,8 +26,6 @@ public function execute(CommandSender $sender, $label, array $args){ $sender->sendMessage("Scheduled authentication and package reload. If you don't get a success message try again."); $auth = new AuthenticateTask($this->getPlugin(), [], ($sender instanceof Player ? $sender : false)); $auth->call(); - $fetch = new ReloadCategoriesTask($this->getPlugin(), [], ($sender instanceof Player ? $sender : false)); - $fetch->call(); } else{ $sender->sendMessage("Not authenticated with BuyCraft.net."); diff --git a/src/buycraft/packages/Category.php b/src/buycraft/packages/Category.php index bda4287..569babc 100644 --- a/src/buycraft/packages/Category.php +++ b/src/buycraft/packages/Category.php @@ -8,12 +8,28 @@ class Category{ private $name; private $desc; private $item; + private $niceId; - public function __construct($id, $name, $desc, $item){ + public function __construct($id, $name, $desc, $item, $niceId){ $this->desc = $desc; $this->id = $id; $this->item = $item; $this->name = $name; + $this->niceId = $niceId; + } + + /** + * @param mixed $niceId + */ + public function setNiceId($niceId){ + $this->niceId = $niceId; + } + + /** + * @return mixed + */ + public function getNiceId(){ + return $this->niceId; } /** diff --git a/src/buycraft/packages/Package.php b/src/buycraft/packages/Package.php index 655e023..769cfb2 100644 --- a/src/buycraft/packages/Package.php +++ b/src/buycraft/packages/Package.php @@ -8,6 +8,7 @@ class Package{ private $name; private $desc; private $price; + private $niceId; public function __construct($id, $name, $desc, $price, $item, Category $c = null){ $this->category = $c; @@ -21,6 +22,20 @@ public function __construct($id, $name, $desc, $price, $item, Category $c = null } } + /** + * @param mixed $niceId + */ + public function setNiceId($niceId){ + $this->niceId = $niceId; + } + + /** + * @return mixed + */ + public function getNiceId(){ + return $this->niceId; + } + /** * @return Category */ diff --git a/src/buycraft/packages/PackageManager.php b/src/buycraft/packages/PackageManager.php index 31591b8..6cc047b 100644 --- a/src/buycraft/packages/PackageManager.php +++ b/src/buycraft/packages/PackageManager.php @@ -12,16 +12,18 @@ public function __construct(BuyCraft $main){ $this->main = $main; $this->categories = []; $this->packages = []; + $this->pageSize = $this->main->getConfig()->get('packagePageSize'); } public function addCategory($id, $name, $desc, $item){ - $this->categories[$id] = new Category($id, $name, $desc, $item); + $this->categories[] = new Category($id, $name, $desc, $item, count($this->categories)); } public function addPackage($categoryId, $id, $item, $name, $desc, $price){ - if(isset($this->categories[$categoryId])){ - $this->packages[$id] = new Package($id, $name, $desc, $price, $item, $this->categories[$categoryId]); + $category = $this->getCategoryById($categoryId); + if($category instanceof Category){ + $this->packages[] = new Package($id, $name, $desc, $price, $item, $category); } else{ - $this->packages[$id] = new Package($id, $name, $desc, $price, $item); + $this->packages[] = new Package($id, $name, $desc, $price, $item); } } public function cleanCategories(){ @@ -30,18 +32,42 @@ public function cleanCategories(){ unset($this->categories[$i]); } } + foreach($this->packages as $i => $p){ + $p->setNiceId($i); + } } public function getCategories(){ return $this->categories; } - public function getCategory($id){ - return $this->categories[$id]; + public function getCategory($niceId){ + return (isset($this->categories[$niceId]) ? $this->categories[$niceId] : false); + } + public function getCategoryById($id){ + foreach($this->getCategories() as $category){ + if($category->getId() === $id){ + return $category; + } + } + return false; } public function getPackages(){ return $this->packages; } - public function getPackage($id){ - return $this->packages[$id]; + public function getPackage($niceId){ + return (isset($this->packages[$niceId]) ? $this->packages[$niceId] : false); + } + public function getPage($page = 0, $category = 0){ + $start = $page * $this->pageSize; + if($category === false){ + $outArray = array_slice($this->getPackages(), $start, $this->pageSize); + } + elseif($this->getCategory($category) instanceof Category){ + $outArray = array_slice($this->getCategory($category)->getPackages(), $start, $this->pageSize); + } + else{ + $outArray = false; + } + return $outArray; } public function reset(){ $this->categories = []; diff --git a/src/buycraft/task/ReloadCategoriesTask.php b/src/buycraft/task/ReloadCategoriesTask.php index 1378bf1..b658e01 100644 --- a/src/buycraft/task/ReloadCategoriesTask.php +++ b/src/buycraft/task/ReloadCategoriesTask.php @@ -6,6 +6,7 @@ use buycraft\api\ApiAsyncTask; use buycraft\BuyCraft; use pocketmine\command\CommandSender; +use pocketmine\Player; class ReloadCategoriesTask extends ApiAsyncTask{ public function onConfig(BuyCraft $plugin){ @@ -18,13 +19,13 @@ public function onRun(){ } public function onOutput(BuyCraft $main, CommandSender $sender){ $out = $this->getOutput(); - if($out["code"] === 0){ + if($out['code'] === 0){ $main->getPackageManager()->reset(); - foreach($out["payload"] as $category){ - $main->getPackageManager()->addCategory((isset($category["id"]) ? $category["id"] : 0), $category["name"], $category["shortDescription"], $category["guiItemId"]); + foreach($out['payload'] as $category){ + $main->getPackageManager()->addCategory((isset($category['id']) ? $category['id'] : 0), $category['name'], $category['shortDescription'], $category['guiItemId']); } - $sender->sendMessage("Loaded categories."); - $fetch = new ReloadPackagesTask($main, [], ($player !== null ? $player : false)); + $sender->sendMessage("Loaded " . count($out['payload']) . " categories."); + $fetch = new ReloadPackagesTask($main, [], ($sender instanceof Player ? $sender : false)); $fetch->call(); } else{ diff --git a/src/buycraft/task/VisitLinkTask.php b/src/buycraft/task/VisitLinkTask.php index 76f8521..5278179 100644 --- a/src/buycraft/task/VisitLinkTask.php +++ b/src/buycraft/task/VisitLinkTask.php @@ -17,7 +17,7 @@ public function onRun(){ $this->send(); } public function onOutput(BuyCraft $main, CommandSender $sender){ - $out = $this->getData(); + $out = $this->getOutput()['payload']; if($out !== null && $out !== false){ if(isset($out['url']) && $out['url'] !== null){ $sender->sendMessage($main->getConfig()->get('pleaseVisit') . ": " . $out['url']);