Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # By @Totoka at pkodev.net
- class Item
- {
- protected $id = 0;
- protected $count = 0;
- protected $min_endure = 0;
- protected $max_endure = 0;
- protected $min_energy = 0;
- protected $max_energy = 0;
- protected $forge_level = 0;
- protected $dbid = 0;
- protected $dbparam1 = 0;
- protected $dbparam2 = 0;
- protected $instance = 0; # 0 | 1
- # if is an item instance == 1 {
- protected $attr_id = []; # array of attribute ids
- protected $attr_value = []; # array of attribute values
- # }
- private function __construct()
- {
- }
- public static function Create($itemid, $count)
- {
- $item = new Item;
- $item->id = $itemid;
- $item->count = $count;
- return $item;
- }
- public static function fromString($rawitem)
- {
- $value = explode(',', $rawitem);
- $count = count($value);
- $item = new Item;
- if( $count > 10 )
- {
- $item->id = $value[ 0];
- $item->count = $value[ 1];
- $item->min_endure = $value[ 2];
- $item->max_endure = $value[ 3];
- $item->min_energy = $value[ 4];
- $item->max_energy = $value[ 5];
- $item->forge_level = $value[ 6];
- $item->dbid = $value[ 7];
- $item->dbparam1 = $value[ 8];
- $item->dbparam2 = $value[ 9];
- $item->instance = $value[10];
- for( $i=11; $i<$count; $i+=2 )
- {
- $item->attr_id[] = $value[ $i + 0 ];
- $item->attr_value[] = $value[ $i + 1 ];
- }
- } return $item;
- }
- public function id()
- {
- return $this->id;
- }
- public function count()
- {
- return $this->count;
- }
- public function setEndure($min,$max)
- {
- $this->min_endure = $min;
- $this->max_endure = $max;
- }
- # returns an object with max and min properties
- # $obj->min | $obj->max
- public function endure()
- {
- return (object)[
- 'min'=>$this->min_endure,
- 'max'=>$this->max_endure
- ];
- }
- public function setEnergy($min,$max)
- {
- $this->min_energy = $min;
- $this->max_energy = $max;
- }
- # returns an object with max and min properties
- # $obj->min | $obj->max
- public function energy()
- {
- return (object)[
- 'min'=>$this->min_energy,
- 'max'=>$this->max_energy
- ];
- }
- public function setForgeLv($lv)
- {
- $this->forge_level = $lv;
- }
- public function forgeLv()
- {
- return $this->forge_level;
- }
- public function setDbID($id)
- {
- $this->dbid = $id;
- }
- public function dbid()
- {
- return $this->dbid;
- }
- public function setParam1($param)
- {
- $this->dbparam1 = $param;
- }
- public function param1()
- {
- return $this->dbparam1;
- }
- public function setParam2($param)
- {
- $this->dbparam2 = $param;
- }
- public function param2()
- {
- return $this->dbparam2;
- }
- # return false if @index is out of range [ 0 - 4 ]
- public function setAttr($index,$attrid,$value)
- {
- # instanced items can hold up to 5 attributes
- if($index<0||$index>4) return false;
- if(!$this->instance) {
- $this->attr_id[0] = $this->attr_value[0] = 0;
- $this->attr_id[1] = $this->attr_value[1] = 0;
- $this->attr_id[2] = $this->attr_value[2] = 0;
- $this->attr_id[3] = $this->attr_value[3] = 0;
- $this->attr_id[4] = $this->attr_value[4] = 0;
- } $this->instance = 1;
- $this->attr_id[$index] = $attrid;
- $this->attr_value[$index] = $value;
- return true;
- }
- # if @index exists
- # returns an object with properties id and value
- # return null if this index not exists, or is not used.
- # or false if @index is out of range [ 0 - 4 ]
- # $obj->id
- # $obj->value
- public function attr($index)
- {
- if($index<0||$index>4) return false;
- if(isset($this->attr_id[$index]))
- {
- return (object)[
- 'id' => $this->attr_id[$index],
- 'value' => $this->attr_value[$index]
- ];
- } return null;
- }
- public function getCRC32()
- {
- return sprintf("%d",
- $this->id
- + $this->count
- + $this->min_endure
- + $this->max_endure
- + $this->min_energy
- + $this->max_energy
- + $this->forge_level
- + $this->dbid
- + $this->dbparam1
- + $this->dbparam2
- + array_sum($this->attr_id)
- + array_sum($this->attr_value));
- }
- public function Serialize()
- {
- $str = sprintf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
- $this->id, # ^ ^ ^ ^ ^ ^ ^ ^ ^
- $this->count, # | | | | | | | |
- $this->min_endure, # | | | | | | |
- $this->max_endure, # | | | | | |
- $this->min_energy, # | | | | |
- $this->max_energy, # | | | |
- $this->forge_level, # | | |
- $this->dbid, # | |
- $this->dbparam1, # |
- $this->dbparam2 #
- );
- $count = count($this->attr_id);
- # if has any attribute
- # flag this item as an special instance.
- $str.=sprintf(",%d",($count > 0? '1' : '0'));
- for( $i=0; $i<$count; $i++ )
- {
- $str.=sprintf(",%d,%d",
- $this->attr_id[ $i ],
- $this->attr_value[ $i ]
- );
- } return $str;
- }
- }
- class Bag
- {
- # decrypted bag
- # used by constructor, Parse & Serialize
- protected $rawdec;
- # flag
- protected $is_empty = true;
- protected $crc32_ok = false;
- # raw content header
- protected $capacity = 0;
- protected $version = 0;
- protected $page = 0;
- protected $count = 0;
- protected $items = 0;
- # @rawbag = sql @content field from @resource table.
- public function __construct($rawbag)
- {
- # decrypt
- $key = '19800216';
- $hlh = strpos($rawbag, '#') + 1;
- $len = strlen($rawbag) - $hlh;
- $this->rawdec = substr($rawbag, 0, $hlh);
- for($i=0; $i<$len; $i++)
- {
- $chc = substr($rawbag,$i+$hlh,1);
- $chk = substr($key,$i%8,1);
- $this->rawdec .= chr( ord($chc) - ord($chk) );
- }
- }
- # uses previously loaded @rawdec within the constructor
- public function parse()
- {
- # sets the bag defaults to be empty
- $this->is_empty = true;
- # reset the item counter
- $this->count = 0;
- # retreive bag capacity
- $capacity = explode('@', $this->rawdec, 2);
- if(count($capacity) < 2) {
- return false;
- } $this->capacity = $capacity[0];
- # retreive bag version
- $version = explode('#', $capacity[1], 2); # dangerous index 1
- if(count($version) < 2) {
- return false;
- } $this->version = $version[0];
- # retreive bag page
- $page = explode(';', $version[1], 3); # dangerous index 1
- if(count($page) < 3) {
- return false;
- } $this->page = $page[0];
- # retrieve items count, and data
- $items = explode(';', $page[2]); # dangerous index 2
- $count = count($items);
- # initialize the array of items.
- $this->items = array_fill(0, $this->capacity, false);
- # will return on empty bags
- # 2 elements are required as minimum to continue
- if( $count < 2 ) {
- # true if empty, false on error
- return $count - 1 == $page[1];
- }
- # checks the minimum amount of parsed elements within the array
- if( $count - 1 != $page[1] ) {
- return false;
- }
- for( $i=0; $i<$count-1; $i++)
- {
- $index = explode(',', $items[$i], 2);
- if( $index < 2 ) {
- continue;
- }
- $item = Item::fromString( $index[1] );
- if( $item->getCRC32() ) {
- $this->items[ $index[0] ] = $item;
- } $this->count++;
- }
- $this->is_empty = $this->count == 0;
- # return true if the computed CRC32 is same as
- # the retreived one from the resource.
- return $this->crc32_ok
- = $items[ $count - 1 ] == $this->getCRC32();
- }
- # Returns maximum capacity of items
- public function capacity()
- {
- return $this->capacity;
- }
- # Retrieve raw list of item.
- # Do not modify the array itself, instead, modify its items;
- public function items()
- {
- return $this->items;
- }
- # Tries to add an @item to our bag.
- # It returns true on success
- public function push(Item $item)
- {
- $ret = false;
- for($i=0;$i<$this->capacity;$i++)
- {
- if($this->items[$i] == false)
- {
- $this->items[$i] = $item;
- $this->count++;
- $ret = true;
- break;
- }
- } return $ret;
- }
- # Return true if removed with success
- public function pop($index)
- {
- $ret = false;
- for($i=$index;$i<$this->capacity;$i++)
- {
- if($this->items[$i] != false)
- {
- $this->items[$i] = false;
- $this->count--;
- $ret = true;
- break;
- }
- } return $ret;
- }
- # Returns amount of free slots
- public function getFreeSlots()
- {
- $n = 0;
- for($i=0;$i<$this->capacity;$i++)
- {
- if($this->items[$i] == false)
- {
- $n++;
- }
- } return $n;
- }
- # calculate the CRC32 bag within it's items
- public function getCRC32()
- {
- $n = $this->page;
- $tmp = 0;
- for($i=0;$i<$this->capacity;$i++)
- {
- if($this->items[$i])
- {
- $tmp = $this->items[$i]->getCRC32();
- $n += $tmp;
- }
- } return sprintf("%d",$n);
- }
- # turns this object into an string
- public function Serialize()
- {
- $str = $this->capacity;
- $str.= '@'. $this->version;
- $str.= '#'. $this->page;
- $str.= ';'. $this->count;
- for($i=0;$i<$this->capacity;$i++)
- {
- if($this->items[$i])
- {
- $str.= ';'. $i;
- $str.= ','. $this->items[$i]->Serialize();
- }
- }
- $str.= ';'. $this->getCRC32();
- return $this->rawdec = $str;
- }
- public function Encrypt()
- {
- $key = '19800216';
- $hlh = strpos($this->rawdec, '#') + 1;
- $len = strlen($this->rawdec) - $hlh;
- $ret = substr($this->rawdec, 0, $hlh);
- for($i=0; $i<$len; $i++)
- {
- $chc = substr($this->rawdec,$i+$hlh,1);
- $chk = substr($key,$i%8,1);
- $ret .= chr( ord($chc) + ord($chk) );
- } return $ret;
- }
- };
- #################################################################
- # USAGE
- #################################################################
- # create our bag based on an entry from gamedb resource->content
- # decryption is performed at construction time
- $bag = new Bag( '24@114#btik`^gfakda\b]f]id`\b]f]id`\bllail' );
- ##################################
- # parse previously decrypted data
- if( $bag->parse() == false ) {
- die('parsing error, malformed or not supported data.');
- }
- ##################################
- # bag information
- echo '= Bag Information =' . PHP_EOL;
- echo '= Free slots: '. $bag->getFreeSlots() . PHP_EOL;
- echo '= CRC32: '. $bag->getCRC32() . PHP_EOL . PHP_EOL;
- ##################################
- # inserting items:
- # it does not check against an iteminfo.txt
- # becareful using count, and others attributes.
- $item = Item::Create(
- /*ID */ 1, // id short sword
- /*COUNT */ 1);
- ##################################
- # give it min/max duration
- # ~ ratio 50/1
- $item->setEndure(50*200,50*200);
- ##################################
- # give also some strength
- # use attribute slot 0 [ 0 - 4 ]
- # with attribute id 26 ( STR )
- # and value 100
- $item->setAttr(0,26,100);
- ##################################
- # add 3 sockets
- $item->setParam1(3000000000);
- ##################################
- # add previous created item to our bag
- if( $bag->push($item) == false )
- {
- die('unable to add an item, the bag is full');
- }
- ##################################
- # iterate through each items within the bag
- $bagItems = $bag->items();
- foreach ($bagItems as $index => $slot) {
- # check if there is an item within this slot
- if( $slot )
- { # it will be false if there is no item.
- # print item's information
- $x = null;
- echo '= Item is being printed =' . PHP_EOL;
- echo '= Slot #: '. $index . PHP_EOL;
- echo '= ID: '. $slot->id() . PHP_EOL;
- echo '= Count: '. $slot->count() . PHP_EOL;
- $x=$slot->endure();
- echo '= Endure: '. $x->min . '/' . $x->max . PHP_EOL;
- $x=$slot->energy();
- echo '= Energy: '. $x->min . '/' . $x->max . PHP_EOL;
- echo '= Forge Level: '. $slot->forgeLv() . PHP_EOL;
- echo '= DBID: '. $slot->dbid() . PHP_EOL;
- echo '= DBParam1: '. $slot->param1() . PHP_EOL;
- echo '= DBParam2: '. $slot->param2() . PHP_EOL;
- if( $slot->id() == 1234 )
- {
- # removes item from bag
- # with id 1234
- if($bag->pop($index) == false)
- {
- die('= Item can not be removed');
- }
- } echo PHP_EOL;
- }
- }
- ##################################
- # turn the bag into string
- echo '= BAG To string: '. $bag->Serialize() . PHP_EOL . PHP_EOL;
- ##################################
- # bag encryption
- die( '= BAG Encrypted: '. $bag->Encrypt() );
- #################################################################
Add Comment
Please, Sign In to add comment