Page MenuHomeCode

No OneTemporary

diff --git a/includes/geo/body.php b/includes/geo/body.php
new file mode 100644
--- /dev/null
+++ b/includes/geo/body.php
@@ -0,0 +1,143 @@
+<?php
+
+/*
+ * Geo body class
+ *
+ * 0.1 2010-01-27 21:51 Autogenerated by Pluton Scaffolding
+ *
+ * @package Zed
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+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
+ */
+ 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 status
+ */
+ function get_status () {
+ $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)
+ */
+ 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 "";
+ }
+
+ /*
+ * 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();
+ $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();
+ }
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/includes/geo/location.php b/includes/geo/location.php
new file mode 100644
--- /dev/null
+++ b/includes/geo/location.php
@@ -0,0 +1,257 @@
+<?php
+
+require_once('body.php');
+require_once('place.php');
+
+/*
+ * Geo location class
+ *
+ * 0.1 2010-01-28 18:52 DcK
+ *
+ * @package Zed
+ * @subpackage Geo
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+class GeoLocation {
+ private $data;
+ public $body = null;
+ public $place = null;
+
+ function __construct ($global = null, $local = null) {
+ if (!$global) {
+ $this->data = array();
+ } elseif (ereg("[BS][0-9]{5}[0-9]{3}", $global)) {
+ $this->data[0] = $global;
+ } elseif (ereg("[BS][0-9]{5}", $global)) {
+ $this->data[0] = $global;
+ } else {
+ global $db;
+ $name = $db->sql_escape($global);
+ $sql = "SELECT location_code FROM " . TABLE_LOCATIONS . " WHERE location_name LIKE '$name'";
+ $code = $db->sql_query_express($sql);
+ if ($code) {
+ $this->data[0] = $code;
+ return;
+ }
+ throw new Exception("Invalid expression: $global");
+ }
+
+ $this->load_classes();
+
+ //TODO: handle $local
+ }
+
+ function load_classes () {
+ //No data, no class to load
+ if (!count($this->data))
+ return;
+
+ //Loads global classes
+ $global = $this->data[0];
+ $body_code = substr($global, 1, 5);
+ switch ($global[0]) {
+ case 'B':
+ switch (strlen($global)) {
+ case 9:
+ $this->place = GeoPlace::from_code($global);
+
+ case 6:
+ $this->body = new GeoBody($body_code);
+ break;
+ }
+ break;
+ case 'S':
+ break;
+ }
+ }
+
+
+ function __get ($variable) {
+ switch ($variable) {
+ /* main variables */
+
+ case 'global':
+ return $this->data[0];
+ break;
+
+ case 'local':
+ return $this->data[1];
+ break;
+
+ /* global location */
+
+ case 'type':
+ return $this->data[0][0];
+
+ case 'body_code':
+ if ($this->data[0][0] == 'B') {
+ return substr($this->data[0], 1, 5);
+ }
+ return null;
+
+ case 'place_code':
+ if ($this->data[0][0] == 'B') {
+ return substr($this->data[0], 6, 3);
+ }
+ return null;
+
+ case 'body_kind':
+ if ($this->data[0][0] == 'B' && $this->body != null) {
+ if ($kind = $this->body->kind()) {
+ return $kind;
+ }
+ }
+ return 'place';
+
+ default:
+ throw new Exception("Unknown variable: $variable");
+ break;
+ }
+ }
+
+ /*
+ * Checks if the place exists
+ *
+ * @return boolean true if the place exists ; false otherwise
+ */
+ function exists () {
+ $n = count($this->data);
+
+ //If not defined, it doesn't exist
+ if ($n == 0) return false;
+
+ //Checks global location
+ switch ($this->data[0][0]) {
+ case 'B':
+ switch (strlen($this->data[0])) {
+ case 9:
+ if (!$place = GeoPlace::from_code($this->data[0]))
+ return false;
+ break;
+
+ case 6:
+ $body = new GeoBody(substr($this->data[0], 1));
+ if ($body->lastError) return false;
+ break;
+
+ default:
+ message_die(GENERAL_ERROR, "Invalid global location expression size: " . $this->data[0], "GeoLocation exists method", __LINE__, __FILE__);
+
+ }
+ break;
+
+ case 'S':
+ message_die(GENERAL_ERROR, "Handle S", 'TODO', __LINE__, __FILE__);
+ break;
+
+ default:
+ message_die(GENERAL_ERROR, "Invalid global location expression size: " . $this->data[0], "GeoLocation exists method", __LINE__, __FILE__);
+ return false;
+ }
+
+
+ if ($n > 1) {
+ message_die(GENERAL_ERROR, "Can't check if a local place exists yet.", "GeoLocation exists method", __LINE__, __FILE__);
+ }
+
+ return true;
+ }
+
+ function equals ($expression) {
+ if ($expression == $this->data[0]) return true;
+
+ $n1 = strlen($expression);
+ $n2 = strlen($this->data[0]);
+
+ if ($n1 > $n2) {
+ return substr($expression, 0, $n2) == $this->data[0];
+ }
+
+ return false;
+ }
+
+ function __toString () {
+ if (!$this->data[0])
+ return "";
+
+ switch ($this->data[0][0]) {
+ case 'S':
+ message_die(GENERAL_ERROR, "Handle spaceship in GeoLocation __toString()", "TODO", __LINE__, __FILE__);
+ break;
+
+ case 'B':
+ $body = new GeoBody($this->body_code);
+ $location[] = $body->name ? $body->name : lang_get('UnknownBody');
+
+ if (strlen($this->data[0]) == 9) {
+ $place = GeoPlace::from_code($this->data[0]);
+ $location[] = $place->name ? $place->name : lang_get('UnknownPlace');
+ }
+ break;
+
+ default:
+ message_die(GENERAL_ERROR, "Unknown location identifier: $type.<br />Expected: B or S.");
+ }
+
+ return implode(", ", array_reverse($location));
+ }
+
+ function __set ($variable, $value) {
+ switch ($variable) {
+ /* main variables */
+
+ case 'global':
+ $this->data[0] = $value;
+ break;
+
+ case 'local':
+ $this->data[1] = $value;
+ break;
+
+ /* global location */
+
+ case 'type':
+ if ($value == 'B' || $value == 'S') {
+ if (!$this->data[0]) {
+ $this->data[0] = $value;
+ } else {
+ $this->data[0][0] = $value;
+ }
+ }
+ break;
+
+ case 'body_code':
+ if (ereg("[0-9]{1,3}", $value)) {
+ $value = sprintf("%03d", $value);
+ if (!$this->data[0]) {
+ $this->data[0] = "B" . $value;
+ return;
+ } elseif ($this->data[0][0] == 'B') {
+ $this->data[0] = "B" . $value . substr($this->data[0], 5);
+ return;
+ }
+ throw new Exception("Global location isn't a body.");
+ }
+ throw new Exception("$value isn't a valid body code");
+
+ case 'place_code':
+ if (!ereg("[0-9]{1,5}", $value)) {
+ throw new Exception("$value isn't a valid place code");
+ }
+ $value = sprintf("%05d", $value);
+ if ($this->data[0][0] == 'B') {
+ $this->data[0] = substr($this->data[0], 0, 6) . $value;
+ }
+ throw new Exception("Global location isn't a body.");
+
+ default:
+ throw new Exception("Unknown variable: $variable");
+ break;
+ }
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/includes/geo/place.php b/includes/geo/place.php
new file mode 100644
--- /dev/null
+++ b/includes/geo/place.php
@@ -0,0 +1,147 @@
+<?php
+
+/*
+ * place class
+ *
+ * 0.1 2010-01-28 01:48 Autogenerated by Pluton Scaffolding
+ *
+ * @package Zed
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+class GeoPlace {
+
+ public $id;
+ public $body_code;
+ public $code;
+ public $name;
+ public $description;
+
+ 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'];
+ }
+
+ /*
+ * Loads the object place (ie fill the properties) from the database
+ */
+ function load_from_database () {
+ global $db;
+ $sql = "SELECT * FROM geo_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'];
+
+ //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
+ */
+ function get_status () {
+ $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();
+
+ //Updates or inserts
+ $sql = "REPLACE INTO geo_places (`place_id`, `body_code`, `place_code`, `place_name`, `place_description`, `place_status`) VALUES ($id, '$body_code', '$code', '$name', '$description', '$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();
+ }
+ }
+
+ static function from_code ($code) {
+ global $db;
+ $sql = "SELECT * FROM geo_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'];
+
+ //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
+ */
+ static function get_start_location () {
+ global $db;
+ $sql = "SELECT CONCAT('B', body_code, place_code) FROM geo_places WHERE FIND_IN_SET('start', place_status) > 0 ORDER BY rand() LIMIT 1";
+ return $db->sql_query_express($sql);
+ }
+}
+?>
\ No newline at end of file
diff --git a/includes/objects/body.php b/includes/objects/body.php
deleted file mode 100644
--- a/includes/objects/body.php
+++ /dev/null
@@ -1,115 +0,0 @@
-<?php
-
-/*
- * Geo body class
- *
- * 0.1 2010-01-27 21:51 Autogenerated by Pluton Scaffolding
- *
- * @package Zed
- * @copyright Copyright (c) 2010, Dereckson
- * @license Released under BSD license
- * @version 0.1
- *
- */
-class GeoBody {
-
- public $code;
- public $name;
-
- public $hypership;
- public $start;
-
- public $location;
- public $description;
-
- /*
- * 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
- */
- function load_from_form () {
- if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
-
- if (array_key_exists('hypership', $_POST)) $this->hypership = $_POST['hypership'];
- if (array_key_exists('start', $_POST)) $this->start = $_POST['start'];
-
- 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 status
- */
- function get_status () {
- $flags = array('hypership', 'start');
- foreach ($flags as $flag) {
- if ($this->$flag == true) {
- $status[] = $flag;
- }
- }
- return implode(',', $status);
- }
-
- /*
- * 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();
- $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/objects/perso.php b/includes/objects/perso.php
--- a/includes/objects/perso.php
+++ b/includes/objects/perso.php
@@ -1,222 +1,270 @@
<?php
/*
* Perso class
*
* 0.1 2010-01-27 00:39 Autogenerated by Pluton Scaffolding
+ * 0.2 2010-01-29 14:39 Adding flags support
*
* @package Zed
* @copyright Copyright (c) 2010, Dereckson
* @license Released under BSD license
* @version 0.1
*
*/
+
+require_once("includes/geo/location.php");
+
class Perso {
public $id;
public $user_id;
public $name;
public $nickname;
public $race;
public $sex;
public $avatar;
public $location_global;
public $location_local;
+ public $flags;
+
/*
* Initializes a new instance
* @param int $id the primary key
*/
function __construct ($id = null) {
if ($id) {
$this->id = $id;
$this->load_from_database();
} else {
$this->generate_id();
}
}
/*
* Loads the object Perso (ie fill the properties) from the $_POST array
*/
function load_from_form () {
if (array_key_exists('user_id', $_POST)) $this->user_id = $_POST['user_id'];
if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
if (array_key_exists('nickname', $_POST)) $this->nickname = $_POST['nickname'];
if (array_key_exists('race', $_POST)) $this->race = $_POST['race'];
if (array_key_exists('sex', $_POST)) $this->sex = $_POST['sex'];
if (array_key_exists('avatar', $_POST)) $this->avatar = $_POST['avatar'];
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'];
}
/*
* Loads the object Perso (ie fill the properties) from the database
*/
function load_from_database () {
global $db;
- $sql = "SELECT * FROM " . TABLE_PERSOS . " WHERE perso_id = '" . $this->id . "'";
+
+ //Gets perso
+ $id = $db->sql_escape($this->id);
+ $sql = "SELECT * FROM " . TABLE_PERSOS . " WHERE perso_id = '" . $id . "'";
if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query persos", '', __LINE__, __FILE__, $sql);
if (!$row = $db->sql_fetchrow($result)) {
$this->lastError = "Perso unkwown: " . $this->id;
return false;
}
$this->user_id = $row['user_id'];
$this->name = $row['perso_name'];
$this->nickname = $row['perso_nickname'];
$this->race = $row['perso_race'];
$this->sex = $row['perso_sex'];
$this->avatar = $row['perso_avatar'];
$this->location_global = $row['location_global'];
$this->location_local = $row['location_local'];
+
+ //Gets flags
+ $sql = "SELECT flag_key, flag_value FROM " . TABLE_PERSOS_FLAGS .
+ " WHERE perso_id = $this->id";
+ if (!$result = $db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Can't get flags", '', __LINE__, __FILE__, $sql);
+ }
+ while ($row = $db->sql_fetchrow($result)) {
+ $this->flags[$row["flag_key"]] = $row["flag_value"];
+ }
+
+ //Gets location
+ $this->location = new GeoLocation(
+ $this->location_global,
+ $this->location_local
+ );
+
return true;
}
/*
* Saves to database
*/
function save_to_database () {
global $db;
$id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
$user_id = $db->sql_escape($this->user_id);
$name = $db->sql_escape($this->name);
$nickname = $db->sql_escape($this->nickname);
$race = $db->sql_escape($this->race);
$sex = $db->sql_escape($this->sex);
$avatar = $db->sql_escape($this->avatar);
- $location_global = $db->sql_escape($this->location_global);
- $location_local = $db->sql_escape($this->location_local);
+ $location_global = $this->location_global ? "'" . $db->sql_escape($this->location_global) . "'" : 'NULL';
+ $location_local = $this->location_local ? "'" . $db->sql_escape($this->location_local) . "'" : 'NULL';
//Updates or inserts
- $sql = "REPLACE INTO " . TABLE_PERSOS . " (`perso_id`, `user_id`, `perso_name`, `perso_nickname`, `perso_race`, `perso_sex`, `perso_avatar`, `location_global`, `location_local`) VALUES ($id, '$user_id', '$name', '$nickname', '$race', '$sex', '$avatar', '$location_global', '$location_local')";
+ $sql = "REPLACE INTO " . TABLE_PERSOS . " (`perso_id`, `user_id`, `perso_name`, `perso_nickname`, `perso_race`, `perso_sex`, `perso_avatar`, `location_global`, `location_local`) VALUES ($id, '$user_id', '$name', '$nickname', '$race', '$sex', '$avatar', $location_global, $location_local)";
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();
}
}
/*
* Updates the specified field in the database record
*/
function save_field ($field) {
global $db;
if (!$this->id) {
message_die(GENERAL_ERROR, "You're trying to update a record not yet saved in the database");
}
$id = $db->sql_escape($this->id);
$value = $db->sql_escape($this->$field);
$sql = "UPDATE " . TABLE_PERSOS . " SET `$field` = '$value' WHERE perso_id = '$id'";
if (!$db->sql_query($sql)) {
message_die(SQL_ERROR, "Unable to save $field field", '', __LINE__, __FILE__, $sql);
}
}
+ /*
+ * Gets perso location
+ * @return string The location names
+ */
public function where () {
- global $db;
-
- //B00001001 -> B, 1, 1
- list($type, $code, $subcode) = sscanf($this->location_global, "%c%05d%03d");
+ return $this->location->__toString();
+ }
+
+ /*
+ * Moves the perso to a new location
+ */
+ public function move_to ($global = null, $local = null) {
+ //Sets global location
+ if ($global != null) {
+ $this->location_global = $global;
+ }
- switch ($type) {
- case 'B':
- //Gets body
- $sql = "SELECT body_name FROM " . TABLE_BODIES . " WHERE body_code = $code";
-
- if (!$result = $db->sql_query($sql)) {
- message_die(SQL_ERROR, "Unable to query the table", '', __LINE__, __FILE__, $sql);
- }
- if (!$row = $db->sql_fetchrow($result)) {
- message_die(GENERAL_ERROR, "B$code isn't defined as body.", "Unknown body");
- }
- $body = $row['body_name'] ? $row['body_name'] : lang_get('UnknownBody');
-
- //Gets place
- $sql = "SELECT place_name FROM " . TABLE_PLACES . " WHERE body_code = $code AND place_code = $subcode";
- if (!$result = $db->sql_query($sql)) {
- message_die(SQL_ERROR, "Unable to query the table", '', __LINE__, __FILE__, $sql);
- }
- if (!$row = $db->sql_fetchrow($result)) {
- message_die(GENERAL_ERROR, "$this->location_global isn't defined as place.", "Unknown place");
- }
- $place = $row['place_name'] ? $row['place_name'] : lang_get('UnknownPlace');
- break;
-
- case 'S':
- message_die(GENERAL_ERROR, "Handle spaceship in where()", "TODO", __LINE__, __FILE__);
- //break;
-
- default:
- message_die(GENERAL_ERROR, "Unknown location identifier: $type.<br />Expected: B or S.");
+ //Sets local location
+ if ($local != null) {
+ $this->location_local = $local;
}
- return sprintf(lang_get('WherePlace'), $body, $place);
+ //Updates database record
+ if ($global != null && $local != null) {
+ global $db;
+ $perso_id = $db->sql_escape($this->id);
+ $g = $db->sql_escape($this->location_global);
+ $l = $db->sql_escape($this->location_local);
+ $sql = "UPDATE " . TABLE_PERSOS .
+ " SET location_global = '$g', location_local = '$l'" .
+ " WHERE perso_id = '$perso_id'";
+ if (!$db->sql_query($sql))
+ message_die(SQL_ERROR, "Can't save new $global $local location.", '', __LINE__, __FILE__, $sql);
+ } elseif ($global != null) {
+ $this->save_field('location_global');
+ } elseif ($global != null) {
+ $this->save_field('location_local');
+ }
+
+ //Updates location member
+ $this->location = new GeoLocation(
+ $this->location_global,
+ $this->location_local
+ );
+ }
+
+ public function setflag ($key, $value) {
+ //Checks if flag isn't already set at this value
+ if ($this->flags[$key] === $value)
+ return;
+
+ //Saves flag to database
+ global $db;
+ $id = $db->sql_escape($this->id);
+ $key = $db->sql_escape($key);
+ $value = $db->sql_escape($value);
+ $sql = "REPLACE " . TABLE_PERSOS_FLAGS . " SET perso_id = '$id', flag_key = '$key', flag_value = '$value'";
+ if (!$db->sql_query($sql))
+ message_die(SQL_ERROR, "Can't save flag", '', __LINE__, __FILE__, $sql);
+
+ //Sets flag in this perso instance
+ $this->flags[$key] = $value;
}
public static function is_available_id ($id) {
global $db;
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE perso_id = $id LOCK IN SHARE MODE";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't access users table", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
return ($row[0] == 0);
}
/*
* Generates a unique ID for the current object
*/
private function generate_id () {
do {
$this->id = rand(2001, 5999);
} while (!Perso::is_available_id($this->id));
}
/*
* Checks if the nickname is available
* @param string $nickname the nickname to check
*/
public static function is_available_nickname ($nickname) {
global $db;
$nickname = $db->sql_escape($nickname);
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE perso_nickname LIKE '$nickname' LOCK IN SHARE MODE;";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Utilisateurs non parsable", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
return ($row[0] == 0);
}
/*
* Counts the perso a user have
*
* @param int user_id the user ID
*/
public static function get_persos_count ($user_id) {
global $db;
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE user_id = $user_id";
return $db->sql_query_express($sql);
}
/*
* Gets the first perso a user have
* (typically to be used when get_persos_count returns 1 to autoselect)
*
* @param int user_id the user ID
*/
public static function get_first_perso ($user_id) {
global $db;
$sql = "SELECT perso_id FROM " . TABLE_PERSOS . " WHERE user_id = $user_id LIMIT 1";
if ($perso_id = $db->sql_query_express($sql)) {
return new Perso($perso_id);
}
}
}
-?>
-
+?>
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Wed, Nov 20, 20:59 (4 d, 41 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
20960
Default Alt Text
(34 KB)

Event Timeline