[PHP]Player information lib mit cache

03/20/2014 21:24 .Inya#1
Nur für leute mit Php kenntnissen gedacht!

Also wenn ihr auf der hp iwas machen wollt, sei es im ACP details zu einem character auszugeben oder sonstiges könnte euch das hier unterstützen. Habs aus purer lw gemacht aber kommt mir nicht mit kleine rls! Das sind 2h arbeit gewesen ja! xD

Naja die cache files sehen so aus:

PHP Code:
ID:::11159
NAME
:::Rain
LEVEL
:::80
ACC_ID
:::1
EXP
:::277
JOB
:::6
X
:::64184
Y
:::183833
MAP
:::21
E_X
:::64184
E_Y
:::183833
E_MAP
:::21
HP
:::3693
MP
:::1722
PLAYTIME
:::79
VIT
:::3
STR
:::5
INT
:::5
DEX
:::3
STATS
:::238
GOLD
:::0
IP
:::5.148.132.52
SKGRP
:::1
HORSE_LEVEL
:::0
RIDING
:::0
HSTATS
:::0
STATRESET
:::0
CHANGENAME
:::0
SKILL_POINT
:::0
LAST_PLAY
:::2011-05-16 16:02:57
ALIGNMENT
:::491 
Hier der code:


PHP Code:
<?php
class char{
    function 
__construct(){
        
$this->charinfo['id'] = 0;
        
$this->charinfo['name'] = "";
        
$this->charinfo['level'] = 0;
        
$this->charinfo['account_id'] = 0;
        
$this->charinfo['exp'] = 0;
        
$this->charinfo['job'] = 0;
        
$this->charinfo['x'] = 0;
        
$this->charinfo['y'] = 0;
        
$this->charinfo['map_index'] = 0;
        
$this->charinfo['exit_x'] = 0;
        
$this->charinfo['exit_y'] = 0;
        
$this->charinfo['exit_map_index'] = 0;
        
$this->charinfo['hp'] = 0;
        
$this->charinfo['mp'] = 0;
        
$this->charinfo['playtime'] = 0;
        
$this->charinfo['ht'] = 0;
        
$this->charinfo['st'] = 0;
        
$this->charinfo['iq'] = 0;
        
$this->charinfo['dx'] = 0;
        
$this->charinfo['stat_point'] = 0;
        
$this->charinfo['skill_point'] = 0;
        
$this->charinfo['alignment'] = 0;
        
$this->charinfo['last_play'] = "";
        
$this->charinfo['gold'] = 0;
        
$this->charinfo['ip'] = "";
        
$this->charinfo['skill_group'] = 0;
        
$this->charinfo['horse_level'] = 0;
        
$this->charinfo['horse_riding'] = 0;
        
$this->charinfo['horse_skill_point'] = 0;
        
$this->charinfo['stat_reset_count'] = 0;
        
$this->charinfo['change_name'] = 0;
        
$this->cache = new char_cachemanager();
    }
    
    public function 
loadPlayerById($id$reload false){
        
$cached_char $this->cache->loadCharById($id);
        if(
$cached_char != false && $reload == false){
            
$this->charinfo['id'] = $id;
            
$this->charinfo['name'] = $cached_char['NAME'];
            
$this->charinfo['level'] = $cached_char['LEVEL'];
            
$this->charinfo['account_id'] = $cached_char['ACC_ID'];
            
$this->charinfo['exp'] = $cached_char['EXP'];
            
$this->charinfo['job'] = $cached_char['JOB'];
            
$this->charinfo['x'] = $cached_char['X'];
            
$this->charinfo['y'] = $cached_char['Y'];
            
$this->charinfo['map_index'] = $cached_char['MAP'];
            
$this->charinfo['exit_x'] = $cached_char['E_X'];
            
$this->charinfo['exit_y'] = $cached_char['E_Y'];
            
$this->charinfo['exit_map_index'] = $cached_char['E_MAP'];
            
$this->charinfo['hp'] = $cached_char['HP'];
            
$this->charinfo['mp'] = $cached_char['MP'];
            
$this->charinfo['playtime'] = $cached_char['PLAYTIME'];
            
$this->charinfo['ht'] = $cached_char['VIT'];
            
$this->charinfo['st'] = $cached_char['STR'];
            
$this->charinfo['iq'] = $cached_char['INT'];
            
$this->charinfo['dx'] = $cached_char['DEX'];
            
$this->charinfo['stat_point'] = $cached_char['STATS'];
            
$this->charinfo['skill_point'] = $cached_char['SKILL_POINT'];
            
$this->charinfo['alignment'] = $cached_char['ALIGNMENT'];
            
$this->charinfo['last_play'] = $cached_char['LAST_PLAY'];
            
$this->charinfo['gold'] = $cached_char['GOLD'];
            
$this->charinfo['ip'] = $cached_char['IP'];
            
$this->charinfo['skill_group'] = $cached_char['SKGRP'];
            
$this->charinfo['horse_level'] = $cached_char['HORSE_LEVEL'];
            
$this->charinfo['horse_riding'] = $cached_char['RIDING'];
            
$this->charinfo['horse_skill_point'] = $cached_char['HSTATS'];
            
$this->charinfo['stat_reset_count'] = $cached_char['STATRESET'];
            
$this->charinfo['change_name'] = $cached_char['CHANGENAME'];
            return 
$this->charinfo;
        } else {
            
$player_mysql mysql_query("SELECT `id`, `account_id`, `name`, `job`, `voice`, `dir`, `x`, `y`, `z`, `map_index`, `exit_x`, `exit_y`, `exit_map_index`, `hp`, `mp`, `stamina`, `random_hp`, `random_sp`, `playtime`, `level`, `level_step`, `st`, `ht`, `dx`, `iq`, `exp`, `gold`, `stat_point`, `skill_point`, `quickslot`, `ip`, `part_main`, `part_base`, `part_hair`, `skill_group`, `skill_level`, `alignment`, `last_play`, `change_name`, `mobile`, `sub_skill_point`, `stat_reset_count`, `horse_hp`, `horse_stamina`, `horse_level`, `horse_hp_droptime`, `horse_riding`, `horse_skill_point` FROM `player`.`player` WHERE id = ".$id);
            
$player mysql_fetch_array($player_mysql);
            if(!
$player)
                return 
false;
            foreach (
$player as $key => $info){
                if(!
is_numeric($key))
                    
$this->charinfo[$key] = $info;
            }
            
$this->cache->writeCache($this->charinfo);
            return 
$this->charinfo;
        }
    }
    
    public function 
loadPlayerByName($name$reload false){
        
$cached_char $this->cache->loadCharByName($name);
        if(
$cached_char != false && $reload == false){
            
$this->charinfo['id'] = $cached_char['ID'];
            
$this->charinfo['name'] = $cached_char['NAME'];
            
$this->charinfo['level'] = $cached_char['LEVEL'];
            
$this->charinfo['account_id'] = $cached_char['ACC_ID'];
            
$this->charinfo['exp'] = $cached_char['EXP'];
            
$this->charinfo['job'] = $cached_char['JOB'];
            
$this->charinfo['x'] = $cached_char['X'];
            
$this->charinfo['y'] = $cached_char['Y'];
            
$this->charinfo['map_index'] = $cached_char['MAP'];
            
$this->charinfo['exit_x'] = $cached_char['E_X'];
            
$this->charinfo['exit_y'] = $cached_char['E_Y'];
            
$this->charinfo['exit_map_index'] = $cached_char['E_MAP'];
            
$this->charinfo['hp'] = $cached_char['HP'];
            
$this->charinfo['mp'] = $cached_char['MP'];
            
$this->charinfo['playtime'] = $cached_char['PLAYTIME'];
            
$this->charinfo['ht'] = $cached_char['VIT'];
            
$this->charinfo['st'] = $cached_char['STR'];
            
$this->charinfo['iq'] = $cached_char['INT'];
            
$this->charinfo['dx'] = $cached_char['DEX'];
            
$this->charinfo['stat_point'] = $cached_char['STATS'];
            
$this->charinfo['skill_point'] = $cached_char['SKILL_POINT'];
            
$this->charinfo['alignment'] = $cached_char['ALIGNMENT'];
            
$this->charinfo['last_play'] = $cached_char['LAST_PLAY'];
            
$this->charinfo['gold'] = $cached_char['GOLD'];
            
$this->charinfo['ip'] = $cached_char['IP'];
            
$this->charinfo['skill_group'] = $cached_char['SKGRP'];
            
$this->charinfo['horse_level'] = $cached_char['HORSE_LEVEL'];
            
$this->charinfo['horse_riding'] = $cached_char['RIDING'];
            
$this->charinfo['horse_skill_point'] = $cached_char['HSTATS'];
            
$this->charinfo['stat_reset_count'] = $cached_char['STATRESET'];
            
$this->charinfo['change_name'] = $cached_char['CHANGENAME'];
            return 
$this->charinfo;
        } else {
            
$player_mysql mysql_query("SELECT `id`, `account_id`, `name`, `job`, `voice`, `dir`, `x`, `y`, `z`, `map_index`, `exit_x`, `exit_y`, `exit_map_index`, `hp`, `mp`, `stamina`, `random_hp`, `random_sp`, `playtime`, `level`, `level_step`, `st`, `ht`, `dx`, `iq`, `exp`, `gold`, `stat_point`, `skill_point`, `quickslot`, `ip`, `part_main`, `part_base`, `part_hair`, `skill_group`, `skill_level`, `alignment`, `last_play`, `change_name`, `mobile`, `sub_skill_point`, `stat_reset_count`, `horse_hp`, `horse_stamina`, `horse_level`, `horse_hp_droptime`, `horse_riding`, `horse_skill_point` FROM inyaproduction_.`player` WHERE name = '".$name."'") or die(mysql_error());
            
$player mysql_fetch_array($player_mysql);
            if(!
$player)
                return 
false;
            foreach (
$player as $key => $info){
                if(!
is_numeric($key))
                    
$this->charinfo[$key] = $info;
            }
            
$this->cache->writeCache($this->charinfo);
            return 
$this->charinfo;
        }
    }

    public function 
setCacheTime($seconds){
        
$this->cache->setCacheTime($seconds);
    }

    function 
__destruct(){
    }
    
}

class 
char_cachemanager{
    function 
__construct(){
        
$this->cachedir "./cached_chars/";
        if(!
is_dir($this->cachedir)){
            
mkdir($this->cachedir);
        }
        
$this->chars = array();
        if(!
$this->loadCachedChars())
            die(
"Character Cache: Error on loading cache");
        
$this->cachetime 300//5 minutes
    
}

    public function 
setCacheTime($seconds){
        
$this->cachetime $seconds;
    }
    public function 
loadCachedChars(){
        
$cachefiles scandir($this->cachedir);
        foreach (
$cachefiles as $file) {
            if(!(
$file == "." or $file == "..")){
                
$filename $file;
                
$temp explode("."$file);
                
$file $temp[0];
                
$data explode("_"$file);
                
$this->chars[$data[0]]['name'] = $data[1];
                
$this->chars[$data[0]]['time'] = $data[2];
                
$this->chars[$data[0]]['filename'] = $filename;
            }
        };
        return 
true;
    }
    
    public function 
loadFile($filename){
        if(
file_exists($this->cachedir.$filename)){
            
$data file_get_contents($this->cachedir.$filename);
            
$lines explode("\n"$data);
            
$character = array();
            foreach(
$lines as $line){
                
$detail explode(":::"$line);
                
$character[$detail[0]] = $detail[1];
            }
            return 
$character;
        }
        return 
false;
    }
    
    public function 
loadCharById($id){
        if(isset(
$this->chars[$id]) && $this->chars[$id]['time'] + $this->cachetime time())
            return 
$this->loadFile($this->chars[$id]['filename']);

        return 
false;
    }
    
    public function 
loadCharByName($name){
        foreach(
$this->chars as $key => $char){
            if(
$char['name'] == $name && $this->chars[$key]['time'] + $this->$cachetime time())
                return 
$this->loadFile($this->chars[$key]['filename']);
        }
        return 
false;
    }
    
    public function 
writeCache($array){
        
$filename $array['id']."_".$array['name']."_".time();
        
$file fopen($this->cachedir.$filename'w');
        
$text "
ID:::"
.$array['id']."
NAME:::"
.$array['name']."
LEVEL:::"
.$array['level']."
ACC_ID:::"
.$array['account_id']."
EXP:::"
.$array['exp']."
JOB:::"
.$array['job']."
X:::"
.$array['x']."
Y:::"
.$array['y']."
MAP:::"
.$array['map_index']."
E_X:::"
.$array['exit_x']."
E_Y:::"
.$array['exit_y']."
E_MAP:::"
.$array['exit_map_index']."
HP:::"
.$array['hp']."
MP:::"
.$array['mp']."
PLAYTIME:::"
.$array['playtime']."
VIT:::"
.$array['ht']."
STR:::"
.$array['st']."
INT:::"
.$array['iq']."
DEX:::"
.$array['dx']."
STATS:::"
.$array['stat_point']."
GOLD:::"
.$array['gold']."
IP:::"
.$array['ip']."
SKGRP:::"
.$array['skill_group']."
HORSE_LEVEL:::"
.$array['horse_level']."
RIDING:::"
.$array['horse_riding']."
HSTATS:::"
.$array['horse_skill_point']."
STATRESET:::"
.$array['stat_reset_count']."
CHANGENAME:::"
.$array['change_name']."
SKILL_POINT:::"
.$array['skill_point']."
LAST_PLAY:::"
.$array['last_play']."
ALIGNMENT:::"
.$array['alignment'];
        
fwrite($file$text);
    }
    
}

$character = new char();
print_r($character->loadPlayerByName("Rain"));
Have fun with that :P

Methoden:
loadPlayerByName($name)
loadPlayerById($id)
setCacheTime($seconds)

unten drunter istn beispiel mit print_r ;D
mysql verbindung muss stehen
03/20/2014 21:25 ლ(ಠ益ಠლ)#2
Oh, ich habe gerade dein zweites Thema beantwortet - Das solltest du wohl schließen lassen, aber hier gilt das selbe wie im anderen Thema =P
03/20/2014 21:33 .Inya#3
Ups.. mein fail. Hab ne blanke seite beim absenden bekommen deshalb mit F5 neu geladen.. war wohl nicht so schlau xD

#closerequest
03/20/2014 21:42 Zevion#4
#closed