* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ /** * Require dependent entities if not present. */ require_once(PATH::ENTITY . "Area.php"); require_once(PATH::ENTITY . "Unit.php"); /** * A Team. * * Represents an object from the table 'unit'. * * @category Entity */ class Team extends Entity{ /** * @var int Owner's player identifier. */ private $player; /** * @var int Team identifier. */ private $id; /** * @var string Team name. */ private $name; /** * @var string Team description. */ private $description; /** * @var bool Internal flag to check if the team units have been loaded into the entity. */ private $flag_units = false; /** * @var Unit[] Units in the team. */ private $unit = []; /** * @var int ID of the leader unit. */ private $leader; /** * @var int[] List of IDs of the frontline units. */ private $frontline = []; /** * @var Area Area the team is designed for. */ private $area; /** * @var int Stage the team is designed for. It can be null. */ private $stage; /** * @var int difficulty the team is designed for. It can be null. * @see DIFFICULTY_ID */ private $difficulty; /** * @var int Team score for the optmizer */ private $score; /** * Constructor. * * Searches the database and retrieves the information about the * team, populating it and it's items. * * @param int $id Team identifier. */ public function __construct($id){ $statement = get_context()->get_db()->prepare(" SELECT player, id, name, description, area_type, area, stage, difficulty, score FROM team WHERE id = :id; "); $statement->bindValue(':id', $id, SQLITE3_TEXT); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r){ $this->player = $r["player"]; $this->id = $r["id"]; $this->name = HTML::e($r["name"]); $this->description = HTML::e($r["description"]); $this->area = new Area($r["area"], $r["area_type"]); $this->stage = $r["stage"]; $this->score = $r["score"]; } } /** * Loads the team units into the entity. * * Must be called only once before trying to get the party units, leader or positions. Sets * the flag {@link Team:$flag_units} to true. */ private function load_units(){ $statement = get_context()->get_db()->prepare(" SELECT unit, leader, front FROM team_unit WHERE team = :team ORDER BY front DESC, leader DESC; "); $statement->bindValue(':team', $this->id, SQLITE3_TEXT); $q = $statement->execute(); while ($r = $q->fetchArray(SQLITE3_ASSOC)){ array_push($this->unit, new Unit($r["unit"], false)); if ($r["leader"] == 1){ $this->leader = $r["unit"]; } if ($r["front"] == 1){ array_push($this->frontline, $r["unit"]); } } $this->flag_units = true; } /** * Retrieves the account's player identifier. * * @return int Player ID. */ public function get_player(){ return $this->player; } /** * Retrieves the team identifier. * * @return int Team ID. */ public function get_id(){ return $this->id; } /** * Retrieves the team name. * * @return string Team name. */ public function get_name(){ return $this->name; } /** * Retrieves the team description. * * @return string Team description (can be an empty string). */ public function get_description(){ return $this->description; } /** * Retrieves the units in the team. * * @return Unit[] Units in the team. */ public function get_units(){ if (! $this->flag_units){ $this->load_units(); } return $this->unit; } /** * Retrieves the leader unit's identifier. * * @return int Leader unit ID. */ public function get_leader(){ return $this->leader; } /** * Retrieves the identifiers of the units in the frontline. * * @return int[] ID of units in the frontline. */ public function get_frontline(){ return $this->frontline; } /** * Retrieves the area the team is set for. * * @return Area Team area. */ public function get_area(){ return $this->area; } /** * Retrieves the stage the team is intended to. * * @return int Team stage, can be null. */ public function get_stage(){ return $this->stage; } /** * Retrieves the difficulty of the area the team is intended for. * * @see DIFFICULTY_ID * @return int Difficulty ID, can be null. */ public function get_difficulty(){ return $this->difficulty; } /** * Retrieves the score of the team for the optimizer. * * @return int Team score. */ public function get_score(){ return $this->score; } }