Location: PHPKode > projects > PhpMySphere > phpMySphere-devel/src/module/SphereChars.php
<?php
/***************************************************************************
 *   Copyright (C) 2003 by Sebastian Hess                                  *
 *   hide@address.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

class SphereChars {

	var $__Path;
	var $__Chars;
	var $__Items;

	var $__serialChar;
	var $__serialItem;

	/*
		I placed it here and not in CharInfo.php, because it's loaded
		one time here, in the other File it would be loaded for every char
	*/
	var $Skilltable = array("ALCHEMY","ANATOMY","ANIMALLORE","ITEMID","ARMSLORE","PARRYING","BEGGING","BLACKSMITHING","BOWCRAFT","PEACEMAKING","CAMPING","CARPENTRY","CARTOGRAPHY","COOKING","DETECTINGHIDDEN","ENTICEMENT","EVALUATINGINTEL","HEALING","FISHING","FORENSICS","HERDING","HIDING","PROVOCATION","INSCRIPTION","LOCKPICKING","MAGERY","MAGICRESISTANCE","TACTICS","SNOOPING","MUSICIANSHIP","POISONING","ARCHERY","SPIRITSPEAK","STEALING","TAILORING","TAMING","TASTEID","TINKERING","TRACKING","VETERINARY","SWORDSMANSHIP","MACEFIGHTING","FENCING","WRESTLING","LUMBERJACKING","MINING","MEDITATION","STEALTH","REMOVETRAP","NECROMANCY");
	var $Statstable  = array("HITS","STAM","MANA","INT","STR","DEX","FOOD");

	var $Ready;

	/**
		@param $readtype	int		0=everything, 1=only chars 2=only items
	*/
	function SphereChars($pathofcharscp,$readtype=0) {
		$this->Ready = false;
		$this->__serialChar=array();
		$this->__serialItem=array();
		$this->__Path = $pathofcharscp;
		$this->__read($readtype);
	}

	function __read($readtype) {
		/*
			Init the Variables
		*/
		$lastAcc = "";
		$i = 0;
		$Type = "";
		/*
			Open spherechars.scp
		*/
		$handle = fopen($this->__Path,"r");

		/*
			The file doesn't exist or isn't readable
		*/
		if(!$handle)
			return false;

		/*
			Read out the file
		*/
		while (!feof($handle) && $handle) {
			$buffer = chop(fgets($handle, 4096));

			/*
				Check if we got a new User Entry
				Don't stop reading at [EOF] because we check for
				bugs/manipulations in the Accountfile later
			*/
			if ($buffer{0} == "[") {

				/*
					If it's EOF, so exit here, we're done !
				*/
				if($buffer == "[EOF]")
					break;

				$Type = substr($buffer,1,9);
				/*
					Save the ID (this is c_man|c_female for a player and
								i_<itemname> for an item)
				*/
				if($Type == "WORLDITEM") {
					if($readtype != 1) {
						$i = count($this->__Items);
						$this->__Items[$i]["ID"] = substr($buffer,11,-1);
					}
				} elseif($Type == "WORLDCHAR") {
					if($readtype != 2) {
						$i = count($this->__Chars);
						$this->__Chars[$i]["ID"] = substr($buffer,11,-1);
					}
				} else {
					die("The type $Type is not supported in the Worldfile !");
				}
				continue;
			}

			/*
				Split the lines so that
				PLEVEL=Owner
				will be splittet in
				$Parm = PLEVEL
				and
				$Zuweisung = Owner

				I use strtoupper to use the samewriten index everywhere
			*/
			list($Parm,$Zuweisung) = split("=",$buffer);


			$Parm = trim(strtoupper($Parm));

			/*
				Check is both the Name and the Value are avaible
				if not continue with next line
			*/
			if(empty($Parm) || empty($Zuweisung)) continue; // To avoid Errors on saving

			if($Type == "WORLDITEM" && $readtype != 1) {

				/*
					Build Hash of UIDs, to find the Data faster
				*/
				if($Parm == "SERIAL")
					$this->__serialItem[] = $Zuweisung;

				$this->__Items[$i][$Parm] = $Zuweisung;
			} elseif($Type == "WORLDCHAR" && $readtype != 2) {
				/*
					Build Hash of UIDs, to find the Data faster
				*/
				if($Parm == "SERIAL")
					$this->__serialChar[] = $Zuweisung;

				/*
					Check for Playerevents, add them to an array
				*/
				if($Parm == "EVENTS") {
					$this->__Chars[$i]["EVENTS"][count($this->__Chars[$i]["EVENTS"])] = $Zuweisung;
				} else {
					$this->__Chars[$i][$Parm] = $Zuweisung;
				}
			}
		}

		/*
			Close the file handle (PHP may run out of them if there are other homepages)
			Set the Ready Flag
			and Return a success
		*/
		@fclose($handle);
		$this->Ready = true;
		return true;

	}

	function getData($UID) {
		if(in_array($UID,$this->__serialItem))
			$data = &$this->__Items;
		elseif(in_array($UID,$this->__serialChar))
			$data = &$this->__Chars;
		else
			return false;

		foreach($data as $key=>$value) {
			if($value["SERIAL"] == $UID)
				return $data[$key];
		}
	}

	function getCharInfo($UID) {
		include_once("CharInfo.php");
		return new CharInfo(&$this,$UID);
	}
}
?>
Return current item: PhpMySphere