Take a look at the "loot.php", there you have to customize the function for some result you want to get.
have fun with this, also there is a video how it works
Database: handler.php
PHP Code:
<?php
class DB
{
private $sql;
private $result;
private $exec;
function __construct( $database = null )
{
$this->sql = @new MySQLi( DB_HOST, DB_USER, DB_PASS, $database == null ? DB_BASE : $database );
$this->sql->autocommit( false );
if( $this->sql->connect_errno )
{
//change
die( "Unable to connect with the database. Error: " . $this->sql->connect_errno . " - " . $this->sql->connect_error);
}
}
public function Escape( $post )
{
foreach($post as $key => $value)
{
$value = htmlspecialchars( $value );
$value = htmlentities( $value );
$value = strip_tags( $value );
$value = $this->sql->escape_string( $value );
}
return $post;
}
public function Send ( $qry )
{
$this->result = DB::Exec( $qry );
if( $this->sql->errno )
{
die( "Error: " . $this->sql->errno . " - " . $this->sql->error);
}
return DB::Result();
}
public function Send2 ( $qry )
{
$this->result = DB::Exec( $qry );
}
public function Commit()
{
if( $this->sql->errno )
{
$this->sql->rollback();
die( "Error: " . $this->sql->errno . " - " . $this->sql->error);
}
$this->sql->commit();
}
private function Exec( $qry )
{
$exec = @$this->sql->query( $qry );
return $exec;
}
private function Result()
{
if( $this->result != "1" )
{
$arr = array();
if (!function_exists('mysqli_fetch_all')){
$arr['obj'] = [];
while ($row = $this->result->fetch_assoc()) {
$arr['obj'][] = $row;
}
}else {
$arr['obj'] = $this->result->fetch_all(MYSQLI_ASSOC);
}
$arr['rows'] = $this->result->num_rows;
return $arr;
}
else
{
return 3;
}
}
}
PHP Code:
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_BASE", "systems");
require_once( __DIR__ . "/../Database/MySQL/handler.php" );
class GIFTBOX
{
//To store SQL connection
private $sql;
function __construct()
{
//Create SQL connection
$this->sql = new DB( DB_BASE );
}
//To create new boxes
//@param array $object
public function setGiftbox( $object )
{
//XSS & SQL Injection prevention
$object = $this->sql->Escape( $object );
$store = $this->sql->Send( sprintf( "select id from giftbox_boxes where name = '%s' limit 1", $object["name"] ) );
if( $store["rows"] > 0 )
{
return "This giftbox is already in use. Please choose another name.";
}
$this->sql->Send2( sprintf( "insert into giftbox_boxes (`name`, `desc`) values ('%s', '%s')", $object["name"], $object["desc"] ) );
$this->sql->Commit();
return "Giftbox has been created. Now you are able to add items to this box.";
}
//To list all boxes
public function listGiftbox()
{
return $this->sql->Send( "select * from giftbox_boxes" );
}
//To add new items to ad box
public function setItem( $object )
{
//XSS & SQL Injection prevention
$object = $this->sql->Escape( $object );
$store = $this->sql->Send( sprintf( "select id from giftbox_items where itemid = %d and boxid = %d limit 1", (int)$object["id"], (int)$object["box"] ) );
if( $store["rows"] > 0 )
{
return "Can not add an item two times to the same box.";
}
$this->sql->Send2( sprintf( "insert into giftbox_items (`itemid`, `itemname`, `rarity`, `boxid`) values (%d, '%s', %d, %d)", (int)$object["id"], $object["name"], (int)$object["rarity"], (int)$object["box"] ) );
$this->sql->Commit();
return "Item has been added.";
}
//To show a specific item
public function showGiftbox( $object )
{
//XSS & SQL Injection prevention
$object = $this->sql->Escape( $object );
$store = [];
$store["box"] = $this->sql->Send( sprintf( "select * from giftbox_boxes where id = %d limit 1", (int)$object["id"] ) );
$store["items"] = $this->sql->Send( sprintf( "select * from giftbox_items where boxid = %d", (int)$object["id"] ) );
for( $i = 0; $i <= $store["items"]["rows"]-1; $i++ )
{
switch( $store["items"]["obj"][$i]["rarity"] )
{
case 1:
$store["items"]["obj"][$i]["rarity"] = '<font color="grey">Common</font>';
break;
case 2:
$store["items"]["obj"][$i]["rarity"] = '<font color="blue">Rare</font>';
break;
case 3:
$store["items"]["obj"][$i]["rarity"] = '<font color="green">Epic</font>';
break;
case 4:
$store["items"]["obj"][$i]["rarity"] = '<font color="orange">Legendary</font>';
break;
case 5:
$store["items"]["obj"][$i]["rarity"] = '<font color="red">Awful</font>';
break;
}
}
return $store;
}
//To open a giftbox for a present
//@param int $id -> Box ID
public function lootGiftbox( $id )
{
$object = $this->sql->Escape( $id );
$store = $this->sql->Send( sprintf( "select id from giftbox_boxes where id = %d limit 1", $object[0] ) );
if( $store["rows"] <= 0 )
{
return "This giftbox does not exists.";
}
$store = $this->sql->Send( sprintf( "select * from giftbox_items where boxid = %d", $object[0] ) );
if( $store["rows"] <= 1 )
{
return "This box contains less than 3 items. Please contact an administrator to add more items to make this box useable.";
}
$item = self::getCate( $store );
if( !$item[0] )
{
return $item[1];
}
return $item[1];
}
public function getCate( $object )
{
$random = rand(0, 100);
if( $random < 50 )
{
$cate = 1;
}
elseif( $random < 75 )
{
$cate = 2;
}
elseif( $random < 90 )
{
$cate = 3;
}
elseif( $random < 100 )
{
$cate = 4;
}
if( $random === 100 )
{
$cate = 5;
}
for( $i = 0; $i <= $object["rows"]-1; $i++ )
{
if( $object["obj"][$i]["rarity"] != $cate )
{
unset( $object["obj"][$i] );
}
}
if( count($object["obj"]) < 1 )
{
return [false, "Actually you can not open this box, as it is not completed yet. Try again later please."];
}
sort($object["obj"]);
$random = rand(0, count($object["obj"])-1);
//ADD YOUR PERSONAL RESULT WHICH YOU NEED TO CUSTOMIZE THIS GIFTBOX SYSTEM
//EXAMPLE ONLY
//EXAMPLE ONLY
//EXAMPLE ONLY
switch( $object["obj"][$random]["rarity"] )
{
case 1:
$present = '<font color="grey">' . $object["obj"][$random]["itemname"] . '</font>';
break;
case 2:
$present = '<font color="blue">' . $object["obj"][$random]["itemname"] . '</font>';
break;
case 3:
$present = '<font color="green">' . $object["obj"][$random]["itemname"] . '</font>';
break;
case 4:
$present = '<font color="orange">' . $object["obj"][$random]["itemname"] . '</font>';
break;
case 5:
$present = '<font color="red">' . $object["obj"][$random]["itemname"] . '</font>';
break;
}
//EXAMPLE ONLY
//EXAMPLE ONLY
//EXAMPLE ONLY
//ADD YOUR PERSONAL RESULT WHICH YOU NEED TO CUSTOMIZE THIS GIFTBOX SYSTEM
return [true, $present];
}
}
50 % - Xsrf (me) for writing the code
50 % -
for having the idea and supporting me






