<?php

	//Security
	if( ! defined( 'IS_INIT' ) )
	{
		die( 'Unauthorized Access');
	}
	
class characters 
{
	private $Handler;
	private $user;
        public $charinfo = NULL;
	
	function __construct( $user ) 
	{
		$this->user = $user;
		
		global $Handler;
		
		$this->Handler = $Handler;
	}
	
	public function FetchCharName()
	{
		$string = null;
		$do = sqlsrv_prepare( $this->Handler->database->conn , "Select sID from World00_Character.dbo.tCharacter where nUserNo = ? and bDeleted = 0" , array( &$this->user ) );
		sqlsrv_execute( $do );
		if( sqlsrv_has_rows( $do )  == true )
		{
			while( $ret = sqlsrv_fetch_array( $do , SQLSRV_FETCH_ASSOC ) )
			{
				$string .= '<a href="index.php?view=cp&do=characters&char=' . htmlentities($ret['sID']) . '"> ' . htmlentities($ret['sID']) .' </a>';
			}
			return $string;
		}	
	}
	
	public function HasChars()
	{
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select sID from World00_Character.dbo.tCharacter where nUserNo = ? and bDeleted = 0" , array( &$this->user ) );
            sqlsrv_execute( $do );
            if( sqlsrv_has_rows( $do )  == true )
            {
                    return true;
            }
            else
            {
                    return false;
            }
	}
	
        public function CharExists( $char )
        {
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select * from World00_character.dbo.tCharacter where sID = ? and nUserNo = ? and bDeleted = 0" , array( &$char , &$this->user ) );
            sqlsrv_execute( $do );
            if( sqlsrv_has_rows( $do )  == true )
            {
                    $this->charinfo = sqlsrv_fetch_array( $do , SQLSRV_FETCH_ASSOC );
                    return true;
            }
            else
            {
                    return false;
            } 
        }
        
        public function FetchAllInfo( $char = null )
        {
            if( $char == null )
            {
                $char = $this->charinfo['nCharNo'];
            }
            
            $loggedIn = sqlsrv_prepare( $this->Handler->database->conn , "Select nCharNo from World00_Character.dbo.LoggedInChars where nCharNo = ?" , array( &$char ) );
            sqlsrv_execute( $loggedIn );
            if( sqlsrv_has_rows( $loggedIn ) )
            {
                $this->charinfo = array_merge( $this->charinfo , array( "Logged" => "true" ) );
                //print_r( $this->charinfo );
            }
            else
            {
                $this->charinfo =  array_merge( $this->charinfo , array( "Logged" => "false" ) );
                //print_r( $this->charinfo );
            }
            
            $getGuild = sqlsrv_prepare( $this->Handler->database->conn , "Select * from World00_Character.dbo.tGuildMember where nCharNo = ? " , array( &$this->charinfo['nCharNo'] ) );
            sqlsrv_execute( $getGuild );
            if( sqlsrv_has_rows( $getGuild ) )
            {
                $guildres = sqlsrv_fetch_array( $getGuild , SQLSRV_FETCH_ASSOC );
                $guildinfo = $this->GetGuild( $guildres['nNo'] );
                $guildinfo['nGuildGrade'] = $guildinfo['nGrade'];
                unset( $guildinfo['nGrade'] );
                $this->charinfo['Guild'] = $guildinfo['sName'];
                $guild = array_merge( $guildinfo , $guildres );
            }
            else
            {
                $guild = array( "Guild" => null , "sName" => "-" , "nGrade" => '-' );
            }
            $this->charinfo = array_merge( $this->charinfo , $guild );
            
            if( $this->charinfo['Guild'] <> null )
            {
                switch( $this->charinfo['nGrade'] )
                {
                    default:
                        $this->charinfo['nGrade'] = '-';
                        break;
                    case "0":
                        $this->charinfo['nGrade'] = 'Master';
                        break;
                    case "1":
                        $this->charinfo['nGrade'] = 'Member';
                        break;  
                }
            }
            
            $class = sqlsrv_prepare( $this->Handler->database->conn , "Select * from World00_Character.dbo.tCharacterShape where nCharNo = ? " , array( &$char ));
            sqlsrv_execute( $class );
            $this->charinfo = array_merge( $this->charinfo , sqlsrv_fetch_array( $class , SQLSRV_FETCH_ASSOC ) );
            ( $this->charinfo['nGender'] == 0 ) ? $this->charinfo['nGender'] = 'Female' : $this->charinfo['nGender'] = 'Male'; 
            $this->charinfo['nClass'] = $this->RenderClass( $this->charinfo['nClass'] );
        }
        
        public function SetTemplateValues()
        {
            foreach( $this->charinfo as $key => $value )
            {
                if( $key == 'dKQDate' || $key == 'XDTIME' || $key == 'dCreateDate' || 
                    $key == 'dDeletedDate' || $key == 'dLastLoginDate' || $key == 'dWarRequestDate' ||           
                    $key == 'dWarStartDate' || $key == 'dWarEndDate' || $key == 'dDismissDate' || $key == 'dNotifyDate' ||
                    $key == 'dJoinDate'
                        )
                {
                    $value = $value->format('Y-m-d H:i:s');
                }
                $this->Handler->theme->SetValue( $key , htmlentities( $value ) );
            }
            
            if( $this->charinfo['nLevel'] >= 100 )
            {
                $class = $this->charinfo['nClass'];
                if( $class == 'Enchanter' || $class == 'Scout' || $class == 'Warrior' || $class == 'Paladin' )
                {
                    if( $class == 'Enchanter' )
                    {
                        $i = '<select onchange="FrmSub( \'JC\' ,  this.value );">';
                        $i .= '<option>Chose a class</option>';
                        $i .= '<option value="Warlock">Warlock</option>';
                        $i .= '<option value="Wizard">Wizard</option>';
                        $i .= '</select>';
                        $this->Handler->theme->SetValue( 'JC' , $i );      
                    }
                    elseif( $class == 'Scout' )
                    {
                        $i = '<select onchange="FrmSub( \'JC\' ,  this.value );">';
                        $i .= '<option>Chose a class</option>';
                        $i .= '<option value="SharpShooter">SharpShooter</option>';
                        $i .= '<option value="Ranger">Ranger</option>';
                        $i .= '</select>';
                        $this->Handler->theme->SetValue( 'JC' , $i );   
                    }
                    elseif( $class == 'Warrior' )
                    {
                        $i = '<select onchange="FrmSub( \'JC\' ,  this.value );">';
                        $i .= '<option>Chose a class</option>';
                        $i .= '<option value="Knight">Knight</option>';
                        $i .= '<option value="Gladiator">Gladiator</option>';
                        $i .= '</select>';
                        $this->Handler->theme->SetValue( 'JC' , $i );
                    }
                    elseif( $class == 'Paladin' )
                    {
                        $i = '<select onchange="FrmSub( \'JC\' ,  this.value );">';
                        $i .= '<option>Chose a class</option>';
                        $i .= '<option value="HolyKnight">HolyKnight</option>';
                        $i .= '<option value="Guardian">Guardian</option>';
                        $i .= '</select>';
                        $this->Handler->theme->SetValue( 'JC' , $i );
                    }
                    else
                    {
                        $this->Handler->theme->SetValue( 'JC' , 'Class Error' );
                    }
                }
                else
                {
                    $this->Handler->theme->SetValue( 'JC' , 'No Job Change available' );
                }
            }
            else
            {
                $this->Handler->theme->SetValue( 'JC' , 'No Job Change available' );
            }
           // print_r( $this->charinfo );
            //( $this->charinfo['Guild'] == null ) ? "No Guild" : $this->GetGuild( $this->charinfo['Guild'] );
        }
        
        public function GetGuild( $no )
        {
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select * from World00_Character.dbo.tGuild where nNo = ? " , array( &$no ) );
            sqlsrv_execute( $do );
            return sqlsrv_fetch_array( $do , SQLSRV_FETCH_ASSOC );
        }
        
        public function CheckNameAvaiable( $name )
        {
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select nCharNo from World00_Character.dbo.tCharacter where sID = ?" , array( &$name ) );
            sqlsrv_execute( $do );
            if( sqlsrv_has_rows( $do ) == true )
            {
                sqlsrv_free_stmt( $do );
                return false;
            }
            else
            {
                return true;
            }
        }
        
        public function UpdateName( $name , $oldname = null )
        {
            if( $oldname == null )
            {
                $oldname = $this->charinfo['nCharNo'];
            }
            
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Update World00_Character.dbo.tCharacter set sID = ? where nCharNo = ?" , array( &$name , &$oldname ));
            sqlsrv_execute( $do );
        }
        
        public function IsOnline( $char = null )
        {
            if( $char == null )
            {
                $char = $this->charinfo['nCharNo'];
            }
            
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select nCharNo from World00_Character.dbo.LoggedInChars where nCharNo = ?" , array( &$char ) );
            sqlsrv_execute( $do );
            if( sqlsrv_has_rows( $do ) )
            {
                return true;
            }
            else
            {
                return false;
            }
            
        }
        
        public function ResetEXP( $char = null )
        {
            if( $char == null )
            {
                $char = $this->charinfo['nCharNo'];
            }
            $this->Handler->WriteLog( 'EXP Reset' , 'Success' , "$char" , null );
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Update World00_Character.dbo.tCharacter set nEXP = 0 where nCharNo = ?", array( &$char ) );
            sqlsrv_execute( $do );
        }
        
        public function RenderClass( $num )
        {
            switch( $num )
            {
                default:
                    return "-";
                    break;
                case "1":
                    return "Fighter";
                    break;
                case "2":
                    return "Clever Fighter";
                    break;
                case "3":
                    return "Warrior";
                    break;
                case "4":
                    return "Gladiator";
                    break;
                case "5":
                    return "Knight";
                    break;
                case "6":
                    return "-";
                    break;
                case "7":
                    return "-";
                    break;
                case "8":
                    return "Cleric";
                    break;
                case "9":
                    return "High Cleric";
                    break;
                case "10":
                    return "Paladin";
                    break;
                case "11":
                    return "HolyKnight";
                    break;
                case "12":
                    return "Guardian";
                    break;
                case "13":
                    return "-";
                    break;
                case "14":
                    return "-";
                    break;
                case "15":
                    return "Archer";
                    break;
                case "16":
                    return "HawkArcher";
                    break;
                case "17":
                    return "Scout";
                    break;
                case "18":
                    return "SharpShooter";
                    break;
                case "19":
                    return "Ranger";
                    break;
                case "20":
                    return "-";
                    break;
                case "21":
                    return "-";
                    break;
                case "22":
                    return "Mage";
                    break;
                case "23":
                    return "WizMage";
                    break;
                case "24":
                    return "Enchanter";
                    break;
                case "25":
                    return "Warlock";
                    break;
                case "26":
                    return "Wizard";
                    break;
            }
        }
        
        public function IsOwner( $no )
        {
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Select sID from World00_Character.dbo.tCharacter where nCharNo = ? and nUserNo = ? and bDeleted = 0" , array( &$no , &$this->user ) );
            sqlsrv_execute( $do );
            if( sqlsrv_has_rows( $do )  == true )
            {
                    return true;
            }
            else
            {
                    return false;
            }
        }
        
        public function UpdateClass( $no , $char = null )
        {
            if( $char == null )
            {
                $char = $this->charinfo['nCharNo'];
            }
            $this->Handler->WriteLog( 'Job Change' , 'Success' , "{$this->charinfo['sID']}" , $this->charinfo['nClass'] , $this->RenderClass( $no ) );
            $do = sqlsrv_prepare( $this->Handler->database->conn , "Update World00_Character.dbo.tCharacterShape set nClass = ? where nCharNo = ?" , array( &$no , &$char ) );
            sqlsrv_execute( $do );
        }
}

?>