<?php
class Character
{
    public static function GetActivePlayerInfo($Nick = null, $Account = null)
    {
        
        $res = 0;
        if ($Nick == null && $Account == null)
        {
            $res = App::$DBs[$_SESSION['user_db']]
                ->select(array('Name', 'cLevel', App::$DBs[$_SESSION['user_db']]->column_names['Resets'], App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'], 'LevelUpPoint', 'Strength', 'Dexterity', 'Vitality', 'Energy', 'Leadership', 'HideInfoTime'))
                ->from('Character')
                ->where('AccountID = ', $_SESSION['login'])
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'],'DESC')
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['Resets'],'DESC')
                ->order_by('cLevel','DESC')
                ->get();
        }
        else if ($Nick != null)
        {
            $res = App::$DBs[$_SESSION['user_db']]
                ->select(array('Name', 'cLevel', App::$DBs[$_SESSION['user_db']]->column_names['Resets'], App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'], 'LevelUpPoint', 'Strength', 'Dexterity', 'Vitality', 'Energy', 'Leadership', 'HideInfoTime'))
                ->from('Character')
                ->where('Name = ', $Nick)
                ->where('AccountID = ', $_SESSION['login'])
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'],'DESC')
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['Resets'],'DESC')
                ->order_by('cLevel','DESC')
                ->get();
            
        }
        else if ($Account != null)
        {
            $res = App::$DBs[$_SESSION['user_db']]
                ->select(array('Name', 'cLevel', App::$DBs[$_SESSION['user_db']]->column_names['Resets'], App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'], 'LevelUpPoint', 'Strength', 'Dexterity', 'Vitality', 'Energy', 'Leadership', 'HideInfoTime'))
                ->from('Character')
                ->where('AccountID = ', $Account)
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'],'DESC')
                ->order_by(App::$DBs[$_SESSION['user_db']]->column_names['Resets'],'DESC')
                ->order_by('cLevel','DESC')
                ->get();
        }
        else
        {
            return false;
        }
        
        $ResultArr = $res->result_array();

        //Replace to standart index
        if (App::$DBs[$_SESSION['user_db']]->column_names['Resets'] != 'Resets')
            $ResultArr[0]['Resets'] = $ResultArr[0][App::$DBs[$_SESSION['user_db']]->column_names['Resets']];
        if (App::$DBs[$_SESSION['user_db']]->column_names['GrandResets'] != 'GrandResets')
            $ResultArr[0]['GrandResets'] = $ResultArr[0][App::$DBs[$_SESSION['user_db']]->column_names['GrandResets']];
        
        if ($res->num_rows())
        {
            $ResultArr['Valutes'] = ValuteManager::GetAllValutes($_SESSION['user_db']);
            $_SESSION['active_char'] = $ResultArr[0]['Name'];
            $_SESSION['ActivePlayer'] = $ResultArr[0]['Name'];
           
            return $ResultArr;
        }
        else{
            if ($Account == null) {
                Messages::Add('character_not_found');
            }
            return false;
        }
    }
    public static function GetClassName($classnum){
        switch($classnum)
        {
            case 0:$classnum = "Dark Wizard";break;     case 16:$classnum = "Dark Knight";break;
            case 1:$classnum = "Soul Master";break;     case 17:$classnum = "Blade Knight";break;
            case 2:$classnum = "Grand Master";break;    case 18:$classnum = "Blade Master";break;
            case 3:$classnum = "Grand Master";break;    case 19:$classnum = "Blade Master";break;

            case 32:$classnum = "Fairy Elf";break;      case 48:$classnum = "M. Gladiator";break;
            case 33:$classnum = "Muse Elf";break;       case 49:$classnum = "Duel Master";break;
            case 34:$classnum = "High Elf";break;       case 50:$classnum = "Duel Master";break;
            case 35:$classnum = "High Elf";break;

            case 64:$classnum = "Dark Lord";break;      case 80:$classnum = "Summoner";break;
            case 65:$classnum = "Lord Emperor";break;   case 81:$classnum = "B. Summoner";break;
            case 66:$classnum = "Lord Emperor";break;   case 82:$classnum = "Dimension Master";break;
            case 83:$classnum = "Dimension Master";break;
            case 96:$classnum = "Rage Fighter";break;
            case 97:$classnum = "Fist Master";break;
            case 98:$classnum = "Fist Master";break;
            default:$classnum="unknown";break;
        }
        return $classnum;
    }
    public static function GetClassNameShort($classnum){
        switch($classnum)
        {
            case 0:$classnum = "DW";break;		case 16:$classnum = "DK";break;
            case 1:$classnum = "SM";break;		case 17:$classnum = "BK";break;
            case 2:$classnum = "GM";break;	case 18:$classnum = "BM";break;
            case 3:$classnum = "GM";break;	case 19:$classnum = "BM";break;

            case 32:$classnum = "FE";break;		case 48:$classnum = "MG";break;
            case 33:$classnum = "ME";break;		case 49:$classnum = "DM";break;
            case 34:$classnum = "HE";break;		case 50:$classnum = "DM";break;
            case 35:$classnum = "HE";break;

            case 64:$classnum = "DL";break;		case 80:$classnum = "SUM";break;
            case 65:$classnum = "LE";break;	case 81:$classnum = "BS";break;
            case 66:$classnum = "LE";break;	case 82:$classnum = "DS";break;
            case 83:$classnum = "DM";break;
            case 96:$classnum = "RF";break;
            case 97:$classnum = "FM";break;
            case 98:$classnum = "FM";break;
            default:$classnum="unknown";break;
        }
        return $classnum;
    }
    public static function GetImageCharacter($Class)
    {
        switch ($Class) {
            case 0:
                $classnum = "/web/templates/default/images/Characters/sm.png";
                break;
            case 1:
                $classnum = "/web/templates/default/images/Characters/sm.png";
                break;
            case 2:
                $classnum = "/web/templates/default/images/Characters/sm.png";
                break;
            case 3:
                $classnum = "/web/templates/default/images/Characters/sm.png";
                break;

            case 16:
                $classnum = "/web/templates/default/images/Characters/bk.png";
                break;
            case 17:
                $classnum = "/web/templates/default/images/Characters/bk.png";
                break;
            case 18:
                $classnum = "/web/templates/default/images/Characters/bk.png";
                break;
            case 19:
                $classnum = "/web/templates/default/images/Characters/bk.png";
                break;

            case 32:
                $classnum = "/web/templates/default/images/Characters/me.png";
                break;
            case 33:
                $classnum = "/web/templates/default/images/Characters/me.png";
                break;
            case 34:
                $classnum = "/web/templates/default/images/Characters/me.png";
                break;
            case 35:
                $classnum = "/web/templates/default/images/Characters/me.png";
                break;
            case 48:
                $classnum = "/web/templates/default/images/Characters/mg.png";
                break;
            case 49:
                $classnum = "/web/templates/default/images/Characters/mg.png";
                break;
            case 50:
                $classnum = "/web/templates/default/images/Characters/mg.png";
                break;

            case 64:
                $classnum = "/web/templates/default/images/Characters/dl.png";
                break;
            case 65:
                $classnum = "/web/templates/default/images/Characters/dl.png";
                break;
            case 66:
                $classnum = "/web/templates/default/images/Characters/dl.png";
                break;
            case 80:
                $classnum = "/web/templates/default/images/Characters/su.png";
                break;
            case 81:
                $classnum = "/web/templates/default/images/Characters/su.png";
                break;
            case 82:
                $classnum = "/web/templates/default/images/Characters/su.png";
                break;
            case 83:
                $classnum = "/web/templates/default/images/Characters/su.png";
                break;
            case 96:
                $classnum = "/web/templates/default/images/Characters/rf.png";
                break;
            case 97:
                $classnum = "/web/templates/default/images/Characters/rf.png";
                break;
            case 98:
                $classnum = "/web/templates/default/images/Characters/rf.png";
                break;
        }
        return $classnum;
    }
    public static function EmptyInventory($Character)
    {
        $inventory = $Character['Inventory'];
        $inventory = strtoupper(bin2hex($inventory));

        $count_f = 0;
        $inv_f = str_split($inventory);
        for ($i=0; $i < count($inv_f); $i++) { 
            if ($inv_f[$i] == 'F') {
                $count_f++;
            }
        }
        if ($count_f == strlen($inventory)) {
            return true;
        }

        for($i=0 ; $i < (strlen($inventory)/32);$i++){
            $n_items = substr($inventory,($i*32), 32);
            if ($n_items != "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"){
                return false;
            }
        }
        return true;
    }
    public static function PkLevel($Level)
    {
        switch ($Level) {
            case 1:
            case 2:
                return "Герой";
                break;
            case 3:
                return "Обычный";
                break;
            case 4:
                return "Подозреваемый";
                break;
            case 5:
                return "Убийца";
                break;
            case 6:
                return "Маньяк";
                break;
        }
    }
    public static function GetListCharsInAccount($Acc)
    {
        $Array = App::$DBs[$_SESSION['user_db']]->select()->from('AccountCharacter')->where('ID', $_SESSION['login'])->get()->result_array();
        $chars = array();
        for ($i=1; $i <= 5; $i++) {
            $chars[] = $Array[0]['GameID'.$i];
        }
        return $chars;
    }
}