[Web] Giftbox System

01/22/2016 23:01 xsrf#1
Cuz i got some time and want to develop something, here you can download my new giftbox system.

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 MySQLiDB_HOSTDB_USERDB_PASS$database == null DB_BASE $database );
        
$this->sql->autocommitfalse );
        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;
        }
    }
    
}
Giftbox: giftbox.class.php

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 DBDB_BASE );
    }
    
    
//To create new boxes
    //@param array $object
    
public function setGiftbox$object )
    {
        
//XSS & SQL Injection prevention
        
$object $this->sql->Escape$object );
        
        
$store $this->sql->Sendsprintf"select id from giftbox_boxes where name = '%s' limit 1"$object["name"] ) );
        
        if( 
$store["rows"] > )
        {
            return 
"This giftbox is already in use. Please choose another name.";
        }
        
        
$this->sql->Send2sprintf"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->Sendsprintf"select id from giftbox_items where itemid = %d and boxid = %d limit 1", (int)$object["id"], (int)$object["box"] ) );
        
        if( 
$store["rows"] > )
        {
            return 
"Can not add an item two times to the same box.";
        }
        
        
$this->sql->Send2sprintf"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->Sendsprintf"select * from giftbox_boxes where id = %d limit 1", (int)$object["id"] ) );
        
$store["items"] = $this->sql->Sendsprintf"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->Sendsprintf"select id from giftbox_boxes where id = %d limit 1"$object[0] ) );
        
        if( 
$store["rows"] <= )
        {
            return 
"This giftbox does not exists.";
        }
        
        
$store $this->sql->Sendsprintf"select * from giftbox_items where boxid = %d"$object[0] ) );
        
        if( 
$store["rows"] <= )
        {
            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(0100);
        
        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"]) < )
        {
            return [
false"Actually you can not open this box, as it is not completed yet. Try again later please."];
        }
        
        
sort($object["obj"]);
        
        
$random rand(0count($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];
    }
    
}
Credits:

50 % - Xsrf (me) for writing the code

50 % - [Only registered and activated users can see links. Click Here To Register...] for having the idea and supporting me
01/22/2016 23:17 Yasunai#2
Da ich ja bei der Entwicklung zusehen konnte, muss ich sagen, dass man meine Idee für das Zufallssystem in der kurzen Zeit nicht viel besser hätte umsetzen können. Well done!
01/23/2016 05:32 xsrf#3
Ich vergaß zu erwähnen, dass dieses System aktuell auf MySQL basiert und somit noch auf MSSQL sowie eine der aktuellen Webseiten angepasst werden muss.

Dies bedeutet, dass die Querys sowie mein Handler durch euren ersetzt werden müssen.