109 lines
3.9 KiB
PHP
109 lines
3.9 KiB
PHP
|
<?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();
|
||
|
}
|
||
|
}
|