[wip] code smell, psr-12, mvc, other, forgot by 1 year.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Battles\Models;
|
||||
|
||||
class BankModel extends Model
|
||||
{
|
||||
protected static string $tableName = 'bank';
|
||||
protected static string $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* @param int $money
|
||||
* @param int $uid
|
||||
*/
|
||||
public function setMoney(int $money, int $uid): void
|
||||
{
|
||||
$this->columns = [
|
||||
self::$primaryKey => $uid,
|
||||
'money' => $money,
|
||||
];
|
||||
$this->updateByPrimaryKey();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Battles\Models;
|
||||
|
||||
use Battles\Database\Db;
|
||||
|
||||
class Model
|
||||
{
|
||||
protected static string $tableName = '';
|
||||
protected static string $primaryKey = '';
|
||||
protected array $columns = [];
|
||||
|
||||
protected function getColumnValue($column)
|
||||
{
|
||||
return $this->columns[$column];
|
||||
}
|
||||
|
||||
protected function setColumnValue($column, $value)
|
||||
{
|
||||
$this->columns[$column] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Полная перезапись. Отсутствующие параметры превратятся в default|null.
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
{
|
||||
$q = 'replace into ' . self::$tableName . ' (' . implode(', ', array_keys($this->columns)) . ') values(';
|
||||
$keys = [];
|
||||
foreach ($this->columns as $key => $value) {
|
||||
$keys[':' . $key] = $value;
|
||||
}
|
||||
$q .= implode(', ', array_keys($keys)) . ')';
|
||||
Db::getInstance()->execute($q, $keys);
|
||||
}
|
||||
|
||||
protected function updateByPrimaryKey()
|
||||
{
|
||||
$q = 'update ' . self::$tableName . ' set ';
|
||||
$keys = [];
|
||||
foreach ($this->columns as $key => $value) {
|
||||
$keys[':' . $key] = $value;
|
||||
$q .= $key . ' = :' . $key . ', ';
|
||||
}
|
||||
$q = rtrim($q, ',');
|
||||
$q .= 'where ' . self::$primaryKey . ' = :' . self::$primaryKey;
|
||||
Db::getInstance()->execute($q, $keys);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Battles\Models;
|
||||
|
||||
use Battles\Database\Db;
|
||||
|
||||
class UserStats extends Model
|
||||
{
|
||||
protected int $strength;
|
||||
protected int $dexterity;
|
||||
protected int $intuition;
|
||||
protected int $endurance;
|
||||
protected int $intelligence;
|
||||
protected int $wisdom;
|
||||
protected int $health;
|
||||
protected int $mana;
|
||||
protected int $free_stat_points;
|
||||
private int $id;
|
||||
private const KULAK_MIN_DAMAGE = 1;
|
||||
private const KULAK_MAX_DAMAGE = 2;
|
||||
|
||||
protected static string $tableName = 'users';
|
||||
protected static string $primaryKey = 'id';
|
||||
|
||||
public function __construct($uid)
|
||||
{
|
||||
$this->id = $uid;
|
||||
}
|
||||
|
||||
|
||||
public function getFull(): object
|
||||
{
|
||||
$stats = Db::getInstance()->ofetch("
|
||||
select
|
||||
strength,
|
||||
dexterity,
|
||||
intuition,
|
||||
endurance,
|
||||
intelligence,
|
||||
wisdom
|
||||
from users where id = $this->id");
|
||||
$itemBonuses = Db::getInstance()->ofetch("
|
||||
select
|
||||
sum(add_strength) as item_strength,
|
||||
sum(add_dexterity) as item_dexterity,
|
||||
sum(add_intuition) as item_intuition,
|
||||
sum(add_endurance) as item_endurance,
|
||||
sum(add_intelligence) as item_intelligence,
|
||||
sum(add_wisdom) as item_wisdom,
|
||||
sum(add_accuracy) as item_accuracy,
|
||||
sum(add_evasion) as item_evasion,
|
||||
sum(add_criticals) as item_criticals,
|
||||
sum(add_min_physical_damage) as item_min_physical_damage,
|
||||
sum(add_max_physical_damage) as item_max_physical_damage
|
||||
from inventory where dressed_slot != 0 and owner_id = $this->id");
|
||||
$effectBonuses = Db::getInstance()->ofetch("
|
||||
select
|
||||
sum(mod_strength) as effect_strength,
|
||||
sum(mod_dexterity) as effect_dexterity,
|
||||
sum(mod_intuition) as effect_intuition,
|
||||
sum(mod_endurance) as effect_endurance,
|
||||
sum(mod_intelligence) as effect_intelligence,
|
||||
sum(mod_wisdom) as effect_wisdom
|
||||
from users_effects where owner_id = $this->id");
|
||||
$obj = (object)[];
|
||||
$obj->strength = max(0,$stats->strength + $itemBonuses->item_strength + $effectBonuses->effect_strength);
|
||||
$obj->dexterity = max(0,$stats->dexterity + $itemBonuses->item_dexterity + $effectBonuses->effect_dexterity);
|
||||
$obj->intuition = max(0,$stats->intuition + $itemBonuses->item_intuition + $effectBonuses->effect_intuition);
|
||||
$obj->endurance = max(0,$stats->endurance + $itemBonuses->item_endurance + $effectBonuses->effect_endurance);
|
||||
$obj->intelligence = max(0,$stats->intelligence + $itemBonuses->item_intelligence + $effectBonuses->effect_intelligence);
|
||||
$obj->wisdom = max(0,$stats->wisdom + $itemBonuses->item_wisdom + $effectBonuses->effect_wisdom);
|
||||
$obj->accuracy = max(0, $itemBonuses->item_accuracy);
|
||||
$obj->evasion = max(0, $itemBonuses->item_evasion);
|
||||
$obj->criticals = max(0, $itemBonuses->item_criticals);
|
||||
$obj->min_physical_damage = max(self::KULAK_MIN_DAMAGE, self::KULAK_MIN_DAMAGE + $itemBonuses->item_min_physical_damage);
|
||||
$obj->max_physical_damage = max(self::KULAK_MAX_DAMAGE, self::KULAK_MAX_DAMAGE + $itemBonuses->item_max_physical_damage);
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function addPoint(string $stat)
|
||||
{
|
||||
$query = "update users set $stat = $stat + 1, free_stat_points = free_stat_points - 1 where " . self::$primaryKey . " = ?";
|
||||
Db::getInstance()->execute($query, $this->id);
|
||||
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$this->columns = [
|
||||
self::$primaryKey => $this->id,
|
||||
'strength' => $this->strength,
|
||||
'dexterity' => $this->dexterity,
|
||||
'intuition' => $this->intuition,
|
||||
'endurance' => $this->endurance,
|
||||
'intelligence' => $this->intelligence,
|
||||
'wisdom' => $this->wisdom,
|
||||
'health' => $this->health,
|
||||
'mana' => $this->mana,
|
||||
'free_stat_points' => $this->free_stat_points,
|
||||
'level' => $this->level,
|
||||
];
|
||||
$this->updateByPrimaryKey();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user