[PHP]Player information lib mit cache

03/20/2014 21:23 .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
Du solltest den Code evtl. in ein Tag packen, so sieht das echt grausam aus und ich mag es gar nicht bewerten ._. Was du dir aber angewöhnen solltest, lass den mysql_query - Kram weg, nutze neuere Methoden dafür.
03/20/2014 21:27 .Marcel'#3
WIe mein Vorposter schon gesagt hat, bitte die PHP-Tags nutzen, und die mysql_* Funktionen sind veraltet. Das könntest du noch besser machen, aber ansich eine nette Idee.
03/20/2014 21:32 .Inya#4
Ich weiß mysql query ist kacke.. aber ist halt fürs hen cms. Da es eh nur leute mit ahnung nutzen können, kann man da ja wohl easy eben mysqli oder PDO einbauen ;)
03/21/2014 12:33 Sphinx²#5
Du schreibst hier echt so ca. 75% deines Codes umsonst....

Das hab ich kurz in 5 Minuten geschrieben.
Kurze Erklärung: Du kannst mit json_encode & json_decode ganz einfach
Arrays in einen String umwandeln und wieder zurück.
Ich versteh einfach nicht warum du im constructor soviele defines machst die einfach kein Mensch braucht.
PHP Code:
<?php

    
class player {        
        public function 
loadData($name) {
            if(
$this->cacheExists()) {
                
$query dbmanager::instance()->query("SELECT * FROM player where name = '".dbmanager::instance()->escape($name)."'");
                if(
$query) {
                    
$this->player dbmanager::instance()->fetcha($query);
                    
$this->writeToCache(3600);
                    return 
$this->player;
                } else {
                    return 
false;    
                }
            } else {
                return 
$this->getCachedPlayer();
            }
        }
        
        public function 
cacheExists() {
            .....
            return 
false;    
        }
        
        public function 
getCachedPlayer() {
            ...
            
$this->player json_decode($data); // Loaded text from text file
            
return $this->player;
        }
        
        public function 
writeToCache($time) {
            .....
            
$data json_encode($this->player);     // Arrrrraay to text (for text file ;*)
            
fwrite($txthandle,$data,strlen($data));
        }
    }

        
$player = new player();
        
$info $player->loadData("Sphinx");
        
var_dump($info);
?>
Und als Cache würde ich dir phpfastcache library empfehlen.
Die Library hat echt eine hohe Performance und schreibt auch in die Memory wenns der Server unterstützt.
Das was du hier machst ist auch nicht wirklich so toll.

Freundliche Grüsse
Sphinx
03/21/2014 12:48 .Inya#6
Quote:
Originally Posted by Sphinx² View Post
Du schreibst hier echt so ca. 75% deines Codes umsonst....

Das hab ich kurz in 5 Minuten geschrieben.
Kurze Erklärung: Du kannst mit json_encode & json_decode ganz einfach
Arrays in einen String umwandeln und wieder zurück.
Ich versteh einfach nicht warum du im constructor soviele defines machst die einfach kein Mensch braucht.
PHP Code:
<?php

    
class player {        
        public function 
loadData($name) {
            if(
$this->cacheExists()) {
                
$query dbmanager::instance()->query("SELECT * FROM player where name = '".dbmanager::instance()->escape($name)."'");
                if(
$query) {
                    
$this->player dbmanager::instance()->fetcha($query);
                    
$this->writeToCache(3600);
                    return 
$this->player;
                } else {
                    return 
false;    
                }
            } else {
                return 
$this->getCachedPlayer();
            }
        }
        
        public function 
cacheExists() {
            .....
            return 
false;    
        }
        
        public function 
getCachedPlayer() {
            ...
            
$this->player json_decode($data); // Loaded text from text file
            
return $this->player;
        }
        
        public function 
writeToCache($time) {
            .....
            
$data json_encode($this->player);     // Arrrrraay to text (for text file ;*)
            
fwrite($txthandle,$data,strlen($data));
        }
    }

        
$player = new player();
        
$info $player->loadData("Sphinx");
        
var_dump($info);
?>
Und als Cache würde ich dir phpfastcache library empfehlen.
Die Library hat echt eine hohe Performance und schreibt auch in die Memory wenns der Server unterstützt.
Das was du hier machst ist auch nicht wirklich so toll.

Freundliche Grüsse
Sphinx
Ja ich weiß die defines im Constructor sind unnötig.. bin iwie zu sehr an andere sprachen gewöhnt momentan :S an den json kram hatte ich iwie nicht gedacht auch wenn ich andauernd damit arbeiten muss o.o naja wie gesagt war eine lw Sache^^ trd danke für den Gedankenstoß :D
03/21/2014 20:25 Sphinx²#7
Kein Thema...

Btw: Mit der Fastcachelibrary kannst du automatisch arrays speichern.
Sie müssen nicht in einen JSON String umgewandelt werden.
Dazu gibt es ne gute Dokumentation.
Mein neues CMS was ich schreibe verwende ich das ziemlich oft für alle Sachen die eigentlich unnötig oft abgerufen werden.

Evtl. Release ich das wenn ich es nicht mehr brauche und mit Metin2 abgeschlossen habe.

Freundliche Grüsse
Sphinx