<?php
/**
* Created by PhpStorm.
* User: daniel
* Date: 20.03.18
* Time: 20:21
*/
namespace Empire\Core;
use \Empire\Legacy\cgt;
use \Empire\Legacy\db;
class Stance extends AbstractEntity implements Named {
const PRIMARY_FIELD = 'DIST_ID';
const TABLE = 'DIPLOMATIC_STATUS';
const STATI = [
"Imperial Union",
"Ally",
"Friendly",
"Neutral",
"Enemy"
];
const STATUS_COLORS = [
"hsl(120, 100%, 50%)",
"hsl(120, 100%, 30%)",
"hsl(210, 100%, 50%)",
"#FFFF00",
"#E41B17"
];
protected $DIST_ID, $DIST_NAME, $DIST_STATUS, $DIST_DATE;
public $name, $status, $statusColor, $statusNumber, $date, $dateString;
public function initialize() {
$this->id = $this->DIST_ID;
$this->name = $this->DIST_NAME;
$this->status = self::STATI[$this->DIST_STATUS];
$this->statusColor = self::STATUS_COLORS[$this->DIST_STATUS];
$this->statusNumber = $this->DIST_STATUS;
$this->date = cgt::evaluateString($this->DIST_DATE);
$this->dateString = $this->DIST_DATE;
}
/**
* Load all stances ordered by name.
* @return Stance[]
* @throws \Exception
*/
public static function loadAll() {
$stmt = db::prepare('SELECT * FROM DIPLOMATIC_STATUS ORDER BY DIST_NAME ASC');
return db::fetchArrayOfObjects($stmt, self::class);
}
public function getPrimaryField() {
return self::PRIMARY_FIELD;
}
public function getTable() {
return self::TABLE;
}
public function getName() {
return $this->name;
}
public function set($value) {
Event::send(Core::user()->getID(), 0, Core::user()->getName()." has changed the Empire's diplomatic stance toward '$this->name' to " . self::STATI[$value] . ".", cint_EVENTTYPE_DIPLOMACY);
$this->updateFields(['DIST_STATUS' => $value, 'DIST_DATE' => time()]);
$this->refresh();
}
public function delete() {
Event::send(Core::user()->getID(), 0, Core::user()->getName() ." has removed '$this->name' from the diplomatic database.", cint_EVENTTYPE_DIPLOMACY);
return parent::delete();
}
public static function addStance($name, $status) {
$fetch = Core::db()->prepare("SELECT * FROM DIPLOMATIC_STATUS WHERE DIST_NAME = ?");
$fetch->execute([$name]);
if($fetch->rowCount() > 0) {
return false;
}
Core::db()->insert("DIPLOMATIC_STATUS", ["DIST_NAME" => $name, "DIST_STATUS" => $status, "DIST_DATE" => time()]);
$id = Core::db()->lastInsertId();
Event::send(Core::user()->getID(), 0, Core::user()->getName()." has added '$name' to the database, with a diplomatic stance of " . self::STATI[$status] . ".", cint_EVENTTYPE_DIPLOMACY);
return self::load([$id]);
}
}