<?php

/**
 * Created by PhpStorm.
 * User: alex_moor
 * Date: 17.01.2018
 * Time: 17:03
 */


class Invasion {


    public static function getPrepare() {
        return mysql_fetch_assoc(mysql_query("SELECT * FROM `demons_battles` WHERE `status` = 'prepare' ORDER BY `id` DESC LIMIT 1"));
    }

    /**
     * Есть ли битва, которая ожидает начало
     * @return string
     */
    public static function getCountPrepare() {
        return mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_battles` WHERE `status` = 'prepare' ORDER BY `id` DESC LIMIT 1"), 0);
    }

    public static function getBattled() {
        return mysql_fetch_assoc(mysql_query("SELECT * FROM `demons_battles` WHERE `status` = 'battle' ORDER BY `id` DESC LIMIT 1"));
    }

    public static function getCountBattled() {
        return mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_battles` WHERE `status` = 'battle' ORDER BY `id` DESC LIMIT 1"), 0);
    }

    public static function getResulted() {
        return mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_battles` WHERE `status` = 'end'"), 0);
    }

    /**
     * Стаю ли я в очереди или нет
     * @param $id
     * @param $battleId
     * @return string
     */
    public static function getMyBattlePrepare($id, $battleId) {
        return mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_players` WHERE `id_user` = '" . intval($id) . "' AND `battle` = '" . intval($battleId). "'"), 0);
    }

    /**
     * Игроков записано
     * @param $battleId
     * @return string
     */
    public static function getUserBattlePrepare($battleId) {
        return mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_players` WHERE `battle` = '" . intval($battleId). "'"), 0);
    }

    public static function getMyBattle($user) {
        return mysql_fetch_assoc(mysql_query("SELECT * FROM `demons_players` WHERE `id_user` = '" . intval($user). "'"));
    }

    public static function redirect($location) {
        header("Location: " . $location);
        exit();
    }

    public static function getUserCheck($user) {
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_players` WHERE `id_user` = '" . intval($user). "'"), 0) == true) {
            return true;
        }
        return false;
    }

    public static function getStage($stage) {
        if (in_array($stage, ['mob1','mob2','mob3'])) {
            return [
                'name' => 'Помошник демона',
                'str' => 2000,
                'def' => 2000,
                'img' => '/invasion/mob_min.png'
            ];
        } else {
            return [
                'name' => 'Демон',
                'str' => 4500,
                'def' => 4500,
                'img' => '/invasion/demon_min.png'
            ];
        }
    }

    public static function getMyDamage($ability, $user, $mobs) {
        $crit = 10;
        $ChanseCrit = rand(1, 100);
        $dmg = ceil(($user['str'] / 4) - ($mobs['def'] / 8));
        $dmg += rand(1, 10);

        // Крит
        $log_crit = false;
        if ($ChanseCrit <= $crit) {
            $log_crit = true;
            $dmg *= 2;

        }

        $SpellStr = 0;
        if ($ability['statusStr'] == 1) {
            $SpellStr = ceil($dmg + ($ability['spellStrDmg'] + ($user['str'] / 15)));
        }
        $dmg += $SpellStr;
        if ($dmg < 0) {
            $dmg = rand(1, 10);
        }
        return $dmg;
    }

    public static function getAttackStr($ability, $user, $stage, $healthBase) {
        $mobs = self::getStage($stage);

        // Урон
        $dmg = self::getMyDamage($ability, $user, $mobs);


        $dmg2 = ceil(($mobs['str'] / 3) - ($user['def'] / 12));
        $dmg2 += rand(1, 10);


        if ($ability['statusDef'] == 1) {
            $dmg2 = ceil($dmg2 - ($ability['spellDefAbsorb'] + ($user['def'] / 20)));
        }


        if ($dmg2 < 0) {
            $dmg2 = rand(1, 10);
        }

        mysql_query("UPDATE `demons_players` SET `damage` = `damage` + '" . $dmg . "', `time_dmg` = '" . (time() + 4) . "', `health` = `health` - '" . $dmg2 . "' WHERE `id_user` = '" . $user['id'] . "' LIMIT 1");
        mysql_query("UPDATE `demons_battles` SET `" . $healthBase . "` = `" . $healthBase . "` - '" . $dmg . "'");

        $log = null;
        // Убил ли или нет
        if (mysql_result(mysql_query("SELECT COUNT(*) FROM `demons_players` WHERE `id_user` = '" . $user['id'] . "' AND (`health` = '" . $dmg2 . "' OR `health` < '" . $dmg2 . "')"), 0) == true) {
            $log .= '<font color = red>Вы были убиты!</font><br>';
        }

        $log .= '<font color = green> Вы ударили ' . $mobs['name'] . ' на  ' . $dmg . ' </font> ' . color('white') . '' . (isset($log_crit) ? '(крит)</font>' : '</font>') . '<br>
 <font color = red> ' . $mobs['name'] . '  ударил Вас на  ' . $dmg2 . ' </font>';
        $_SESSION['log'] = $log;

    }

    public static function getUserBattle($id, $my, $battle) {
        $query = mysql_query("SELECT demons_players.id_user, users.login, demons_players.damage FROM demons_players, users WHERE demons_players.battle = '" . $battle . "' AND users.id = demons_players.id_user ORDER BY demons_players.damage DESC LIMIT 10");
        $array = [];
        $i = 1;
        while($row = mysql_fetch_assoc($query)) {
            $array[$i] = $row;
            $i ++;
        }
        $array[] = $my;
        return $array;
    }

}