Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Falkirks committed Sep 24, 2014
1 parent 2f99494 commit 653f0db
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 23 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: BuyCraft
main: buycraft\BuyCraft
version: 0.7
version: 1.0
author: Falk
api: [1.0.0]
load: POSTWORLD
Expand Down
4 changes: 4 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

# _
# | |
Expand Down
40 changes: 35 additions & 5 deletions src/buycraft/commands/BuyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand All @@ -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{
Expand All @@ -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{
Expand Down
2 changes: 0 additions & 2 deletions src/buycraft/commands/BuyCraftCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
18 changes: 17 additions & 1 deletion src/buycraft/packages/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/buycraft/packages/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down
42 changes: 34 additions & 8 deletions src/buycraft/packages/PackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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 = [];
Expand Down
11 changes: 6 additions & 5 deletions src/buycraft/task/ReloadCategoriesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion src/buycraft/task/VisitLinkTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down

0 comments on commit 653f0db

Please sign in to comment.