src-empire/Empire/Core/Stance.php line 54

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: daniel
  5.  * Date: 20.03.18
  6.  * Time: 20:21
  7.  */
  8. namespace Empire\Core;
  9. use \Empire\Legacy\cgt;
  10. use \Empire\Legacy\db;
  11. class Stance extends AbstractEntity implements Named {
  12.     const PRIMARY_FIELD 'DIST_ID';
  13.     const TABLE 'DIPLOMATIC_STATUS';
  14.     const STATI = [
  15.         "Imperial Union",
  16.         "Ally",
  17.         "Friendly",
  18.         "Neutral",
  19.         "Enemy"
  20.     ];
  21.     const STATUS_COLORS = [
  22.         "hsl(120, 100%, 50%)",
  23.         "hsl(120, 100%, 30%)",
  24.         "hsl(210, 100%, 50%)",
  25.         "#FFFF00",
  26.         "#E41B17"
  27.     ];
  28.     protected $DIST_ID$DIST_NAME$DIST_STATUS$DIST_DATE;
  29.     public $name$status$statusColor$statusNumber$date$dateString;
  30.     public function initialize() {
  31.         $this->id $this->DIST_ID;
  32.         $this->name $this->DIST_NAME;
  33.         $this->status self::STATI[$this->DIST_STATUS];
  34.         $this->statusColor self::STATUS_COLORS[$this->DIST_STATUS];
  35.         $this->statusNumber $this->DIST_STATUS;
  36.         $this->date cgt::evaluateString($this->DIST_DATE);
  37.         $this->dateString $this->DIST_DATE;
  38.     }
  39.     /**
  40.      * Load all stances ordered by name.
  41.      * @return Stance[]
  42.      * @throws \Exception
  43.      */
  44.     public static function loadAll() {
  45.         $stmt db::prepare('SELECT * FROM DIPLOMATIC_STATUS ORDER BY DIST_NAME ASC');
  46.         return db::fetchArrayOfObjects($stmtself::class);
  47.     }
  48.     public function getPrimaryField() {
  49.         return self::PRIMARY_FIELD;
  50.     }
  51.     public function getTable() {
  52.         return self::TABLE;
  53.     }
  54.     public function getName() {
  55.         return $this->name;
  56.     }
  57.     public function set($value) {
  58.         Event::send(Core::user()->getID(), 0Core::user()->getName()." has changed the Empire's diplomatic stance toward '$this->name' to " self::STATI[$value] . "."cint_EVENTTYPE_DIPLOMACY);
  59.         $this->updateFields(['DIST_STATUS' => $value'DIST_DATE' => time()]);
  60.         $this->refresh();
  61.     }
  62.     public function delete() {
  63.         Event::send(Core::user()->getID(), 0Core::user()->getName() ." has removed '$this->name' from the diplomatic database."cint_EVENTTYPE_DIPLOMACY);
  64.         return parent::delete();
  65.     }
  66.     public static function addStance($name$status) {
  67.         $fetch Core::db()->prepare("SELECT * FROM DIPLOMATIC_STATUS WHERE DIST_NAME = ?");
  68.         $fetch->execute([$name]);
  69.         if($fetch->rowCount() > 0) {
  70.             return false;
  71.         }
  72.         Core::db()->insert("DIPLOMATIC_STATUS", ["DIST_NAME" => $name"DIST_STATUS" => $status"DIST_DATE" => time()]);
  73.         $id Core::db()->lastInsertId();
  74.         Event::send(Core::user()->getID(), 0Core::user()->getName()." has added '$name' to the database, with a diplomatic stance of " self::STATI[$status] . "."cint_EVENTTYPE_DIPLOMACY);
  75.         return self::load([$id]);
  76.     }
  77. }