您最多能選擇 25 個主題 主題必須以字母或數字為開頭,可包含連接號「-」且最長為 35 個字元。

77 行
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]);
}
}
}