Файл: pages/progress/quest.php
Строк: 197
<?php
declare(strict_types=1);
// Physical module map v50.7: public URL stays clean while code lives in /pages.
if (!defined('GAME_ROOT')) define('GAME_ROOT', dirname(__DIR__, 2));
chdir(GAME_ROOT);
if (PHP_SAPI !== 'cli') { $_SERVER['PHP_SELF']='/quest.php'; $_SERVER['SCRIPT_NAME']='/quest.php'; }
require_once './system/common.php';
require_once './system/functions.php';
require_once './system/user.php';
require_once './system/menu.php';
if(!$user){ header('Location:/'); exit; }
$uid=(int)$user['id'];
$title='Задания';
game_quest_seed_v49();
game_daily_quest_ids($uid); // exactly 10 tasks for the current day
if(($_SERVER['REQUEST_METHOD']??'GET')==='POST'){
security_require_csrf();
if(isset($_POST['open_star_chest'])){
$res=game_star_chest_open($uid); $_SESSION[$res['ok']?'ok':'err']=$res['msg'];
header('Location:/quest/'); exit;
}
if(isset($_POST['complete_quest'])){
$qid=max(0,(int)$_POST['complete_quest']);
try{
$pdo=db_pdo(); $pdo->beginTransaction(); $day=game_daily_quest_day_key();
$row=db_fetch_one('SELECT uq.*,q.`title`,q.`c` goal,q.`_gold`,q.`_plat`,q.`_exp` FROM `daily_quest_assignment` a JOIN `user_q` uq ON uq.`user`=a.`user` AND uq.`q`=a.`quest_id` JOIN `quest` q ON q.`id`=a.`quest_id` WHERE a.`user`=:u AND a.`day_key`=:d AND a.`quest_id`=:q LIMIT 1 FOR UPDATE',['u'=>$uid,'d'=>$day,'q'=>$qid]);
if(!$row || (int)$row['complete']!==0) throw new RuntimeException('Это задание не входит в сегодняшнюю десятку или уже получено.');
if((int)$row['c']<(int)$row['goal']) throw new RuntimeException('Задание ещё не выполнено.');
$gold=min(10,max(0,(int)$row['_gold'])); $plat=min(1,max(0,(int)$row['_plat'])); $exp=min(5000,max(0,(int)$row['_exp']));
db_execute('UPDATE `user_q` SET `complete`=1,`time`=0 WHERE `user`=:u AND `q`=:q',['u'=>$uid,'q'=>$qid]);
db_execute('UPDATE `users` SET `plat`=`plat`+:p,`exp`=`exp`+:e,`quests`=`quests`+1 WHERE `id`=:u',['p'=>$plat,'e'=>$exp,'u'=>$uid]);
$goldGranted=$gold>0 ? game_economy_award_gold($uid,$gold,'daily_quest',150) : 0;
$drop=(date('N')<=5) ? game_award_daily_relic($uid) : null;
$pdo->commit();
$_SESSION['ok']='Задание выполнено. Награда: '.($goldGranted?'<img src="/images/icon/gold.png" alt=""> '.n_f($goldGranted).' золота ':'').($plat?'<img src="/images/icon/crys/diamond.png" alt=""> '.n_f($plat).' крист. ':'').($exp?'<img src="/images/icon/exp.png" alt=""> '.n_f($exp).' опыта ':'').($drop?'<img src="/images/icon/relic.png" alt=""> +1 реликвия':'');
}catch(Throwable $e){ if(db_pdo()->inTransaction()) db_pdo()->rollBack(); $_SESSION['err']=$e->getMessage(); }
header('Location:/quest/'); exit;
}
}
require './system/h.php';
if(!empty($_SESSION['ok'])){ echo '<div class="ok center">'.security_system_text((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']); }
?>
<div class="title">Задания</div>
<?php $dailyDone=game_daily_quest_completed($uid); $nextDay=strtotime('tomorrow 00:00:00'); ?><div class="block_zero center blue">Ежедневно доступно ровно <b>10 заданий</b>. Прогресс боёв засчитывается сразу.<br><span class="green">Выполнено сегодня: <?=n_f($dailyDone)?> из 10</span><br><span class="grey">Новые задания через <span data-live-until="<?=$nextDay?>" data-live-format="short"><?=_time(max(0,$nextDay-time()))?></span></span></div>
<?php
$sc=game_star_chest_state($uid); $thr=[6420,21400]; $rw=[10,25]; $st=(int)$sc['sunduk']; $en=(int)$sc['energy']; $next=max(0,(int)$sc['time']-time());
echo '<div class="star-chests center"><div class="blue">Используй в боях энергию, чтобы открыть сундуки</div><div class="medium">Потрачено энергии: <b>'.n_f($en).'</b> · Звездная пыль: <b>'.n_f((int)$sc['dust']).'</b></div>';
for($ci=0;$ci<2;$ci++){
$opened=$st>$ci; $activeChest=$st===$ci; $can=$activeChest&&$en>=$thr[$ci]; $cls=$opened?'opened':($activeChest?'current':'locked');
$img=$opened?'/images/chest_open.png':'/images/chest.png'; if(!is_file(GAME_ROOT.$img)) $img='/images/chest.png';
echo '<div class="star-chest '.$cls.'"><img src="'.$img.'" alt=""><div>'.($opened?'Открыт':n_f(min($en,$thr[$ci])).' / '.n_f($thr[$ci])).'</div><div class="medium">Награда: '.$rw[$ci].' звездной пыли</div>'.($can?'<form method="post" action="/quest/" class="inline-form">'.security_csrf_field().'<input type="hidden" name="open_star_chest" value="1"><button class="btn" type="submit"><span class="end"><span class="label">Открыть</span></span></button></form>':'').'</div>';
}
echo '<div class="medium">Новые сундуки через: <span data-live-until="'.(int)$sc['time'].'" data-live-format="short">'._time($next).'</span></div></div><div class="mini-line"></div>';
$rows=game_daily_quest_rows($uid);
$routes=game_quest_routes();
if(!$rows) echo '<div class="block_zero center grey">Не удалось сформировать ежедневные задания.</div>';
foreach($rows as $q){
$progress=max(0,(int)($q['progress']??0)); $goal=max(1,(int)$q['goal']); $done=(int)($q['complete']??0)===1; $ready=!$done&&$progress>=$goal; $route=$routes[(int)$q['place']]??'/fights/';
$sep=str_contains($route,'?')?'&':'?'; $route.=$sep.'quest='.(int)$q['q'];
echo '<section class="quest-card-v507'.($ready?' is-complete':'').($done?' is-claimed':'').'">';
echo '<div class="quest-card-v507__title"><img src="/images/icon/quest.png" alt=""> <b>'.security_html((string)$q['title']).'</b>'.($ready?' <span class="green">(+)</span>':'').($done?' <span class="grey">✓ награда получена</span>':'').'</div>';
echo '<div class="quest-card-v507__text">'.security_html((string)$q['description']).'</div>';
echo game_progress_markup($progress,$goal);
echo '<div class="quest-card-v507__reward"><span class="blue">Награда:</span> '.((int)$q['_gold']>0?'<img src="/images/icon/gold.png" alt=""> '.n_f((int)$q['_gold']).' ':'').((int)$q['_plat']>0?'<img src="/images/icon/crys/diamond.png" alt=""> '.n_f((int)$q['_plat']).' ':'').((int)$q['_exp']>0?'<img src="/images/icon/exp.png" alt=""> '.n_f((int)$q['_exp']):'').'</div>';
if($ready){
echo '<form method="post" action="/quest/" class="center">'.security_csrf_field().'<button class="btn" type="submit" name="complete_quest" value="'.(int)$q['q'].'"><span class="end"><span class="label">Получить награду</span></span></button></form>';
}elseif(!$done){
echo '<div class="center"><a class="btn" href="'.security_html($route).'"><span class="end"><span class="label">Перейти к выполнению</span></span></a></div>';
}
echo '</section>';
}
require './system/f.php';