Skip to content

Instantly share code, notes, and snippets.

@xBeastMode
Created November 30, 2017 22:30
Show Gist options
  • Save xBeastMode/27cd063fd1c317c31358af82f7e72288 to your computer and use it in GitHub Desktop.
Save xBeastMode/27cd063fd1c317c31358af82f7e72288 to your computer and use it in GitHub Desktop.
<?php
namespace PocketLegion\Levels;
use _64FF00\PurePerms\PurePerms;
use PocketLegion\Core\PocketLegionCore;
use pocketmine\block\Block;
use pocketmine\item\Item;
use pocketmine\Player;
class PocketLegionLevels{
/** @var String[] */
public $players = [];
/** @var \SQLite3 */
public $db;
public function __construct(\SQLite3 $db){
$this->db = $db;
$this->db->exec("CREATE TABLE IF NOT EXISTS levels (name TEXT, level INT, deaths INT, kills INT);");
}
/**
* @API
*
* @param $p
*
* @return bool
*/
public function playerExists($p){
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
$query = $this->db->query("SELECT * FROM levels WHERE name='{$name}';");
if(count($query->fetchArray(SQLITE3_ASSOC)) != 0) {
return true;
}
return false;
}
/**
* @API
*
* @param $p
*/
public function addNewPlayer($p){
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
$zero = 0;
$query = $this->db->prepare("INSERT INTO levels (name, level, deaths, kills) VALUES (:name, :level, :deaths, :kills);");
$query->bindParam(':name', $name);
$query->bindParam(':level', $zero);
$query->bindParam(':deaths', $zero);
$query->bindParam(':kills', $zero);
$query->execute();
}
/**
* @API
*
* @param $p
*
* @param $level
*/
public function setLevel($p, $level){
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
if($this->playerExists($p)){
$this->db->querySingle("UPDATE levels SET level = '{$level}' WHERE name = '{$name}';", true);
}
}
/**
* @API
*
* @param $p
*
* @param $deaths
*/
public function setDeaths($p, $deaths){
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
if($this->playerExists($p)){
$this->db->querySingle("UPDATE levels SET deaths = '{$deaths}' WHERE name = '{$name}';", true);
}
}
/**
* @API
*
* @param $p
*
* @param $kills
*/
public function setKills($p, $kills){
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
if($this->playerExists($p)){
$this->db->querySingle("UPDATE levels SET kills = '{$kills}' WHERE name = '{$name}';", true);
}
}
/**
* @API
*
* @param $p
*
* @return int
*/
public function getLevel($p){
if($this->playerExists($p)) {
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
$query = $this->db->query("SELECT * FROM levels WHERE name = '{$name}';");
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
return $row['level'];
}
}
$this->addNewPlayer($p);
return 0;
}
/**
* @API
*
* @param $p
*
* @return int
*/
public function getDeaths($p){
if($this->playerExists($p)) {
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
$query = $this->db->query("SELECT * FROM levels WHERE name = '{$name}';");
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
return $row['deaths'];
}
}
$this->addNewPlayer($p);
return 0;
}
/**
* @API
*
* @param $p
*
* @return int
*/
public function getKills($p){
if($this->playerExists($p)) {
if($p instanceof Player) {
$name = strtolower($p->getName());
}else{
$name = strtolower($p);
}
$query = $this->db->query("SELECT * FROM levels WHERE name = '{$name}';");
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
return $row['kills'];
}
}
$this->addNewPlayer($p);
return 0;
}
/**
* @API
*
* @param $p
*
* @return bool
*/
public function playerLevelUp($p){
/** @var PurePerms $pp */
$pp = PocketLegionCore::getInstance()->getServer()->getPluginManager()->getPlugin('PurePerms');
$pll = PocketLegionCore::getInstance()->getConfig()->getAll()['levels'];
$kills = $this->getKills($p);
$level = $this->getLevel($p);
switch($level){
case 0:
if($kills >= (int)$pll['lv1']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv1']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 1:
if($kills >= (int)$pll['lv2']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv2']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 2:
if($kills >= (int)$pll['lv3']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv3']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 3:
if($kills >= (int)$pll['lv4']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv4']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 4:
if($kills >= (int)$pll['lv5']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv5']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 5:
if($kills >= (int)$pll['lv6']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv6']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
case 6:
if($kills >= (int)$pll['lv7']['kills']){
$this->setLevel($p, $level+1);
foreach($pll['lv7']['permissions'] as $perm){
$pp->getUserDataMgr()->setPermission($p, $perm);
}
return true;
}
break;
}
return false;
}
/**
* @API
*
* @param Player $p
*
* @param Block $block
*
* @param Item $item1
*
* @return bool
*/
public function getCrateRandom(Player $p, Block $block, Item $item1){
$level = $this->getLevel($p);
if($level <= 0){
return false;
}
$percentages = PocketLegionCore::getInstance()->getConfig()->getAll()['levels_crates'];
$item_id = $percentages[$level]['crate_item_id'];
$item_name = $percentages[$level]['create_item_name'];
$block_id = $percentages[$level]['crate_block_id'];
if($block->getId() == $block_id and $item1->getId() == $item_id and $item1->getCustomName() === $item_name){
$item = $percentages[$level]['items'];
$item = $item[array_rand($item)];
$item = explode(':', $item);
$item = Item::get((int)$item[0], (int)$item[1], (int)$item[2]);
$p->getInventory()->addItem($item);
$item1->setCount(1);
$p->getInventory()->removeItem($item1);
return true;
}
return false;
}
/**
* @API
*
* @param Player $p
*
* @return bool
*/
public function getCrate(Player $p){
$rand = mt_rand(0, 100);
$level = $this->getLevel($p);
if($level <= 0){
return false;
}
$percentages = PocketLegionCore::getInstance()->getConfig()->getAll()['levels_crates'];
if($rand <= $percentages[$level]['chance']){
$item = $percentages[$level]['crate_item_id'];
$item = Item::get($item)->setCustomName($percentages[$level]['create_item_name']);
$p->getInventory()->addItem($item);
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment