ソースを参照

Code improvements: Staged controller. Global context. Encapsulation (in progress).

Iñigo Valentin 5 年 前
コミット
f4de505c54
85 ファイル変更1150 行追加983 行削除
  1. 2 2
      application/API/v1/profile/DELETE.php
  2. 5 5
      application/API/v1/profile/GET.php
  3. 2 2
      application/API/v1/profile/POST.php
  4. 6 7
      application/API/v1/profile/PUT.php
  5. 2 2
      application/API/v1/run/POST.php
  6. 6 7
      application/API/v1/units/GET.php
  7. 96 0
      application/Context.php
  8. 178 190
      application/Controller.php
  9. 1 3
      application/action/change_game_mode.php
  10. 2 4
      application/action/delete_team.php
  11. 4 7
      application/action/edit_profile.php
  12. 2 5
      application/action/login.php
  13. 0 2
      application/action/logout.php
  14. 3 6
      application/action/new_team.php
  15. 9 13
      application/action/optimize_get_rune_list.php
  16. 1 3
      application/action/rate_team.php
  17. 5 7
      application/action/save_run_team.php
  18. 39 29
      application/entity/Area.php
  19. 4 8
      application/entity/Artifact.php
  20. 66 41
      application/entity/Artifact_Effect.php
  21. 37 26
      application/entity/Artifact_Stat.php
  22. 1 4
      application/entity/Buildable.php
  23. 1 4
      application/entity/Building.php
  24. 1 4
      application/entity/Decoration.php
  25. 2 5
      application/entity/Decorative.php
  26. 1 4
      application/entity/Effect.php
  27. 2 5
      application/entity/Enchantment.php
  28. 2 5
      application/entity/Guild.php
  29. 1 4
      application/entity/Guild_Member.php
  30. 1 4
      application/entity/Guild_Skill.php
  31. 2 5
      application/entity/Guild_Skill_Group.php
  32. 1 3
      application/entity/Inventory.php
  33. 2 5
      application/entity/Item.php
  34. 1 4
      application/entity/Leader_Skill.php
  35. 5 19
      application/entity/Monster.php
  36. 2 7
      application/entity/Monster_Transformation.php
  37. 387 162
      application/entity/Player.php
  38. 4 7
      application/entity/Record.php
  39. 9 12
      application/entity/Run.php
  40. 6 10
      application/entity/Rune.php
  41. 1 4
      application/entity/Rune_Set.php
  42. 1 4
      application/entity/Rune_Stat.php
  43. 1 4
      application/entity/Server.php
  44. 3 6
      application/entity/Skill.php
  45. 1 4
      application/entity/Skill_Level.php
  46. 1 3
      application/entity/Source.php
  47. 3 6
      application/entity/Team.php
  48. 16 32
      application/entity/Unit.php
  49. 2 4
      application/entity/User.php
  50. 3 15
      application/page/Artifacts_Page.php
  51. 2 7
      application/page/Enchantments_Page.php
  52. 3 8
      application/page/Guild_Page.php
  53. 3 6
      application/page/Home_Page.php
  54. 4 9
      application/page/Monsters_Page.php
  55. 3 10
      application/page/Optimizer_Page.php
  56. 2 7
      application/page/Profile_Page.php
  57. 5 13
      application/page/Report_Rune_Enchantment_Page.php
  58. 5 12
      application/page/Report_Skillup_Page.php
  59. 3 11
      application/page/Runes_Page.php
  60. 2 7
      application/page/Runs_Page.php
  61. 13 18
      application/page/Stats_Page.php
  62. 4 11
      application/page/Teams_Page.php
  63. 3 8
      application/page/Units_Page.php
  64. 4 3
      application/view/artifacts.php
  65. 6 5
      application/view/catalog.php
  66. 4 3
      application/view/enchantments.php
  67. 2 1
      application/view/error.php
  68. 2 1
      application/view/guild.php
  69. 2 1
      application/view/help.php
  70. 50 47
      application/view/home.php
  71. 17 19
      application/view/inc/header.php
  72. 2 1
      application/view/monster.php
  73. 7 6
      application/view/optimizer.php
  74. 6 5
      application/view/optimizer_unit.php
  75. 2 1
      application/view/profile.php
  76. 5 3
      application/view/report.php
  77. 5 4
      application/view/report_rune_enchantment.php
  78. 5 4
      application/view/report_skillup.php
  79. 4 3
      application/view/runes.php
  80. 7 6
      application/view/runs.php
  81. 4 3
      application/view/stats.php
  82. 8 7
      application/view/teams.php
  83. 5 4
      application/view/unit.php
  84. 5 4
      application/view/units.php
  85. 13 1
      public/index.php

+ 2 - 2
application/API/v1/profile/DELETE.php

@@ -15,8 +15,6 @@
      * @category API
      */
 
-    global $db;
-
     try{
 
         header("Content-type: application/json; charset=utf-8");
@@ -44,6 +42,8 @@
             syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
             return 400;
         }
+        
+        $db = get_context()->get_db();
 
         // Authenticate
         $statement = $db->prepare("

+ 5 - 5
application/API/v1/profile/GET.php

@@ -15,8 +15,6 @@
      * @category API
      */
 
-    global $db;
-
     try{
         header("Content-type: application/json; charset=utf-8");
 
@@ -32,6 +30,8 @@
             return 403;
         }
 
+        $db = get_context()->get_db();
+
         // Get player
         $statement = $db->prepare("
           SELECT
@@ -67,9 +67,9 @@
         // Build array
         // TODO: Add way more data
         $player = [
-            "username" => $player->name,
-            "level" => $player->level,
-            "public" => $player->public
+            "username" => $player->get_name(),
+            "level" => $player->get_level(),
+            "public" => $player->is_public()
         ];
         echo(json_encode($player));
         header("HTTP/1.1 200 Success.");

+ 2 - 2
application/API/v1/profile/POST.php

@@ -17,8 +17,6 @@
      * @category API
      */
 
-    global $db;
-
     try{
 
         header("Content-type: application/json; charset=utf-8");
@@ -68,6 +66,8 @@
             $public = 0;
         }
 
+        $db = get_context()->get_db();
+
         // Check if player exists
         $statement = $db->prepare("SELECT COUNT(id) AS c FROM data.player WHERE id = :id;");
         $statement->bindValue(":id", $id, SQLITE3_INTEGER);

+ 6 - 7
application/API/v1/profile/PUT.php

@@ -13,9 +13,6 @@
      * @category API
      */
 
-    global $db;
-
-
     /**
      * Parses an artifact and saves it to the database.
      *
@@ -23,7 +20,7 @@
      * @param JSON artifact JSON fragment of the artifact.
      */
     function parse_artifact($player_id, $artifact){
-        global $db;
+        $db = get_context()->get_db();
         $statement = $db->prepare("
           INSERT INTO artifact (
             player,
@@ -190,7 +187,7 @@
         }
 
         // Instantiate and calculate efficiency:
-        $instance = new Artifact($artifact->{"rid"});
+        new Artifact($artifact->{"rid"});
 
         // Assigned?
         if (intval($artifact->{"occupied_id"}) != 0){
@@ -213,7 +210,7 @@
      * @param JSON rune JSON fragment of the rune.
      */
     function parse_rune($player_id, $rune, $lock_list){
-        global $db;
+        $db = get_context()->get_db();
         $statement = $db->prepare("
           INSERT INTO rune (
             player,
@@ -371,7 +368,7 @@
         }
 
         // Instantiate and calculate efficiency:
-        $instance = new Rune($rune->{"rune_id"});
+        new Rune($rune->{"rune_id"});
 
         // Assigned?
         if (intval($rune->{"occupied_id"}) != 0){
@@ -1467,6 +1464,8 @@
             return 405;
         }
 
+        $db = get_context()->get_db();
+
         // Get user info and Authenticate
         switch ($command){
             case "HubUserLogin":

+ 2 - 2
application/API/v1/run/POST.php

@@ -1304,8 +1304,6 @@
         return "201 Created.";
     }
 
-    global $db;
-
     try{
 
         header("Content-type: application/json; charset=utf-8");
@@ -1378,6 +1376,8 @@
         $player_id = $json["result_res"]->{"wizard_info"}->{"wizard_id"};
         $command = $json["result_res"]->{"command"};
 
+        $db = get_context()->get_db();
+
         // Authenticate
         $statement = $db->prepare("
           SELECT COUNT(player.id) AS c 

+ 6 - 7
application/API/v1/units/GET.php

@@ -10,8 +10,6 @@
      * @category API
      */
 
-    global $db;
-
     /**
      * Gets info on a unit.
      *
@@ -19,12 +17,13 @@
      * @param int $id Unit identifier
      * @return mixed[] Content.
      */
-    function get_unit($db, $id){
+    function get_unit($id){
         $result = [
             "status" => 404,
             "message" => "Unit not found",
             "content" => ""
         ];
+        $db = get_context()->get_db();
         $statement = $db->prepare("
           SELECT
             monster.id AS id,
@@ -288,10 +287,10 @@
     /**
      * Gets a list of units, paginated.
      *
-     * @param resource $db Database connection.
      * @return mixed[] Content.
      */
-    function get_unit_list($db){
+    function get_unit_list(){
+        $db = get_context()->get_db();
         $result = [
             "status" => 200,
             "message" => "Success.",
@@ -438,10 +437,10 @@
         /** @var mixed $query Request parameters, from API_CONTROLLER*/
         if (strlen($query[0]) > 0){
             $id = $query[0];
-            $result = get_unit($db, $id);
+            $result = get_unit($id);
         }
         else{
-            $result = get_unit_list($db);
+            $result = get_unit_list();
         }
         header("HTTP/1.1 " . $result["status"] . " " . $result["message"]);
         echo(json_encode($result["content"]));

+ 96 - 0
application/Context.php

@@ -0,0 +1,96 @@
+<?php
+    /**
+     * Context file.
+     *
+     * Provides a class to retrieve a context.
+     *
+     * @category Context
+     */
+
+    /**
+     * Application context.
+     *
+     * Stores usefull data to be retrieved anywhere in the app.
+     *
+     * @category Context
+     */
+    class Context {
+
+        private $user;
+        private $player;
+        private $db;
+        private $game_mode;
+
+        /**
+         * Retrieves the selected user.
+         *
+         * @return User The selected user, null if no specified.
+         */
+        public function get_user(){
+            return $this->user;
+        }
+
+        /**
+         * Retrieves the logged in player.
+         *
+         * @return Player The current player, null if not logged in.
+         */
+        public function get_player(){
+            return $this->player;
+        }
+
+        /**
+         * Retrieves the database connection.
+         *
+         * @return SQLITE3 The database connection.
+         */
+        public function get_db(){
+            return $this->db;
+        }
+
+        /**
+         * Retrieves the current game mode.
+         *
+         * @return int Game mode.
+         */
+        public function get_game_mode(){
+            return $this->game_mode;
+        }
+
+        /**
+         * Sets the current user.
+         *
+         * @param The current user.s
+         */
+        protected function set_user($user){
+            $this->user = $user;
+        }
+
+        /**
+         * Sets the currently logged-in player.
+         *
+         * @param Player Logged-in player.
+         */
+        protected function set_player($player){
+            $this->player = $player;
+        }
+
+        /**
+         * Sets the database connection
+         *
+         * @param SQLITE3 Database connection.
+         */
+        protected function set_db($db){
+            $this->db = $db;
+        }
+
+        /**
+         * Sets the game mode.
+         *
+         * @param int Game mode.
+         */
+        protected function set_game_mode($game_mode){
+            $this->game_mode = $game_mode;
+        }
+    }
+?>

+ 178 - 190
application/Controller.php

@@ -20,26 +20,7 @@
     require_once(PATH::ENTITY . "User.php");
     require_once(PATH::ENTITY . "Player.php");
     require_once(__DIR__ . "/Constant.php");
-
-    /**
-     * @global User $USER.
-     */
-    $USER = -1;
-
-    /**
-     * @global Player $PLAYER.
-     */
-    $PLAYER = -1;
-
-    /**
-     * @global int $MODE.
-     */
-    $MODE = GAME_MODE_ID::NORMAL;
-
-    /**
-     * @global resource $DB.
-     */
-    $db = null;
+    require_once(__DIR__ . "/Context.php");
 
     /**
      * Application controller.
@@ -49,61 +30,34 @@
      *
      * @category Controller
      */
-    class Controller {
-
-        /**
-         * Constructor.
-         *
-         * Handles every request, creating the required models and selecting the
-         * view.
-         *
-         * @param mixed[] $params POST parameters of the request.
-         * @return int HTTP status codes 200 or 404;
-         * @global int User ID.
-         * @global resource Databse connection.
-         */
-        public function __construct($params){
-
-            global $USER;
-            global $PLAYER;
-            global $MODE;
-            global $db;
-
-            $page = null;
-
-            session_start();
-            $db = DB::start();
+    class Controller extends Context{
 
-            // Parse parameters, get lang and build the route.
-            $pars = [];
-            foreach($params as $p){
-                if (strlen($p) > 0){
-                    array_push($pars, $p);
-                }
-            }
+        private $player;
+        private $user;
+        private $mode;
+        private $page;
+        private $db;
+        private $component; // web, api, action
+        private $command;
+        private $source;
+        private $params = [];
+        private $context;
 
+        public function action(){
             // API call: Use API controller
-            if (count($pars) > 0 && strtoupper($pars[0]) == "API"){
-                if (count($pars) == 1){
+            if (count($this->params) > 0 && strtoupper($this->params[0]) == "API"){
+                if (count($this->params) == 1){
                     // TODO: API MAIN PAGE
                 }
                 else{
-                    $version = strtolower($pars[1]);
-                    $api_params = $pars;
+                    $version = strtolower($this->params[1]);
+                    $api_params = $this->params;
                     array_shift($api_params);
                     array_shift($api_params);
                     switch ($version){
                         case "v1":
                             require_once(__DIR__ . "/API/v1/API_Controller.php");
-                            new API_Controller($params);
-                            break;
-                        case "v2":
-                            require_once(__DIR__ . "/API/v2/API_Controller.php");
-                            new API_Controller($params);
-                            break;
-                        case "v3":
-                            require_once(__DIR__ . "/API/v3/API_Controller.php");
-                            new API_Controller($params);
+                            new API_Controller($this->params);
                             break;
                     }
                 }
@@ -112,8 +66,8 @@
             // Special case: Actions.
             // Not redirecting to page, just execute a function
             // TODO: Secure actions
-            elseif (count($pars) > 1 && strtoupper($pars[0]) == "ACTION"){
-                switch (strtoupper($pars[1])){
+            elseif (count($this->params) > 1 && strtoupper($this->params[0]) == "ACTION"){
+                switch (strtoupper($this->params[1])){
                     case "CHANGE_GAME_MODE":
                         require_once(PATH::ACTION . "change_game_mode.php");
                         action();
@@ -188,13 +142,146 @@
                         http_response_code(404);
                 }
             }
+        
+            // Select the model to load.
+            array_shift($this->params); // Remove first one, should be player_id
+            if (sizeof($this->params) == 0){
+                require_once(PATH::PAGE . "Home_Page.php");
+                $page = new Home_Page();
+            }
+            else{
+                if (strtoupper($this->params[0]) == "RUNES"){
+                    require_once(PATH::PAGE . "Runes_Page.php");
+                    $page = new Runes_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "ENCHANTMENTS"){
+                    require_once(PATH::PAGE . "Enchantments_Page.php");
+                    $page = new Enchantments_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "ARTIFACTS"){
+                    require_once(PATH::PAGE . "Artifacts_Page.php");
+                    $page = new Artifacts_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "GUILD"){
+                    require_once(PATH::PAGE . "Guild_Page.php");
+                    $page = new Guild_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "RUNS"){
+                    require_once(PATH::PAGE . "Runs_Page.php");
+                    $page = new Runs_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "STATS"){
+                    require_once(PATH::PAGE . "Stats_Page.php");
+                    $page = new Stats_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "TEAMS"){
+                    require_once(PATH::PAGE . "Teams_Page.php");
+                    $page = new Teams_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "PROFILE"){
+                    require_once(PATH::PAGE . "Profile_Page.php");
+                    $page = new Profile_Page();
+                }
+                elseif (strtoupper($this->params[0]) == "UNITS"){
+                    if (count($this->params) == 1){
+                        require_once(PATH::PAGE . "Units_Page.php");
+                        $page = new Units_Page();
+                    }
+                    else{
+                        require_once(PATH::PAGE . "Unit_Page.php");
+                        $page = new Unit_Page($this->params[1]);
+                    }
+                }
+                elseif (strtoupper($this->params[0]) == "CATALOG"){
+                    if (count($this->params) == 1){
+                        require_once(PATH::PAGE . "Monsters_Page.php");
+                        $page = new Monsters_Page();
+                    }
+                    else{
+                        require_once(PATH::PAGE . "Monster_Page.php");
+                        $page = new Monster_Page($this->params[1]);
+                    }
+                }
+                elseif (strtoupper($this->params[0]) == "REPORT"){
+                    if (count($this->params) == 1){
+                        require_once(PATH::PAGE . "Report_Page.php");
+                        $page = new Report_Page();
+                    }
+                    elseif (strtoupper($this->params[1]) == "SKILLUP"){
+                        require_once(PATH::PAGE . "Report_Skillup_Page.php");
+                        $page = new Report_Skillup_Page($this->params[1]);
+                    }
+                    elseif (strtoupper($this->params[1]) == "RUNE_ENCHANTMENT"){
+                        require_once(PATH::PAGE . "Report_Rune_Enchantment_Page.php");
+                        $page = new Report_Rune_Enchantment_Page($this->params[1]);
+                    }
+                }
+                elseif (strtoupper($this->params[0]) == "OPTIMIZER"){
+                    if (count($this->params) == 1){
+                        require_once(PATH::PAGE . "Optimizer_Page.php");
+                        $page = new Optimizer_Page();
+                    }
+                    else{
+                        require_once(PATH::PAGE . "Optimizer_Unit_Page.php");
+                        $page = new Optimizer_Unit_Page($this->params[1]);
+                    }
+                }
+            }
 
-            // Authentication
-            $USER = null;
+            // Load the view, or set an error code.
+            if (isset($page)){
+                require_once($page->view);
+                return 200;
+            }
+            else{
+                require_once(PATH::PAGE . "Error_Page.php");
+                $page = new Error_Page(404);
+                http_response_code(404);
+                return 404;
+            }
+        }
+
+        public function get_context(){
+            return $this->context;
+        }
+
+        /**
+         * Constructor.
+         *
+         * Handles every request, creating the required models and selecting the
+         * view.
+         *
+         * @param mixed[] $params POST parameters of the request.
+         * @return int HTTP status codes 200 or 404;
+         */
+        public function __construct(){
+            $this->context = new Context();
+            session_start();
+            $this->db = DB::start();
+            $this->context->set_db($this->db);
+        }
+
+        /**
+         * Constructor.
+         *
+         * Receives the request params and gets itself ready.
+         *
+         * @param mixed[] $params POST parameters of the request.
+         * @return int HTTP status codes 200 or 404;
+         */
+        public function prepare($params){
+
+            // Parse parameters.
+            foreach($params as $p){
+                if (strlen($p) > 0){
+                    array_push($this->params, $p);
+                }
+            }
 
+            // Authentication
             if (array_key_exists("user_token", $_COOKIE) && array_key_exists("user_id", $_COOKIE)){
                 // Authenticated user, check token.
-                $statement = $db->prepare("
+                $statement = $this->db->prepare("
                   SELECT count(token) AS c
                   FROM token
                   WHERE
@@ -215,7 +302,8 @@
                     // Token is valid, renew session, and assign user.
                     $_SESSION["user_id"] = $_COOKIE["user_id"];
                     $_SESSION["session"] = true;
-                    $USER = new User($_COOKIE["user_id"]);
+                    $this->user = new User($_COOKIE["user_id"]);
+                    $this->context->set_user($this->user);
                 }
 
             }
@@ -225,11 +313,10 @@
 
 
             // Validate URL (check if it has the player ID).
-            $PLAYER = null;
-            if (sizeof($pars) > 0 && preg_match("/^[0-9]{6,12}$/", $pars[0]) == 1){
-error_log("PLAYER IN URL: " . $pars[0]);
+            $this->player = null;
+            if (sizeof($this->params) > 0 && preg_match("/^[0-9]{6,12}$/", $this->params[0]) == 1){
                 // Player ID passed in URL. Check it in database
-                $statement = $db->prepare("
+                $statement = $this->db->prepare("
                   SELECT
                     id,
                     user,
@@ -237,20 +324,21 @@ error_log("PLAYER IN URL: " . $pars[0]);
                   FROM player
                   WHERE id = :id
                 ");
-                $statement->bindValue(":id", $pars[0], SQLITE3_TEXT);
+                $statement->bindValue(":id", $this->params[0], SQLITE3_TEXT);
                 $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
                 if ($r){
                     // The player exists...
-                    if (null != $USER && $r["user"] == $USER->id){
+                    if (null != $this->user && $r["user"] == $this->user->id){
                         // ... and it belongs to the user. Proceed.
-                        $PLAYER = new Player($r["id"]);
-error_log("PLAYER SET: " . $PLAYER->id);
+                        $this->player = new Player($r["id"]);
+                        $this->context->set_player($this->player);
                     }
-                    elseif (null == $USER || $r["user"] != $USER->id){
+                    elseif (null == $this->user || $r["user"] != $this->user->id){
                         // ... and it doesn't belong to the user...
                         if ($r["public"] == 1){
                             // ... and it belongs to a public profile. Proceed.
-                            $PLAYER = new Player($r[$id]);
+                            $this->player = new Player($r["id"]);
+                            $this->context->set_player($this->player);
                         }
                         else{
                             // ... and it belongs to a private profile. Deny access.
@@ -260,10 +348,8 @@ error_log("PLAYER SET: " . $PLAYER->id);
                 }
             }
             else{
-error_log("PLAYER NOT IN URL: ");
                 // Player ID is not passed as URL...
-                if (null == $USER){
-error_log("    USER NOT LOGGED IN: ");
+                if (null == $this->user){
                     // ... and no user logged in. Go to the landing page.
                     require_once(PATH::PAGE . "Landing_Page.php");
                     $page = new Landing_Page();
@@ -271,15 +357,14 @@ error_log("    USER NOT LOGGED IN: ");
                     return;
                 }
                 else{
-error_log("    USER LOGGED IN: " . $USER->id);
                     // ... and the user is logged in. Get last accessed account.
-                    $statement = $db->prepare("
+                    $statement = $this->db->prepare("
                       SELECT id
                       FROM player
                       WHERE user = :user
                       ORDER BY last_access DESC
                     ");
-                    $statement->bindValue(":user", $USER->id, SQLITE3_INTEGER);
+                    $statement->bindValue(":user", $this->user->id, SQLITE3_INTEGER);
                     $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
                     if ($r){
                         header("Location: /" . $r["id"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH));
@@ -295,115 +380,18 @@ error_log("    USER LOGGED IN: " . $USER->id);
             }
 
             // Get game mode
-            $statement = $db->prepare("SELECT mode FROM player WHERE id = :id;");
-            $statement->bindValue(":id", $PLAYER->id, SQLITE3_INTEGER);
+            $statement = $this->db->prepare("SELECT mode FROM player WHERE id = :id;");
+            $statement->bindValue(":id", $this->player->get_id(), SQLITE3_INTEGER);
             $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
             if ($r && $r["mode"] == GAME_MODE_ID::RTA){
-                $MODE = GAME_MODE_ID::RTA;
-            }
-            else{
-                $MODE = GAME_MODE_ID::NORMAL;
-            }
-
-            // Select the model to load.
-            array_shift($pars); // Remove first one, should be player_id
-            if (sizeof($pars) == 0){
-                require_once(PATH::PAGE . "Home_Page.php");
-                $page = new Home_Page();
-            }
-            else{
-                if (strtoupper($pars[0]) == "FUSION"){
-                    require_once(PATH::PAGE . "Fusion_Page.php");
-                    $page = new Fusion_Page();
-                }
-                elseif (strtoupper($pars[0]) == "RUNES"){
-                    require_once(PATH::PAGE . "Runes_Page.php");
-                    $page = new Runes_Page();
-                }
-                elseif (strtoupper($pars[0]) == "ENCHANTMENTS"){
-                    require_once(PATH::PAGE . "Enchantments_Page.php");
-                    $page = new Enchantments_Page();
-                }
-                elseif (strtoupper($pars[0]) == "ARTIFACTS"){
-                    require_once(PATH::PAGE . "Artifacts_Page.php");
-                    $page = new Artifacts_Page();
-                }
-                elseif (strtoupper($pars[0]) == "GUILD"){
-                    require_once(PATH::PAGE . "Guild_Page.php");
-                    $page = new Guild_Page();
-                }
-                elseif (strtoupper($pars[0]) == "RUNS"){
-                    require_once(PATH::PAGE . "Runs_Page.php");
-                    $page = new Runs_Page();
-                }
-                elseif (strtoupper($pars[0]) == "STATS"){
-                    require_once(PATH::PAGE . "Stats_Page.php");
-                    $page = new Stats_Page();
-                }
-                elseif (strtoupper($pars[0]) == "TEAMS"){
-                    require_once(PATH::PAGE . "Teams_Page.php");
-                    $page = new Teams_Page();
-                }
-                elseif (strtoupper($pars[0]) == "PROFILE"){
-                    require_once(PATH::PAGE . "Profile_Page.php");
-                    $page = new Profile_Page();
-                }
-                elseif (strtoupper($pars[0]) == "UNITS"){
-                    if (count($pars) == 1){
-                        require_once(PATH::PAGE . "Units_Page.php");
-                        $page = new Units_Page();
-                    }
-                    else{
-                        require_once(PATH::PAGE . "Unit_Page.php");
-                        $page = new Unit_Page($pars[1]);
-                    }
-                }
-                elseif (strtoupper($pars[0]) == "CATALOG"){
-                    if (count($pars) == 1){
-                        require_once(PATH::PAGE . "Catalog_Page.php");
-                        $page = new Catalog_Page();
-                    }
-                    else{
-                        require_once(PATH::PAGE . "Monster_Page.php");
-                        $page = new Monster_Page($pars[1]);
-                    }
-                }
-                elseif (strtoupper($pars[0]) == "REPORT"){
-                    if (count($pars) == 1){
-                        require_once(PATH::PAGE . "Report_Page.php");
-                        $page = new Report_Page();
-                    }
-                    elseif (strtoupper($pars[1]) == "SKILLUP"){
-                        require_once(PATH::PAGE . "Report_Skillup_Page.php");
-                        $page = new Report_Skillup_Page($pars[1]);
-                    }
-                    elseif (strtoupper($pars[1]) == "RUNE_ENCHANTMENT"){
-                        require_once(PATH::PAGE . "Report_Rune_Enchantment_Page.php");
-                        $page = new Report_Rune_Enchantment_Page($pars[1]);
-                    }
-                }
-                elseif (strtoupper($pars[0]) == "OPTIMIZER"){
-                    if (count($pars) == 1){
-                        require_once(PATH::PAGE . "Optimizer_Page.php");
-                        $page = new Optimizer_Page();
-                    }
-                    else{
-                        require_once(PATH::PAGE . "Optimizer_Unit_Page.php");
-                        $page = new Optimizer_Unit_Page($pars[1]);
-                    }
-                }
-            }
-            // Load the view, or set an error code.
-            if (isset($page)){
-                require_once($page->view);
-                return 200;
+                $this->mode = GAME_MODE_ID::RTA;
             }
             else{
-                require_once(PATH::PAGE . "Error_Page.php");
-                $page = new Error_Page(404);
-                http_response_code(404);
-                return 404;
+                $this->mode = GAME_MODE_ID::NORMAL;
             }
+            $this->context->set_game_mode($this->mode);
+            
+            
         }
     }
 ?>

+ 1 - 3
application/action/change_game_mode.php

@@ -19,16 +19,14 @@
      *
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
-        global $db;
         $player = filter_input(INPUT_GET, "player");
         $mode = filter_input(INPUT_GET, "mode");
         if (mode != GAME_MODE_ID::NORMAL && mode != GAME_MODE_ID::RTA){
             return 400;
         }
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           UPDATE player
           SET mode = :mode
           WHERE id = :id;

+ 2 - 4
application/action/delete_team.php

@@ -17,18 +17,16 @@
      * 
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
 
-        global $db;
         $player = filter_input(INPUT_POST, 'player');
         $id = filter_input(INPUT_POST, 'id');
-        $statement = $db->prepare('DELETE FROM team WHERE player = :player AND id = :id;');
+        $statement = get_context()->get_db()->prepare('DELETE FROM team WHERE player = :player AND id = :id;');
         $statement->bindValue(':player', $player, SQLITE3_TEXT);
         $statement->bindValue(':id', $id, SQLITE3_TEXT);
         $statement->execute();
-        $statement = $db->prepare('DELETE FROM team_unit WHERE team = :id;');
+        $statement = get_context()->get_db()->prepare('DELETE FROM team_unit WHERE team = :id;');
         $statement->bindValue(':id', $id, SQLITE3_TEXT);
         $statement->execute();
         return 0;

+ 4 - 7
application/action/edit_profile.php

@@ -21,12 +21,9 @@
      * @return int|string 0 on success, negative values on error. If the API
      * key has been updated, the new key.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
 
-        global $db;
-
         $response = null;
 
         // TODO: Dont pass user as argument, use currently logged in one.
@@ -37,7 +34,7 @@
             if (!filter_var($mail, FILTER_VALIDATE_EMAIL)){
                 return -1;
             }
-            $s = $db->prepare('UPDATE user SET mail = :mail WHERE id = :user;');
+            $s = get_context()->get_db()->prepare('UPDATE user SET mail = :mail WHERE id = :user;');
             $s->bindValue(':user', $user, SQLITE3_TEXT);
             $s->bindValue(':mail', $mail, SQLITE3_TEXT);
             if(!$s->execute()){
@@ -48,7 +45,7 @@
         if (filter_input(INPUT_POST, "pass")){
             $pass = sha1(filter_input(INPUT_POST, 'pass'));
             $currentPass = sha1(filter_input(INPUT_POST, 'currentPass'));
-            $s = $db->prepare('SELECT COUNT(id) AS count FROM user WHERE id = :id AND password = :currentPass;');
+            $s = get_context()->get_db()->prepare('SELECT COUNT(id) AS count FROM user WHERE id = :id AND password = :currentPass;');
             $s->bindValue(':id', $user, SQLITE3_TEXT);
             $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
             $q = $s->execute();
@@ -56,7 +53,7 @@
             if ($r["count"] != 1){
                 return -3;
             }
-            $s = $db->prepare('UPDATE user SET password = :pass WHERE id = :id AND password = :currentPass;');
+            $s = get_context()->get_db()->prepare('UPDATE user SET password = :pass WHERE id = :id AND password = :currentPass;');
             $s->bindValue(':id', $user, SQLITE3_TEXT);
             $s->bindValue(':pass', $pass, SQLITE3_TEXT);
             $s->bindValue(':currentPass', $currentPass, SQLITE3_TEXT);
@@ -72,7 +69,7 @@
             for ($i = 0; $i < $charactersLength; $i++) {
                 $api .= $characters[rand(0, $charactersLength - 1)];
             }
-            $s = $db->prepare('UPDATE user SET api_key = :api WHERE id = :id;');
+            $s = get_context()->get_db()->prepare('UPDATE user SET api_key = :api WHERE id = :id;');
             $s->bindValue(':id', $user, SQLITE3_TEXT);
             $s->bindValue(':api', $api, SQLITE3_TEXT);
             if(!$s->execute()){

+ 2 - 5
application/action/login.php

@@ -19,12 +19,9 @@
      *
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
 
-        global $db;
-
         if (!isset($_POST['uname'], $_POST['password'])){
             header("Location: " . URL::BASE);
             exit();
@@ -40,7 +37,7 @@
             exit();
         }
         $password = hash('sha256', $password);
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           SELECT
             id,
             name
@@ -69,7 +66,7 @@
         //$_COOKIE["user_token"]) && isset($COOKIE["user_id"])
         $token = bin2hex(random_bytes(32));
         $expiry = time() + 5 * 24 * 60 * 60; // 5 days
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           INSERT INTO token (user, token, expiry) VALUES
           (:user, :token, :expiry);
         ");

+ 0 - 2
application/action/logout.php

@@ -15,10 +15,8 @@
      * 
      * @return int 0.
      * @category Action
-     * @global User The logged-in user.
      */
     function action(){
-        $USER = null;
         setcookie("user_id", null, time() - 3600);
         setcookie("user_token", null, time() - 3600);
         session_regenerate_id();

+ 3 - 6
application/action/new_team.php

@@ -24,12 +24,9 @@
      * 
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
 
-        global $db;
-
         $player = filter_input(INPUT_POST, 'player');
         $area_type = filter_input(INPUT_POST, 'area_type');
         $area = filter_input(INPUT_POST, 'area');
@@ -168,10 +165,10 @@
             return -4;
         }
         $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
-        $q = $db->query($s);
+        $q = get_context()->get_db()->query($s);
         $r = $q->fetchArray(SQLITE3_ASSOC);
         $team_id = $r["id"];
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           INSERT INTO team (
             player,
             id,
@@ -218,7 +215,7 @@
             if ($j < $total_front){
                 $front = 1;
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               INSERT INTO team_unit (team, unit, leader, front)
               VALUES (:team, :unit, :leader, :front);
             ");

+ 9 - 13
application/action/optimize_get_rune_list.php

@@ -22,13 +22,9 @@
      * @return int|string 0 on success, negative values on error. If the API
      * key has been updated, the new key.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
 
-        global $db;
-        global $MODE;
-
         // Increase max execution time.
         set_time_limit(60);
         
@@ -119,29 +115,29 @@
             case 0: // Storage only (or itself)
                 $base_s .= "
                   (
-                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = $MODE)
-                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = $MODE)
+                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = " . get_context()->get_game_mode() . ")
+                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = " . get_context()->get_game_mode() . ")
                   )";
                 break;
             case 1: // Units in no teams (or itself)
                 $base_s .= "
                   (
-                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = $MODE)
-                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = $MODE)
-                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = $MODE AND unit NOT IN (SELECT DISTINCT unit FROM team_unit))
+                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = " . get_context()->get_game_mode() . ")
+                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = " . get_context()->get_game_mode() . ")
+                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = " . get_context()->get_game_mode() . " AND unit NOT IN (SELECT DISTINCT unit FROM team_unit))
                   )
                 ";
                 break;
             case 2: // Units in teams with 0 score (or itself)
                 $base_s .= "
                   (
-                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = $MODE)
-                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = $MODE)
+                    rune.id IN (SELECT DISTINCT unit FROM data.unit_rune WHERE unit = :unit_id AND rta = " . get_context()->get_game_mode() . ")
+                    OR rune.id NOT IN (SELECT DISTINCT unit FROM data.unit_rune WHERE rta = " . get_context()->get_game_mode() . ")
                     OR rune.id NOT IN (
                       SELECT DISTINCT unit 
                       FROM unit_rune 
                       WHERE
-                        rta = $MODE
+                        rta = " . get_context()->get_game_mode() . "
                         AND unit NOT IN (
                           SELECT DISTINCT unit 
                           FROM
@@ -177,7 +173,7 @@
 
         $total_candidates = 0;
         for ($i = 1; $i <= 6; $i ++){
-            $statement = $db->prepare($s[$i]);
+            $statement = get_context()->get_db()->prepare($s[$i]);
             $statement->bindValue(':player', $player, SQLITE3_TEXT);
             $statement->bindValue(':unit_id', $unit_id, SQLITE3_TEXT);
             $statement->bindValue(':stat_0', $stats[0], SQLITE3_TEXT);

+ 1 - 3
application/action/rate_team.php

@@ -19,14 +19,12 @@
      *
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
-        global $db;
         $player = filter_input(INPUT_GET, "player");
         $team = filter_input(INPUT_GET, "team");
         $score = intval(filter_input(INPUT_GET, "score"));
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           UPDATE team
           SET score = :score
           WHERE

+ 5 - 7
application/action/save_run_team.php

@@ -19,10 +19,8 @@
      * 
      * @return int 0 on success, negative values on error.
      * @category Action
-     * @global resource Database connection.
      */
     function action(){
-        global $db;
         $player = filter_input(INPUT_POST, 'player');
         $run = filter_input(INPUT_POST, 'run');
         $name = filter_input(INPUT_POST, 'name');
@@ -31,10 +29,10 @@
             return -1;
         }
         $s = "SELECT CASE WHEN MAX(id) IS NULL THEN 1 ELSE MAX(id) + 1 END AS id FROM team;";
-        $q = $db->query($s);
+        $q = get_context()->get_db()->query($s);
         $r = $q->fetchArray(SQLITE3_ASSOC);
         $team_id = $r["id"];
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           SELECT
             run.area AS area,
             run.stage AS stage,
@@ -55,7 +53,7 @@
         if (!$r){
             return -2;
         }
-        $statement = $db->prepare("
+        $statement = get_context()->get_db()->prepare("
           INSERT INTO team (
             player,
             id,
@@ -91,9 +89,9 @@
             return -3;
         }
         $s = "SELECT unit FROM data.run_party WHERE run = $run AND unit IS NOT NULL;";
-        $q = $db->query($s);
+        $q = get_context()->get_db()->query($s);
         while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               INSERT INTO data.team_unit (team, unit)
               VALUES (:team, :unit)
             ");

+ 39 - 29
application/entity/Area.php

@@ -1,14 +1,14 @@
 <?php
     /**
-     * Area type entity file.
+     * Area entity file.
      *
      * Creates the entity and makes it available.
      *
      * @category Entity
      */
 
-    /**
-     * Require dependent entities if not present.
+    /*
+     * Require entity dependencies if not present.
      */
     require_once(PATH::ENTITY . "Entity.php");
 
@@ -22,38 +22,23 @@
      */
     class Area extends Entity{
 
-        /**
-         * @var int Area identifier (it may not be unique!).
-         */
-        public $id;
-
-        /**
-         * @var int Area type.
-         */
-        public $type;
-
-        /**
-         * @var string Area name.
-         */
-        public $name;
+        private $id;
+        private $type;
+        private $name;
 
         /**
          * Constructor.
          *
-         * Searches the database and retrieves the information about the
-         * run, populating it and it's items.
+         * Retrieves the area information from the database.
          *
          * @param int $id Area identifier.
-         * @param int $type Area type (required).
-         * @global resource Database connection.
+         * @param int $type Area type.
          */
         public function __construct($id, $type){
 
-            global $db;
-
             $this->id = $id;
             $this->type = $type;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   id,
                   type,
@@ -75,7 +60,7 @@
                 $this->name = "Hall of Heroes";
             }
             else{
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT name
                   FROM area_type
                   WHERE id = :type;
@@ -88,11 +73,36 @@
         }
 
         /**
-         * Gets the path to the area image. The image must be in the
-         * img/area/ directory, and its name must be the area type id,
-         * padded with '0' to 9 digites, and the extension must be '.png'.
+         * Retrieves the area identifier.
+         *
+         * @return int Area ID.
+         */
+        public function getId(){
+            return $this->id;
+        }
+    
+        /**
+         * Retrieves Area type identifier.
+         *
+         * @return int Area type ID
+         */
+        public function getType(){
+            return $this->type;
+        }
+    
+        /**
+         * Retrieves the area name.
+         *
+         * @return string The area name.
+         */
+        public function getName(){
+            return $this->name;
+        }
+    
+        /**
+         * Gets the URL to the area image.
          *
-         * @param string $stage Stage number, for a better image in Cairos.
+         * @param string $stage Stage number, for a more accurate image in Dimension Hole.
          * @return string Image URL, or a fixed unknown image.
          */
         public function get_image($stage = null){

+ 4 - 8
application/entity/Artifact.php

@@ -122,13 +122,10 @@
          * artifact, populating it and it's items.
          *
          * @param int $id Artifact id.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 type,
@@ -165,7 +162,7 @@
                 $this->stat = new Artifact_Stat($this->id);
 
                 // Get effects
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT slot
                   FROM artifact_effect
                   WHERE artifact = :artifact
@@ -181,7 +178,7 @@
                 if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
                     $this->efficiency = $this->calculate_efficiency();
                     $this->max_efficiency = $this->calculate_maximum_efficiency();
-                    $statement = $db->prepare("
+                    $statement = get_context()->get_db()->prepare("
                       UPDATE artifact
                       SET
                         efficiency = :efficiency,
@@ -225,7 +222,6 @@
          * @return float Efficiency
          */
         private function calculate_efficiency(){
-            global $db;
             $eff = [0.0, 0.0, 0.0, 0.0];
 
             $query = "
@@ -246,7 +242,7 @@
                 if (sizeof($this->effects) > $i && $this->effects[$i] != null && $this->effects[$i]->value > 0){
                     $value = $this->effects[$i]->value;
                     $value = $this->effects[$i]->value;
-                    $statement = $db->prepare($query);
+                    $statement = get_context()->get_db()->prepare($query);
                     $statement->bindValue(':initial', 1, SQLITE3_TEXT);
                     $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
                     $statement->bindValue(':effect', $this->effects[$i]->effect, SQLITE3_TEXT);

+ 66 - 41
application/entity/Artifact_Effect.php

@@ -7,7 +7,7 @@
      * @category Entity
      */
 
-    /**
+    /*
      * Require dependent entities if not present.
      */
     require_once(PATH::ENTITY . "Entity.php");
@@ -21,55 +21,24 @@
      */
     class Artifact_Effect extends Entity{
 
-        /**
-         * @var int Effect slot.
-         *
-         * @see ARTIFACT_EFFECT_SLOT_ID
-         */
-        public $slot;
-
-        /**
-         * @var int Effect identifier.
-         *
-         * @see ARTIFACT_EFFECT_ID
-         */
-        public $effect;
-
-        /**
-         * @var string Effect name (uppercase).
-         */
-        public $name = "";
-
-        /**
-         * @var int Effect value.
-         */
-        public $value;
-
-        /**
-         * @var int Ammount gained by effect grindstone.
-         */
-        public $grind;
-
-        /**
-         * @var int Indicates if a gem has been used.
-         */
-        public $enchant;
+        private $slot;
+        private $effect;
+        private $name = "";
+        private $value;
+        private $grind;
+        private $enchant;
 
         /**
          * Constructor.
          *
-         * Searches the database and retrieves the information about the
-         * artifact effect, populating it and it's items.
+         * Retrieves the artifact effect information from the database.
          *
          * @param int $artifact Artifact id.
          * @param int $slot Artifact slot id.
-         * @global resource Database connection.
          */
         public function __construct($artifact, $slot){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 slot,
                 effect,
@@ -89,7 +58,7 @@
                 $this->value = $r["value"];
                 $this->enchant = $r["enchant"];
                 $this->grind = $r["grind"];
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT name
                   FROM effect_artifact
                   WHERE id = :id;
@@ -103,5 +72,61 @@
             }
         }
 
+        /**
+         * Retrieves the artifact effect's slot identifier.
+         *
+         * @see ARTIFACT_EFFECT_SLOT_ID
+         *
+         * @return int Effect slot ID.
+         */
+        public function getSlot(){
+            return $this->slot;
+        }
+        
+        /**
+         * Artifact effect identifier.
+         *
+         * @return int Effect ID.
+         */
+        public function getEffect(){
+            return $this->effect;
+        }
+        
+        /**
+         * Retrieves the artifact effect name.
+         *
+         * @return string The effect name.
+         */
+        public function getName(){
+            return $this->name;
+        }
+        
+        /**
+         * Retrieves the value of the artifact effect, excluding grinds.
+         *
+         * @return number Effect value.
+         */
+        public function getValue(){
+            return $this->value;
+        }
+        
+        /**
+         * Retrieves the grinded value on the effect.
+         *
+         * @return number Effect grinded value.
+         */
+        public function getGrind(){
+            return $this->grind;
+        }
+        
+        /**
+         * Checks if the effect has been enchanted.
+         *
+         * @return boolean True if the effect was enchanted, false otherwise
+         */
+        public function isEnchanted(){
+            return filter_var($this->enchant, FILTER_VALIDATE_BOOLEAN);
+        }
+
     }
 ?>

+ 37 - 26
application/entity/Artifact_Stat.php

@@ -7,7 +7,7 @@
      * @category Entity
      */
 
-    /**
+    /*
      * Require dependent entities if not present.
      */
     require_once(PATH::ENTITY . "Entity.php");
@@ -21,37 +21,20 @@
      */
     class Artifact_Stat extends Entity{
 
-        /**
-         * @var int Stat of the artifact.
-         *
-         * @see ARTIFACT_STAT_ID
-         */
-        public $stat;
-
-        /**
-         * @var string Stat name (uppercase).
-         */
-        public $name = "";
-
-        /**
-         * @var int Stat value of the rune.
-         */
-        public $value;
+        private $stat;
+        private $name = "";
+        private $value;
 
         /**
          * Constructor.
          *
-         * Searches the database and retrieves the information about the
-         * artifact stat, populating it and it's items.
+         * Retrieves the artifact stat information from the database.
          *
-         * @param int $id Artifact id.
-         * @global resource Database connection.
+         * @param int $artifact_id Artifact id.
          */
-        public function __construct($artifact){
+        public function __construct($artifact_id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 stat,
                 value
@@ -59,7 +42,7 @@
               WHERE
                 artifact = :artifact
             ");
-            $statement->bindValue(':artifact', $artifact, SQLITE3_TEXT);
+            $statement->bindValue(':artifact', $artifact_id, SQLITE3_TEXT);
             $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
             if ($r){
                 $this->stat = $r["stat"];
@@ -77,6 +60,34 @@
                 }
             }
         }
+        /**
+         * Retrieves the artifact stat identifier.
+         * 
+         * @see ARTIFACT_STAT_ID
+         *
+         * @return int Artifact stat ID.
+         */
+        public function getStat(){
+            return $this->stat;
+        }
+
+        /**
+         * Retrieves the artifact stat name.
+         *
+         * @return string Artifact stat name.
+         */
+        public function getName(){
+            return $this->name;
+        }
+
+        /**
+         * Retrieves the value of the artifact stat value.
+         *
+         * @return number Stat value.
+         */
+        public function getValue(){
+            return $this->value;
+        }
 
     }
 ?>

+ 1 - 4
application/entity/Buildable.php

@@ -44,13 +44,10 @@
          * building, populating it and it's items.
          *
          * @param int $id Buildable identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,

+ 1 - 4
application/entity/Building.php

@@ -45,13 +45,10 @@
          * building, populating it and it's items.
          *
          * @param int $id Building identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 player,
                 buildable,

+ 1 - 4
application/entity/Decoration.php

@@ -44,13 +44,10 @@
          * decoration, populating it and it's items.
          *
          * @param int $id Building identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 player,
                 decorative,

+ 2 - 5
application/entity/Decorative.php

@@ -74,13 +74,10 @@
          * decoration, populating it and it's items.
          *
          * @param int $id Decorative identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   id,
                   name,
@@ -102,7 +99,7 @@
                 $this->stat = $r["affected_stat"];
                 $this->element = $r["element"];
                 $this->max_level = $r["max_level"];
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       level,
                       bonus,

+ 1 - 4
application/entity/Effect.php

@@ -48,13 +48,10 @@
          * skill level, populating it and it's items.
          *
          * @param int $id Effect identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,

+ 2 - 5
application/entity/Enchantment.php

@@ -45,7 +45,7 @@
         public $quality_name;
 
         /**
-         * @var K_Rune_Set Rune set the item can be used on.
+         * @var Rune_Set Rune set the item can be used on.
          */
         public $rune;
 
@@ -108,13 +108,10 @@
          * rune, populating it and it's items.
          *
          * @param int $id Enchantment id.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 enchantment.player AS player,
                 enchantment.id AS id,

+ 2 - 5
application/entity/Guild.php

@@ -79,13 +79,10 @@
          * guild, populating it and it's items.
          *
          * @param int $id Guild identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,
@@ -114,7 +111,7 @@
                 $this->comment = HTML::e($r["comment"]);
                 $this->notice = HTML::e($r["notice"]);
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT member
               FROM guild_member
               WHERE guild = :guild

+ 1 - 4
application/entity/Guild_Member.php

@@ -84,13 +84,10 @@
          * guild, populating it and it's items.
          *
          * @param int $id Guild identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 member,
                 guild,

+ 1 - 4
application/entity/Guild_Skill.php

@@ -48,13 +48,10 @@
          * skill, populating it and it's items.
          *
          * @param int $id Skill identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 skill_group,

+ 2 - 5
application/entity/Guild_Skill_Group.php

@@ -49,13 +49,10 @@
          * skill group, populating it and it's items.
          *
          * @param int $id Skill group identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,
@@ -69,7 +66,7 @@
                 $this->id = $r["id"];
                 $this->name = HTML::e($r["name"]);
                 $this->description = HTML::e($r["description"]);
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT id
                   FROM guild_skill
                   WHERE skill_group = :id;

+ 1 - 3
application/entity/Inventory.php

@@ -49,10 +49,8 @@
          *
          * @param int $id Item identifier.
          * @param int $type Item type identifier. Optional, will try to guess if null.
-         * @global resource Database connection.
          */
         public function __construct($id, $type = null){
-            global $db;
             $s = "
               SELECT
                 inventory.id AS id,
@@ -69,7 +67,7 @@
             if ($type != null){
                 $s .= "AND inventory.type = :type";
             }
-            $statement = $db->prepare($s);
+            $statement = get_context()->get_db()->prepare($s);
             $statement->bindValue(':id', $id, SQLITE3_TEXT);
             $statement->bindValue(':type', $type, SQLITE3_TEXT);
             $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);

+ 2 - 5
application/entity/Item.php

@@ -60,12 +60,9 @@
          *
          * @param int $id Item identifier.
          * @param int $type Item type identifier. Optional, will try to guess if null.
-         * @global resource Database connection.
          */
         public function __construct($id, $type = null){
 
-            global $db;
-
             $s = "
                 SELECT
                   item.id AS id,
@@ -87,7 +84,7 @@
             if ($type != null){
                 $s .= "AND inventory.type = :type";
             }
-            $statement = $db->prepare($s);
+            $statement = get_context()->get_db()->prepare($s);
             $statement->bindValue(':id', $id, SQLITE3_TEXT);
             $statement->bindValue(':type', $type, SQLITE3_TEXT);
             $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
@@ -96,7 +93,7 @@
                 $this->amount = $r["amount"];
                 if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
                     $this->is_monster_pices = true;
-                    $this->id = new K_Unit($r["id"]);
+                    $this->id = new Monster($r["id"]);
                     $this->name = HTML::e($this->id->title) . " Pieces";
                     $this->description = HTML::e($this->id->title) . " Pieces";
                 }

+ 1 - 4
application/entity/Leader_Skill.php

@@ -53,13 +53,10 @@
          * skill, populating it and it's items.
          *
          * @param int $id Skill identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 stat,

+ 5 - 19
application/entity/Monster.php

@@ -232,12 +232,10 @@
          *
          * @param int $id Monster identifier.
          * @param bool $complete If false, query only monster.
-         * @global resource Database connection.
          */
         public function __construct($id, $complete = true){
-            global $db;
             $this->id = $id;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 family,
@@ -385,13 +383,10 @@
          * It is autoatically called at construction time if the
          * $complete parameter is true (by default). No point in calling it
          * twice.
-         *
-         * @global resource Database connection.
          */
         public function load_essences(){
-            global $db;
             $this->essences = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 item,
                 amount
@@ -417,13 +412,10 @@
          * It is automatically called at construction time if the
          * $complete parameter is true (by default). No point in calling it
          * twice.
-         *
-         * @global resource Database connection.
          */
         public function load_transformation(){
-            global $db;
             $this->transformation = null;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT id
               FROM monster_transformation
               WHERE monster = :monster;
@@ -441,13 +433,10 @@
          * It is autoatically called at construction time if the
          * $complete parameter is true (by default). No point in calling it
          * twice.
-         *
-         * @global resource Database connection.
          */
         public function load_skills(){
-            global $db;
             $this->skill = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT skill.id AS skill
               FROM
                 skill,
@@ -470,13 +459,10 @@
          * It is autoatically called at construction time if the
          * $complete parameter is true (by default). No point in calling it
          * twice.
-         *
-         * @global resource Database connection.
          */
         public function load_sources(){
-            global $db;
             $this->sources = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT source
               FROM monster_source
               WHERE monster = :monster;

+ 2 - 7
application/entity/Monster_Transformation.php

@@ -52,11 +52,9 @@
          * trnsformed monster, populating it and it's items.
          *
          * @param int $id Transformed monster identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
-            global $db;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 family,
@@ -76,13 +74,10 @@
 
         /**
          * Loads the information about the monster skills in the transformed form.
-         *
-         * @global resource Database connection.
          */
         private function load_skills(){
-            global $db;
             $this->skill = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT skill
               FROM
                 skill,

+ 387 - 162
application/entity/Player.php

@@ -27,115 +27,30 @@
      */
     class Player extends Entity{
 
-        /**
-         * @var int Player ID.
-         */
-        public $id;
-
-        /**
-         * @var Server Player server.
-         */
-        public $server;
-
-        /**
-         * @var string Player country (XX).
-         */
-        public $country;
-
-        /**
-         * @var string Player language (xx).
-         */
-        public $language;
-
-        /**
-         * @var int Player level.
-         */
-        public $level;
-
-        /**
-         * @var string Player name.
-         */
-        public $name;
-
-        /**
-         * @var int Total experience.
-         */
-        public $experience;
-
-        /**
-         * @var Unit Representative Unit.
-         */
-        public $rep;
-
-        /**
-         * @var int Timestamp of player registration.
-         */
-        public $joined = 0;
-
-        /**
-         * @var int Top rank in arena.
-         */
-        public $top_rank_arena = 0;
-
-        /**
-         * @var int Top rank in world arena.
-         */
-        public $top_rank_world_arena = 0;
-
-        /**
-         * @var int Top rank in special league.
-         */
-        public $top_rank_special_league = 0;
-
-        /**
-         * @var int Top rank in guild war.
-         */
-        public $top_rank_gw = 0;
-
-        /**
-         * @var int Top rank in guild siege.
-         */
-        public $top_rank_siege = 0;
-
-        /**
-         * @var int Top rank in World Boss.
-         */
-        public $top_rank_wboss = 0;
-
-        /**
-         * @var int Top rank in TOA normal.
-         */
-        public $top_rank_toan = 0;
-
-        /**
-         * @var int Top rank in TOA HARD.
-         */
-        public $top_rank_toah = 0;
-
-        /**
-         * @var int Top rank in TOA HELL.
-         */
-        public $top_rank_toal = 0;
-
-        /**
-         * @var \Unit[] Units in Arena defense.
-         */
-        public $defense = [];
-
-        /**
-         * @var \Unit[][] Units in Guild War defense.
-         */
-        public $gw_defense = [[], []];
-
-        /**
-         * @var \Unit[][] Units in Siege Battle defense.
-         */
-        public $siege_defense = [];
-
-        /**
-         * @var int[] Currency.
-         */
-        public $currency = [
+        private $id;
+        private $server;
+        private $country;
+        private $language;
+        private $level;
+        private $name;
+        private $experience;
+        private $rep = null;
+        private $rep_id = null;
+        private $joined = 0;
+        private $top_rank_arena = 0;
+        private $top_rank_world_arena = 0;
+        private $top_rank_special_league = 0;
+        private $top_rank_gw = 0;
+        private $top_rank_siege = 0;
+        private $top_rank_wboss = 0;
+        private $top_rank_toan = 0;
+        private $top_rank_toah = 0;
+        private $top_rank_toal = 0;
+        private $flag_defense = false;
+        private $arena_defense = [];
+        private $gw_defense = [[], []];
+        private $siege_defense = [];
+        private $currency = [
             "mana" => 0,
             "crystal" => 0,
             "energy" => 0,
@@ -152,41 +67,17 @@
             "ancient_stone" => 0,
             "costume_point" => 0,
         ];
-
-        /**
-         * @var \Building[] List of Buildings.
-         */
-        public $building = [];
-
-        /**
-         * @var \Decoration[] List of Decorations.
-         */
-        public $decoration = [];
-
-        /**
-         * @var \item[] List of Scrolls in item.
-         */
-        public $item_scroll = [];
-
-        /**
-         * @var \item[] List of rune crafting items in item.
-         */
-        public $item_craft_rune = [];
-
-        /**
-         * @var \item[] List of rune crafting items in item.
-         */
-        public $item_craft = [];
-
-        /**
-         * @var \item[] List of rune crafting items in item.
-         */
-        public $item_essence = [];
-
-        /**
-         * @var \Record[] Player record runs.
-         */
-        public $record = [];
+        private $flag_building = false;
+        private $building = [];
+        private $decoration = [];
+        private $flag_item = false;
+        private $item_scroll = [];
+        private $item_craft_rune = [];
+        private $item_craft = [];
+        private $item_essence = [];
+        private $flag_record = false;
+        private $record = [];
+        private $public = false;
 
 
         /**
@@ -197,11 +88,9 @@
          *
          * @param int $id Monster identifier.
          * @param bool $complete If false, query only monster and k_monster.
-         * @global resource Database connection.
          */
         public function __construct($id, $complete = true){
-            global $db;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 server,
@@ -234,7 +123,8 @@
                 top_rank_wboss,
                 top_rank_toan,
                 top_rank_toah,
-                top_rank_toal
+                top_rank_toal,
+                public
               FROM player
               WHERE id = :id;
             ");
@@ -249,7 +139,7 @@
                 $this->level = $r["level"];
                 $this->experience = $r["experience"];
                 if (strlen($r["rep"]) > 0){
-                    $this->rep = new Unit($r["rep"]);
+                    $this->rep_id = $r["rep"];
                 }
                 $this->currency["mana"] = $r["mana"];
                 $this->currency["crystal"] = $r["crystal"];
@@ -265,7 +155,8 @@
                 $this->currency["event_coin"] = $r["event_coin"];
                 $this->currency["social_point"] = $r["social_point"];
                 $this->currency["costume_point"] = $r["costume_point"];
-                $this->joined = $r["joined"];
+                $this->joined = new Datetime();
+                $this->joined->setTimestamp($r["joined"]);
                 $this->top_rank_arena = $r["top_rank_arena"];
                 $this->top_rank_world_arena = $r["top_rank_world_arena"];
                 $this->top_rank_special_league = $r["top_rank_special_league"];
@@ -275,8 +166,14 @@
                 $this->top_rank_toan = $r["top_rank_toan"];
                 $this->top_rank_toah = $r["top_rank_toah"];
                 $this->top_rank_toal = $r["top_rank_toal"];
+                if (strlen($r["public"]) == 1){
+                    $this->public = true;
+                }
             }
-            $statement = $db->prepare("
+        }
+
+        private function load_defenses(){
+            $statement = get_context()->get_db()->prepare("
                 SELECT unit
                 FROM defense
                 WHERE
@@ -288,9 +185,9 @@
             $statement->bindValue(':area_type', AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                array_push($this->defense, new Unit($r["unit"], false));
+                array_push($this->arena_defense, new Unit($r["unit"], false));
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   unit,
                   position
@@ -312,7 +209,11 @@
                 }
             }
             // TODO: Siege defenses
-            $statement = $db->prepare("
+            $this->flag_defense = true;
+        }
+
+        private function load_buildings(){
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   building.id AS id,
                   min(building.buildable) AS min
@@ -321,7 +222,7 @@
                   buildable
                 WHERE
                   player = :player AND
-                  building.buildable = buildable.id 
+                  building.buildable = buildable.id
                 GROUP BY building.buildable;
             ");
             $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
@@ -329,7 +230,7 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->building, new Building($r["id"]));
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT id
                 FROM decoration
                 WHERE player = :player;
@@ -339,7 +240,11 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->decoration, new Decoration($r["id"]));
             }
-            $statement = $db->prepare("
+            $this->flag_building = true;
+        }
+
+        private function load_items(){
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   id,
                   type
@@ -357,7 +262,7 @@
                 array_push($this->item_scroll, new item($r["id"], $r["type"]));
             }
             // Rune crafting items are marked as generic crafting.
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   id,
                   type
@@ -374,7 +279,7 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->item_craft_rune, new item($r["id"], $r["type"]));
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   id,
                   type
@@ -391,7 +296,7 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->item_craft, new item($r["id"], $r["type"]));
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   item.id AS id,
                   item.type AS type
@@ -422,7 +327,11 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->item_essence, new item($r["id"], $r["type"]));
             }
-            $statement = $db->prepare("
+            $this->flag_item = true;
+        }
+
+        private function load_records(){
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   player,
                   area_type,
@@ -448,7 +357,7 @@
                     WHEN (area_type = 2 AND area = 4001) THEN 15 -- Hall of Wind
                     WHEN (area_type = 2 AND area = 7001) THEN 16 -- Hall of Light
                     WHEN (area_type = 2 AND area = 1001) THEN 17 -- Hall of Dark
-                    ELSE area 
+                    ELSE area
                     END ASC
             ");
             $statement->bindValue(':player', $this->id, SQLITE3_TEXT);
@@ -456,7 +365,323 @@
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->record, new Record($r["player"], $r["area_type"], $r["area"]));
             }
+            $this->flag_record = true;
+        }
+
+        /**
+         * Retrieves the player identifier
+         *
+         * @return int Plyaer ID.
+         */
+        public function get_id(){
+            return $this->id;
+        }
+
+        /**
+         * Retrieves the player server.
+         *
+         * @return Server The player's account server.
+         */
+        public function get_server(){
+            return $this->server;
+        }
+
+        /**
+         * Retrieves the player's country.
+         *
+         * @return string Two digit country code.
+         */
+        public function get_country(){
+            return $this->country;
+        }
+
+        /**
+         * Retrieves the player's language.
+         *
+         * @return string Two digit language code.
+         */
+        public function get_language(){
+            return $this->language;
+        }
+
+        /**
+         * Retrieves the player's level,
+         *
+         * @return int Player's level
+         */
+        public function get_level(){
+            return $this->level;
+        }
+
+        /**
+         * Retrieves the player's name
+         *
+         * @return string The playe's name.
+         */
+        public function get_name(){
+            return $this->name;
+        }
+
+        /**
+         * Retrieves the total experience earned by the user.
+         *
+         * @return number Total experience.
+         */
+        public function get_experience(){
+            return $this->experience;
+        }
+
+        /**
+         * Retrieves the player's representative unit.
+         *
+         * @return Unit Player's representative
+         */
+        public function get_rep(){
+            if ($this->rep_id != null && $this->rep == null){
+                $this->rep = new Unit($this->rep_id);
+            }
+            return $this->rep;
+        }
+
+        /**
+         * Gets the date the user joined the game.
+         *
+         * If $format is supplied, a string in the desired format will be returned. If not, the raw
+         * DateTime object will e returned.
+         *
+         * @param string $format A date format (optional).
+         * @return DateTime | string The raw date, or a formatted version.
+         */
+        public function get_joined($format = null){
+            if (null == $format){
+                return $this->joined;
+            }
+            else{
+                return $this->joined->format($format);
+            }
+        }
+
+        /**
+         * Retrieves the user's top rank in the Arena.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_arena(){
+            return $this->top_rank_arena;
+        }
+
+        /**
+         * Retrieves the user's top rank in the WorldArena.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_world_arena(){
+            return $this->top_rank_world_arena;
+        }
+
+        /**
+         * Retrieves the user's top rank in the Arena.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_special_league(){
+            return $this->top_rank_special_league;
+        }
+
+        /**
+         * Retrieves the user's top rank in Guild War.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_gw(){
+            return $this->top_rank_gw;
+        }
+
+        /**
+         * Retrieves the user's top rank in Siege battle.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_siege(){
+            return $this->top_rank_siege;
+        }
+
+        /**
+         * Retrieves the user's top rank in the World Boss.
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_wboss(){
+            return $this->top_rank_wboss;
+        }
+
+        /**
+         * Retrieves the user's top rank in the Trial of Ascension (normal difficulty).
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_toan(){
+            return $this->top_rank_toan;
+        }
+
+        /**
+         * Retrieves the user's top rank in the Trial of Ascension (hard difficulty).
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_toah(){
+            return $this->top_rank_toah;
+        }
+
+        /**
+         * Retrieves the user's top rank in the Trial of Ascension (hell difficulty).
+         *
+         * @return int Top rank.
+         */
+        public function get_top_rank_toal(){
+            return $this->top_rank_toal;
+        }
+
+        /**
+         * Retrieves the Arena defense units.
+         *
+         * @return \Unit[] Units in the arena defense.
+         */
+        public function get_arena_defense(){
+            if (!$this->flag_defense){
+                $this->load_defenses();
+            }
+            return $this->arena_defense;
         }
 
+        /**
+         * Retrieves the Guild War defenses.
+         *
+         * @return Unit[][]: Units in Guild War defense, grouped by team.
+         */
+        public function get_gw_defense(){
+            if (!$this->flag_defense){
+                $this->load_defenses();
+            }
+            return $this->gw_defense;
+        }
+
+        /**
+         * Retrieves the Guild War defenses.
+         *
+         * @return Unit[][]: Units in Siege Battle defense, grouped by team.
+         */
+        public function get_siege_defense(){
+            if (!$this->flag_defense){
+                $this->load_defenses();
+            }
+            return $this->siege_defense;
+        }
+
+        /**
+         * Retrieves various currencies.
+         *
+         * @return int[] Array with the keys: mana, crystal, energy, energy_max, arena_energy,
+         * darkportal_energy, dimension_energy, honor_point, guild_point, honor_medal, honor_mark,
+         * event_coin, social_point, ancient_stone, costume_point,
+         */
+        public function get_currencies(){
+            return $this->currency;
+        }
+
+        /**
+         * Retrieves the list of buildings owned by the user.
+         *
+         * @return \Building[] Buildings owned.
+         */
+        public function get_buildings(){
+            if (!$this->flag_building){
+                $this->load_buildings();
+            }
+            return $this->building;
+        }
+
+        /**
+         * Retrieves the list of decorations owned by the user.
+         *
+         * @return \Decoration[] Owned decorations.
+         */
+        public function get_decorations(){
+            if (!$this->flag_building){
+                $this->load_buildings();
+            }
+            return $this->decoration;
+        }
+
+        /**
+         * Retrieves the list of owned scrolls.
+         *
+         * @return \Item[] Scrolls.
+         */
+        public function get_scrolls(){
+            if (!$this->flag_item){
+                $this->load_items();
+            }
+            return $this->item_scroll;
+        }
+
+        /**
+         * Retrieves the list of rune crafting mateials.
+         *
+         * @return \Item[] Rune craft materials.
+         */
+        public function get_rune_craft_materials(){
+            if (!$this->flag_item){
+                $this->load_items();
+            }
+            return $this->item_craft_rune;
+        }
+
+        /**
+         * Retrieves the list of crafting materials.
+         *
+         * @return \Item[] Crafting materials
+         */
+        public function get_craft_materials(){
+            if (!$this->flag_item){
+                $this->load_items();
+            }
+            return $this->item_craft;
+        }
+
+        /**
+         * Retrieves the list of essences owned by the player.
+         *
+         * @return \Item[] Essences.
+         */
+        public function get_essences(){
+            if (!$this->flag_item){
+                $this->load_items();
+            }
+            return $this->item_essence;
+        }
+
+        /**
+         * Retrieves the list of the player's records in the logbook.
+         *
+         * @return \Record[] List of records.
+         */
+        public function get_records(){
+            if (!$this->flag_record){
+                $this->load_records();
+            }
+            return $this->record;
+        }
+
+        /**
+         * Checks if the player profile is public.
+         *
+         * @return bool True for public profile, false otherwise.
+         */
+        public function is_public(){
+            return $this->public;
+        }
+
+
     }
 ?>

+ 4 - 7
application/entity/Record.php

@@ -81,13 +81,10 @@
          * @param int $player_id Selected player identifier.
          * @param int $area_type Area type identifier.
          * @param int $area Area identifier.
-         * @global resource Database connection.
          */
         public function __construct($player_id, $area_type, $area){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
                 SELECT
                   player,
                   area_type,
@@ -102,7 +99,7 @@
                   area_type = :area_type AND
                   area = :area;
             ");
-            $statement->bindValue(':player', $player_, SQLITE3_TEXT);
+            $statement->bindValue(':player', $player_id, SQLITE3_TEXT);
             $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
             $statement->bindValue(':area', $area, SQLITE3_TEXT);
             $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
@@ -113,7 +110,7 @@
                 $this->time = $r["time"];
                 $this->score = $r["score"];
                 $this->rank = $r["rank"];
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       unit,
                       monster,
@@ -129,7 +126,7 @@
                       front DESC,
                       leader DESC;
                 ");
-                $statement->bindValue(':player', $player, SQLITE3_TEXT);
+                $statement->bindValue(':player', $player_id, SQLITE3_TEXT);
                 $statement->bindValue(':area_type', $area_type, SQLITE3_TEXT);
                 $statement->bindValue(':area', $area, SQLITE3_TEXT);
                 $q = $statement->execute();

+ 9 - 12
application/entity/Run.php

@@ -172,13 +172,10 @@
          * run, populating it and it's items.
          *
          * @param int $id Run identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 player,
@@ -219,7 +216,7 @@
                 $this->helper = $r["helper"];
                 $this->score = $r["score"];
                 $this->rank = $r["rank"];
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       unit,
                       monster,
@@ -248,7 +245,7 @@
                         array_push($this->frontline, $r["unit"]);
                     }
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       item,
                       amount
@@ -262,7 +259,7 @@
                     $i->amount =  $r["amount"];
                     array_push($this->item, $i);
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT monster
                     FROM run_drop_monster
                     WHERE run = :run;
@@ -272,7 +269,7 @@
                 while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                     array_push($this->monster, new Monster($r["monster"]));
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT monster
                     FROM run_drop_sd
                     WHERE run = :run;
@@ -282,7 +279,7 @@
                 while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                     array_push($this->sd, new Monster($r["monster"]));
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       monster,
                       amount
@@ -298,7 +295,7 @@
                     ];
                     array_push($this->unit_piece, $i);
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT amount
                     FROM run_drop_shapeshifting
                     WHERE run = :run;
@@ -309,7 +306,7 @@
                 if ($r){
                     $this->shapeshifting = $r["amount"];
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT
                       id,
                       type,
@@ -402,7 +399,7 @@
                     }
                     array_push($this->rune, $rune);
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT
                     run_drop_enchantment.id AS id,
                     type,

+ 6 - 10
application/entity/Rune.php

@@ -136,13 +136,10 @@
          * rune, populating it and it's items.
          *
          * @param int $id Rune id.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 type,
@@ -176,7 +173,7 @@
                 $this->locked = $r["locked"];
 
                 // Get substats
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT slot
                   FROM rune_stat
                   WHERE rune = :rune
@@ -204,7 +201,7 @@
                 if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
                     $this->efficiency = $this->calculate_efficiency();
                     $this->max_efficiency = $this->calculate_maximum_efficiency();
-                    $statement = $db->prepare("
+                    $statement = get_context()->get_db()->prepare("
                       UPDATE rune
                       SET
                         efficiency = :efficiency,
@@ -287,7 +284,6 @@
          * @return float Efficiency
          */
         private function calculate_efficiency(){
-            global $db;
             $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
 
             $query = "
@@ -309,7 +305,7 @@
             // Fixed stat 
             if ($this->innate_stat != null && $this->innate_stat->value > 0){
                 $value = $this->innate_stat->value;
-                $statement = $db->prepare($query);
+                $statement = get_context()->get_db()->prepare($query);
                 $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
                 $statement->bindValue(':initial', 1, SQLITE3_TEXT);
                 $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
@@ -326,14 +322,14 @@
                 if (sizeof($this->stats) > $i && $this->stats[$i] != null && $this->stats[$i]->value > 0){
                     $value = $this->stats[$i]->value;
                     $value = $this->stats[$i]->value;
-                    $statement = $db->prepare($query);
+                    $statement = get_context()->get_db()->prepare($query);
                     $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
                     $statement->bindValue(':initial', 1, SQLITE3_TEXT);
                     $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
                     $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
                     $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
                     $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
-                    $statement = $db->prepare($query);
+                    $statement = get_context()->get_db()->prepare($query);
                     $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
                     $statement->bindValue(':initial', 0, SQLITE3_TEXT);
                     $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);

+ 1 - 4
application/entity/Rune_Set.php

@@ -48,13 +48,10 @@
          * set, populating it and it's items.
          *
          * @param int $id Set identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,

+ 1 - 4
application/entity/Rune_Stat.php

@@ -62,13 +62,10 @@
          * rune, populating it and it's items.
          *
          * @param int $id Rune id.
-         * @global resource Database connection.
          */
         public function __construct($rune, $slot){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 slot,
                 stat,

+ 1 - 4
application/entity/Server.php

@@ -38,13 +38,10 @@
          * server, populating it and it's items.
          *
          * @param int $id Server identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name

+ 3 - 6
application/entity/Skill.php

@@ -96,13 +96,10 @@
          * skill, populating it and it's items.
          *
          * @param int $id Skill identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,
@@ -130,7 +127,7 @@
                 $this->aoe = $r["aoe"];
                 $this->max_level = $r["max_level"];
                 $this->multiplier_formula = HTML::e($r["multiplier_formula"]);
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT
                     skill,
                     level
@@ -143,7 +140,7 @@
                 while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                     array_push($this->level, new Skill_Level($r["skill"], $r["level"]));
                 }
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                   SELECT DISTINCT effect
                   FROM skill_effect
                   WHERE skill = :skill;

+ 1 - 4
application/entity/Skill_Level.php

@@ -44,13 +44,10 @@
          *
          * @param int $skill Skill identifier.
          * @param int $level Skill level.
-         * @global resource Database connection.
          */
         public function __construct($skill, $level){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 skill,
                 level,

+ 1 - 3
application/entity/Source.php

@@ -49,11 +49,9 @@
          * source, populating it and it's items.
          *
          * @param int $id Surce identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
-            global $db;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,

+ 3 - 6
application/entity/Team.php

@@ -58,7 +58,7 @@
         public $frontline = [];
 
         /**
-         * @var \K_Area Area the team is designed for.
+         * @var Area Area the team is designed for.
          */
         public $area;
 
@@ -87,13 +87,10 @@
          *
          * @param int $id Monster identifier.
          * @param bool $complete If false, it won't load the units.
-         * @global resource Database connection.
          */
         public function __construct($id, $complete = true){
 
-            global $db;
-
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 player,
                 id,
@@ -118,7 +115,7 @@
                 $this->stage = $r["stage"];
                 $this->score = $r["score"];
                 if ($complete){
-                    $statement = $db->prepare("
+                    $statement = get_context()->get_db()->prepare("
                       SELECT
                         unit,
                         leader,

+ 16 - 32
application/entity/Unit.php

@@ -367,16 +367,12 @@
          *
          * @param int $id Monster identifier.
          * @param bool $complete If false, query only unit and monster.
-         * @global int Player ID.
-         * @global resource Database connection.
          */
         public function __construct($id, $complete = true, $player = -1){
-            global $PLAYER;
-            global $db;
-            if ($player == -1 && isset($PLAYER->id) && null != $PLAYER){
-                $player = $PLAYER->id;
+            if ($player == -1 && null != get_context()->get_player()){
+                $player = get_context()->get_player()->get_id();
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 player,
                 id,
@@ -438,7 +434,7 @@
                     $this->load_teams();
                     $this->check_storage();
                     $this->lock = $r["lock"];
-                    $statement = $db->prepare("
+                    $statement = get_context()->get_db()->prepare("
                       SELECT level
                       FROM
                         unit_skill,
@@ -453,7 +449,7 @@
                     while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                         array_push($this->skill_levels, $r["level"]);
                     }
-                    $statement = $db->prepare("
+                    $statement = get_context()->get_db()->prepare("
                       SELECT exp
                       FROM experience
                       WHERE
@@ -480,13 +476,9 @@
         /**
          * Checks if the monster is in storage and sets {@see $in_storage} to
          * true or false.
-         *
-         * @global Player Currently selected Player.
-         * @global resource Database connection.
          */
         public function check_storage(){
-            global $db;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT 1
               FROM
                 unit,
@@ -517,17 +509,12 @@
          *
          * @gobal Player Currently selected player.
          * @gobal int Game mode (Normal/RTA).
-         * @global resource Database connection.
          */
         public function load_stats(){
 
-            global $PLAYER;
-            global $MODE;
-            global $db;
-
             // Runes
             $this->rune = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT rune
               FROM unit_rune
               WHERE
@@ -535,7 +522,7 @@
                 rta = :rta;
             ");
             $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
-            $statement->bindValue(':rta', $MODE, SQLITE3_TEXT);
+            $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->rune, new Rune($r["rune"]));
@@ -551,7 +538,7 @@
 
             // Artifacts
             $this->artifact = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT artifact
               FROM unit_artifact
               WHERE
@@ -559,7 +546,7 @@
                 rta = :rta;
             ");
             $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
-            $statement->bindValue(':rta', $MODE, SQLITE3_TEXT);
+            $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $artifact = new Artifact($r["artifact"]);
@@ -569,21 +556,21 @@
                 else{
                     $this->artifact_element = $artifact;
                 }
-                switch($artifact->stat->stat){
+                switch($artifact->stat->getStat()){
                     case ARTIFACT_STAT_ID::ATK:
-                        $this->artifact_attack += $artifact->stat->value;
+                        $this->artifact_attack += $artifact->stat->getValue();
                         break;
                     case ARTIFACT_STAT_ID::DEF:
-                        $this->artifact_defense += $artifact->stat->value;
+                        $this->artifact_defense += $artifact->stat->getValue();
                         break;
                     case ARTIFACT_STAT_ID::HP:
-                        $this->artifact_hp += $artifact->stat->value;
+                        $this->artifact_hp += $artifact->stat->getValue();
                         break;
                 }
             }
 
             // Buildings
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 affected_stat,
                 bonus,
@@ -672,13 +659,10 @@
 
         /**
          * Loads the {@see teams} array, with the teams the unit is on.
-         *
-         * @global resource Database connection.
          */
         public function load_teams(){
-            global $db;
             $this->teams = [];
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT DISTINCT team.id AS id
               FROM
                 team,

+ 2 - 4
application/entity/User.php

@@ -59,11 +59,9 @@
          * user, populating it and it's items.
          *
          * @param int $id User identifier.
-         * @global resource Database connection.
          */
         public function __construct($id){
-            global $db;
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT
                 id,
                 name,
@@ -83,7 +81,7 @@
                 $this->admin = $r["admin"];
 
                 // Get all accounts
-                $statement = $db->prepare("
+                $statement = get_context()->get_db()->prepare("
                     SELECT id
                     FROM player
                     WHERE user = :user

+ 3 - 15
application/page/Artifacts_Page.php

@@ -30,24 +30,16 @@
          */
         public $artifacts = [];
 
-        /**
-         * @var \Filter[] Custom filters for this page.
-         */
-        public $custom_filters = [];
-
         /**
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "artifacts.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $artifact = new Artifact($r["id"]);
                 if ($r["unit"]){
@@ -113,12 +105,8 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
-         * @global int Game mode
          */
         private function build_query(){
-            global $PLAYER;
-            global $MODE;
             $s = "
               SELECT
                 id,
@@ -127,11 +115,11 @@
                   FROM unit_artifact
                   WHERE
                     artifact = artifact.id AND
-                    rta = $MODE
+                    rta = " . get_context()->get_game_mode() . "
                 ) AS unit
               FROM artifact artifact
               WHERE
-                player = '" . $PLAYER->id . "' AND
+                player = '" . get_context()->get_player()->get_id() . "' AND
                 level >= " . $this->filters["MIN_LEVEL"] . " AND 
                 level <= " . $this->filters["MAX_LEVEL"] . " AND
                 quality >= " . $this->filters["MIN_QUALITY"] . " AND

+ 2 - 7
application/page/Enchantments_Page.php

@@ -35,15 +35,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "enchantments.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $enchantment = new Enchantment($r["id"]);
                 //if ($enchantment->gem == true){
@@ -120,15 +117,13 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
          */
         private function build_query(){
-            global $PLAYER;
             $s = "
               SELECT id
               FROM enchantment
               WHERE
-                player = '" . $PLAYER->id . "' AND
+                player = '" . get_context()->get_player()->get_id() . "' AND
                 quality >= " . $this->filters["MIN_QUALITY"] . " AND
                 quality <= " . $this->filters["MAX_QUALITY"] . "
             ";

+ 3 - 8
application/page/Guild_Page.php

@@ -35,20 +35,15 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global Player Currenty selected player.
-         * @global resource Connection to the database.
          */
         public function __construct(){
-            global $PLAYER;
-            global $db;
             $this->view = PATH::VIEW . "guild.php";
             $s = "
               SELECT id
               FROM guild
-              WHERE id = (SELECT guild FROM guild_member WHERE member = '" . $PLAYER->id . "');
+              WHERE id = (SELECT guild FROM guild_member WHERE member = '" . get_context()->get_player()->get_id() . "');
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             $r = $q->fetchArray(SQLITE3_ASSOC);
             if ($r){
                 $guild_id =  $r["id"];
@@ -60,7 +55,7 @@
                   FROM guild_skill_group
                   ORDER BY id;
                 ";
-                $q = $db->query($s);
+                $q = get_context()->get_db()->query($s);
                 while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                     array_push($this->skill_groups, new Guild_Skill_Group($r["id"]));
                 }

+ 3 - 6
application/page/Home_Page.php

@@ -30,15 +30,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global int Player ID.
          */
         public function __construct(){
-            global $PLAYER;
-            $this->player = $PLAYER;
+            $this->player = get_context()->get_player();
             $this->view = PATH::VIEW . "home.php";
-            $this->title = $this->player->name . " - SWDB";
-            $this->description = $this->player->name . " - Summoners War DataBase";
+            $this->title = get_context()->get_player()->get_name() . " - SWDB";
+            $this->description = get_context()->get_player()->get_name() . " - Summoners War DataBase";
             $this->canonical = URL::BASE;
         }
     }

+ 4 - 9
application/page/Catalog_Page.php → application/page/Monsters_Page.php

@@ -19,7 +19,7 @@
      *
      * @category Page
      */
-    class Catalog_Page extends Page{
+    class Monsters_Page extends Page{
 
         /**
          * @var \Monster[] List of monsters to show.
@@ -45,17 +45,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Connection to the database.
-         * @global Player Currently selected player.
          */
         public function __construct(){
-            global $db;
-            global $PLAYER;
             $this->view = PATH::VIEW . "catalog.php";
             $this->parse_filters();
             $s_mon = $this->build_query();
-            $q_mon = $db->query($s_mon);
+            $q_mon = get_context()->get_db()->query($s_mon);
             while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->monsters, new Monster($r_mon["id"], false));
             }
@@ -63,10 +58,10 @@
               SELECT monster
               FROM collection
               WHERE
-                player = " . $PLAYER->id . " AND
+                player = " . get_context()->get_player()->get_id() . " AND
                 open = 1
             ";
-            $q_open = $db->query($s_open);
+            $q_open = get_context()->get_db()->query($s_open);
             while ($r_open = $q_open->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->monsters_open, $r_open["monster"]);
             }

+ 3 - 10
application/page/Optimizer_Page.php

@@ -29,23 +29,18 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global Player Currently selected player.
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $PLAYER;
-            global $db;
             $this->view = PATH::VIEW . "optimizer.php";
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->teams, new Team($r["id"]));
             }
             $s = "
               SELECT id
               FROM unit
-              WHERE player = '" . $PLAYER->id . "';
+              WHERE player = '" . get_context()->get_player()->get_id() . "';
             ";
             $this->title = "Optmizer - SWDB";
             $this->description = "Optimizer";
@@ -57,14 +52,12 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
          */
         private function build_query(){
-            global $PLAYER;
             $s = "
               SELECT id
               FROM team
-              WHERE player = '" . $PLAYER->id . "'
+              WHERE player = '" . get_context()->get_player()->get_id() . "'
               ORDER BY
                 area_type,
                 area

+ 2 - 7
application/page/Profile_Page.php

@@ -39,13 +39,8 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global User Logged-in user.
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $USER;
-            global $db;
             $this->view = PATH::VIEW . "profile.php";
             $s = "
               SELECT
@@ -53,9 +48,9 @@
                 mail,
                 api_key
               FROM user
-              WHERE id = '" . $USER->id . "';
+              WHERE id = '" . get_context()->get_user()->id . "';
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             if ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $this->name = $r["name"];
                 $this->mail = $r["mail"];

+ 5 - 13
application/page/Report_Rune_Enchantment_Page.php

@@ -96,15 +96,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "report_rune_enchantment.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             $prev_monster = "";
             $prev_rune = "";
             $upgrade = null;
@@ -248,13 +245,8 @@
          * Builds the query for the page, using the providded or default filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
-         * @global int Game mode.
          */
         private function build_query(){
-            global $PLAYER;
-            global $MODE;
-
             $s = "
               SELECT DISTINCT
                 unit.id AS unit,
@@ -268,12 +260,12 @@
                 enchantment,
                 enchantment_type
               WHERE
-                unit.player = '" . $PLAYER->id . "'
-                AND rune.player = '" . $PLAYER->id . "'
-                AND enchantment.player = '" . $PLAYER->id . "'
+                unit.player = '" . get_context()->get_player()->get_id() . "'
+                AND rune.player = '" . get_context()->get_player()->get_id() . "'
+                AND enchantment.player = '" . get_context()->get_player()->get_id() . "'
                 AND unit.id = unit_rune.unit
                 AND rune.id = unit_rune.rune
-                AND unit_rune.rta = $MODE
+                AND unit_rune.rta = " . get_context()->get_game_mode() . "
                 AND unit.monster = monster.id
                 AND enchantment.type = enchantment_type.id
                 -- RUNE FILTERS

+ 5 - 12
application/page/Report_Skillup_Page.php

@@ -47,15 +47,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "report_skillup.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->units, new Unit($r["id"]));
             }
@@ -123,12 +120,8 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currenty selected player.
-         * @global int Game mode.
          */
         private function build_query(){
-            global $PLAYER;
-            global $MODE;
             $s = "
               SELECT DISTINCT
                 unit.id AS id,
@@ -161,7 +154,7 @@
                 monster_skill,
                 skill
               WHERE
-                unit.player = '" . $PLAYER->id . "' AND
+                unit.player = '" . get_context()->get_player()->get_id() . "' AND
                 unit.monster = monster.id AND
                 unit_skill.unit = unit.id AND ";
             switch ($this->filters["UNIT"]){
@@ -177,7 +170,7 @@
                             team_unit
                           WHERE
                             team.id = team_unit.team AND
-                            team.player = '" . $PLAYER->id . "'
+                            team.player = '" . get_context()->get_player()->get_id() . "'
                         )
                       ) AND ";
                     break;
@@ -191,7 +184,7 @@
                             team_unit
                           WHERE
                             team.id = team_unit.team AND
-                            team.player = '" . $PLAYER->id . "' AND
+                            team.player = '" . get_context()->get_player()->get_id() . "' AND
                             team.score > 0
                         )
                       ) AND ";
@@ -251,7 +244,7 @@
                 $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
             }
             if (!$this->filters["WITHOUT_RUNES"]){
-                $s = $s . " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = $MODE) ";
+                $s = $s . " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = " . get_context()->get_game_mode() . ") ";
             }
             if (!$this->filters["FAMILY"]){
                 $s = $s . " AND unit.monster NOT IN (SELECT id FROM monster m WHERE family IN (SELECT family FROM monster k WHERE k.family = m.family AND k.natural_stars < m.natural_stars AND k.natural_stars < " . $this->filters["MIN_NATURAL_STARS"] . ")) ";

+ 3 - 11
application/page/Runes_Page.php

@@ -58,17 +58,13 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global int Player ID.
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "runes.php";
             $this->parse_filters();
             $s = $this->build_query();
             $this->filters["GROUP"] = 1;
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $rune = new Rune($r["id"]);
                 if ($r["unit"]){
@@ -161,12 +157,8 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Curently sleected player.
-         * @global int Game mode.
          */
         private function build_query(){
-            global $PLAYER;
-            global $MODE;
             $s = "
               SELECT
                 id,
@@ -175,12 +167,12 @@
                   FROM unit_rune
                   WHERE
                     rune = rune.id AND
-                    rta = $MODE
+                    rta = " . get_context()->get_game_mode() . "
                 ) AS unit
               FROM
                 rune rune
               WHERE
-                player = '" . $PLAYER->id . "' AND
+                player = '" . get_context()->get_player()->get_id() . "' AND
                 stars >= " . $this->filters["MIN_STARS"] . " AND
                 stars <= " . $this->filters["MAX_STARS"] . " AND
                 level >= " . $this->filters["MIN_LEVEL"] . " AND

+ 2 - 7
application/page/Runs_Page.php

@@ -29,15 +29,12 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "runs.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->runs, new Run($r["id"]));
             }
@@ -93,14 +90,12 @@
          * filters.
          * 
          * @return string The query to be executed.
-         * @global Player Currently selected player.
          */
         private function build_query(){
-            global $PLAYER;
             $s = "
                 SELECT id
                 FROM run
-                WHERE player = '" . $PLAYER->id . "'
+                WHERE player = '" . get_context()->get_player()->get_id() . "'
              ";
             if ($this->filters["AREA_TYPE"] > 0){
                 $s = $s . " AND area_type =  " . $this->filters["AREA_TYPE"] . " ";

+ 13 - 18
application/page/Stats_Page.php

@@ -83,13 +83,8 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global Player Currently selected player.
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $PLAYER;
-            global $db;
             $this->view = PATH::VIEW . "stats.php";
             $this->title = "Stats - SWDB";
             $this->description = "Player statistics";
@@ -105,7 +100,7 @@
 
             // Build query modifier:
             $query_where = "
-              run.player = '" . $PLAYER->id . "' AND
+              run.player = '" . get_context()->get_player()->get_id() . "' AND
               run.area = " . $this->filters["AREA"] . "
             ";
             if ($this->filters["STAGE"] > 0){
@@ -127,7 +122,7 @@
               ORDER BY time DESC
               LIMIT 1;
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             if ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $this->best_run = new Run($r["id"]);
             }
@@ -138,21 +133,21 @@
                 (SELECT COUNT(win) FROM run WHERE $query_where AND win = 1 ) AS win,
                 (SELECT COUNT(win) FROM run WHERE $query_where AND win = 0) AS lose;
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             $r = $q->fetchArray(SQLITE3_ASSOC);
             $this->win_lose = [$r["win"], $r["lose"]];
 
             // Drop type rate
             $s = "
               SELECT
-                (SELECT COUNT(run) FROM run run, run_drop_rune run_drop_rune WHERE $query_where AND run.id = run_drop_rune.run AND player = '" . $PLAYER->id . "') AS rune,
-                (SELECT COUNT(run) FROM run run, run_drop_rune_craft run_drop_rune_craft WHERE $query_where AND run.id = run_drop_rune_craft.run AND player = '" . $PLAYER->id . "') AS rune_craft,
-                (SELECT COUNT(run) FROM run run, run_drop_item run_drop_item WHERE $query_where AND run.id = run_drop_item.run AND player = '" . $PLAYER->id . "') AS item,
-                (SELECT COUNT(run) FROM run run, run_drop_sd run_drop_sd WHERE $query_where AND run.id = run_drop_sd.run AND player = '" . $PLAYER->id . "') AS sd,
-                (SELECT COUNT(run) FROM run run, run_drop_shapeshifting run_drop_shapeshifting WHERE $query_where AND run.id = run_drop_shapeshifting.run AND player = '" . $PLAYER->id . "') AS shapeshifting,
-                (SELECT COUNT(run) run run, run_drop_unit run_drop_unit WHERE $query_where AND run.id = run_drop_unit.run AND player = '" . $PLAYER->id . "') AS unit;
+                (SELECT COUNT(run) FROM run run, run_drop_rune run_drop_rune WHERE $query_where AND run.id = run_drop_rune.run AND player = '" . get_context()->get_player()->get_id() . "') AS rune,
+                (SELECT COUNT(run) FROM run run, run_drop_rune_craft run_drop_rune_craft WHERE $query_where AND run.id = run_drop_rune_craft.run AND player = '" . get_context()->get_player()->get_id() . "') AS rune_craft,
+                (SELECT COUNT(run) FROM run run, run_drop_item run_drop_item WHERE $query_where AND run.id = run_drop_item.run AND player = '" . get_context()->get_player()->get_id() . "') AS item,
+                (SELECT COUNT(run) FROM run run, run_drop_sd run_drop_sd WHERE $query_where AND run.id = run_drop_sd.run AND player = '" . get_context()->get_player()->get_id() . "') AS sd,
+                (SELECT COUNT(run) FROM run run, run_drop_shapeshifting run_drop_shapeshifting WHERE $query_where AND run.id = run_drop_shapeshifting.run AND player = '" . get_context()->get_player()->get_id() . "') AS shapeshifting,
+                (SELECT COUNT(run) run run, run_drop_unit run_drop_unit WHERE $query_where AND run.id = run_drop_unit.run AND player = '" . get_context()->get_player()->get_id() . "') AS unit;
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             $r = $q->fetchArray(SQLITE3_ASSOC);
             $this->drop_type["rune"] = $r["rune"];
             $this->drop_type["rune_craft"] = $r["rune_craft"];
@@ -175,7 +170,7 @@
               GROUP BY slot
               ORDER BY slot;
             ";
-            $q = $db->query($s);
+                $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $this->drop_rune_slot[$r["slot"]] = $r["count"];
             }
@@ -194,7 +189,7 @@
               GROUP BY stars
               ORDER BY stars DESC;
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $this->drop_rune_stars[strval($r["stars"])] = $r["count"];
             }
@@ -213,7 +208,7 @@
               GROUP BY quality
               ORDER BY quality DESC;
             ";
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 $this->drop_rune_quality[strval($r["quality"])] = $r["count"];
             }

+ 4 - 11
application/page/Teams_Page.php

@@ -35,26 +35,21 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global Player Currently selected player.
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $PLAYER;
-            global $db;
             $this->view = PATH::VIEW . "teams.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->teams, new Team($r["id"]));
             }
-            $statement = $db->prepare("
+            $statement = get_context()->get_db()->prepare("
               SELECT id
               FROM unit
               WHERE player = :player;
             ");
-            $statement->bindValue(':player', $PLAYER->id, SQLITE3_TEXT);
+            $statement->bindValue(':player', get_context()->get_player()->get_id(), SQLITE3_TEXT);
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->units, new Unit($r["id"], false));
@@ -84,14 +79,12 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
          */
         private function build_query(){
-            global $PLAYER;
             $s = "
               SELECT id
               FROM team
-              WHERE player = '" . $PLAYER->id . "'
+              WHERE player = '" . get_context()->get_player()->get_id() . "'
               ORDER BY
                 area_type,
                 area

+ 3 - 8
application/page/Units_Page.php

@@ -49,21 +49,18 @@
          * Constructor.
          *
          * Retrieves the data and initializes the variables.
-         *
-         * @global resource Database connection.
          */
         public function __construct(){
-            global $db;
             $this->view = PATH::VIEW . "units.php";
             $this->parse_filters();
             $s = $this->build_query();
-            $q = $db->query($s);
+            $q = get_context()->get_db()->query($s);
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->units, new Unit($r["id"]));
             }
             // Get skill effects
             $s_eff = "SELECT id FROM effect ORDER BY is_buff DESC";
-            $q_eff = $db->query($s_eff);
+            $q_eff = get_context()->get_db()->query($s_eff);
             while ($r_eff = $q_eff->fetchArray(SQLITE3_ASSOC)){
                 array_push($this->effects, new Effect($r_eff["id"]));
             }
@@ -117,17 +114,15 @@
          * filters.
          *
          * @return string The query to be executed.
-         * @global Player Currently selected player.
          */
         private function build_query(){
-            global $PLAYER;
             $s = "
                 SELECT unit.id AS id
                 FROM
                   unit,
                   monster
                 WHERE
-                  unit.player = '" . $PLAYER->id . "' AND
+                  unit.player = '" . get_context()->get_player()->get_id() . "' AND
                   unit.monster = monster.id AND 
                   stars >= " . $this->filters["MIN_STARS"] . " AND 
                   stars <= " . $this->filters["MAX_STARS"];

+ 4 - 3
application/view/artifacts.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Artifacts_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -56,7 +57,7 @@
             <p>
                 Manage your enchantments
             </p>
-            <form action='/<?=$PLAYER->id?>/artifacts/' method='get' id='filter_artifacts' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/artifacts/' method='get' id='filter_artifacts' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>

+ 6 - 5
application/view/catalog.php

@@ -5,8 +5,9 @@
      * Contains the view layout and some code to present the data
      *
      * @category View
-     * @var Catalog_Page $page The page model
-     * @var player $PLAYER Currently selected player.
+     * @var Monsters_Page $page The page model
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -57,7 +58,7 @@
             <p>
                 A list with every monster in the game.
             </p>
-            <form action='/<?=$PLAYER->id?>/catalog/' method='get' id='filter_catalog' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/catalog/' method='get' id='filter_catalog' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>
@@ -127,7 +128,7 @@
                         }
 ?>
                         <div class='monster <?=$closed?>'>
-                            <a href='/<?=$PLAYER->id?>/catalog/<?=$mon->id?>'>
+                            <a href='/<?=get_context()->get_player()->get_id()?>/catalog/<?=$mon->id?>'>
                                 <div class='monster_panel'>
                                     <?=HTML::unit_panel($mon)?>
                                 </div>

+ 4 - 3
application/view/enchantments.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Artifacts_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -93,7 +94,7 @@
             <p>
                 Manage your enchantments
             </p>
-            <form action='/<?=$PLAYER->id?>/enchantments/' method='get' id='filter_artifacts' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/enchantments/' method='get' id='filter_artifacts' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>

+ 2 - 1
application/view/error.php

@@ -6,6 +6,7 @@
      *
      * @category View
      * @var Landing_Page $page The page model.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -18,7 +19,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php

+ 2 - 1
application/view/guild.php

@@ -6,6 +6,7 @@
      *
      * @category View
      * @var Guild_Page $page The page model.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -18,7 +19,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php

+ 2 - 1
application/view/help.php

@@ -6,6 +6,7 @@
      *
      * @category View
      * @var Page $page The page model.
+     * @var int get_context()->get_game_mode() Game mode.
      */
     // TODO: Implement
 ?>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php

+ 50 - 47
application/view/home.php

@@ -6,8 +6,9 @@
      *
      * @category View
      * @var Home_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
      * @var int[] $EFFECT_AREA Required constants.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -20,7 +21,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -52,10 +53,10 @@
 ?>
         <aside>
             <h2>
-                <?=$page->player->name?>
+                <?=get_context()->get_player()->get_name()?>
             </h2>
             <p>
-                <?=$page->player->name?>'s profile
+                <?=get_context()->get_player()->get_name()?>'s profile
             </p>
             <table id='profile'>
                 <tr>
@@ -63,7 +64,7 @@
                         Lv.
                     </td>
                     <td class='value'>
-                        <?=$page->player->level?>
+                        <?=get_context()->get_player()->get_level()?>
                     </td>
                 </tr>
                 <tr>
@@ -71,7 +72,7 @@
                         Exp.
                     </td>
                     <td class='value'>
-                        <?=$page->player->experience?>
+                        <?=get_context()->get_player()->get_experience()?>
                     </td>
                 </tr>
                 <tr>
@@ -79,7 +80,7 @@
                         Country
                     </td>
                     <td class='value'>
-                        <?=$page->player->country?>
+                        <?=get_context()->get_player()->get_country()?>
                     </td>
                 </tr>
                 <tr>
@@ -87,7 +88,7 @@
                         Since
                     </td>
                     <td class='value'>
-                        <?=date('Y-m-d', $page->player->joined)?>
+                        <?=get_context()->get_player()->get_joined('Y-m-d')?>
                     </td>
                 </tr>
                 <tr>
@@ -95,7 +96,7 @@
                         Days
                     </td>
                     <td class='value'>
-                        <?=round((time() - $page->player->joined) / (60 * 60 * 24))?>
+                        <?=round((time() - get_context()->get_player()->get_joined('U')) / (60 * 60 * 24))?>
                     </td>
                 </tr>
             </table>
@@ -118,7 +119,7 @@
                                     Rep monster:
                                 </h5>
                                 <div class='monster_panel'>
-                                    <?=HTML::unit_panel($page->player->rep)?>
+                                    <?=HTML::unit_panel(get_context()->get_player()->get_rep())?>
                                 </div> <!-- .monster_panel -->
                             </div>
                             <div id='placements_arena'>
@@ -127,7 +128,7 @@
                                 </h5>
 <?php
                                 $first = true;
-                                foreach ($page->player->defense as $defense){
+                                foreach (get_context()->get_player()->get_arena_defense() as $defense){
 ?>
                                     <div class='monster_panel'>
                                         <?=HTML::unit_panel($defense)?>
@@ -150,7 +151,8 @@
                                 </h5>
 <?php
                                 $first = true;
-                                foreach ($page->player->gw_defense[0] as $defense){
+                                // TODO: Watch if null
+                                foreach (get_context()->get_player()->get_gw_defense()[0] as $defense){
 ?>
                                     <div class='monster_panel'>
                                         <?=HTML::unit_panel($defense)?>
@@ -169,7 +171,8 @@
                                 <hr/>
                         <?php
                                 $first = true;
-                                foreach ($page->player->gw_defense[1] as $defense){
+                                // TODO: Watch if null
+                                foreach (get_context()->get_player()->get_gw_defense()[1] as $defense){
 ?>
                                     <div class='monster_panel'>
                                         <?=HTML::unit_panel($defense)?>
@@ -193,9 +196,9 @@
                             </h4>
 <?php
                             // Buildings
-                            $half = sizeof($page->player->building) / 2;
+                            $half = sizeof(get_context()->get_player()->get_buildings()) / 2;
                             $i = 0;
-                            foreach ($page->player->building as $building){
+                            foreach (get_context()->get_player()->get_buildings() as $building){
                                 if ($i == $half){
 ?>
                                     <br class='desktop'/>
@@ -210,9 +213,9 @@
                             <br/>
 <?php
                             // Decorations
-                            $half = sizeof($page->player->decoration) / 2;
+$                           $half = sizeof(get_context()->get_player()->get_decorations()) / 2;
                             $i = 0;
-                            foreach ($page->player->decoration as $decoration){
+                            foreach (get_context()->get_player()->get_decorations() as $decoration){
                                 if ($i == $half){
 ?>
                                     <br class='desktop'/>
@@ -232,66 +235,66 @@
                             <ul>
                                 <li>
                                     <img title='Energy' src='<?=URL::IMG["CURRENCY"]?>energy.png'/>
-                                    <?=$page->player->currency["energy"]?> / <?=$page->player->currency["energy_max"]?>
+                                    <?=get_context()->get_player()->get_currencies()["energy"]?> / <?=get_context()->get_player()->get_currencies()["energy_max"]?>
                                 </li>
                                 <li>
                                     <img title='Arena Invitations' src='<?=URL::IMG["CURRENCY"]?>arenaenergy.png'/>
-                                    <?=$page->player->currency["arena_energy"]?> / 10
+                                    <?=get_context()->get_player()->get_currencies()["arena_energy"]?> / 10
                                 </li>
                                 <li>
                                     <img title='Dimensional Energy' src='<?=URL::IMG["CURRENCY"]?>dimensionenergy.png'/>
-                                    <?=$page->player->currency["dimension_energy"]?> / 10
+                                    <?=get_context()->get_player()->get_currencies()["dimension_energy"]?> / 10
                                 </li>
                                 <li>
                                     <img title='Dimensional Crystal' src='<?=URL::IMG["CURRENCY"]?>darkportalenergy.png'/>
-                                    <?=$page->player->currency["darkportal_energy"]?> / 10
+                                    <?=get_context()->get_player()->get_currencies()["darkportal_energy"]?> / 10
                                 </li>
                                 <li>
                                     <img title='Mana' src='<?=URL::IMG["CURRENCY"]?>mana.png'/>
-                                    <?=$page->player->currency["mana"]?>
+                                    <?=get_context()->get_player()->get_currencies()["mana"]?>
                                 </li>
                                 <li>
                                     <img title='Crystal' src='<?=URL::IMG["CURRENCY"]?>crystal.png'/>
-                                    <?=$page->player->currency["crystal"]?>
+                                    <?=get_context()->get_player()->get_currencies()["crystal"]?>
                                 </li>
                                 <li>
                                     <img title='Social Points' src='<?=URL::IMG["CURRENCY"]?>socialpoint.png'/>
-                                    <?=$page->player->currency["social_point"]?>
+                                    <?=get_context()->get_player()->get_currencies()["social_point"]?>
                                 </li>
                                 <li>
                                     <img title='Honor Points' src='<?=URL::IMG["CURRENCY"]?>honor.png'/>
-                                    <?=$page->player->currency["honor_point"]?>
+                                    <?=get_context()->get_player()->get_currencies()["honor_point"]?>
                                 </li>
                                 <li>
                                     <img title='Guild Points' src='<?=URL::IMG["CURRENCY"]?>guildpoint.png'/>
-                                    <?=$page->player->currency["guild_point"]?>
+                                    <?=get_context()->get_player()->get_currencies()["guild_point"]?>
                                 </li>
                                 <li>
                                     <img title='Honor Medals' src='<?=URL::IMG["CURRENCY"]?>badge.png'/>
-                                    <?=$page->player->currency["honor_medal"]?>
+                                    <?=get_context()->get_player()->get_currencies()["honor_medal"]?>
                                 </li>
                                 <li>
                                     <img src='<?=URL::IMG["CURRENCY"]?>mark.png'/>
-                                    <?=$page->player->currency["honor_mark"]?>
+                                    <?=get_context()->get_player()->get_currencies()["honor_mark"]?>
                                 </li>
                                 <li>
                                     <img title='Ancient Coins' src='<?=URL::IMG["CURRENCY"]?>ancientcoin.png'/>
-                                    <?=$page->player->currency["event_coin"]?>
+                                    <?=get_context()->get_player()->get_currencies()["event_coin"]?>
                                 </li>
                                 <li>
                                     <img title='Ancient Crystal' src='<?=URL::IMG["CURRENCY"]?>ancientstone.png'/>
-                                    <?=$page->player->currency["ancient_stone"]?>
+                                    <?=get_context()->get_player()->get_currencies()["ancient_stone"]?>
                                 </li>
                                 <li>
                                     <img title='Shapeshifting Stones' src='<?=URL::IMG["CURRENCY"]?>costumestone.png'/>
-                                    <?=$page->player->currency["costume_point"]?>
+                                    <?=get_context()->get_player()->get_currencies()["costume_point"]?>
                                 </li>
                             </ul>
 <?php
                             // Scrolls
-                            $half = sizeof($page->player->building) / 2;
+                            $half = sizeof(get_context()->get_player()->get_scrolls()) / 2;
                             $i = 0;
-                            foreach ($page->player->item_scroll as $item){
+                            foreach (get_context()->get_player()->get_scrolls() as $item){
                                 if ($half > 6 && $i == $half){
 ?>
                                     <br class='desktop'/>
@@ -307,7 +310,7 @@
 <?php
                             // Rune craft materials
                             $prev = 0;
-                            foreach ($page->player->item_craft_rune as $item){
+                            foreach (get_context()->get_player()->get_rune_craft_materials() as $item){
 ?>
                                 <?=HTML::item_panel($item)?>
 <?php
@@ -318,7 +321,7 @@
 <?php
                             // Other craft materials
                             $prev = 0;
-                            foreach ($page->player->item_craft as $item){
+                            foreach (get_context()->get_player()->get_craft_materials() as $item){
                                 if ($prev != 0 && $prev < 5000 && $item->id > 5000){
 ?>
                                     <br class='desktop'/>
@@ -335,7 +338,7 @@
                             // Essences
                             $prev = "";
                             $i = 0;
-                            foreach ($page->player->item_essence as $item){
+                            foreach (get_context()->get_player()->get_essences() as $item){
                                 //if ($prev != "" && $prev != preg_split('/\s+/', $item->name)[2]){
                                 if ($i > 0 && $i % 6 == 0){
 ?>
@@ -363,7 +366,7 @@
                                             Arena
                                         </td>
                                         <td class='value rank'>
-                                            <?=HTML::arena_rank_icon($page->player->top_rank_arena)?>
+                                            <?=HTML::arena_rank_icon(get_context()->get_player()->get_top_rank_arena())?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -371,7 +374,7 @@
                                             World Arena
                                         </td>
                                         <td class='value rank'>
-                                            <?=HTML::arena_rank_icon($page->player->top_rank_world_arena)?>
+                                            <?=HTML::arena_rank_icon(get_context()->get_player()->get_top_rank_world_arena())?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -379,7 +382,7 @@
                                             Special League
                                         </td>
                                         <td class='value rank'>
-                                            <?=HTML::arena_rank_icon($page->player->top_rank_special_league)?>
+                                            <?=HTML::arena_rank_icon(get_context()->get_player()->get_top_rank_special_league())?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -387,7 +390,7 @@
                                             Guild War
                                         </td>
                                         <td class='value rank'>
-                                            <?=HTML::arena_rank_icon($page->player->top_rank_gw - 10)?>
+                                            <?=HTML::arena_rank_icon(get_context()->get_player()->get_top_rank_gw() - 10)?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -395,7 +398,7 @@
                                             Siege Battle
                                         </td>
                                         <td class='value rank'>
-                                            <?=HTML::arena_rank_icon($page->player->top_rank_siege)?>
+                                            <?=HTML::arena_rank_icon(get_context()->get_player()->get_top_rank_siege())?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -404,7 +407,7 @@
                                         </td>
                                         <td class='value wboss'>
                                             <!-- TODO Rank 12?? -->
-                                            <?=WORLD_BOSS_RANK_ID::RANK[$page->player->top_rank_wboss]?>
+                                            <?=WORLD_BOSS_RANK_ID::RANK[get_context()->get_player()->get_top_rank_wboss()]?>
                                         </td>
                                     </tr>
                                     <tr>
@@ -412,7 +415,7 @@
                                             Trial of Ascension (N)
                                         </td>
                                         <td class='value toa'>
-                                            <?=$page->player->top_rank_toan?>F
+                                            <?=get_context()->get_player()->get_top_rank_toan()?>F
                                         </td>
                                     </tr>
                                     <tr>
@@ -420,13 +423,13 @@
                                             Trial of Ascension (N)
                                         </td>
                                         <td class='value toa'>
-                                            <?=$page->player->top_rank_toah?>F
+                                            <?=get_context()->get_player()->get_top_rank_toah()?>F
                                         </td>
                                     </tr>
                                 </table>
                                 <table id='battle_records'>
 <?php
-                                    foreach ($page->player->record as $record){
+                                    foreach (get_context()->get_player()->get_records() as $record){
 ?>
                                         <tr>
                                             <td class='record_dungeon'>
@@ -493,7 +496,7 @@
                                                         <hr class='frontline'/>
 <?php
                                                     }
-                                                    if ($party instanceof K_Unit){
+                                                    if ($party instanceof Monster){
                                                         $disabled = "disabled";
                                                     }
                                                     else{
@@ -501,7 +504,7 @@
                                                     }
                                                     if ($disabled == ""){
 ?>
-                                                        <a target='_blank' href='/<?=$PLAYER->id?>/monster/<?=$party->id?>'>
+                                                        <a target='_blank' href='/<?=get_context()->get_player()->get_id()?>/monster/<?=$party->id?>'>
 <?php
                                                     }
 ?>

+ 17 - 19
application/view/inc/header.php

@@ -6,8 +6,6 @@
      *
      * @category View
      */
-
-    /** @var int $PLAYER Player id. */
 ?>
 <script>
     function setMode(rta){
@@ -20,7 +18,7 @@
         else{
             return false;
         }
-        params = '?player=<?=$PLAYER->id?>&mode=' + mode;
+        params = '?player=<?=get_context()->get_player()->get_id()?>&mode=' + mode;
         var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
             if (this.readyState == 4){
@@ -54,71 +52,71 @@
                         </div>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/'>
                             Home
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/units/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/units/'>
                             Units
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/runes/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/runes/'>
                             Runes
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/artifacts/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/artifacts/'>
                             Artifacts
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/enchantments/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/enchantments/'>
                             Enchantments
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/teams/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/teams/'>
                             Teams
                         </a>
                     </td>
                     <td class='last profile'>
-                        <a href='/<?=$PLAYER->id?>/profile/'>
-                            <?=$PLAYER->name?>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/profile/'>
+                            <?=get_context()->get_player()->get_name()?>
                         </a>
                     </td>
                 </tr>
                 <tr>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/catalog/'>
-                            Catalog
+                        <a href='/<?=get_context()->get_player()->get_id()?>/monsters/'>
+                            Monsters
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/guild/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/guild/'>
                             Guild
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/runs/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/runs/'>
                             Runs
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/report/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/report/'>
                             Reports
                         </a>
                     </td>
                     <td class='item'>
-                        <a href='/<?=$PLAYER->id?>/optimizer/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/optimizer/'>
                             Optimizer
                         </a>
                     </td>
                     <td class='no_item'>
                     </td>
                     <!--<td class='item'>
-                        <a href='/<?=$PLAYER->id?>/stats/'>
+                        <a href='/<?=get_context()->get_player()->get_id()?>/stats/'>
                             Stats
                         </a>
                     </td>-->
@@ -128,7 +126,7 @@
                                 N<span class='desktop'>ormal</span>
                             </span>
                             <label class='switch'>
-                                <input type="checkbox" <?=($MODE == GAME_MODE_ID::RTA ? "checked='cheecked'" : "")?> onChange='setMode(this.checked);'/>
+                                <input type="checkbox" <?=(get_context()->get_game_mode() == GAME_MODE_ID::RTA ? "checked='cheecked'" : "")?> onChange='setMode(this.checked);'/>
                                 <span class="slider round"></span>
                             </label>
                             <span class='mode'>

+ 2 - 1
application/view/monster.php

@@ -6,6 +6,7 @@
      *
      * @category View
      * @var Monster_Page $page The page model.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 
 ?>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php

+ 7 - 6
application/view/optimizer.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Optimizer_Page $page The page model.
-     * @var Player $PLAYER Currently selected player.
+     * @var Player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -85,7 +86,7 @@
              * @return true on success, false on error.
              */
             function rateTeam(team, score){
-                params = '?player=<?=$PLAYER->id?>&team=' + team + '&score=' + score;
+                params = '?player=<?=get_context()->get_player()->get_id()?>&team=' + team + '&score=' + score;
                 console.log(params);
                 var xhttp = new XMLHttpRequest();
                 xhttp.onreadystatechange = function() {
@@ -107,7 +108,7 @@
              * @param unit ID of the unit to optimize.
              */
             function optimize(unit){
-                window.location.href = "/<?=$PLAYER->id?>/optimizer/" + unit;
+                window.location.href = "/<?=get_context()->get_player()->get_id()?>/optimizer/" + unit;
             }
 
             /**
@@ -185,7 +186,7 @@
                 <br/>
                 <br/>
                 <br/>
-                To optimize units that are not on teams, do so from the <a href='/<?=$PLAYER->id?>/monsters/'>monsters page</a>.
+                To optimize units that are not on teams, do so from the <a href='/<?=get_context()->get_player()->get_id()?>/monsters/'>monsters page</a>.
             </p>
         </aside>
         <main>
@@ -226,7 +227,7 @@
 <?php
                                         }
 ?>
-                                        <a href='/<?=$PLAYER->id?>/monsters/<?=$unit->id?>' target='_blank'>
+                                        <a href='/<?=get_context()->get_player()->get_id()?>/monsters/<?=$unit->id?>' target='_blank'>
                                             <div class='monster_panel'>
                                                 <?=HTML::unit_panel($unit)?>
 <?php

+ 6 - 5
application/view/optimizer_unit.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Optimizer_Unit_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -230,7 +231,7 @@
                 var x;
 
                 // Player id - mandatory
-                params = 'player=<?=$PLAYER->id?>';
+                params = 'player=<?=get_context()->get_player()->get_id()?>';
 
                 // Unit id - mandatory
                 params += '&unit=<?=$page->unit->id?>';
@@ -744,7 +745,7 @@
                         }
                     }
                 };
-                params = 'player=<?=$PLAYER->id?>&unit=<?=$page->unit->id?>&tuning=' + document.getElementById('tuning').options[document.getElementById('tuning').selectedIndex].value;
+                params = 'player=<?=get_context()->get_player()->get_id()?>&unit=<?=$page->unit->id?>&tuning=' + document.getElementById('tuning').options[document.getElementById('tuning').selectedIndex].value;
                 params += '&options=' + JSON.stringify(valid_combinations);
                 xhttp.open('POST', '/action/optimize_get_options/', true);
                 xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
@@ -1258,7 +1259,7 @@
                                 foreach($page->unit->teams as $team){
 ?>
                                     <li>
-                                        <a target='blank' href='/<?=$PLAYER-id?>/teams/<?=$team->id?>'>
+                                        <a target='blank' href='/<?=get_context()->get_player()-id?>/teams/<?=$team->id?>'>
                                             <?=$team->name?>
                                         </a>
                                     </li>

+ 2 - 1
application/view/profile.php

@@ -7,6 +7,7 @@
      * @category View
      * @var Profile_Page $page The page model.
      * @var User $USER Currently logged-in user.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php

+ 5 - 3
application/view/report.php

@@ -6,6 +6,8 @@
      *
      * @category View
      * @var Report_Page $page The page model.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -18,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -67,7 +69,7 @@
                         Shows a list of the monstes in need of skilling-up
                         It sorts monster not by the absolute ammount of remaining power-ups, but by a ponderated list tht takes into account to which skills the previous power ups have gone, placing more value in the more advanced skills. 
                     </p>
-                    <a class='a_button' href='/<?=$PLAYER->id?>/report/skillup/'>
+                    <a class='a_button' href='/<?=get_context()->get_player()->get_id()?>/report/skillup/'>
                         Go
                     </a>
                 </article>
@@ -82,7 +84,7 @@
                         Shows monsters with runes that can be grinded or enchanted.
                         It list the grindstones and gems that are available for each rune.
                     </p>
-                    <a class='a_button' href='/<?=$PLAYER->id?>/report/rune_enchantment/'>
+                    <a class='a_button' href='/<?=get_context()->get_player()->get_id()?>/report/rune_enchantment/'>
                         Go
                     </a>
                 </article>

+ 5 - 4
application/view/report_rune_enchantment.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Report_Rune_Enchantment_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -110,7 +111,7 @@
             <h2>
                 Report filters
             </h2>
-            <form action='/<?=$PLAYER->id?>/report/rune_enchantment/' method='get' id='filter_runeenchantment' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/report/rune_enchantment/' method='get' id='filter_runeenchantment' class='filter'>
                 <table>
                     <tr>
                         <td colspan='2' class='header'>
@@ -472,7 +473,7 @@
     ?>
                         <tr>
                             <td class='header' colspan='2'>
-                                <a href='/<?=$PLAYER->id?>/monsters/<?=$upgrade["unit"]->id?>/' target='_blank'>
+                                <a href='/<?=get_context()->get_player()->get_id()?>/monsters/<?=$upgrade["unit"]->id?>/' target='_blank'>
                                     <div class='monster_panel'>
                                         <?=HTML::unit_panel($upgrade["unit"])?>
                                     </div>

+ 5 - 4
application/view/report_skillup.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Report_Skillup_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -54,7 +55,7 @@
                 Report filters
             </h2>
             <article id='filters_content'>
-                <form action='/<?=$PLAYER->id?>/report/skillup/' method='get' id='filter_skillup' class='filter'>
+                <form action='/<?=get_context()->get_player()->get_id()?>/report/skillup/' method='get' id='filter_skillup' class='filter'>
                     <table>
                         <tr>
                             <td class='filter' id='filter_stars'>
@@ -234,7 +235,7 @@
                         </tr>
                         <tr>
                             <td rowspan='3' class='img'>
-                                <a href='/<?=$PLAYER->id?>/monsters/<?=$unit->id?>/' target='_blank'>
+                                <a href='/<?=get_context()->get_player()->get_id()?>/monsters/<?=$unit->id?>/' target='_blank'>
                                     <div class='monster_panel'>
                                         <?=HTML::unit_panel($unit)?>
                                     </div>

+ 4 - 3
application/view/runes.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Runes_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -112,7 +113,7 @@
             <p>
                 Manage your runes
             </p>
-            <form action='/<?=$PLAYER->id?>/runes/' method='get' id='filter_runes' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/runes/' method='get' id='filter_runes' class='filter'>
                 <table>
                     <tr>
                         <td class='label' >

+ 7 - 6
application/view/runs.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Runs_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -83,7 +84,7 @@
                 };
                 xhttp.open("POST", "/action/save_run_team/", true);
                 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
-                xhttp.send('player=<?=$PLAYER->id?>&run=' + id + '&name=' + encodeURI(name) + '&description=' + encodeURI(description));
+                xhttp.send('player=<?=get_context()->get_player()->get_id()?>&run=' + id + '&name=' + encodeURI(name) + '&description=' + encodeURI(description));
             }
 
             /**
@@ -154,7 +155,7 @@
             <p>
                 View and compare saved runs
             </p>
-            <form action='/<?=$PLAYER->id?>/runs/' method='get' id='filter_runeupgrade' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/runs/' method='get' id='filter_runeupgrade' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>
@@ -350,7 +351,7 @@
                                             <hr class='frontline'/>
 <?php
                                         }
-                                        if ($party instanceof K_Unit){
+                                        if ($party instanceof Monster){
                                             $enable_save = false;
                                             $disabled = "disabled";
                                         }
@@ -359,7 +360,7 @@
                                         }
                                         if ($disabled == ""){
 ?>
-                                            <a target='_blank' href='/<?=$PLAYER->id?>/monster/<?=$party->id?>'>
+                                            <a target='_blank' href='/<?=get_context()->get_player()->get_id()?>/monster/<?=$party->id?>'>
 <?php
                                         }
 ?>

+ 4 - 3
application/view/stats.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Stats_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -532,7 +533,7 @@
             <h2>
                 Statistics
             </h2>
-            <form action='/<?=$PLAYER-id?>/stats/' method='get' class='filter'>
+            <form action='/<?=get_context()->get_player()-id?>/stats/' method='get' class='filter'>
                 <table>
                     <tr>
                         <td class='filter'>

+ 8 - 7
application/view/teams.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Teams_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -79,7 +80,7 @@
              * @return true on success, false on error.
              */
             function rateTeam(team, score){
-                params = '?player=<?=$PLAYER->id?>&team=' + team + '&score=' + score;
+                params = '?player=<?=get_context()->get_player()->get_id()?>&team=' + team + '&score=' + score;
                 console.log(params);
                 var xhttp = new XMLHttpRequest();
                 xhttp.onreadystatechange = function() {
@@ -176,7 +177,7 @@
                 }
 
                 var request =
-                  'player=<?=$PLAYER->id?>' +
+                  'player=<?=get_context()->get_player()->get_id()?>' +
                   '&area_type=' + area_type +
                   '&area=' + area +
                   '&difficulty=' + difficulty +
@@ -235,7 +236,7 @@
                 if (!confirm("Permanently delete team?")){
                     return
                 }
-                var request = 'player=<?=$PLAYER->id?>&id=' + id;
+                var request = 'player=<?=get_context()->get_player()->get_id()?>&id=' + id;
                 var xhttp = new XMLHttpRequest();
                 xhttp.onreadystatechange = function() {
                     if (this.readyState == 4){ 
@@ -711,7 +712,7 @@
             <p>
                 View and manage teams
             </p>
-            <form action='/<?=$PLAYER->id?>/teams/' method='get'  id='filter_monsters' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/teams/' method='get'  id='filter_monsters' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>
@@ -804,7 +805,7 @@
 <?php
                                         }
 ?>
-                                        <a href='/<?=$PLAYER->id?>/monsters/<?=$unit->id?>'>
+                                        <a href='/<?=get_context()->get_player()->get_id()?>/monsters/<?=$unit->id?>'>
                                             <div class='monster_panel'>
                                                 <?=HTML::unit_panel($unit)?>
 <?php

+ 5 - 4
application/view/unit.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Monster_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -92,7 +93,7 @@
                                 foreach($page->unit->teams as $team){
 ?>
                                     <li>
-                                        <a target='blank' href='/<?=$PLAYER->id?>/teams/<?=$team->id?>'>
+                                        <a target='blank' href='/<?=get_context()->get_player()->get_id()?>/teams/<?=$team->id?>'>
                                             <?=$team->name?>
                                         </a>
                                     </li>
@@ -104,7 +105,7 @@
 <?php
                     }
 ?>
-                    <a id='optimize' class='a_button' href='/<?=$PLAYER->id?>/optimizer/<?=$page->unit->id?>'>
+                    <a id='optimize' class='a_button' href='/<?=get_context()->get_player()->get_id()?>/optimizer/<?=$page->unit->id?>'>
                         Optimize
                     </a>
                 </div>

+ 5 - 4
application/view/units.php

@@ -6,7 +6,8 @@
      *
      * @category View
      * @var Monsters_Page $page The page model.
-     * @var player $PLAYER Currently selected player.
+     * @var Player get_context()->get_player() Currently selected player.
+     * @var int get_context()->get_game_mode() Game mode.
      */
 ?>
 <!DOCTYPE html>
@@ -19,7 +20,7 @@
         <!-- CSS files -->
         <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
 <?php
-        if ($MODE == GAME_MODE_ID::RTA){
+        if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
 ?>
             <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui-rta.css'/>
 <?php
@@ -56,7 +57,7 @@
             <p>
                 Monsters
             </p>
-            <form action='/<?=$PLAYER->id?>/units/' method='get'  id='filter_monsters' class='filter'>
+            <form action='/<?=get_context()->get_player()->get_id()?>/units/' method='get'  id='filter_monsters' class='filter'>
                 <table>
                     <tr>
                         <td class='label'>
@@ -176,7 +177,7 @@
                     foreach ($page->units as $unit){
 ?>
                         <div class='monster'>
-                            <a href='/<?=$PLAYER->id?>/units/<?=$unit->id?>'>
+                            <a href='/<?=get_context()->get_player()->get_id()?>/units/<?=$unit->id?>'>
                                 <div class='monster_panel'>
                                     <?=HTML::unit_panel($unit)?>
                                 </div>

+ 13 - 1
public/index.php

@@ -9,7 +9,19 @@
 
     require_once(__DIR__ . "/../application/Controller.php");
 
+    $controller = new Controller();
+
+    $request = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
+    $params = explode('/', $request);
+
+    function get_context(){
+        global $controller;
+        return $controller->get_context();
+    }
+
     $request = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
     $params = explode('/', $request);
-    new Controller($params);
+    $controller->prepare($params);
+    $controller->action();
+
 ?>