Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

103 righe
3.1 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 PlayerStats {
private $userid = 0;
private $claims = 0;
private $attacks = 0;
private $defends = 0;
private $scans = 0;
private $distancekm = 0.0;
const CLAIMS = 1;
const ATTACKS = 2;
const DEFENDS = 3;
const SCANS = 4;
const DISTANCEKM = 5;
public function __construct(User $user) {
global $database;
$this->userid = $user->getUID();
if ($database->has("player_stats", ["accountid" => $this->userid])) {
$stats = $database->get("player_stats", ["claims", "attacks", "defends", "scans", "distancekm"], ["accountid" => $this->userid]);
$this->claims = $stats["claims"];
$this->attacks = $stats["attacks"];
$this->defends = $stats["defends"];
$this->scans = $stats["scans"];
$this->distancekm = $stats["distancekm"];
}
}
public function getStat(int $stat) {
switch ($stat) {
case PlayerStats::CLAIMS:
return $this->claims;
case PlayerStats::ATTACKS:
return $this->attacks;
case PlayerStats::DEFENDS:
return $this->defends;
case PlayerStats::SCANS:
return $this->scans;
case PlayerStats::DISTANCEKM:
return $this->distancekm;
default:
return null;
}
}
public function updateStat(int $stat, $diff) {
switch ($stat) {
case PlayerStats::CLAIMS:
$this->claims += $diff;
break;
case PlayerStats::ATTACKS:
$this->attacks += $diff;
break;
case PlayerStats::DEFENDS:
$this->defends += $diff;
break;
case PlayerStats::SCANS:
$this->scans += $diff;
break;
case PlayerStats::DISTANCEKM:
$this->distancekm += $diff;
break;
}
}
public function save() {
global $database;
if ($database->has("player_stats", ["accountid" => $this->userid])) {
$database->update("player_stats", [
"claims" => $this->claims,
"attacks" => $this->attacks,
"defends" => $this->defends,
"scans" => $this->scans,
"distancekm" => $this->distancekm
], [
"accountid" => $this->userid
]
);
} else {
$database->insert("player_stats", [
"accountid" => $this->userid,
"claims" => $this->claims,
"attacks" => $this->attacks,
"defends" => $this->defends,
"scans" => $this->scans,
"distancekm" => $this->distancekm
]
);
}
}
}