<?php
class pit
{
// const PLAYER_FREE_AP = 10; // Бесплатное кормление
// узнать количество очков силы
public static function get_satiety($id_user)
{
$sql = 'select satiety from pit where id_user = ? limit 1';
$sth = db::$pdo->prepare($sql);
$sth->execute([$id_user]);
return $sth->fetch(PDO::FETCH_NUM)[0];
}
// создать запись с игроком
public static function player_create($id_user)
{
$sql = 'insert into pit set id_user = ?';
$sth = db::$pdo->prepare($sql);
$sth->execute([$id_user]);
}
// проверить есть ли запись с игроком
public static function is_player($id_user)
{
$sql = 'select count(*) from pit where id_user = ? limit 1';
$sth = db::$pdo->prepare($sql);
$sth->execute([$id_user]);
if ($sth->fetch(PDO::FETCH_NUM)[0] > 0) {
return true;
}
return false;
}
// добавить очки силы
public static function set_satiety($num, $id_user)
{
$sql = 'update pit set satiety = satiety + ? where id = ? limit 1';
$sth = db::$pdo->prepare($sql);
$sth->execute([$num, $id_user]);
}
// контроллер игрока
public static function player_ctrl()
{
$id_user = ID;
if (!self::is_player($id_user)) { // если записи с игроком нет
self::player_create($id_user);
}
}
// контроллер кормления
public static function satiety_ctrl($korm)
{
$id_user = ID;
if (get_mutagen_by_id($id_user) >= $korm ) {//Если достаточно мутагена
self::set_satiety($korm, $id_user);
set_mutagen(- $korm,$id_user);
}
}
}
class pit_model
{
public function get()
{
$satiety = pit::get_satiety(ID);
return [
'satiety' => $satiety
];
}
}
<?php
require_once(ROOT . 'project/internal/core.php'); // грузим ядро
pit::player_ctrl();
$act = '';
if ( ! empty($_GET['korm'])) {
$act = $_GET['korm'];
}
if ($act == '1' || $act == '5' || $act =='15') {
pit::satiety_ctrl($act);
// что бы при перезагркзеи страницы не повторялся код
redirect::go()->ruler_cut('korm');
}
view('tpl/cap');
view('tpl/header');
view('tpl/header-enter');
view('pit', (new pit_model)->get());
view('tpl/footer-enter');
view('tpl/footer');
class pit_model
{
public function get()
{
$satiety = pit::get_satiety(ID);
return [
'satiety' => $satiety
];
}
}
function get_pit($id)
{
return $id;
}