<?php
class AddStats extends CharacterModule
{
    public function Add($Character)
    {
        if ($this->AccountOnline)
        {
            return;
        }
        if (!in_array($Character, Character::GetListCharsInAccount($_SESSION['login']))) {
            Logs::WriteLog("Module: [AddStats], Account: [{$_SESSION['login']}] Nick: [{$Character}]. Fail Add (Nick excluded to account)", 'Modules.log');
            Messages::Add('fail_ADD');
            return false;
        }
        $Config = App::$Configs->mod_AddStats;
        if (!$Config['Enable'])
            return;

        if (property_exists(App::$Post, 'str') && App::$Post->str > 0)
            $str = App::$Post->str;
        else
            $str = 0;

        if (property_exists(App::$Post, 'agi') && App::$Post->agi > 0)
            $agi = App::$Post->agi;
        else
            $agi = 0;

        if (property_exists(App::$Post, 'vit') && App::$Post->vit > 0)
            $vit = App::$Post->vit;
        else
            $vit = 0;

        if (property_exists(App::$Post, 'ene') && App::$Post->ene > 0)
            $ene = App::$Post->ene;
        else
            $ene = 0;

        if (property_exists(App::$Post, 'cmd') && App::$Post->cmd > 0)
            $cmd = App::$Post->cmd;
        else
            $cmd = 0;
        
        $Character = Character::GetActivePlayerInfo($Character);
        $Character = $Character[0];
        $SumStats = $str + $agi + $vit + $ene + $cmd;
        
        if ($SumStats > $Character['LevelUpPoint'] || $SumStats > $Config['MaxStats']){
            Logs::WriteLog("Module: [AddStats], Account [{$_SESSION['login']}]. Error limit add points", 'Modules.log');
            Messages::Add('Ошибка: Допущен предел раскиданных статов');
            return;
        }
        if (App::$DBs[$_SESSION['user_db']]->where('Name', $Character['Name'])
                ->set('Strength', "Strength + " . $str , false)
                ->set('Dexterity', "Dexterity + " . $agi , false)
                ->set('Vitality', "Vitality + " . $vit , false)
                ->set('Energy', "Energy + " . $ene , false)
                ->set('Leadership', "Leadership + " . $cmd , false)
                ->set('LevelUpPoint', "LevelUpPoint - " . $SumStats , false)   
                ->update('Character')
        
        ){

            Logs::WriteLog("Module: [AddStats], Account [{$_SESSION['login']}]. Success add points({$SumStats})", 'Modules.log');
            Messages::Add('success_addstats');
        }
        else{
            Logs::WriteLog("Module: [AddStats], Account [{$_SESSION['login']}]. Error sql query", 'Modules.log');
            Messages::Add('fail_addstats');
        }


    }
}