From 4728b237994adf365b9b89d0046df529a05d2433 Mon Sep 17 00:00:00 2001
From: mamen <markusme@edu.aau.at>
Date: Fri, 8 Jun 2018 20:59:43 +0200
Subject: [PATCH 1/2] * added json-serialization for each object

Signed-off-by: mamen <markusme@edu.aau.at>
---
 core/classes/data/building.php  |  6 ++-
 core/classes/data/defense.php   |  6 ++-
 core/classes/data/fleet.php     |  6 ++-
 core/classes/data/galaxy.php    |  6 ++-
 core/classes/data/planet.php    |  6 ++-
 core/classes/data/tech.php      |  6 ++-
 core/classes/data/units.php     | 25 ++++++++----
 core/classes/data/user.php      |  6 ++-
 core/classes/loader.php         | 27 +++++++++----
 core/classes/units/building.php |  6 ++-
 core/classes/units/defense.php  |  6 ++-
 core/classes/units/fleet.php    |  6 ++-
 core/classes/units/planet.php   | 72 +++++++++++++++++----------------
 core/classes/units/research.php | 10 +++--
 core/classes/units/unit.php     | 18 +++++----
 15 files changed, 143 insertions(+), 69 deletions(-)

diff --git a/core/classes/data/building.php b/core/classes/data/building.php
index 62bdeae..7176eef 100644
--- a/core/classes/data/building.php
+++ b/core/classes/data/building.php
@@ -8,7 +8,7 @@
      * This class maps the 'buildings'-table to an php object and contains
      * all necessary getters and setters.
      */
-    class D_Building {
+    class D_Building implements JsonSerializable {
 
         /** @var int Level of Metal Mine */
         private $metal_mine;
@@ -521,4 +521,8 @@ public function setBuildingByID(int $id, int $level) : void {
             }
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/defense.php b/core/classes/data/defense.php
index b7e52ce..866e909 100644
--- a/core/classes/data/defense.php
+++ b/core/classes/data/defense.php
@@ -7,7 +7,7 @@
     /**
      * This class maps the 'defense'-table to an php object.
      */
-    class D_Defense {
+    class D_Defense implements JsonSerializable {
 
         /** @var int Amount of Rocket Launcher */
         private $rocket_launcher;
@@ -362,4 +362,8 @@ public function setDefenseByID(int $id, int $level) {
             }
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/fleet.php b/core/classes/data/fleet.php
index 7f0fb6b..0440877 100644
--- a/core/classes/data/fleet.php
+++ b/core/classes/data/fleet.php
@@ -7,7 +7,7 @@
     /**
      * This class maps the 'fleet'-table to an php object.
      */
-    class D_Fleet {
+    class D_Fleet implements JsonSerializable {
 
         /** @var int Amount of Small Cargo Ship */
         private $small_cargo_ship;
@@ -491,4 +491,8 @@ public function setFleetByID(int $id, int $level) {
             }
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/galaxy.php b/core/classes/data/galaxy.php
index 3aeec54..00fee80 100644
--- a/core/classes/data/galaxy.php
+++ b/core/classes/data/galaxy.php
@@ -7,7 +7,7 @@
     /**
      * This class maps the 'galaxy'-table to an php object.
      */
-    class D_Galaxy {
+    class D_Galaxy implements JsonSerializable {
 
         /** @var int Amount of Metal in the Debris */
         private $debris_metal;
@@ -75,4 +75,8 @@ public function setDebrisCrystal(int $debris_crystal) : void {
             }
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/planet.php b/core/classes/data/planet.php
index 5afab63..1856c79 100644
--- a/core/classes/data/planet.php
+++ b/core/classes/data/planet.php
@@ -8,7 +8,7 @@
     /**
      * This class maps the 'planet'-table to an php object.
      */
-    class D_Planet {
+    class D_Planet implements JsonSerializable {
 
         /** @var int The ID of the planet */
         private $planetID;
@@ -772,4 +772,8 @@ public function setDestroyed(bool $destroyed) : void {
 
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/tech.php b/core/classes/data/tech.php
index 4340466..889a70e 100644
--- a/core/classes/data/tech.php
+++ b/core/classes/data/tech.php
@@ -7,7 +7,7 @@
     /**
      * This class maps the 'tech'-table to an php object.
      */
-    class D_Tech {
+    class D_Tech implements JsonSerializable {
 
         private $espionage_tech;
 
@@ -446,4 +446,8 @@ public function setTechByID(int $id, int $level) {
             }
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/data/units.php b/core/classes/data/units.php
index 18099bd..538fadd 100644
--- a/core/classes/data/units.php
+++ b/core/classes/data/units.php
@@ -8,24 +8,24 @@
      * This class contains all information about every unit, including
      * prices, dependencies and language-bindings.
      */
-    class D_Units {
+    class D_Units implements JsonSerializable {
 
         /** @var array Mapping of Unit-ID to Unit-String-ID */
-        private static $units;
+        protected static $units;
 
         /** @var array Mapping of Unit-ID to the name in the currently loaded language */
-        private static $names;
+        protected static $names;
 
         /** @var array Mapping of Unit-ID to the description, according to the current language-file */
-        private static $descriptions;
+        protected static $descriptions;
 
         /** @var array Mapping of Unit-ID to the price for the unit */
-        private static $pricelist;
+        protected static $pricelist;
 
         /** @var array Mapping of Unit-ID to the requirements for the unit */
-        private static $requeriments;
+        protected static $requeriments;
 
-        private static $initialized = false;
+        protected static $initialized = false;
 
         /**
          *
@@ -630,4 +630,15 @@ static function getEnergyConsumption(int $level) : float {
                 return 10 * $level * pow(1.1, $level);
             }
         }
+
+        public function jsonSerialize() {
+            if(!self::$initialized) self::init();
+
+            return [
+                "units" => self::$units,
+                "descriptions" => self::$descriptions,
+                "pricelist" => self::$pricelist,
+                "requeriments" => self::$requeriments
+            ];
+        }
     }
diff --git a/core/classes/data/user.php b/core/classes/data/user.php
index 764fa82..fe4b215 100644
--- a/core/classes/data/user.php
+++ b/core/classes/data/user.php
@@ -8,7 +8,7 @@
      * This class maps the 'user'-table to an php object and is also responsible of keeping it up-to-date.
      *
      */
-    class D_User {
+    class D_User implements JsonSerializable {
 
         private $userID;
 
@@ -190,4 +190,8 @@ public function setOldRank($old_rank) : void {
             $this->old_rank = $old_rank;
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
diff --git a/core/classes/loader.php b/core/classes/loader.php
index 694608c..a4e4195 100644
--- a/core/classes/loader.php
+++ b/core/classes/loader.php
@@ -4,7 +4,7 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class Loader {
+    class Loader implements JsonSerializable {
 
         private static $user = null;
 
@@ -30,13 +30,6 @@ class Loader {
 
         private static $initialized = false;
 
-        private function __construct($userID) {
-            if (!self::$initialized) {
-                self::init($userID);
-                self::$initialized = true;
-            }
-        }
-
         public static function init($userID) {
 
             $dbConnection = new Database();
@@ -545,4 +538,22 @@ public static function getFleetData() : D_Fleet {
             return self::$fleetData;
         }
 
+        public function jsonSerialize() {
+            if(!self::$initialized) self::init(1);
+
+            return [
+                "user" => self::$user,
+                "planet" => self::$planet,
+                "galaxy" => self::$galaxy,
+                "buildingData" => self::$buildingData,
+                "buildingList" => self::$buildingList,
+                "defenseData" => self::$defenseData,
+                "defenseList" => self::$defenseList,
+                "techData" => self::$techData,
+                "techList" => self::$techList,
+                "fleetData" => self::$fleetData,
+                "fleetList" => self::$fleetList
+            ];
+        }
+
     }
diff --git a/core/classes/units/building.php b/core/classes/units/building.php
index 77937ae..4253f50 100644
--- a/core/classes/units/building.php
+++ b/core/classes/units/building.php
@@ -4,7 +4,7 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class U_Building extends U_Unit {
+    class U_Building extends U_Unit implements JsonSerializable {
 
         /** @var int the current level */
         private $level;
@@ -93,4 +93,8 @@ public function getEnergyConsumption($metPercent, $crystPercent, $deutPercent) :
         public function getLevel() : int {
             return $this->level;
         }
+
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
     }
\ No newline at end of file
diff --git a/core/classes/units/defense.php b/core/classes/units/defense.php
index 1f9f654..c41192f 100644
--- a/core/classes/units/defense.php
+++ b/core/classes/units/defense.php
@@ -4,7 +4,7 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class U_Defense extends U_Unit {
+    class U_Defense extends U_Unit implements JsonSerializable {
 
         /** @var int The current amount */
         private $amount;
@@ -53,4 +53,8 @@ public function getCostEnergy() : float {
         public function getAmount() : int {
             return $this->amount;
         }
+
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
     }
\ No newline at end of file
diff --git a/core/classes/units/fleet.php b/core/classes/units/fleet.php
index 653c641..5797a68 100644
--- a/core/classes/units/fleet.php
+++ b/core/classes/units/fleet.php
@@ -4,7 +4,7 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class U_Fleet extends U_Unit {
+    class U_Fleet extends U_Unit implements JsonSerializable {
 
         /** @var int The current amount */
         private $amount;
@@ -54,4 +54,8 @@ public function getAmount() : int {
         public function setAmount($amt) {
             $this->amount = $amt;
         }
+
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
     }
\ No newline at end of file
diff --git a/core/classes/units/planet.php b/core/classes/units/planet.php
index 1fcb9e6..97fc6df 100644
--- a/core/classes/units/planet.php
+++ b/core/classes/units/planet.php
@@ -4,73 +4,73 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class U_Planet {
+    class U_Planet implements JsonSerializable {
 
-        private $planetID;
+        protected $planetID;
 
-        private $ownerID;
+        protected $ownerID;
 
-        private $name;
+        protected $name;
 
-        private $galaxy;
+        protected $galaxy;
 
-        private $system;
+        protected $system;
 
-        private $planet;
+        protected $planet;
 
-        private $last_update;
+        protected $last_update;
 
-        private $planet_type;
+        protected $planet_type;
 
-        private $image;
+        protected $image;
 
-        private $diameter;
+        protected $diameter;
 
-        private $fields_current;
+        protected $fields_current;
 
-        private $fields_max;
+        protected $fields_max;
 
-        private $temp_min;
+        protected $temp_min;
 
-        private $temp_max;
+        protected $temp_max;
 
-        private $metal;
+        protected $metal;
 
-        private $crystal;
+        protected $crystal;
 
-        private $deuterium;
+        protected $deuterium;
 
-        private $energy_used;
+        protected $energy_used;
 
-        private $energy_max;
+        protected $energy_max;
 
-        private $metal_mine_percent;
+        protected $metal_mine_percent;
 
-        private $crystal_mine_percent;
+        protected $crystal_mine_percent;
 
-        private $deuterium_synthesizer_percent;
+        protected $deuterium_synthesizer_percent;
 
-        private $solar_plant_percent;
+        protected $solar_plant_percent;
 
-        private $fusion_reactor_percent;
+        protected $fusion_reactor_percent;
 
-        private $solar_satellite_percent;
+        protected $solar_satellite_percent;
 
-        private $b_building_id;
+        protected $b_building_id;
 
-        private $b_building_endtime;
+        protected $b_building_endtime;
 
-        private $b_tech_id;
+        protected $b_tech_id;
 
-        private $b_hangar_start_time;
+        protected $b_hangar_start_time;
 
-        private $b_tech_endtime;
+        protected $b_tech_endtime;
 
-        private $b_hangar_id;
+        protected $b_hangar_id;
 
-        private $b_hangar_plus;
+        protected $b_hangar_plus;
 
-        private $destroyed;
+        protected $destroyed;
 
         /**
          * U_Planet constructor.
@@ -1093,4 +1093,8 @@ public function print() : void {
             echo '</pre>';
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
\ No newline at end of file
diff --git a/core/classes/units/research.php b/core/classes/units/research.php
index 9149e7c..1e87847 100644
--- a/core/classes/units/research.php
+++ b/core/classes/units/research.php
@@ -4,11 +4,11 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    class U_Research extends U_Unit {
+    class U_Research extends U_Unit implements JsonSerializable {
 
-        private $level;
+        protected $level;
 
-        private $costFactor;
+        protected $costFactor;
 
         /**
          * Unit constructor.
@@ -63,4 +63,8 @@ public function getCostEnergy() : float {
         public function getLevel() : int {
             return $this->level;
         }
+
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
     }
\ No newline at end of file
diff --git a/core/classes/units/unit.php b/core/classes/units/unit.php
index be91533..eb6353e 100644
--- a/core/classes/units/unit.php
+++ b/core/classes/units/unit.php
@@ -4,25 +4,25 @@
 
     defined('INSIDE') OR exit('No direct script access allowed');
 
-    abstract class U_Unit {
+    abstract class U_Unit implements JsonSerializable {
 
         /** @var int The unitID */
-        private $unitID;
+        protected $unitID;
 
         /** @var float the metal cost */
-        private $costMetal;
+        protected $costMetal;
 
         /** @var float the crystal cost */
-        private $costCrystal;
+        protected $costCrystal;
 
         /** @var float the deuterium cost */
-        private $costDeuterium;
+        protected $costDeuterium;
 
         /** @var float the energy cost */
-        private $costEnergy;
+        protected $costEnergy;
 
         /** @var float the cost factor */
-        private $costFactor;
+        protected $costFactor;
 
         /**
          * Unit constructor.
@@ -85,4 +85,8 @@ public function getFactor() : float {
             return $this->costFactor;
         }
 
+        public function jsonSerialize() {
+            return get_object_vars($this);
+        }
+
     }
\ No newline at end of file

From 28079b5f2e61c6240c7983c83bc35f9bf4066f88 Mon Sep 17 00:00:00 2001
From: mamen <markusme@edu.aau.at>
Date: Sun, 10 Jun 2018 19:52:01 +0200
Subject: [PATCH 2/2] * api-call for unitData

Signed-off-by: mamen <markusme@edu.aau.at>
---
 api/unitData.php | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 api/unitData.php

diff --git a/api/unitData.php b/api/unitData.php
new file mode 100644
index 0000000..590c0b5
--- /dev/null
+++ b/api/unitData.php
@@ -0,0 +1,14 @@
+<?php
+
+    define('INSIDE', true);
+
+    require_once "../core/config.php";
+    require_once "../core/classes/data/units.php";
+
+    Config::init();
+    D_Units::init();
+
+    header('Content-Type: application/json');
+
+    echo json_encode(D_Units::jsonSerialize());
+    die();
\ No newline at end of file