Костыли и велосипеды!

This commit is contained in:
Igor Barkov (iwork) 2022-01-25 19:59:18 +02:00
parent b1ba212c8c
commit e4cce2e96e
2 changed files with 121 additions and 0 deletions

12
arena.php Normal file
View File

@ -0,0 +1,12 @@
<?php
/**
* Арена. Принцип: https://src.liks.pw/lopar/battles-game-test/issues/3
*/
use Battles\Arena;
use Battles\Template;
Template::header('Арена');
$fight = new Arena(1);
$fight->Init([[2,1],[3,2]]);

109
classes/Battles/Arena.php Normal file
View File

@ -0,0 +1,109 @@
<?php
namespace Battles;
use SQLite3;
class Arena
{
private $db;
private const DB_INIT = '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,
last_turn_time integer)';
public const MELEE_ATTACK = 1;
public const RANGED_ATTACK = 2;
public const USE_MAGIC = 3;
public const MOVE = 4;
public const PASS = 0;
public function __construct(int $battleId)
{
$dbname = 'battle-' . $battleId;
if (!file_exists($dbname)) {
$this->db = new SQLite3($dbname);
$this->db->query(self::DB_INIT);
}
}
// 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)) {
$fighter = new UserStats($uid);
$stats = $fighter->getFullStats();
$stmt->bindValue(1, $fighter->getId());
$stmt->bindValue(2, $stats->strength);
$stmt->bindValue(3, $stats->dexterity);
$stmt->bindValue(4, $stats->intuition);
$stmt->bindValue(5, $stats->endurance);
$stmt->bindValue(6, $stats->intelligence);
$stmt->bindValue(7, $stats->wisdom);
$stmt->bindValue(8, $stats->accuracy);
$stmt->bindValue(9, $stats->evasion);
$stmt->bindValue(10, $stats->criticals);
$stmt->bindValue(11, $fighter->getHealth());
$stmt->bindValue(12, $fighter->getMaxHealth());
$stmt->bindValue(13, $fighter->getMana());
$stmt->bindValue(14, $fighter->getMaxMana());
$stmt->bindValue(15, $stats->min_physical_damage);
$stmt->bindValue(16, $stats->min_physical_damage);
$stmt->bindValue(17, $team);
$stmt->bindValue(18, 1);
$stmt->execute();
}
}
public function Turn(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);
/* 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])) {
$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(2, $uid);
}
if ($action === self::MELEE_ATTACK) {
echo 'Melee!';
}
if ($action === self::RANGED_ATTACK) {
echo 'Ranged!';
}
if ($action === self::USE_MAGIC) {
echo '!MAGIC!';
}
if ($action === self::MOVE) {
echo 'I have legs!!';
}
if ($action === self::PASS) {
echo 'I pass this turn.';
}
$stmt_update_timer->execute();
}
}