Вход Регистрация
Файл: pages/economy/petshop.php
Строк: 160
<?php
/* Питомцы v50.7 — список и магазин. by Метриум (Стэлп) */
declare(strict_types=1);
if (!
defined('GAME_ROOT')) define('GAME_ROOT'dirname(__DIR__2));
chdir(GAME_ROOT);
if (
PHP_SAPI !== 'cli') { $_SERVER['PHP_SELF']='/petshop.php'$_SERVER['SCRIPT_NAME']='/petshop.php'; }
require_once 
'./system/common.php';
require_once 
'./system/functions.php';
require_once 
'./system/user.php';
require_once 
'./system/pets_v507.php';
if(!
$user){header('Location:/');exit;}
pet_v507_bootstrap();
$title='Питомцы'$uid=(int)$user['id'];

if((
$_SERVER['REQUEST_METHOD']??'GET')==='POST'){
    try{
        
security_require_csrf(); $act=(string)($_POST['act']??'');
        if(
$act==='buy'){
            
$pid=max(0,(int)($_POST['pet_id']??0));
            
$pet=db_fetch_one('SELECT * FROM `pet` WHERE `id`=:p LIMIT 1',['p'=>$pid]);
            if(!
$pet) throw new RuntimeException('Питомец не найден.');
            if(
db_fetch_one('SELECT `id` FROM `pet_u` WHERE `user_id`=:u AND `pet_id`=:p LIMIT 1',['u'=>$uid,'p'=>$pid])) throw new RuntimeException('Этот питомец уже куплен.');
            
$name=trim((string)($_POST['name']??$pet['name']));
            if(
mb_strlen($name,'UTF-8')<2||mb_strlen($name,'UTF-8')>16) throw new RuntimeException('Прозвище питомца: от 2 до 16 символов.');
            
$pdo=db_pdo(); $pdo->beginTransaction();
            try{
                
$pay=db_prepare('UPDATE `users` SET `g`=`g`-:c,`str`=`str`+:str,`vit`=`vit`+:vit,`agi`=`agi`+:agi,`def`=`def`+:def,`mana`=`mana`+:mana WHERE `id`=:u AND `g`>=:c2',[
                    
'c'=>(int)$pet['cena'],'str'=>(int)$pet['str'],'vit'=>(int)$pet['vit'],'agi'=>(int)$pet['agi'],'def'=>(int)$pet['def'],'mana'=>(int)$pet['mana'],'u'=>$uid,'c2'=>(int)$pet['cena']
                ]);
                if(
$pay->rowCount()!==1) throw new RuntimeException('Не хватает золота.');
                
db_execute('INSERT INTO `pet_u` (`pet_id`,`user_id`,`name`,`screen`,`level`,`lvl_str`,`lvl_vit`,`lvl_agi`,`lvl_def`,`lvl_mana`,`vit`,`str`,`agi`,`def`,`mana`) VALUES (:p,:u,:n,:sc,1,1,1,1,1,1,:vit,:str,:agi,:def,:mana)',[
                    
'p'=>$pid,'u'=>$uid,'n'=>$name,'sc'=>(string)$pet['screen'],'vit'=>(int)$pet['vit'],'str'=>(int)$pet['str'],'agi'=>(int)$pet['agi'],'def'=>(int)$pet['def'],'mana'=>(int)$pet['mana']
                ]);
                
$newId=(int)$pdo->lastInsertId(); $pdo->commit();
                
$_SESSION['ok']='Питомец куплен и добавлен в список.'header('Location:/pet/'.$newId.'/');exit;
            }catch(
Throwable $e){if($pdo->inTransaction())$pdo->rollBack();throw $e;}
        }
    }catch(
Throwable $e){$_SESSION['err']=$e->getMessage();}
    
header('Location:/petshop/');exit;
}
require 
'./system/h.php';
if(!empty(
$_SESSION['ok'])){echo '<div class="ok center">'.security_html(strip_tags((string)$_SESSION['ok'])).'</div>';unset($_SESSION['ok']);}
if(!empty(
$_SESSION['err'])){echo '<div class="ok center">'.security_html((string)$_SESSION['err']).'</div>';unset($_SESSION['err']);}
$owned=[]; foreach(db_prepare('SELECT * FROM `pet_u` WHERE `user_id`=:u ORDER BY `level` DESC,`id` ASC',['u'=>$uid])->fetchAll() as $r)$owned[(int)$r['pet_id']]=$r;
?>
<div class="pets-list-v507">
 <div class="center blue pets-list-v507__intro">Все купленные питомцы навсегда усиливают героя. Максимальный уровень каждого питомца — 80.</div>
 <div class="pet-banner"><img src="/images/town/hd/petshop.jpg" alt="Питомцы"></div>
 <div class="title">Мои питомцы</div>
 <?php if(!$owned):?><div class="block_zero center grey">У вас пока нет питомцев.</div><?php endif;?>
 <?php foreach($owned as $pu): $lvl=pet_v507_level($pu); ?>
   <a class="pet-list-row-v507" href="/pet/<?=(int)$pu['id']?>/">
     <img class="pet-list-row-v507__img" src="/images/pet/mini/<?=security_html((string)$pu['screen'])?>" alt="">
     <span class="pet-list-row-v507__main"><b><?=security_html((string)$pu['name'])?></b><small><img src="/images/icon/level.png" alt=""> <?=$lvl?> уровень</small><span class="green">+<?=n_f(pet_v507_total($pu))?> к параметрам</span></span>
     <span class="pet-list-row-v507__arrow">›</span>
   </a>
 <?php endforeach; ?>
 <div class="title">Магазин питомцев</div>
 <?php foreach(db_prepare('SELECT * FROM `pet` ORDER BY `id`')->fetchAll() as $p): $has=isset($owned[(int)$p['id']]); ?>
   <div class="pet-shop-row-v507">
     <img class="pet-list-row-v507__img" src="/images/pet/mini/<?=security_html((string)$p['screen'])?>" alt="">
     <div class="pet-list-row-v507__main"><b><?=security_html((string)$p['name'])?></b><span class="green">+<?=n_f((int)$p['str']+(int)$p['vit']+(int)$p['agi']+(int)$p['def'])?> к параметрам</span><small>Цена: <img src="/images/icon/gold.png" alt=""> <?=n_f((int)$p['cena'])?> золота</small></div>
     <?php if($has):?><a class="old-btn" href="/pet/<?=(int)$owned[(int)$p['id']]['id']?>/">Открыть</a><?php else:?><form method="post" class="pet-buy-form-v507"><?=security_csrf_field()?><input type="hidden" name="act" value="buy"><input type="hidden" name="pet_id" value="<?=(int)$p['id']?>"><input name="name" maxlength="16" value="<?=security_html((string)$p['name'])?>"><button class="old-btn" type="submit">Купить</button></form><?php endif;?>
   </div>
 <?php endforeach; ?>
</div>
<?php require './system/f.php';
Онлайн: 3
Реклама