📄 class_entity_list.php
字号:
<?php/*** Netlands World Server is the coordinator of the VR in the Netlands Project* Copyright (C) 2002 Ricard Pillosu** 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.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*//* vim: set expandtab tabstop=4 shiftwidth=4 *//*** Class to manage list of entities** In order to avoid using global arrays and other "not so clean" stuff. The* concept is that this class knows and manage all entities defined thought* itselft. Thanks to {@link mailto:supercoco@menta.net Tony Aguilar} for his* concept contribution.** @version $Id: class_entity_list.php,v 1.7 2002/09/29 02:31:52 doneval Exp $* @author Ricard Pillosu <ricardpillosu@dorna.com>* @copyright Ricard Pillosu 2002* @since Wed, 31 Jul 2002 23:21:04 +0200*/class entity_list { /** * The first and "root" key (like '/') for a filesystem * @var object */ private $root_entity; /** * @var string */ private $root_name; /** * Maybe this data could be a 2 dimension array with types * @var array $list All the data is here :) */ var $list; /** * @var string Stores where to save/load files */ private $directory; /** * @var string Stores default file name to save data */ private $default_file; /** * Storages last error operating with this object * * Each "return(FALSE)" will store a string explaining the error * Each "return(TRUE)" will flush this variable to "Success" * * @var string $last_error */ private $last_error; /** * Constants about entity types */ const T_NDEF = 0; const T_OBJK = 1; const T_CHAR = 2; const T_ROOM = 3; const T_DOOR = 4; const T_AREA = 5; /** * Array containing class names of the entities */ private $entity_types; /** * Only reset vars */ function __construct($directory, $default_file = 'entities') { $this->root_entity = NULL; $this->root_name = "__ROOT__"; $this->list = array(); $this->flush_error(); $this->directory = $directory; $this->default_file = $default_file; $this->entity_types[T_NDEF] = "entity"; $this->entity_types[T_OBJK] = "objekt"; $this->entity_types[T_CHAR] = "character"; $this->entity_types[T_ROOM] = "room"; $this->entity_types[T_DOOR] = "door"; $this->entity_types[T_AREA] = "area"; } /** * Maybe force deletion of root_entity */ function __destruct() { // All references to my entities are lost, there is no need to // call delete_entity foreach object in $list } /** * Check validity of a "name" for a entity */ function is_valid_name($string) { $expr = "^[0-9a-zA-z _\-\.]{3,25}$"; if(ereg($expr, $string) == FALSE) { $this->last_error = "invalid name"; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } $this->flush_error(); return(TRUE); } /** * Returns the "name" of the entity type */ function get_type_name($type=0) { if(isset($this->entity_types[$type])) { $this->flush_error(); return($this->entity_types[$type]); } $this->last_error = "invalid type"; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } /** * Returns the entity type from the "name" */ function get_type_from_name($string) { $type = array_search($string, $this->entity_types); if($type === FALSE) { $this->last_error = "invalid type name"; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } $this->flush_error(); return($type); // array_search return FALSE if not found } /** * Save my state in a file * * @param string $file_name */ function save($file = '') { global $config; // Filter $file if(empty($file)) $file = $this->default_file; // Check file to write $file_name = $this->directory.$file; if(file_exists($file_name)) { if(is_writable($file_name) != TRUE) { $this->last_error = "file is not writeable >".$file_name; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } } else { // File is new if(is_writable($this->directory) != TRUE) { $this->last_error = "directory is not writeable >"; $this->last_error.= $this->directory; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } } // Save root_entity if($this->is_entity($this->root_entity) == FALSE) { // Nothing to save :) $this->flush_error(); return(TRUE); } $e = & $this->root_entity; $data_to_save['id'] = $e->get_key(); $data_to_save['name'] = $this->root_name; $data_to_save['xml_name'] = $this->get_type_name($e->type); $data = get_xml_from_array($data_to_save); // Rotate $list and add XML lines to $data foreach(array_keys($this->list) as $key) { $e = & $this->list[$key]; $data_to_save = $e->get_game_vars(); $data_to_save['xml_name'] = $this->get_type_name($e->type); $data_to_save['id'] = $e->get_key(); $data_to_save['parent_id'] = $e->parent->get_key(); $data.= "\n".get_xml_from_array($data_to_save); } // Save $data to $file_name $fp = fopen("$file_name", "w"); if(!$fp) { $msg = "could not open >$file_name"; $this->last_error = $msg; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } $written = fwrite($fp, $data); if($written < 0) { fclose($fp); $msg = "could not write to >$file_name"; $this->last_error = $msg; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } fclose($fp); $this->flush_error(); return(TRUE); } /** * Load a entities file */ function load($file = '') { // Filter $file if(empty($file)) $file = $this->default_file; // Check file to load $file_name = $this->directory.$file; if(is_readable($file_name) == FALSE) { $this->last_error = "file is not readable >".$file_name; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } // Load file $data = file($file_name); $chars_to_link = array(); $moves = array(); $updates = array(); // Rotate lines foreach($data as $xml) { $command = get_array_from_xml($xml); if(is_array($command) != TRUE) { continue; } $type = get_type_from_name($command['xml_name']); $key = $command['id']; // is this the root key ? if(isset($command["name"]) && $command["name"] == $this->root_name) { if($this->root_entity == NULL) { $this->root_entity = &new entity($this, $key); $msg = "Loaded ".$command['xml_name']." $this->root_name ($key)"; log_msg($msg, "game"); } } else { $this->add_entity($key, '', $type); // Log $msg = "Loaded ".$command["xml_name"]." $key"; log_msg($msg, "game"); // Save positions for do it later if($command['parent_id'] != $this->root_entity->get_key()) { $moves[] = array( 'entity_key' => $key, 'parent_key' => $command['parent_id']); } // Saves all remaining for updates unset($command['id']); unset($command['parent_id'], $command['xml_name']); if(is_array($command) && count($command) > 0) { $updates[$key] = $command; } } } // Now that all entities exists, move them foreach($moves as $m) { $entity = & get_entity($m['entity_key']); $parent = & get_entity($m['parent_key']); $this->move_entity_to($entity, $parent, ($foo = NULL), FALSE); $msg = "Moved ".$entity->get_key()." -> ".$parent->get_key(); log_msg($msg, "game"); } // Now update other infos for them foreach($updates as $key => $data_to_update) { $entity = & get_entity($key); $entity->update_game_vars($data_to_update); $msg = "Updated ".$entity->get_key(); foreach(array_keys($data_to_update) as $k) { $msg.= " ".$k; } log_msg($msg, "game"); } // Save with good parents and updates $this->save(); $this->flush_error(); return(TRUE); } /** * Empty error buffer */ function flush_error() { $this->last_error = "Success"; return(TRUE); } /** * Return last error */ function get_last_error() { return($this->last_error); } /** * Creates a new key (unique id) * * We are discussing if it is necessary to make a object type "key" * that could include server, time created, unique id, etc ... */ function create_key() { do { $key = md5(microtime()); } while($this->key_exists($key, FALSE)); return($key); } /** * Check the validity of a key */ function is_valid_key($key) { $expr = "^[0-9a-f]{32}$"; if(!ereg($expr, $key)) { // Maybe PCRE better? $this->last_error = "key is not valid"; throw new exception($this->last_error, debug_backtrace()); return(FALSE); } return(TRUE); } /** * Check if key is defined or is the key of $root_entity */ function key_exists($key, $exception=TRUE) { if(isset($this->list[$key])) return(TRUE); if($this->is_entity($this->root_entity)) { if($key === $this->root_entity->get_key()) return(TRUE); } if($exception == TRUE) { $this->last_error = "key does not exists"; throw new exception($this->last_error, debug_backtrace()); } return(FALSE); } /** * Return TRUE if object is a valid entity */ function is_entity(&$object, $exception=TRUE) { if(is_a($object, "entity") == FALSE) { if($exception == TRUE) { $this->last_error = "object is not an entity"; throw new exception($this->last_error, debug_backtrace(), $object); } return(FALSE); } $this->flush_error(); return(TRUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -