Jump to content
.InyaProduction

[PHP]Player information lib with cache v1.0b

Recommended Posts

This one is just interesting for ppl with php knowledge.

You have to have an active mysql connection to your server with SELECT rights to player.player

There are 2 methods for now:

loadPlayerByName($string);

loadPlayerById($int);

it will return an array with the most important information, aswell as caching the informations for 5 minutes (variable with setCacheTime($seconds) ;)

The cache will look like this atm:

 

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
Ok without further speaking heres the code (at the bottom is an example for initialising an an print_r on the return:

 

<?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

php.zip

  • Love 7

Share this post


Link to post

Clean coded. You can port to your CMS. :)  Thank you!

Share this post


Link to post

You could improve your coding even more.

  • Try using PDO/MySQLi instead of php's native mysql lib
  • Use some sort of config options instead of harcoding databases/tables names
  • If you declare every function as "public" even if they don't need to be public, it will make no sense to specify it's visibility
  • Try using | as a separator instead of :::
  • When you write player's cache, you can use a foreach to iterate through player data, no need to write all those lines

Share this post


Link to post

Ignoring the fact that mysql_* is deprecated.

When you make a fetch like in this example:  SELECT * FROM player.player WHERE id = 'someID'; the mysqli or pdo methods will return an array.

How you could optimize the script (by number of lines)

<?php

class Player
{
	private $data;

	public function __construct($playerId)
	{
		/** 
		 * if playerId is integer fetch player data by `id` field else
		 * by `name` field 
		 */
	}

	private function fetchById($id)
	{
		$this->data = .....;
	}

	private function fetchByName($name)
	{
		$this->data = .....;
	}

	public function __get($name)
	{
		if (isset($this->data[$name]))
		{
			return $this->data[$name];
		}
		return false;
	}

	public function __set($name, $value)
	{
		if (isset($this->data[$name]))
		{
			$this->data[$name] = $value;

			// mark field to be updated
		}
	}

	public function __destruct()
	{
		// update the player table query
	}
}

$p = new Player(10);
// $p = new Player('Tdog');

echo $p->name;
echo $p->job;

$p->name = 'JBond'; // will update the field in the database

As for the cache, you could use JSON because is easy to use and portable. (You can load info about char with javascript, C# ... )

Anyway thanks for the share :D

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.