diff --git a/includes/geo/body.php b/includes/geo/body.php index 15cd881..86bebfa 100755 --- a/includes/geo/body.php +++ b/includes/geo/body.php @@ -1,172 +1,172 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ /** * Geo body class */ class GeoBody { public $code; public $name; public $hypership; public $asteroid; public $moon; public $planet; public $star; public $orbital; public $hidden; public $location; public $description; public $lastError; /** * Initializes a new instance * * @param int $code the primary key */ function __construct ($code = null) { if ($code) { $this->code = $code; $this->load_from_database(); } } /** * Loads the object body (ie fill the properties) from the $_POST array * * @param bool $readBoolean if false, don't read any form item matching a boolean field to avoid to set it to false if there are absent from a form. */ function load_from_form ($readBoolean = true) { if (array_key_exists('name', $_POST)) $this->name = $_POST['name']; if ($readBoolean) { if (array_key_exists('hypership', $_POST)) $this->hypership = $_POST['hypership']; if (array_key_exists('star', $_POST)) $this->start = $_POST['star']; if (array_key_exists('asteroid', $_POST)) $this->hypership = $_POST['asteroid']; if (array_key_exists('moon', $_POST)) $this->start = $_POST['moon']; if (array_key_exists('planet', $_POST)) $this->start = $_POST['planet']; if (array_key_exists('orbital', $_POST)) $this->start = $_POST['orbital']; if (array_key_exists('hidden', $_POST)) $this->start = $_POST['hidden']; } if (array_key_exists('location', $_POST)) $this->location = $_POST['location']; if (array_key_exists('description', $_POST)) $this->description = $_POST['description']; } /** * Loads the object body (ie fill the properties) from the database */ function load_from_database () { global $db; $sql = "SELECT * FROM " . TABLE_BODIES . " WHERE body_code = '" . $this->code . "'"; if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query geo_bodies", '', __LINE__, __FILE__, $sql); if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "body unkwown: " . $this->code; return false; } $this->name = $row['body_name']; $this->location = $row['body_location']; $this->description = $row['body_description']; if ($row['body_status']) { $flags = explode(',', $row['body_status']); foreach ($flags as $flag) { $this->$flag = true; } } return true; } /** * Gets the status field * * @return string the status field value (e.g. "asteroid,hidden") */ - function get_status () { + function getStatus () { $flags = array('hypership','asteroid','moon','planet','star','orbital','hidden'); foreach ($flags as $flag) { if ($this->$flag) { $status[] = $flag; } } return implode(',', $status); } /** * Gets the kind of place the body is (e.g. asteroid) * * @return string the kind of place */ function kind () { //If a location can be described by 2 flags, order the relevant flags list //by priority, as it'll return the first trigerred. //e.g. a moon converted in hypership will be "hypership" and not "moon". $relevantFlags = array('hypership','asteroid','moon','planet','star','orbital'); foreach ($relevantFlags as $flag) { if ($this->$flag) { return $flag; } } return ""; } /** * Gets the name of the body, as a string representation of the object * * @return string the name of the body */ function __toString () { return $this->name; } /** * Saves to database */ function save_to_database () { global $db; $code = $this->code ? "'" . $db->sql_escape($this->code) . "'" : 'NULL'; $name = $db->sql_escape($this->name); - $status = get_status(); + $status = getStatus(); $location = $db->sql_escape($this->location); $description = $db->sql_escape($this->description); //Updates or inserts $sql = "REPLACE INTO " . TABLE_BODIES . " (`body_code`, `body_name`, `body_status`, `body_location`, `body_description`) VALUES ($code, '$name', '$status', '$location', '$description')"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql); } if (!$code) { //Gets new record code value $this->code = $db->sql_nextid(); } } } diff --git a/includes/geo/place.php b/includes/geo/place.php index 1e85432..8edfe57 100755 --- a/includes/geo/place.php +++ b/includes/geo/place.php @@ -1,214 +1,214 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ /** * Default local location format * * The local_location format is a PCRE regular expression * * By default, local_location format is an (x, y, z) expression */ define('LOCATION_LOCAL_DEFAULT_FORMAT', '/^\([0-9]+( )*,( )*[0-9]+( )*,( )*[0-9]+\)$/'); /** * Geo place * * A place is a city or a hypership district. * * It's identified by a 9 chars geocode like B0001001. * The 5 first chars indicates the body (class GeoBody) where the place is and * the 3 last digits is the place number. * * This class maps the geo_places table. */ class GeoPlace { public $id; public $body_code; public $code; public $name; public $description; public $location_local_format; public $start; public $hidden; /** * Initializes a new instance * * @param int $id the primary key */ function __construct ($id = null) { if ($id) { $this->id = $id; $this->load_from_database(); } } /** * Loads the object place (ie fill the properties) from the $_POST array */ function load_from_form () { if (array_key_exists('body_code', $_POST)) $this->body_code = $_POST['body_code']; if (array_key_exists('code', $_POST)) $this->code = $_POST['code']; if (array_key_exists('name', $_POST)) $this->name = $_POST['name']; if (array_key_exists('description', $_POST)) $this->description = $_POST['description']; if (array_key_exists('status', $_POST)) $this->status = $_POST['status']; if (array_key_exists('location_local_format', $_POST)) $this->location_local_format = $_POST['location_local_format']; } /** * Loads the object place (ie fill the properties) from the database */ function load_from_database () { global $db; $sql = "SELECT * FROM " . TABLE_PLACES . " WHERE place_id = '" . $this->id . "'"; if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query geo_places", '', __LINE__, __FILE__, $sql); if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "place unkwown: " . $this->id; return false; } $this->body_code = $row['body_code']; $this->code = $row['place_code']; $this->name = $row['place_name']; $this->description = $row['place_description']; $this->location_local_format = $row['location_local_format']; //Explodes place_status SET field in boolean variables if ($row['place_status']) { $flags = explode(',', $row['place_status']); foreach ($flags as $flag) { $this->$flag = true; } } return true; } /** * Gets status field value * * @return string the status field value (e.g. "requiresPTA,default") */ - function get_status () { + function getStatus () { $flags = array('start', 'hidden'); foreach ($flags as $flag) { if ($this->$flag == true) { $status[] = $flag; } } return implode(',', $status); } /** * Saves to database */ function save_to_database () { global $db; $id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL'; $body_code = $db->sql_escape($this->body_code); $code = $db->sql_escape($this->code); $name = $db->sql_escape($this->name); $description = $db->sql_escape($this->description); - $status = $this->get_status(); + $status = $this->getStatus(); $location_local_format = $db->sql_escape($this->location_local_format); //Updates or inserts $sql = "REPLACE INTO " . TABLE_PLACES . " (`place_id`, `body_code`, `place_code`, `place_name`, `place_description`, `place_status`, `location_local_format`) VALUES ($id, '$body_code', '$code', '$name', '$description', '$status', '$location_local_format')"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql); } if (!$id) { //Gets new record id value $this->id = $db->sql_nextid(); } } /** * Determines if the specified local location looks valid * * @param string $local_location the local location * @return boolean true if the specified local location looks valid ; otherwise, false. */ function is_valid_local_location ($local_location) { $format = $this->location_local_format ? $this->location_local_format : LOCATION_LOCAL_DEFAULT_FORMAT; return preg_match($format, $local_location) > 0; } /** * Gets a string representation of the current place * * @return string A Bxxxxxyyy string like B00001001, which represents the current place. */ function __tostring () { return 'B' . $this->body_code . $this->code; } /** * Creates a Place instance, from the specified body/place code * * @param $code the place's code * @return GeoPlace the place instance */ static function from_code ($code) { global $db; $sql = "SELECT * FROM " . TABLE_PLACES . " WHERE CONCAT('B', body_code, place_code) LIKE '$code'"; if (!$result = $db->sql_query($sql)) message_die(SQL_ERROR, "Unable to query geo_places", '', __LINE__, __FILE__, $sql); if (!$row = $db->sql_fetchrow($result)) { return null; } $place = new GeoPlace(); $place->id = $row['place_id']; $place->body_code = $row['body_code']; $place->code = $row['place_code']; $place->name = $row['place_name']; $place->description = $row['place_description']; $place->location_local_format = $row['location_local_format']; //Explodes place_status SET field in boolean variables if ($row['place_status']) { $flags = explode(',', $row['place_status']); foreach ($flags as $flag) { $place->$flag = true; } } return $place; } /** * Gets a start location * * @return string The global location code of a start location * * @TODO sql optimisation (query contains ORDER BY RAND()) */ static function get_start_location () { global $db; $sql = "SELECT CONCAT('B', body_code, place_code) FROM " . TABLE_PLACES . " WHERE FIND_IN_SET('start', place_status) > 0 ORDER BY rand() LIMIT 1"; return $db->sql_query_express($sql); } } diff --git a/includes/objects/port.php b/includes/objects/port.php index 81c236c..def95e8 100755 --- a/includes/objects/port.php +++ b/includes/objects/port.php @@ -1,197 +1,197 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ require_once("includes/geo/location.php"); /** * Port class * * This class maps the ports table. * * The class also provides helper methods to handle ports at specified location. */ class Port { public $id; public $location_global; public $location_local; public $name; public $hidden; public $requiresPTA; public $default; /** * Initializes a new instance * @param int $id the primary key */ function __construct ($id = NULL) { if ($id) { $this->id = $id; $this->load_from_database(); } } /** * Loads the object Port (ie fill the properties) from the $_POST array */ function load_from_form () { if (array_key_exists('location_global', $_POST)) $this->location_global = $_POST['location_global']; if (array_key_exists('location_local', $_POST)) $this->location_local = $_POST['location_local']; if (array_key_exists('name', $_POST)) $this->name = $_POST['name']; if (array_key_exists('hidden', $_POST)) $this->hidden = $_POST['hidden'] ? true : false; if (array_key_exists('requiresPTA', $_POST)) $this->requiresPTA = $_POST['requiresPTA'] ? true : false; if (array_key_exists('default', $_POST)) $this->hidden = $_POST['default'] ? true : false; } /** * Loads the object Port (ie fill the properties) from the database */ function load_from_database () { global $db; $id = $db->sql_escape($this->id); $sql = "SELECT * FROM " . TABLE_PORTS . " WHERE port_id = '" . $id . "'"; if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query ports", '', __LINE__, __FILE__, $sql); if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "Port unkwown: " . $this->id; return false; } $this->location_global = $row['location_global']; $this->location_local = $row['location_local']; $this->name = $row['port_name']; //Explodes place_status SET field in boolean variables if ($row['place_status']) { $flags = explode(',', $row['port_status']); foreach ($flags as $flag) { $this->$flag = true; } } return true; } /** * Gets status field value * * @return string the status field value (e.g. "requiresPTA,default") */ - function get_status () { + function getStatus () { $flags = array('hidden', 'requiresPTA', 'default'); foreach ($flags as $flag) { if ($this->$flag) { $status[] = $flag; } } return implode(',', $status); } /** * Saves to database */ function save_to_database () { global $db; $id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL'; $location_global = $db->sql_escape($this->location_global); $location_local = $db->sql_escape($this->location_local); $name = $db->sql_escape($this->name); - $status = $this->get_status(); + $status = $this->getStatus(); //Updates or inserts $sql = "REPLACE INTO " . TABLE_PORTS . " (`port_id`, `location_global`, `location_local`, `port_name`, `port_status`) VALUES ($id, '$location_global', '$location_local', '$name', '$status')"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql); } if (!$id) { //Gets new record id value $this->id = $db->sql_nextid(); } } /** * Determines if the specified location have a port * * @param string $location_global the global location * @return boolean true if there is a spatioport exactly at the specified location ; otherwise, false. */ static function have_port ($location_global) { return (get_port_id($location_global) !== NULL); } /** * Gets the port situated exactly at the specified global location * * @param string $location_global the global location * @return int the port ID */ static function get_port_id ($location_global) { global $db; $location_global = $db->sql_escape($location_global); $sql = "SELECT port_id FROM " . TABLE_PORTS . " WHERE location_global = '$location_global'"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to get ports", '', __LINE__, __FILE__, $sql); } if ($row = $db->sql_fetchrow($result)) { return $row['port_id']; } return null; } /** * Gets default port, from specified global location * * @param string $location_global the global location * @return Port the port near this location ; null if there isn't port there. */ static function from_location ($location_global) { $havePlace = strlen($location_global) == 9; $port_id = null; if ($havePlace) { //Checks if there's a port at specified location $port_id = self::get_port_id($location_global); } if ($port_id == null) { //Nearest default port. //If place have been specified (B0001001), we've to found elsewhere //==> B00001% global $db; $loc = $db->sql_escape(substr($location_global, 0, 6)); $sql = "SELECT port_id FROM " . TABLE_PORTS . " WHERE location_global LIKE '$loc%'"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Can't get port", '', __LINE__, __FILE__, $sql); } if ($row = $db->sql_fetchrow($result)) { $port_id = $row['port_id']; } else { return null; } } return new Port($port_id); } }