⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class_user_list.php

📁 一个基于web的rpg游戏源代码
💻 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 *//*** Manages user list** Create, destroy, link to character, search. Since disconnected players are* stored in xml files, the load and save functions are a bit complex** @see user** @version $Id: class_user_list.php,v 1.2 2002/09/29 02:31:51 doneval Exp $* @author Ricard Pillosu <ricardpillosu@dorna.com>* @copyright Ricard Pillosu 2002* @since Sun, 25 Aug 2002 10:03:34 +0200*/class user_list {   /**    * @var array All references to connected users    */    var $list;    /**    * @var string Stores where to save/load files    */    var $directory;    /**    * Define user's access levels    */    const L_FROZEN      = 0;    const L_CONNECTED   = 100;    const L_LOGGED      = 200;    const L_PLAYING     = 300;    const L_EDITOR      = 400;    const L_ADMIN       = 500;    /**    * @var array languages list    */    var $languages;    /**    * @var string Stores last error    */    private $last_error;    function __construct($directory) {        // The rest get values by default        $this->list = array();        $this->directory = $directory;        $this->languages = array(            "english" => "en",            "spanish" => "es",            "catalan" => "ct",            "deutch"  => "de",            "italian" => "it",            "french"  => "fr",            "default" => "en"        );    }    function __destruct() {    }    /**    * Empty error stack    *    * Erase private $last_error var. Do it every time you return(TRUE);    */    function flush_error() {        $this->last_error = "Success";        return(TRUE);    }    /**    * Return last error generated    *    * Simply dumps $last_error. In case of no error, it returns "Success"    */    function get_last_error() {        return($this->last_error);    }    function &create_user($data) {        // Will create a temporary obj        $new_user = & new user();        $result = $new_user->set_data($data);        if($result != TRUE) {            $this->last_error = $new_user->get_last_error();            throw new exception($new_user->get_last_error(), debug_backtrace());            return(FALSE);        }                // Ok, return a reference to it        $this->flush_error();        return($new_user);    }    function destroy_user($login) {        // Filter $login        if(user::is_login($login) != TRUE) {            $this->last_error = "bad user name";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Look if that the user is not connected        if(get_connected_user($login) != FALSE) {            $this->last_error = "cannot destroy connected users";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Destroy user's file        $file_name = get_saved_user($login);        if($file_name == FALSE) {            $this->last_error = "user does not exists";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // ok, go        if(unlink($file_name) != TRUE) {            $this->last_error = "coult not remove >$file_name";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // all ok :)        $this->flush_error();        return(TRUE);    }    function link_character_to_user(&$character, &$user) {        // Filter $character        if(is_a($character, "character") != TRUE) {            $this->last_error = "bad character";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Filter $user        if(is_a($user, "user") != TRUE) {            $this->last_error = "bad user";            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Delete this character from old user's list of characters        if(is_a($character->user, "user") == TRUE) {            $old_user = & $character->user;            $res = $old_user->del_character($character);            if($res != TRUE) {                $this->last_error = $old_user->get_last_error();                throw new exception($this->last_error, debug_backtrace());                return(FALSE);            }        }        // Add this character to user's list of characters        $res = $user->add_character($character);        if($res != TRUE) {            $old_user->add_character($character); // Rollback            $this->last_error = $user->get_last_error();            throw new exception($this->last_error, debug_backtrace());            return(FALSE);        }        // Upgrade from LOGGED to PLAYING        if($user->get_access() == user_list::L_LOGGED) {            $user->set_access(user_list::L_PLAYING);        }        // Finish        $this->flush_error();        return(TRUE);    }    function &get_connected_user($login) {        // filter $login        if(user::is_login($login) != TRUE) {            $this->last_error = "bad user name";            return(FALSE);        }        // Search connected user        if(!isset($this->list[$login])) {            $this->last_error = "user name is not connected";            return(FALSE);        }        $this->flush_error();        return($this->list[$login]);    }    function get_saved_user($login) {        // filter $login        if(user::is_login($login) != TRUE) {            $this->last_error = "bad user name";            return(FALSE);        }        $file_name = $this->directory.$login;        if(is_readable($file_name) != TRUE) {            $this->last_error = "user does not exists";            return(FALSE);        }        $this->flush_error();        return($file_name);    }    /**    * Check existence of a user    *    * Will return the reference to the user if it connected. If it is    * not connected will return TRUE. If not exists will return FALSE    */    function &get_user_by_login($login, $exception=TRUE) {        $user = & get_connected_user($login);        if($user != FALSE) {            return($user);        }        if(get_saved_user($login) != FALSE) {            $this->flush_error();            return(TRUE);        }        $this->last_error = "user does not exists";        if($exception == TRUE) {            throw new exception($this->last_error, debug_backtrace());        }        return(FALSE);    }    function save_user(&$user) {        // Filter $user        if(is_a($user, "user") != TRUE) {            $this->last_error("bad user");            throw new exception($this->last_error);            return(FALSE);        }        // Check file to write        $file_name = $this->directory.$user->get_login();        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);                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);                return(FALSE);            }        }        // Fill $data with xml to store in the file        $data_to_save["login"]          = $user->get_login();        $data_to_save["password"]       = $user->get_password();        $data_to_save["access"]         = $user->get_access();        $data_to_save["language"]       = $user->get_language();        $data_to_save["email"]          = $user->get_email();        $data_to_save["last_ip"]        = $user->get_last_ip();        $data_to_save["last_connected"] = $user->get_last_connected();        $data = get_xml_from_array($data_to_save, "create_user");        // Add links with characters        foreach(array_keys($user->character_list) as $key) {            $character = & $user->character_list[$key];            $char_to_save["id"]  = $character->get_key();            $name = "link_character_to_user";            $data.= "\n".get_xml_from_array($char_to_save, $name);        }        // 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);            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);            return(FALSE);        }        fclose($fp);        $this->flush_error();        return(TRUE);    }    function &load_user($login) {        global $entities;        // filter $login        if(user::is_login($login) != TRUE) {            $this->last_error = "bad user name";            throw new exception($this->last_error);            return(FALSE);        }        // search file        $file_name = get_saved_user($login);        if($file_name == FALSE) {            $this->last_error = "user does not exists";            throw new exception($this->last_error);            return(FALSE);        }        // Load file        $data = file($file_name);        $chars_to_link = array();        // create new object and associiate connection        $chars = array();        foreach($data as $xml) {            $command = get_array_from_xml($xml);            if(is_array($command) != TRUE) {                continue;            }            switch ($command["xml_name"]) {            default:                continue;            case "create_user":                $new_user = & $this->create_user($command);                if($new_user == FALSE) {                    // last_error is already filled by create_user                    throw new exception($this->last_error);                    return(FALSE);                }               break;            case "link_character_to_user":                try {                    $char = & $entities->get_entity($command["id"]);                } catch(exception $exception) {                    continue;                }                $chars[] = & $char;                break;            }        }        // We have the user and the chars to link ... rotate        foreach(array_keys($chars) as $key) {            try {                $r = $this->link_character_to_user($chars[$key], $new_user);            } catch (exception $exception) {                // stop?            }        }        $this->flush_error();        return($new_user);    }    function add_user(&$user) {        // Filter $user        if(is_a($user, "user") != TRUE) {            $this->last_error("bad user");            return(FALSE);        }        $login = $user->get_login();        // Save the reference        $this->list[$login] = & $user;        $this->flush_error();        return(TRUE);    }    function del_user(&$user) {        // Filter $user        if(is_a($user, "user") != TRUE) {            $this->last_error="bad user";            return(FALSE);        }        // Search the reference by login (it is unique)        $login = $user->get_login();        if(isset($this->list[$login]) == FALSE) {            $this->last_error = "user not found";            return(FALSE);        }        // We found it        $user->connection->user = NULL;        // Erase all character's link        foreach(array_keys($user->character_list) as $key) {            $char = & $user->character_list[$key];            $user->del_character($char);        }        // Unset from global list        unset($this->list[$login]);        // ok all :)        $this->flush_error();        return(TRUE);    }}?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -