Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-Serialization #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/unitData.php
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 5 additions & 1 deletion core/classes/data/building.php
Original file line number Diff line number Diff line change
@@ -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);
}

}
6 changes: 5 additions & 1 deletion core/classes/data/defense.php
Original file line number Diff line number Diff line change
@@ -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;
@@ -365,4 +365,8 @@ public function setDefenseByID(int $id, int $level) {
}
}

public function jsonSerialize() {
return get_object_vars($this);
}

}
6 changes: 5 additions & 1 deletion core/classes/data/fleet.php
Original file line number Diff line number Diff line change
@@ -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;
@@ -493,4 +493,8 @@ public function setFleetByID(int $id, int $level) {
}
}

public function jsonSerialize() {
return get_object_vars($this);
}

}
6 changes: 5 additions & 1 deletion core/classes/data/galaxy.php
Original file line number Diff line number Diff line change
@@ -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);
}

}
6 changes: 5 additions & 1 deletion core/classes/data/planet.php
Original file line number Diff line number Diff line change
@@ -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);
}

}
6 changes: 5 additions & 1 deletion core/classes/data/tech.php
Original file line number Diff line number Diff line change
@@ -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;

@@ -448,4 +448,8 @@ public function setTechByID(int $id, int $level) {
}
}

public function jsonSerialize() {
return get_object_vars($this);
}

}
25 changes: 18 additions & 7 deletions core/classes/data/units.php
Original file line number Diff line number Diff line change
@@ -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;

static function destruct() {
self::$units = null;
@@ -661,4 +661,15 @@ static function getEnergyConsumption(int $level) : float {

return -1.0;
}

public function jsonSerialize() {
if(!self::$initialized) self::init();

return [
"units" => self::$units,
"descriptions" => self::$descriptions,
"pricelist" => self::$pricelist,
"requeriments" => self::$requeriments
];
}
}
6 changes: 5 additions & 1 deletion core/classes/data/user.php
Original file line number Diff line number Diff line change
@@ -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;

@@ -193,4 +193,8 @@ public function setOldRank($old_rank) : void {
$this->old_rank = $old_rank;
}

public function jsonSerialize() {
return get_object_vars($this);
}

}
27 changes: 19 additions & 8 deletions core/classes/loader.php
Original file line number Diff line number Diff line change
@@ -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
];
}

}
6 changes: 5 additions & 1 deletion core/classes/units/building.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 5 additions & 1 deletion core/classes/units/defense.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 5 additions & 1 deletion core/classes/units/fleet.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
75 changes: 38 additions & 37 deletions core/classes/units/planet.php
Original file line number Diff line number Diff line change
@@ -4,76 +4,73 @@

defined('INSIDE') OR exit('No direct script access allowed');

/**
* Class U_Planet
*/
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.
@@ -1088,4 +1085,8 @@ public function print() : void {
echo '</pre>';
}

public function jsonSerialize() {
return get_object_vars($this);
}

}
10 changes: 7 additions & 3 deletions core/classes/units/research.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 11 additions & 7 deletions core/classes/units/unit.php
Original file line number Diff line number Diff line change
@@ -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);
}

}