public function prepare()
 {
     global $schema, $asset_info, $settings;
     $this->template = "item";
     $this->tab = 'item';
     $b = new backpack(false);
     $item_stats = cache::Memcached()->get('item_stats');
     if ($item_stats === false) {
         $json = file_get_contents($settings['cache']['folder'] . 'item_stats.json');
         $item_stats = json_decode($json, true);
         cache::Memcached()->set('item_stats', $item_stats, time() + 60 * 15);
     }
     cache::inc('tf2_item_info.php');
     $defindex = $this->request[1];
     $url_name = $this->request[0];
     $s = $item_stats['items'][$defindex];
     $si = $schema['items'][$defindex];
     $info = $asset_info[$defindex];
     //var_dump($info);
     $i = $b->get_item($si);
     $this->canonical = sprintf('http://tf2stats.net/item/%s/%s/', $i['name_url'], $i['defindex']);
     $i['owned'] = $s['unique_total'] / $item_stats['total_players'];
     $i['equipped'] = $s['total_equipped'] / $item_stats['total_players'];
     $i['owned_equipped'] = $s['unique_total'] > 0 ? $s['total_equipped'] / $s['unique_total'] : 0;
     // Colors
     if (!empty($s['colors'])) {
         arsort($s['colors']);
         foreach ($s['colors'] as $c => $num) {
             $id = get_paint_by_color($c);
             $si = $schema['items'][$id];
             $it = $b->get_item($si);
             $name = $it['name'];
             if (strlen($name) > 25) {
                 $name = substr($name, 0, 25) . "...";
             }
             //var_dump($it);
             $html = dechex($c);
             if ($html === '0') {
                 continue;
             }
             //$html = '914c3f';
             $key = $num;
             while ($i['colors'][$key] > 0) {
                 $key++;
             }
             $i['colors'][$key] = array('name' => $name, 'color' => $html, 'num' => $num / $s['total_colored']);
         }
     }
     // qualities
     if (!empty($s['qualities'])) {
         arsort($s['qualities']);
         foreach ($s['qualities'] as $c => $num) {
             $key = $num;
             while ($i['qualities'][$key] > 0) {
                 $key++;
             }
             $quality = int_to_quality($c);
             $label = quality_to_label($quality);
             //$color = quality_to_color($quality);
             $i['qualities'][$key] = array('color' => $color, 'num' => $num / $s['total'], 'label' => $label, 'quality' => $quality);
         }
     }
     // Origins
     if (!empty($s['origins'])) {
         arsort($s['origins']);
         foreach ($s['origins'] as $c => $num) {
             $key = $num;
             while ($i['origins'][$key] > 0) {
                 $key++;
             }
             $label = int_to_origin($c);
             //$color = quality_to_color($quality);
             $i['origins'][$key] = array('num' => $num / $s['total'], 'label' => $label);
         }
         arsort($s['effects']);
     }
     // effects
     if (!empty($s['effects'])) {
         $total_effects = 0;
         foreach ($s['effects'] as $c => $num) {
             $total_effects += $num;
         }
         foreach ($s['effects'] as $c => $num) {
             $t = $s['total_effect'] - $s['effects']['-1'];
             if ($c == -1) {
                 continue;
             }
             $key = $num;
             while ($i['effects'][$key] > 0) {
                 $key++;
             }
             $label = int_to_effect($c);
             $i['effects'][$key] = array('color' => $color, 'num' => $num / $total_effects, 'label' => $label);
         }
     }
     if (!empty($i['used_by_classes'])) {
         if (count($i['used_by_classes']['class']) < 1) {
             for ($c = 0; $c < 9; $c++) {
                 $num = $s['equipped'][$c];
                 if ($s['total_equipped'] > 0) {
                     $i['classes'][] = array('class' => int_to_class($c), 'id' => $c, 'num' => $num / $s['total_equipped']);
                 }
             }
         }
     }
     $this->title = sprintf("%s statistics", htmlspecialchars($i['name']));
     $this->params['item'] = $i;
     $this->params['url_name'] = $url_name;
     $this->params['stats'] = $s;
     $this->params['total'] = $item_stats['total_players'];
     $this->params['info'] = $info;
     $this->params['debug'] = $debug;
 }
 public function prepare()
 {
     global $schema, $settings;
     $this->template = "cosmetic";
     $this->tab = 'item';
     $this->title = 'Cosmetic item statistics';
     $this->canonical = 'http://tf2stats.net/misc_items/';
     $sort_table = array('owned' => 'total', 'equipped' => 'total_equipped', 'own_equip' => 'owned_equipped');
     $b = new backpack(false);
     $sortkey = 'total';
     if ($this->request['sort'] && array_key_exists($this->request['sort'], $sort_table)) {
         $sortkey = $sort_table[$this->request['sort']];
         $this->params['sort'][$this->request['sort']] = 'selected';
     } else {
         $this->params['sort']['owned'] = 'selected';
     }
     $item_stats = cache::Memcached()->get('item_stats');
     if ($item_stats === false) {
         $json = file_get_contents($settings['cache']['folder'] . 'item_stats.json');
         $item_stats = json_decode($json, true);
         cache::Memcached()->set('item_stats', $item_stats, time() + 60 * 15);
     }
     foreach ($item_stats['items'] as $defindex => $s) {
         if (in_array($defindex, $settings['items']['ignore'])) {
             continue;
         }
         $si = $schema['items'][$defindex];
         if ($si['item_slot'] == 'misc') {
             $i = $b->get_item($si);
             $i['owned'] = $s['total'] / $item_stats['total_players'];
             if ($i['owned'] > 1) {
                 $i['owned'] = 1;
             }
             $i['equipped'] = $s['total_equipped'] / $item_stats['total_players'];
             $i['owned_equipped'] = $s['total_equipped'] / $s['total'];
             $s['owned_equipped'] = intval($s['total_equipped'] / $s['total'] * 1000);
             // Colors
             arsort($s['colors']);
             foreach ($s['colors'] as $c => $num) {
                 $html = dechex($c);
                 if ($html === '0') {
                     continue;
                 }
                 //$html = '914c3f';
                 $key = $num;
                 while ($i['colors'][$key] > 0) {
                     $key++;
                 }
                 $i['colors'][$key] = array('color' => $html, 'num' => $num / $s['total_colored']);
             }
             if (count($i['used_by_classes']['class']) < 1) {
                 for ($c = 1; $c <= 9; $c++) {
                     $num = $s['equipped'][$c];
                     if ($s['total_equipped'] > 0) {
                         $i['classes'][] = array('class' => int_to_class($c), 'id' => $c, 'num' => $num / $s['total_equipped']);
                     }
                 }
             }
             $key = $s[$sortkey];
             while ($this->params['hats'][$key] > 0) {
                 $key++;
             }
             $this->params['hats'][$key] = $i;
         }
     }
     krsort($this->params['hats']);
 }
 function get_item($i)
 {
     global $schema;
     $i['id'] = sprintf("%u", $i['id']);
     $it = $schema['items'][$i['defindex']];
     if (is_array($it['attributes'])) {
         foreach ($it['attributes'] as $a) {
             $indexed[$a['name']] = $a;
         }
     }
     $it['attributes'] = $indexed;
     if ($i['attributes']) {
         foreach ($i['attributes'] as $a) {
             // Remove existing attrs with this index
             $v = $schema['attributes'][$a['defindex']];
             $v['value'] = $a['value'];
             $v['float_value'] = $a['float_value'];
             $it['attributes'][$v['name']] = $v;
         }
     }
     foreach ($i as $key => $var) {
         if ($key != 'attributes') {
             $it[$key] = $var;
         }
     }
     $it['position'] = $i['inventory'] & 0xffff;
     //$e = get_equipped($i['inventory']);
     //$e = equipped_convert($i['equipped']);
     $it['equipped'] = $i['equipped'];
     // language keys
     /*$it['type'] = get_lang_key('ItemTypeDesc',array(
     		1 => ($it['level'] > 0) ? $it['level'] : 1,
     		2 => get_lang_key($it['item_type_name'])
     		));*/
     $it['type'] = sprintf("Level %s %s", isset($it['level']) ? $it['level'] : 1, get_lang_key($it['item_type_name']));
     $it['name'] = htmlspecialchars(get_item_name($it));
     $it['name_url'] = rawurlencode(preg_replace("/[^a-zA-Z0-9]/", "-", $it['name']));
     $it['desc'] = $it['item_description'];
     if ($i['custom_desc']) {
         $it['desc'] = htmlspecialchars($i['custom_desc']);
     }
     //$it['attributes'] = $it['attributes']['attribute'];
     $dd = 0;
     if ($it['attributes']) {
         foreach ($it['attributes'] as $a) {
             $a_class = get_attribute($a['name']);
             if ($a['class'] == 'set_item_tint_rgb') {
                 $it['color'] = attribute_value($a);
                 //echo $a['value']."<br>";
             }
             if ($a['name'] == 'set supply crate series') {
                 $series = attribute_value($a);
                 $it['crate_series'] = $series;
                 $it['tooltip_tail'] .= get_crate_tooltip($series);
                 //echo $it['tooltip_tail'];
             }
             if ($a['name'] == 'kill eater') {
                 $it['kill_eater_kills'] = attribute_value($a);
                 $it['kill_eater_rank'] = get_kill_eater_rank($a);
             }
             $value = $a['value'];
             //echo $a_class['description_string'];
             //$description = sprintf($format_string,$value);
             if ($a['hidden'] == "1" || $a_class['hidden'] == "1") {
                 continue;
             }
             $desc = get_attribute_text($value, $a_class, $a['float_value']);
             $it['attrs'][] = $desc;
             // Colors use format imagename.color.png
             //var_dump($a);
             //die();
         }
     }
     $bits = explode('/', $it['image_url']);
     $it['image'] = $bits[count($bits) - 1];
     $it['tooltip'] = backpack::generate_tooltip($it);
     if ($it['color'] && $it['image_inventory'] == 'backpack/player/items/crafting/paintcan') {
         $it['image'] = str_replace('.png', '.' . $it['color'] . '.png', $it['image']);
     }
     if ($it['equipped']) {
         foreach ($it['equipped'] as $e) {
             $cn = int_to_class($e['class']);
             $it['equipped_by'][$cn] = true;
             $this->equipped[$cn][int_to_slot($e['slot'])] = $it;
         }
     }
     return $it;
 }