Use artifacts in place defense
This commit is contained in:
parent
954fb0a836
commit
2dcaaed09b
@ -16,7 +16,7 @@ if ($place->isClaimed()) {
|
||||
if ($place->energy->getEnergy() == $place->energy->getMaxEnergy()) {
|
||||
sendJsonResp($Strings->get("Nothing happened.", false));
|
||||
}
|
||||
$place->changeEnergy(10);
|
||||
$place->doDefend(10);
|
||||
$player->changeEnergy(-5);
|
||||
$player->addExp();
|
||||
$player->stats->updateStat(PlayerStats::DEFENDS, 1);
|
||||
@ -25,7 +25,7 @@ if ($place->isClaimed()) {
|
||||
sendJsonResp($Strings->get("Defending...", false));
|
||||
} else {
|
||||
// Different teams, player is attacking
|
||||
$place->changeEnergy(-10);
|
||||
$place->doAttack(10);
|
||||
$player->changeEnergy(-5);
|
||||
$player->addExp();
|
||||
$player->stats->updateStat(PlayerStats::ATTACKS, 1);
|
||||
|
76
lib/Artifact.lib.php
Normal file
76
lib/Artifact.lib.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -25,6 +25,7 @@ class Place {
|
||||
private $osmid = null;
|
||||
public $energy = null;
|
||||
private $data = "[]";
|
||||
private $artifacts = [];
|
||||
|
||||
/**
|
||||
* @var int Last player activity at the place, as UNIX timestamp
|
||||
@ -46,6 +47,12 @@ class Place {
|
||||
$this->energy->setEnergy($game["currentlife"]);
|
||||
$this->data = $game["data"];
|
||||
$this->lastactivity = strtotime($game["lastactivity"]);
|
||||
|
||||
$artifacts = $database->select("artifacts", "artuuid", ["locationid" => $game["locationid"]]);
|
||||
foreach ($artifacts as $art) {
|
||||
$this->artifacts[] = new Artifact($art);
|
||||
}
|
||||
|
||||
$this->gameexists = true;
|
||||
} else {
|
||||
$this->gameexists = false;
|
||||
@ -102,6 +109,9 @@ class Place {
|
||||
* @param int $diff
|
||||
*/
|
||||
public function changeEnergy(int $diff) {
|
||||
if ($diff < 0) {
|
||||
|
||||
}
|
||||
$this->energy->setEnergy($this->energy->getEnergy() + $diff);
|
||||
if ($this->energy->getEnergy() == 0) {
|
||||
$this->unclaim();
|
||||
@ -130,11 +140,41 @@ class Place {
|
||||
$this->updateLastActivity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset place to natural unclaimed state.
|
||||
*/
|
||||
public function unclaim() {
|
||||
$this->energy = new Energy(0, 0);
|
||||
$this->ownerid = null;
|
||||
$this->teamid = null;
|
||||
$this->updateLastActivity();
|
||||
foreach ($this->artifacts as $artifact) {
|
||||
$artifact->delete();
|
||||
}
|
||||
}
|
||||
|
||||
public function getArtifacts() {
|
||||
return $this->artifacts;
|
||||
}
|
||||
|
||||
public function doAttack(int $damage) {
|
||||
$artifact = false;
|
||||
foreach ($this->artifacts as $art) {
|
||||
if (!$art->deleted()) {
|
||||
$artifact = $art;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($artifact) {
|
||||
// Artifact takes damage for place
|
||||
$artifact->changeEnergy($damage * -1);
|
||||
} else {
|
||||
$this->changeEnergy($damage * -1);
|
||||
}
|
||||
}
|
||||
|
||||
public function doDefend(int $amount) {
|
||||
$this->changeEnergy($amount);
|
||||
}
|
||||
|
||||
public function save() {
|
||||
@ -150,6 +190,10 @@ class Place {
|
||||
"osmid" => $this->osmid
|
||||
]
|
||||
);
|
||||
|
||||
foreach ($this->artifacts as $artifact) {
|
||||
$artifact->save();
|
||||
}
|
||||
} else {
|
||||
$database->insert("locations", [
|
||||
"teamid" => $this->teamid,
|
||||
|
Loading…
x
Reference in New Issue
Block a user