From d8ccb138731f7a7d771ebd76fbd5edc9d57e3ca5 Mon Sep 17 00:00:00 2001 From: lopar Date: Thu, 27 Jan 2022 00:56:03 +0200 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arena.php | 4 +- classes/Battles/Arena.php | 92 ++++++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/arena.php b/arena.php index 02985da..4b3312a 100644 --- a/arena.php +++ b/arena.php @@ -8,5 +8,5 @@ use Battles\Template; Template::header('Арена'); -$fight = new Arena(1); -$fight->Init([[2,1],[3,2]]); \ No newline at end of file +$dbname = new SQLite3('name.db'); +Arena::$current = new Arena($dbname); \ No newline at end of file diff --git a/classes/Battles/Arena.php b/classes/Battles/Arena.php index cdb0cd7..6d441fb 100644 --- a/classes/Battles/Arena.php +++ b/classes/Battles/Arena.php @@ -6,8 +6,9 @@ use SQLite3; class Arena { + public static Arena $current; private $db; - private const DB_INIT = 'create table if not exists fighters ( + private const DB_TEMPLATE = 'create table if not exists fighters ( uid integer not null, strength integer not null, dexterity integer not null, @@ -26,29 +27,62 @@ class Arena melee_max integer not null, teamid integer not null, rowid integer not null, - last_turn_time integer)'; + turn_timeout integer)'; public const MELEE_ATTACK = 1; public const RANGED_ATTACK = 2; public const USE_MAGIC = 3; public const MOVE = 4; + public const FLEE = 5; public const PASS = 0; + private int $turn_timeout; - public function __construct(int $battleId) + public function __construct(SQLite3 $battleId) { - $dbname = 'battle-' . $battleId; + //Придумать человеческий путь, чтобы не создавалось в папке с классами + $dbname = 'battle-' . $battleId . '.db'; if (!file_exists($dbname)) { - $this->db = new SQLite3($dbname); - $this->db->query(self::DB_INIT); - + $this->createDb($dbname); } } + private function createDb($dbname) + { + $this->db = new SQLite3($dbname); + $this->db->query(self::DB_TEMPLATE); + } + // array type: [[uid1,teamid1], [uid2,teamid2], ..., [uid_N,teamid_N]]. public function Init(array $fighters) { - $init_query = 'insert into fighters (uid, strength, dexterity, intuition, endurance, intelligence, wisdom, accuracy, evasion, criticals, health, max_health, mana, max_mana, melee_min, melee_max, teamid, rowid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; - $stmt = $this->db->prepare($init_query); - foreach ($fighters as list($uid, $team)) { + $init_db_query = 'create table if not exists fighters ( + uid integer not null, + strength integer not null, + dexterity integer not null, + intuition integer not null, + endurance integer not null, + intelligence integer not null, + wisdom integer not null, + accuracy integer not null, + evasion integer not null, + criticals integer not null, + health integer not null, + max_health integer not null, + mana integer not null, + max_mana integer not null, + melee_min integer not null, + melee_max integer not null, + teamid integer not null, + rowid integer not null, + turn_timeout integer)'; + $init_table_query = 'insert into fighters ( + uid, + strength, dexterity, intuition, endurance, intelligence, wisdom, + accuracy, evasion, criticals, + health, max_health, mana, max_mana, melee_min, melee_max, + teamid, rowid) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; + $stmt = $this->db->prepare($init_table_query); + foreach ($fighters as [$uid, $team]) { $fighter = new UserStats($uid); $stats = $fighter->getFullStats(); $stmt->bindValue(1, $fighter->getId()); @@ -73,37 +107,63 @@ class Arena } } - public function Turn(int $action, int $uid): void + public function playerTurn(int $action, int $uid): void { - $now_minus_3_minutes = date('U', strtotime('-3 minute')); - $check_turn_timer = $this->db->querySingle('select last_turn_time from fighters where uid = ' . $uid); + // Перед ходом проверить, а жив ли ты вообще? + if (empty($this->turn_timeout)) { + $this->turn_timeout = $this->db->querySingle('select turn_timeout from fighters where uid = ' . $uid); + } + $now = date('U'); + /* select from last_turn_time and look at $now_plus_3_minutes - if ok, continue, if no, do nothing */ - if ($now_minus_3_minutes > $check_turn_timer && !in_array($action, [self::MELEE_ATTACK, self::RANGED_ATTACK, self::USE_MAGIC, self::MOVE, self::PASS])) { + if ($now > $this->turn_timeout && !in_array($action, [self::MELEE_ATTACK, self::RANGED_ATTACK, self::USE_MAGIC, self::MOVE, self::PASS, self::FLEE])) { $action = self::PASS; $stmt_update_timer = $this->db->prepare('update fighters set last_turn_time = ? where uid = ?'); - $stmt_update_timer->bindValue(1, date('U')); + $stmt_update_timer->bindValue(1, date('U', strtotime('+3 minute'))); $stmt_update_timer->bindValue(2, $uid); } if ($action === self::MELEE_ATTACK) { + //Выполнимо только с клетки 1, только по вражеской клетке 1. + //Выполнимо по клетке 2, если клетка 1 пуста _у всех сторон_; + //Выполнимо по клетке 3, если клетка 2 пуста _у всех сторон_; + //Стоя на клетке 2 при пустой клетке 1 - атака невозможна! echo 'Melee!'; } if ($action === self::RANGED_ATTACK) { + //С клетки 1 атака на вражеские клетки 1 и 2; + //С клетки 2 атака на свою клетку 1 и вражескую клетку 1; + //С клетки 2 атака на вражескую 2, только если пустая клетка 1, либо нет клеток 1 _ни у одной из сторон_. echo 'Ranged!'; } if ($action === self::USE_MAGIC) { + //Достаёт кого угодно откуда угодно в любых обстоятельствах. + //ОЧЕНЬ внимательно проверять цель. Случайный хил трупа вызовёт апокалипсис в логике. echo '!MAGIC!'; } if ($action === self::MOVE) { + //клетка 1 - ближний ряд, только шаг назад + //клетка 2 - средний ряд, вперёд или назад + //клетка 3 - тыл, только вперёд + //В момент хода при соблюдении условий удара может прилететь неблокируемая атака на расстоянии. + //Перемещение - это ручной гарантированный уворот от всех летящих физических атак. + //Перемещение на пустующую клетку 1 с клетки 2 - это ручной гарантированный уворот всех стоящих на клетке 2 от всех летящих немагических атак по всей клетке. echo 'I have legs!!'; } - + if ($action === self::FLEE) { + //побег из боя, только с клетки 3. + echo 'Help me, mommy!'; + } if ($action === self::PASS) { + //Пропуск хода. echo 'I pass this turn.'; } + + // ПИСАТЬ РЕЗУЛЬТАТ ХОДА ТОЛЬКО ПОСЛЕ ПОВТОРНОЙ ПРОВЕРКИ НА НАЛИЧИЕ ПРОТИВНИКА - ОН МОГ УСПЕТЬ ОТОЙТИ! + // !!ИЛИ УМЕРЕТЬ!! $stmt_update_timer->execute(); } } \ No newline at end of file