<?php defined('SYSPATH') or die('No direct script access.'); 

        class Controller_Match extends Controller { 
    
            public function action_playmatch() 
        {
               
        $data = array("value"=>0,"value2"=>3);    
                
                $view = View::factory("match",$data);  
                $this->request->response = $view;  
        }
            public function action_adminmatchmotor(){
                $me = Session::instance()->get("fbUserId");
                $playera = ORM::factory("player")->where('teamid','=',$me)
                                        ->find_all()->as_array();
                $playerb = ORM::factory("player")->where('teamid','=','527283150')
                                        ->find_all()->as_array(); 
                $economy =  ORM::factory("economy")->where('id','=',$me)->find();
                $stadium =  ORM::factory("stadium")->where('id','=',$me)->find();
                       
                
        $team1name=ORM::factory("team")->where('id','=',$me)->find();
        $team2name=ORM::factory("team")->where('id','=','647424887')->find();

        $economy->total_money += $stadium->capacity * $stadium->ticket_price;
        $economy->weekly_total_income += $stadium->capacity * $stadium->ticket_price;
        $economy->save();

        //echo $team2name->team_name;
        $gol1=0;
        $gol2=0;
        $positions=array();
        $goals=array();

        
                //ARAYDE KAÇ NUMARALI OYUNCU HANGI BÖLGEDE
        ///////////////////////////////////////////
        //        Sol            Sag
        
        //Forvet        11    12    13
        
        //Ortasaha    6    7    8    9    10
        
        //Defans    1    2    3    4    5
        
        // Kaleci                0
        
        ///////////////////////////////////////////
        
        //
        //OYUNCULARIN BÜTÜN DEGERLERININ HESAPLANMASI
        //
        
        //oyuncularin degerlerinin maça etkisi, her degerin stamina*form*1.2 ile çarpilmasiyla bulunuyor.                                                      
                for ($i=0; $i<count($playera); $i++)
        {
            $temp = ($playera[$i]->form * $playera[$i]->stamina * 1.2)/100;
            $playera[$i]->goalkeeping = $playera[$i]->goalkeeping * $temp;
            $playera[$i]->defending = $playera[$i]->defending * $temp;
            $playera[$i]->playmaking = $playera[$i]->playmaking * $temp;
            $playera[$i]->winger = $playera[$i]->winger * $temp;
            $playera[$i]->scoring = $playera[$i]->scoring * $temp;
            $playera[$i]->technique = $playera[$i]->technique * $temp;
            $playera[$i]->passing = $playera[$i]->passing * $temp;
                }
                for ($j=0; $j<count($playerb); $j++)
        {
            $temp = ($playerb[$j]->form * $playerb[$j]->stamina *1.2)/100;
            $playerb[$j]->goalkeeping = $playerb[$j]->goalkeeping * $temp;
            $playerb[$j]->defending = $playerb[$j]->defending * $temp;
            $playerb[$j]->playmaking = $playerb[$j]->playmaking * $temp;
            $playerb[$j]->winger = $playerb[$j]->winger * $temp;
            $playerb[$j]->scoring = $playerb[$j]->scoring * $temp;
            $playerb[$j]->technique = $playerb[$j]->technique * $temp;
            $playerb[$j]->passing = $playerb[$j]->passing * $temp;
                }
        $xml = new SimpleXMLElement('<match></match>');
		$xml2 = new SimpleXMLElement('<match></match>');
		$xml2->addChild('home',htmlspecialchars(utf8_encode(0)));
		$xml2->addChild('away',htmlspecialchars(utf8_encode(0)));
		$xml2->asXML($me.'.xml');
                $xml2->asXML('647424887.xml');
        $totalPlaymakinga=0;
        $leftAttacka=0;
        $rightAttacka=0;
        $centerAttacka=0;
        $leftDefenda=0;
        $rightDefenda=0;
        $centerDefenda=0;
        $totalPlaymakingb=0;
        $leftAttackb=0;
        $rightAttackb=0;
        $centerAttackb=0;
        $leftDefendb=0;
        $rightDefendb=0;
        $centerDefendb=0;
        $totalTechniquea=0;
        $totalTechniqueb=0;
        $totalDefendNumbera=0;
        $totalDefendNumberb=0;
        $totalDefendTechniquea=0;
        $totalDefendTechniqueb=0;
        $k=0;

                $effi = ORM::factory("training")->where('id','=',$me)->find();
                $efficiency = $effi->training_effect/100.;
                $database_training = $effi->training_type;

                switch($database_training)
        {
            case 1:
                $efficiency/=4.;
            break;
            case 2:
                $efficiency/=6.;
            break;
            case 3:
                $efficiency/=5.;
            break;
            case 4:
                $efficiency/=5.;
            break;
            case 5:
                $efficiency/=6.;
            break;
            case 6:
                $efficiency/=4.;
            break;
            case 7:
                $efficiency/=2.5;
            break;
            default:
            break;
        }

        for($i=0; $i<count($playera); $i++)
        {
            if($playera[$i]->position != 0)$totalTechniquea+=$playera[$i]->technique;
            if($playera[$i]->position == 0)//kaleci
            {
                $leftDefenda+=$playera[$i]->goalkeeping *0.7;
                $rightDefenda+=$playera[$i]->goalkeeping *0.7;
                $centerDefenda+= $playera[$i]->goalkeeping;
                if($database_training==1)
                {
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->goalkeeping+=$temp_efficiency;
                                        $temp_player->save();
                }
            }        
            else if ($playera[$i]->position == 1)//sol bek
            {
                $totalDefendTechniquea+=$playera[$i]->technique;
                $totalDefendNumbera++;
                $leftAttacka+=$playera[$i]->winger *0.4;
                $leftDefenda+= $playera[$i]->defending;
                $centerDefenda+= $playera[$i]->defending*0.6;
$wingbackNameA[]=$playera[$i]->name;
                                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 2||$playera[$i]->position == 3||$playera[$i]->position == 4)//defans
            {
                $totalDefendTechniquea+=$playera[$i]->technique;
                $totalDefendNumbera++;
                $leftDefenda+= $playera[$i]->defending*0.6;
                $rightDefenda+= $playera[$i]->defending*0.6;
                $centerDefenda+= $playera[$i]->defending;
                $defenderNameA[]=$playera[$i]->name;
                                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 5)//sag bek
            {
                $totalDefendTechniquea+=$playera[$i]->technique;
                $totalDefendNumbera++;
                $rightAttacka+=$playera[$i]->winger *0.4;
                $rightDefenda+= $playera[$i]->defending;
                $centerDefenda+= $playera[$i]->defending*0.6;
$wingbackNameA[]=$playera[$i]->name;
                                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 6)//sol kanat
            {
                    if ($playera[$i]->prefferedFoot == "right")
                {
                    $playera[$i]->defending = $playera[$i]->defending*0.7;
                    $playera[$i]->winger = $playera[$i]->winger*0.7;
                    $playera[$i]->scoring = $playera[$i]->scoring*1.2;
                    $playera[$i]->technique = $playera[$i]->technique*0.7;
                    $playera[$i]->passing = $playera[$i]->passing*0.7;
                }
                $leftDefenda+= $playera[$i]->defending*0.4;
                $totalPlaymakinga += $playera[$i]->playmaking*0.4;
                $leftAttacka += $playera[$i]->winger;
                $leftAttacka += $playera[$i]->passing*0.4;
                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 7||$playera[$i]->position == 8||$playera[$i]->position == 9)//orta saha
            {
                $totalPlaymakinga += $playera[$i]->playmaking;
                $centerAttacka += $playera[$i]->passing*0.3;
                $centerAttacka += $playera[$i]->scoring*0.1;
$midfielderNameA[]=$playera[$i]->name;
                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/36;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 10)//sag kanat
            {
                if ($playera[$i]->prefferedFoot == "left")
                {
                    $playera[$i]->defending = $playera[$i]->defending*0.7;
                    $playera[$i]->winger = $playera[$i]->winger*0.7;
                    $playera[$i]->scoring = $playera[$i]->scoring*1.2;
                    $playera[$i]->technique = $playera[$i]->technique*0.7;
                    $playera[$i]->passing = $playera[$i]->passing*0.7;
                }
                $rightDefenda+= $playera[$i]->defending*0.4;
                $totalPlaymakinga += $playera[$i]->playmaking*0.4;
                $rightAttacka += $playera[$i]->winger;
                $rightAttacka += $playera[$i]->passing*0.4;
                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playera[$i]->position == 11||$playera[$i]->position == 12||$playera[$i]->position == 13)//forvet
            {
                $leftAttacka += $playera[$i]->scoring*0.3;
                $rightAttacka += $playera[$i]->scoring*0.3;
                $leftAttacka += $playera[$i]->scoring*0.1;
                $rightAttacka += $playera[$i]->scoring*0.1;
                $centerAttacka += $playera[$i]->scoring;
                $centerAttacka += $playera[$i]->passing*0.3;
                $forwardNameA[]=$playera[$i]->name;
                switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 4:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->scoring+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
        }
        $effi = ORM::factory("training")->where('id','=','527283150')->find();
        $efficiency = $effi->training_effect/100.;
        $database_training = $effi->training_type;
        for($i=0; $i<count($playerb); $i++)
        {
            if($playerb[$i]->position != 0)$totalTechniqueb+=$playerb[$i]->technique;
            if($playerb[$i]->position == 0)//kaleci
            {
                $leftAttackb+=$playerb[$i]->goalkeeping *0.5;
                $rightAttackb+=$playerb[$i]->goalkeeping *0.5;
                $centerDefendb+= $playerb[$i]->goalkeeping;
if($database_training==1)
                {
                    $temp_efficiency = $efficiency*(3-($playera[$i]->age-18)/15)*(3-($playera[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playera[$i]->id)->find();
                    $temp_player->goalkeeping+=$temp_efficiency;
                                        $temp_player->save();
                }
            }        
            else if ($playerb[$i]->position == 1)//sol bek
            {
                $totalDefendTechniqueb+=$playerb[$i]->technique;
                $totalDefendNumberb++;
                $leftAttackb+=$playerb[$i]->winger *0.4;
                $leftDefendb+= $playerb[$i]->defending;
                $centerDefendb+= $playerb[$i]->defending*0.6;
$wingbackNameB[]=$playera[$i]->name;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 2||$playerb[$i]->position == 3||$playerb[$i]->position == 4)//defans
            {
                $totalDefendTechniqueb+=$playerb[$i]->technique;
                $totalDefendNumberb++;
                $leftDefendb+= $playerb[$i]->defending*0.6;
                $rightDefendb+= $playerb[$i]->defending*0.6;
                $centerDefendb+= $playerb[$i]->defending;
                $defenderNameB[]=$playera[$i]->name;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 5)//sag bek
            {
                $totalDefendTechniqueb+=$playerb[$i]->technique;
                $totalDefendNumberb++;
                $rightAttackb+=$playerb[$i]->winger *0.4;
                $rightDefendb+= $playerb[$i]->defending;
                $centerDefendb+= $playerb[$i]->defending*0.6;
$wingbackNameB[]=$playera[$i]->name;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 6)//sol kanat
            {
                    if ($playerb[$i]->prefferedFoot == "right")
                {
                    $playerb[$i]->defending = $playerb[$i]->defending*0.7;
                    $playerb[$i]->winger = $playerb[$i]->winger*0.7;
                    $playerb[$i]->scoring = $playerb[$i]->scoring*1.2;
                    $playerb[$i]->technique = $playerb[$i]->technique*0.7;
                    $playerb[$i]->passing = $playerb[$i]->passing*0.7;
                }
                $leftDefendb+= $playerb[$i]->defending*0.4;
                $totalPlaymakingb += $playerb[$i]->playmaking*0.4;
                $leftAttackb += $playerb[$i]->winger;
                $leftAttackb += $playerb[$i]->passing*0.4;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 7||$playerb[$i]->position == 8||$playerb[$i]->position == 9)//orta saha
            {
                $totalPlaymakingb += $playerb[$i]->playmaking;
                $centerAttackb += $playerb[$i]->passing*0.3;
                $centerAttackb += $playerb[$i]->scoring*0.1;
$midfielderNameB[]=$playera[$i]->name;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/36;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 10)//sag kanat
            {
                if ($playerb[$i]->prefferedFoot == "left")
                {
                    $playerb[$i]->defending = $playerb[$i]->defending*0.7;
                    $playerb[$i]->winger = $playerb[$i]->winger*0.7;
                    $playerb[$i]->scoring = $playerb[$i]->scoring*1.2;
                    $playerb[$i]->technique = $playerb[$i]->technique*0.7;
                    $playerb[$i]->passing = $playerb[$i]->passing*0.7;
                }
                $leftDefendb+= $playerb[$i]->defending*0.4;
                $totalPlaymakingb += $playerb[$i]->playmaking*0.4;
                $rightAttackb += $playerb[$i]->winger;
                $rightAttackb += $playerb[$i]->passing*0.4;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 3:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/18;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->playmaking+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 5:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->defending+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 6:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->winger+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
            else if ($playerb[$i]->position == 11||$playerb[$i]->position == 12||$playerb[$i]->position == 13)//forvet
            {
                $leftAttackb += $playerb[$i]->scoring*0.3;
                $rightAttackb += $playerb[$i]->scoring*0.3;
                $leftAttackb += $playerb[$i]->scoring*0.1;
                $rightAttackb += $playerb[$i]->scoring*0.1;
                $centerAttackb += $playerb[$i]->scoring;
                $centerAttackb += $playerb[$i]->passing*0.3;
                $forwardNameB[]=$playera[$i]->name;
switch($database_training)
                {
                    case 2:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->passing+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 4:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->scoring+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    case 7:
                    $temp_efficiency = $efficiency*(3-($playerb[$i]->age-18)/15)*(3-($playerb[$i]->age-18)/15)/9;
                                        $temp_player = ORM::factory("player")->where('id','=',$playerb[$i]->id)->find();
                    $temp_player->technique+=$temp_efficiency;
                                        $temp_player->save();
                    break;
                    default:
                    break;
                }
            }
        
        }
        //$mys='aaaa <br /> aaaa';
        //echo $mys;
        
        $totalDefendTechniquea/=$totalDefendNumbera;//hata özel olay? için
        $totalDefendTechniqueb/=$totalDefendNumberb;//
        
        
        //her iki takiminda ortasaha degerleri 20 üzerinden oranlanarak 20 atak takimlara paylastiriliyor
                $total = $totalPlaymakinga + $totalPlaymakingb;
                $attackNumbera = floor(($totalPlaymakinga *20) / $total);
                $attackNumberb = 20 - $attackNumbera;
                
        //bir takimin toplam atak sayisinin yarisi orta ataga, 1/4 ü sag ataga, 1/4 ü de sola ataga olmak üzre paylastirilir
                $wingAttackNumbera = floor($attackNumbera/4);
                $centerAttackNumbera = $attackNumbera -(2 * $wingAttackNumbera); 
                
                $wingAttackNumberb = floor($attackNumberb/4);
                $centerAttackNumberb = $attackNumberb -(2 * $wingAttackNumberb);
        
        
                $data = array("value"=>0,"value2"=>3);
        
                $k = 0;
        
        //
        //POZISYONLARIN HESAPLANMASI VE POSITION ARRAYINE ATILMASI
        //ILK HARF POZISYONUN NE OLDUGUNU IKINCI HARFTE KIME AIT OLDUGUNU BELIRTIYOR
        //    ÖRNEGIN 'RA' A TAKIMININ -EV SAHIBI TAKIM- SAG KANAT ATAGI DEMEK
        //R=SAG ATAK    L=SOL ATAK    C=ORTA ATAK    X=KIRMIZI KART    P=PENALTI
        //
        
        $position = array();
        
        
        //atak pozisyonlarinin gol olmasi için atak yapan takimin atak degerinin karsi takimin ilgili defans degerinden
        //büyük olmasi gerekiyor. Sans faktörünü de icin içine dahil etmek için atak degeri 0.7 ile 1.2 arasinda
        //rastgele bir deger ile çarpiliyor.
        
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //kanat ve orta ataklarinin gol olmasi hesaplanirken for döngusu yerine basit bir formül kullanilarak 5 ataktan
        // 3ü gol olsun denilebilir.
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        
        //ev sahibi takimin kanat ataklarinin hesaplanmasi
                for ($i = 0; $i<$wingAttackNumbera;$i++)
        {
                        if ($leftAttacka*(rand(7,15) / 10.) > $rightDefendb)
                        {
                $position[]="LA";
                $k++;                                
            } // LeftAttack(A);
                        if ($rightAttacka*(rand(7,15) / 10.) > $leftDefendb)
            {
                $position[]="RA";
                $k++;
            } // RightAttack(A);
                }
        
        //deplasman takiminin kanat ataklarinin hesaplanmasi                
                for ($i = 0; $i<$wingAttackNumberb;$i++)
        {
                        if ($leftAttackb*(rand(7,15) / 10.) > $rightDefenda)
            {
                $position[]="LB";
                $k++;
            }  // LeftAttack(B);
                        if ($rightAttackb*(rand(7,15) / 10.) > $leftDefenda)
                        {
                $position[]="RB";
                $k++;
            }//      RightAttack(B);
                }
        
        
                for ( $i = 0; $i<$centerAttackNumbera; $i++)
        {
                        if ($centerAttacka*(rand(7,15) / 10.) > $centerDefendb)
                        {
                $position[]="CA";
                $k++;
            }//      CenterAttack(A);
                }
        
                for ( $i = 0; $i<$centerAttackNumberb; $i++)
        {
                        if ($centerAttackb*(rand(7,15) / 10.) > $centerDefenda)
            {
                $position[]="CB";
                $k++;
            }//     CenterAttack(B);
                }
                
        //her bir takiminin 1/4 sans ile kirmizi kart görme ihtimali var
                if(rand(0,4)==0) 
                {
            $position[]="XA";
            $k++;
        }// Card(A);
                if(rand(0,4)==0)
                {
            $position[]="XB";
            $k++;
        }// Card(B);
                
                
        //aynen kart gibi her takimin 1/4 ihtimal ile penalti pozisyonu sansi var
                if(rand(0,4)==0) 
                {
            $position[]="PA";
            $k++;
        }// Penalty(A);
                if(rand(0,4)==0)
                {
            $position[]="PB";
            $k++;
        }//   Penalty(B);
                
        $str1="1";
        $str2="2";
        $str3="-";
        //pozisyonlarin ilk yari ikinci yari ayri ayri eklenmesi
        
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // for içerisinde if else yerine ilk yari ve ikinci yari için iki ayri for
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //$firstHalf = floor((rand(8,12)/10.)*($k/2.));
        //echo $k;
        //$k=count($position);
        //echo $k;
        $firstHalf = floor($k/2.);
        $minute=0;
        
        for($i=0;$i<91;$i++)
            $positions[$i]="";
        
        for($i=0; $i<$k; $i++)
        {
            if($i < $firstHalf) 
            {
                while(1)
                {
                    $q=rand(0,$k-1);
                    if(!empty($position[$q]))
                    {
                        if($position[$q]=="RA" || $position[$q]=="LA" || $position[$q]=="CA" || $position[$q]=="PA")
                            $gol1=$gol1+1;
                        else if($position[$q]=="RB" || $position[$q]=="LB" || $position[$q]=="CB" || $position[$q]=="PB")
                            $gol2=$gol2+1;
                        break;
                    }
                }
                while(1)
                {
                    $minute = rand(1,45);
                    if($minute!=0&&$minute!=1&&empty($positions[$minute]))
                    {
                        if($minute>3&&empty($positions[$minute-1]))
                            if($minute>4&&empty($positions[$minute-2]))
                            if($minute>5&&empty($positions[$minute-3]))
                            if($minute<45&&empty($positions[$minute+1]))
                            if($minute<44&&empty($positions[$minute+2]))
                            if($minute<43&&empty($positions[$minute+3]))
                            break;
                    }
                }
                
                                $positions[$minute]=$str3.$position[$q].$str1;
                $position[$q]="";
            }
            else 
            {
                while(1)
                {
                    $q=rand(0,$k-1);
                    if(!empty($position[$q]))
                    {
                        if($position[$q]=="RA" || $position[$q]=="LA" || $position[$q]=="CA" || $position[$q]=="PA")
                            $gol1=$gol1+1;
                        else if($position[$q]=="RB" || $position[$q]=="LB" || $position[$q]=="CB" || $position[$q]=="PB")
                            $gol2=$gol2+1;
                        break;
                    }
                }
                while(1)
                {
                    $minute =45 + rand(1,45);
                    if($minute!=45&&$minute!=46&&empty($positions[$minute]))
                        if($minute>47&&empty($positions[$minute-1]))
                            if($minute>48&&empty($positions[$minute-2]))
                            if($minute>49&&empty($positions[$minute-3]))
                            if($minute<90&&empty($positions[$minute+1]))
                            if($minute<89&&empty($positions[$minute+2]))
                            if($minute<88&&empty($positions[$minute+3]))
                            break;

                }
                $positions[$minute]=$str3.$position[$q].$str2;
                $position[$q]="";
            }
        }
        
        
        $firstSent = ORM::factory("comment")->where('id','=',rand(57,59))->find();
        if(rand(0,1))
        {
            $positions[0] = $team1name->team_name." ".$firstSent->comment2;
            $firstSent = ORM::factory("comment")->where('id','=',rand(57,59))->find();
            $positions[46] = $team2name->team_name." ".$firstSent->comment2;
        }else
        {
            $positions[0] = $team2name->team_name." ".$firstSent->comment2;
            $firstSent = ORM::factory("comment")->where('id','=',rand(57,59))->find();
            $positions[46] = $team2name->team_name." ".$firstSent->comment2;
        }
        for($i=0;$i<91;$i++)
        {
            if(empty($positions[$i]))
            {
                //$comType=rand(0,5);
                
                $comType=rand(0,1);
                $comment="";
                $team=rand(0,2);
                switch($comType)
                {
                    //array comment-name-comment-name-comment-name-comment ?eklinde olacak
                    //p1 t1 herzaman atak eden taraf
                    case 0:    //aerial challange
                        //## AERIAL CHALLENGE: Player beats his marker in the air
                        //## AERIAL CHALLENGE: Player is dominant in the air today
                        $firstIndex[0] = rand(1,12);
                        $firstIndex[1] = rand(13,28);
                        $indexCount = 2;
                        if($team==0)
                        {
                            $player1 = $defenderNameA[rand(0,count($defenderNameA)-1)]." ";
                            $player2 = $forwardNameB[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team1name->team_name." ";
                            $team2 = $team2name->team_name." ";
                        } else
                        {
                            $player1 = $defenderNameB[rand(0,count($defenderNameA)-1)]." ";
                            $player2 = $forwardNameA[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team2name->team_name." ";
                            $team2 = $team1name->team_name." ";
                        }
                        break;
                    case 1://counter attack
                        //## ATTACK: Team are on the counter attack
                        //## ATTACK: Team are on the counter with No against No
                        //ikisi birlesik ## ATTACK: Team survive attack## ATTACK: The attack comes to nothing
                        $firstIndex[0] = rand(29,36);
                        $firstIndex[1] = rand(37,46);
                        $firstIndex[2] = rand(47,56);
                        if($team==0)
                        {
                            $player2 = $defenderNameA[rand(0,count($defenderNameA)-1)]." ";
                            $player1 = $forwardNameB[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team1name->team_name." ";
                            $team2 = $team2name->team_name." ";
                        }
                                                {
                            $player2 = $defenderNameB[rand(0,count($defenderNameA)-1)]." ";
                            $player1 = $forwardNameA[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team2name->team_name." ";
                            $team2 = $team1name->team_name." ";
                        }
                        $indexCount = 3;
                        break;
                    case 2://## CROSS TO PLAYER: Player crosses the ball to a team mate------## DRIBBLING: Great first touch by player
                        $firstIndex[0] = rand(75,81);
                        $firstIndex[1] = rand(82,95);
                        $indexCount = 2;
                        if($team==0)
                        {
                            $player1 = $wingbackNameA[rand(0,count($defenderNameA)-1)]." ";
                            $player2 = $midfielderNameA[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team1name->team_name." ";
                            $team2 = $team2name->team_name." ";
                        } else
                        {
                            $player1 = $wingbackNameB[rand(0,count($defenderNameA)-1)]." ";
                            $player2 = $midfielderNameB[rand(0,count($forwardNameA)-1)]." ";
                            $team1 = $team2name->team_name." ";
                            $team2 = $team1name->team_name." ";
                        }
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                }
                $team = rand(0,1);
                for($q=0;$q<$indexCount;$q++)
                {                            
                    $firstSent = ORM::factory("comment")->where('id','=',$firstIndex[$q])->find();

                    for($z=0;$z<3;$z++)
                    {
                        switch($z)
                        {
                        case 0:
                            $comment = $comment.$firstSent->comment1." ";
                            $type = $firstSent->name1;
                            break;
                        case 1:
                            $comment = $comment.$firstSent->comment2." ";
                            $type = $firstSent->name2;
                            break;
                        case 2:
                            $comment = $comment.$firstSent->comment3." ";
                            $type = $firstSent->name3;
                            break;
                        }
                        switch($type)
                        {
                            case "p1":
                                $comment = $comment.$player1;
                                break;
                            case "p2":
                                $comment = $comment.$player2;
                                break;
                            case "t1":
                                $comment = $comment.$team1;
                                break;
                            case "t2":
                                $comment = $comment.$team2;
                                break;
                        }

                    }
                    $comment = $comment.$firstSent->comment4." ";
                }
                $positions[$i]= $comment;                
            }
        }
        $posi = $xml->addChild('time',htmlspecialchars(utf8_encode(time())));
        $posi = $xml->addChild('scoreA',$team1name->team_name);
        $posi->addAttribute('score',htmlspecialchars(utf8_encode($gol1)));
        $posi = $xml->addChild('scoreB',$team2name->team_name);
        $posi->addAttribute('score',htmlspecialchars(utf8_encode($gol2)));
        for($i=0;$i<91;$i++)
        {
            $val = $positions[$i];
            if(!empty($val))
            {
                $posi = $xml->addChild('position');
                $posi->addAttribute('min',htmlspecialchars(utf8_encode($i)));
                if(strpos($val,'-') ===0)
                {
                    $val=trim($val, "-");
                    if(strpos($val,'XA') ===0)
                    {
                        $flash = $posi->addChild('flash',$team1name->team_name." Yellow Card");
                        $flash->addAttribute('file',$val);
                    }
                    else if(strpos($val,'XB') ===0)
                    {
                        $flash = $posi->addChild('flash',$team2name->team_name." Yellow Card");
                        $flash->addAttribute('file',$val);
                    }
                    else if(strpos($val,'A') ===1)
                    {
                        $flash = $posi->addChild('flash',$team1name->team_name." Goal");
                        $flash->addAttribute('file',$val);
                    }
                    else if(strpos($val,'B') ===1)
                    {
                        $flash = $posi->addChild('flash',$team2name->team_name." Goal");
                        $flash->addAttribute('file',$val);
                    }
                }        
                else
                {
                    $comm = $posi->addChild('comment',$val);
                }
                }
        }
        $xml->asXML('test_file.xml');



            }
            public function action_matchmotor(){

        $view = View::factory("match");  
                $this->request->response = $view;
        
            }
        }
?>