Du kannst nicht mehr als 25 Themen auswählen
Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
77 Zeilen
2.2 KiB
PHP
77 Zeilen
2.2 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
class Artifact {
|
|
|
|
private $uuid = null;
|
|
private $locationid = null;
|
|
private $energy = 0;
|
|
private $delete = false;
|
|
|
|
/**
|
|
* Create a new artifact.
|
|
* @global type $database
|
|
* @param int $ownerid
|
|
* @param int $locationid
|
|
* @param \Energy $energy
|
|
* @param int $itemid
|
|
* @return \Artifact The newly created artifact.
|
|
*/
|
|
public static function create(int $ownerid, int $locationid, \Energy $energy, int $itemid) {
|
|
global $database;
|
|
$database->insert("artifacts", ["currentlife" => $energy->getEnergy(), "maxlife" => $energy->getMaxEnergy(), "locationid" => $locationid, "accountid" => $ownerid, "itemid" => $itemid]);
|
|
return (new Artifact($database->id()));
|
|
}
|
|
|
|
public function __construct($uuid) {
|
|
global $database;
|
|
$data = $database->get("artifacts", ["artuuid", "currentlife", "maxlife", "locationid"], ["artuuid" => $uuid]);
|
|
|
|
$this->uuid = $data["artuuid"];
|
|
$this->energy = new Energy($data["currentlife"], $data["maxlife"]);
|
|
$this->locationid = $data["locationid"];
|
|
}
|
|
|
|
public function changeEnergy(int $diff) {
|
|
$this->energy->setEnergy($this->energy->getEnergy() + $diff);
|
|
}
|
|
|
|
public function getEnergy(): \Energy {
|
|
return $this->energy;
|
|
}
|
|
|
|
/**
|
|
* Check if the artifact will be deleted on save.
|
|
* @return bool
|
|
*/
|
|
public function deleted(): bool {
|
|
if ($this->energy->getEnergy() == 0 || $this->delete) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Mark for deletion on save.
|
|
*/
|
|
public function delete() {
|
|
$this->delete = true;
|
|
}
|
|
|
|
public function save() {
|
|
global $database;
|
|
|
|
if ($this->deleted()) {
|
|
$database->delete("artifacts", ["artuuid" => $this->uuid]);
|
|
} else {
|
|
$database->update("artifacts", ["currentlife" => $this->energy->getEnergy(), "maxlife" => $this->energy->getMaxEnergy()], ["artuuid" => $this->uuid]);
|
|
}
|
|
}
|
|
|
|
}
|