exists()) { throw new InvalidArgumentException("User must exist."); } $this->user = $user; $this->energy = new Energy(100, 100); $this->stats = new PlayerStats($user); if ($database->has("players", ["accountid" => $user->getUID()])) { $profile = $database->get("players", ["accountid (id)", "nickname (name)", "level", "energy", "maxenergy", "teamid", "lastping", "stealth"], ["accountid" => $user->getUID()]); $this->name = $profile["name"]; $this->level = $profile["level"] * 1.0; $this->energy->setMaxEnergy($profile["maxenergy"]); $this->energy->setEnergy($profile["energy"]); $this->teamid = $profile["teamid"]; $this->lastping = strtotime($profile["lastping"]); $this->stealth = $profile["stealth"] == true; $this->exists = true; $this->badges = $database->select("player_badges", ["[>]badges" => "badgeid"], ["name", "desc", "icon", "color", "background", "gotdate (date)"], ["accountid" => $user->getUID()]); } else { $this->exists = false; } } public function exists(): bool { return $this->exists; } public function getBadges(): array { return $this->badges; } public function toArray(): array { //"accountid (id)", "nickname (name)", "level", "energy", "maxenergy", "teamid" return [ "id" => $this->getUID(), "name" => $this->name, "level" => floor($this->level), "energy" => $this->energy->getEnergy(), "maxenergy" => $this->energy->getMaxEnergy(), "teamid" => $this->getTeamID(), "badges" => $this->getBadges() ]; } public function getUID(): int { return $this->user->getUID(); } public function getLevel(): float { return $this->level; } public function getTeamID(): int { return $this->teamid; } public function setTeamID(int $id) { $this->teamid = $id; } public function getCredits(): int { return $this->credits; } /** * Add the specified amount of energy to the player. * @param int $diff */ public function changeEnergy(int $diff) { $this->energy->setEnergy($this->energy->getEnergy() + $diff); } /** * Set the player's last ping time to now. */ public function doPing() { $this->lastping = time(); } /** * Add experience to the user's level for doing something. */ public function addExp() { $exp = pow(pow(floor($this->level * 1.0) + 1, 2), -1.2); $this->level = $this->level + $exp; $this->recalculateStats(); } public function recalculateStats() { $healthpercent = ($this->energy->getEnergy() * 1.0) / ($this->energy->getMaxEnergy() * 1.0); $maxenergy = $this->energy->getMaxEnergy(); $this->energy->setMaxEnergy(floor($this->level) * 100); // If the max energy increased, adjust the current energy // to the same percentage as it was before if ($this->energy->getMaxEnergy() != $maxenergy) { $this->energy->setEnergy($this->energy->getMaxEnergy() * $healthpercent); } } public function createPlayer() { global $database; if ($this->exists) { throw new Exception("This player already exists."); } $database->insert("players", [ "accountid" => $this->user->getUID(), "level" => $this->getLevel(), "energy" => $this->energy->getEnergy(), "maxenergy" => $this->energy->getMaxEnergy(), "credits" => $this->getCredits(), "lastping" => date("Y-m-d H:i:s", $this->lastping), "teamid" => $this->getTeamID(), "nickname" => $this->user->getName() ]); $this->stats->save(); $this->exists = true; } public function save() { global $database; if (!$this->exists) { $this->createPlayer(); return; } $database->update("players", [ "level" => $this->getLevel(), "energy" => $this->energy->getEnergy(), "maxenergy" => $this->energy->getMaxEnergy(), "credits" => $this->getCredits(), "lastping" => date("Y-m-d H:i:s", $this->lastping), "teamid" => $this->getTeamID(), "nickname" => $this->user->getName() ], [ "accountid" => $this->user->getUID() ] ); $this->stats->save(); } }