<?php

foreach ($aReports as $datas){
     ++$ireports;
     $arDetails = $battlelog->BattleReportDetail($bID,$datas,$uPlatform);
     if (!isset($arDetails['status'])){
           $bestKills = "";
         if (isset($arDetails['general']['gameReport']['prizes']['kills'])){	 
         	  if ($row['platform'] != 'pc'){  
         	  	$bestKills = $arDetails['general']['gameReport']['prizes']['kills']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['kills']['score'];
         	    }else{
              $bestKills = $arDetails['general']['gameReport']['prizes']['kills']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['kills']['score'];
            }
         }
          
            $jetcrew = "";
         if (isset($arDetails['general']['gameReport']['prizes']['jetcrew'])){	
         	  if ($row['platform'] != 'pc'){ 
         	  	$jetcrew = $arDetails['general']['gameReport']['prizes']['jetcrew']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['jetcrew']['score'];
         	  }else{    
             $jetcrew = $arDetails['general']['gameReport']['prizes']['jetcrew']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['jetcrew']['score'];
           }
         }
         
            $helicrew = "";
         if (isset($arDetails['general']['gameReport']['prizes']['helicrew'])){	   
         	   if ($row['platform'] != 'pc'){ 
         	   	$helicrew = $arDetails['general']['gameReport']['prizes']['helicrew']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['helicrew']['score'];       
         	   	}else{ 
             $helicrew = $arDetails['general']['gameReport']['prizes']['helicrew']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['helicrew']['score'];  
           }
         }
         
            $vehiclecrew = "";
         if (isset($arDetails['general']['gameReport']['prizes']['vehiclecrew'])){	
         	   if ($row['platform'] != 'pc'){
         	   	$vehiclecrew = $arDetails['general']['gameReport']['prizes']['vehiclecrew']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['vehiclecrew']['score'];
         	   	}else{     
              $vehiclecrew = $arDetails['general']['gameReport']['prizes']['vehiclecrew']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['vehiclecrew']['score'];
            }
         }
         
             $bestRecon = "";
         if (isset($arDetails['general']['gameReport']['prizes']['recon'])){	  
         	    if ($row['platform'] != 'pc'){ 
         	    	$bestRecon = $arDetails['general']['gameReport']['prizes']['recon']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['recon']['score'];
         	    	}else{  
                $bestRecon = $arDetails['general']['gameReport']['prizes']['recon']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['recon']['score'];
           }
         }
         
             $awardscore = "";
         if (isset($arDetails['general']['gameReport']['prizes']['awardscore'])){	
         	  if ($row['platform'] != 'pc'){
         	  	$awardscore = $arDetails['general']['gameReport']['prizes']['awardscore']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['awardscore']['score'];
         	  	}else{
              $awardscore = $arDetails['general']['gameReport']['prizes']['awardscore']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['awardscore']['score'];
           }
         }
         
             $support = "";
         if (isset($arDetails['general']['gameReport']['prizes']['support'])){	
         	  if ($row['platform'] != 'pc'){   
         	  	$support = $arDetails['general']['gameReport']['prizes']['support']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['support']['score'];
         	  	}else{  
             $support = $arDetails['general']['gameReport']['prizes']['support']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['support']['score'];
           }
         }
         
             $assault = "";
         if (isset($arDetails['general']['gameReport']['prizes']['assault'])){
         	if ($row['platform'] != 'pc'){ 
         		$assault = $arDetails['general']['gameReport']['prizes']['assault']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['assault']['score'];
         		}else{ 	   
             $assault = $arDetails['general']['gameReport']['prizes']['assault']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['assault']['score'];
         
 }      
         }
         
             $engineer = "";
         if (isset($arDetails['general']['gameReport']['prizes']['engineer'])){	 
         	  if ($row['platform'] != 'pc'){ 
         	  	$engineer = $arDetails['general']['gameReport']['prizes']['engineer']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['engineer']['score'];
         	  	}else{   
              $engineer = $arDetails['general']['gameReport']['prizes']['engineer']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['engineer']['score'];
           }
         }
         
             $accuracy = "";
         if (isset($arDetails['general']['gameReport']['prizes']['accuracy'])){	
         	   if ($row['platform'] != 'pc'){  
         	   	$accuracy = $arDetails['general']['gameReport']['prizes']['accuracy']['persona']['personaName'].','.$arDetails['general']['gameReport']['prizes']['accuracy']['score'];
         	   	}else{   
              $accuracy = $arDetails['general']['gameReport']['prizes']['accuracy']['user']['username'].','.$arDetails['general']['gameReport']['prizes']['accuracy']['score'];
           }
         }
         
            
          
          
          $duration = $arDetails['general']['gameReport']['duration'];
         if ($arDetails['general']['gameReport']['gameServer']['mapMode'] == "8"){
          	$ticket1 = $arDetails['general']['gameReport']['mapModeResult']['kill'][1];
            $ticket2 = $arDetails['general']['gameReport']['mapModeResult']['kill'][2];
            $ticket3 = $arDetails['general']['gameReport']['mapModeResult']['kill'][3];;
            $ticket4 = $arDetails['general']['gameReport']['mapModeResult']['kill'][4];;
          }elseif ($arDetails['general']['gameReport']['gameServer']['mapMode'] == "32" OR $arDetails['general']['gameReport']['gameServer']['mapMode'] == "2048" OR $arDetails['general']['gameReport']['gameServer']['mapMode'] == "512"){
            $ticket1 = $arDetails['general']['gameReport']['mapModeResult']['kill'][1];
            $ticket2 = $arDetails['general']['gameReport']['mapModeResult']['kill'][2];
            $ticket3 = "999999";
            $ticket4 = "999999";
          }elseif ($arDetails['general']['gameReport']['gameServer']['mapMode'] == "2" OR $arDetails['general']['gameReport']['gameServer']['mapMode'] == "4"){
            $ticket1 = $arDetails['general']['gameReport']['mapModeResult']['life'][1];
            $ticket2 = $arDetails['general']['gameReport']['mapModeResult']['life'][2];
            $ticket3 = "999999";
            $ticket4 = "999999";
          }else{
            $ticket1 = $arDetails['general']['gameReport']['mapModeResult']['ticket'][1];
            $ticket2 = $arDetails['general']['gameReport']['mapModeResult']['ticket'][2];
            $ticket3 = "999999";
            $ticket4 = "999999";
        }
          
         $playerT1 = array();
         $playerT2 = array(); 
         $playerT3 = array(); 
         $playerT4 = array();
       //Spielerliste Player Team 1
       if (isset($arDetails['general']['gameReport']['players'][1])){
       	 $i = 0;	
       	foreach ($arDetails['general']['gameReport']['players'][1] as $players){
       		$i++;
       		$playerT1[$i]['kills'] = $players['kills'];
       		$playerT1[$i]['deaths'] = $players['deaths'];
       		$playerT1[$i]['squadId'] = $players['squadId'];
       		$playerT1[$i]['rank'] = $players['rank'];
       		$playerT1[$i]['combatScore'] = $players['combatScore'];
       		$playerT1[$i]['gravatarMd5'] = $players['user']['gravatarMd5'];
       		if ($row['platform'] != 'pc'){
       			$playerT1[$i]['username'] = $players['persona']['personaName'];
       		  }else{
       		  $playerT1[$i]['username'] = $players['user']['username'];
       		}
       	} 
      }
          $T1count = count($playerT1);
       	    if ($T1count > 0){
       	   $playerT1 = addslashes(json_encode($playerT1));
       	 }else{
       	 	 $playerT1 = "";
       	}
      
      //Spielerliste Player Team 2
      if (isset($arDetails['general']['gameReport']['players'][2])){	
       	  $i = 0;	
       	foreach ($arDetails['general']['gameReport']['players'][2] as $players){
       		$i++;
       		$playerT2[$i]['kills'] = $players['kills'];
       		$playerT2[$i]['deaths'] = $players['deaths'];
       		$playerT2[$i]['squadId'] = $players['squadId'];
       		$playerT2[$i]['rank'] = $players['rank'];
       		$playerT2[$i]['combatScore'] = $players['combatScore'];
       		$playerT2[$i]['gravatarMd5'] = $players['user']['gravatarMd5'];
       		if ($row['platform'] != 'pc'){
       			$playerT2[$i]['username'] = $players['persona']['personaName'];
       		  }else{
       		  $playerT2[$i]['username'] = $players['user']['username'];
       		}
       	}  
      } 
       $T2count = count($playerT2);
       	    if ($T2count > 0){
       	   $playerT2 = addslashes(json_encode($playerT2));
       	 }else{
       	 	 $playerT2 = "";
       	}   
      
      
      
      //Spielerliste Player Team 3
      if (isset($arDetails['general']['gameReport']['players'][3]) && $arDetails['general']['gameReport']['gameServer']['mapMode'] == "8"){	
       	  $i = 0;	
       	foreach ($arDetails['general']['gameReport']['players'][3] as $players){
       		$i++;
       		$playerT3[$i]['kills'] = $players['kills'];
       		$playerT3[$i]['deaths'] = $players['deaths'];
       		$playerT3[$i]['squadId'] = $players['squadId'];
       		$playerT3[$i]['rank'] = $players['rank'];
       		$playerT3[$i]['combatScore'] = $players['combatScore'];
       		$playerT3[$i]['gravatarMd5'] = $players['user']['gravatarMd5'];
       		if ($row['platform'] != 'pc'){
       			$playerT3[$i]['username'] = $players['persona']['personaName'];
       		  }else{
       		  $playerT3[$i]['username'] = $players['user']['username'];
       		}
       	}  
      } 
       $T3count = count($playerT3);
       	    if ($T3count > 0){
       	   $playerT3 = addslashes(json_encode($playerT3));
       	 }else{
       	 	 $playerT3 = "";
       	}   
      
      //Spielerliste Player Team 4
      if (isset($arDetails['general']['gameReport']['players'][4]) && $arDetails['general']['gameReport']['gameServer']['mapMode'] == "8"){	
       	 $i = 0;	
       	foreach ($arDetails['general']['gameReport']['players'][4] as $players){
       		$i++;
       		$playerT4[$i]['kills'] = $players['kills'];
       		$playerT4[$i]['deaths'] = $players['deaths'];
       		$playerT4[$i]['squadId'] = $players['squadId'];
       		$playerT4[$i]['rank'] = $players['rank'];
       		$playerT4[$i]['combatScore'] = $players['combatScore'];
       		$playerT4[$i]['gravatarMd5'] = $players['user']['gravatarMd5'];
       		if ($row['platform'] != 'pc'){
       			$playerT4[$i]['username'] = $players['persona']['personaName'];
       		  }else{
       		  $playerT4[$i]['username'] = $players['user']['username'];
       		}
       	}  	     
      } 
       $T4count = count($playerT4);
       	    if ($T4count > 0){
       	   $playerT4 = addslashes(json_encode($playerT4));
       	 }else{
       	 	 $playerT4 = "";
       	}   


     $winner = $arDetails['general']['gameReport']['winner'];
     $Servername = addslashes($arDetails['general']['gameReport']['gameServer']['name']);
     $Map =  $arDetails['general']['gameReport']['gameServer']['map'];
     $Mode =  $arDetails['general']['gameReport']['gameServer']['mapMode'];
     
     
         $topWeapon = "";
     if (isset($arDetails['detail']['topWeapon'])){	   
           $topWeapon = $arDetails['detail']['topWeapon']['image'].','.$arDetails['detail']['topWeapon']['kills'];
         }
          
         $uWeapons = "";
     if (count($arDetails['detail']['unlocks']['weapons']) > 0){  
        $i = 0;
        foreach($arDetails['detail']['unlocks']['weapons'] as $weapons){
        	$i++;
         	$uWeapons[$i]['name'] = $weapons['image'];
        }
        $uWeapons = addslashes(json_encode($uWeapons));
     }
          
          
       $uVehiclespecs = "";
     if (count($arDetails['detail']['unlocks']['vehiclespecs']) > 0){
     	   $i = 0;	   
        foreach($arDetails['detail']['unlocks']['vehiclespecs'] as $Vehiclespecs){
         	$i++;
         	$uVehiclespecs[$i]['name'] = $Vehiclespecs['image'];
        }
        $uVehiclespecs = addslashes(json_encode($uVehiclespecs));
     }     
     
     
        $uSoldierspecializations = "";
     if (count($arDetails['detail']['unlocks']['soldierspecializations']) > 0){
     	   $i = 0;	   
        foreach($arDetails['detail']['unlocks']['soldierspecializations'] as $Soldierspecializations){
         	$i++;
         	$uSoldierspecializations[$i]['name'] = $Soldierspecializations['image'];
        }
        $uSoldierspecializations = addslashes(json_encode($uSoldierspecializations));
     }   
     
     
        $uGadgets = "";
     if (count($arDetails['detail']['unlocks']['gadgets']) > 0){
     	   $i = 0;	   
        foreach($arDetails['detail']['unlocks']['gadgets'] as $Gadgets){
         	$i++;
         	$uGadgets[$i]['name'] = $Gadgets['image'];
        }
        $uGadgets = addslashes(json_encode($uGadgets));
     }   
     
     
       $aAwards = "";
     if (count($arDetails['detail']['unlocks']['awards']) > 0){
     	   $i = 0;	   
        foreach($arDetails['detail']['unlocks']['awards'] as $awards){
         	$i++;
         	$aAwards[$i]['name'] = $awards['image'];
         	$aAwards[$i]['group'] = $awards['group'];
         	$aAwards[$i]['unlockId'] = $awards['unlockId'];
        }
        $aAwards = addslashes(json_encode($aAwards));
     }  

        $uWeaponaddons = "";
     if (count($arDetails['detail']['unlocks']['weaponaddons']) > 0){
     	   $i = 0;	   
        foreach($arDetails['detail']['unlocks']['weaponaddons'] as $weaponaddons){
         	$i++;
         	$uWeaponaddons[$i]['name'] = $weaponaddons['image'];
         	$uWeaponaddons[$i]['weapon'] = $weaponaddons['parentImage'];
        }
        $uWeaponaddons = addslashes(json_encode($uWeaponaddons));
     }  
     
     $elo = "00.00";
     if (isset($arDetails['detail']['playerReport']['floatData']['elo'])){  
         	$elo = $arDetails['detail']['playerReport']['floatData']['elo'];
     }  
     
      $topVehicle = "";
     if (isset($arDetails['detail']['topVehicle']) && count($arDetails['detail']['topVehicle']) > 1){	   
           $topVehicle = $arDetails['detail']['topVehicle']['image'].','.$arDetails['detail']['topVehicle']['kills'];
         } 

 
        $rankSpanPreviousScore = $arDetails['detail']['rankInfo']['rankSpanPreviousScore'];
        $pointsNeededNext = $arDetails['detail']['rankInfo']['nextRank']['pointsNeeded'];
        $pointsNeededcurr = $arDetails['detail']['rankInfo']['currentRank']['pointsNeeded'];
        $rankSpanIncrementScore = $arDetails['detail']['rankInfo']['rankSpanIncrementScore'];
  
        $intData = addslashes(json_encode($arDetails['detail']['playerReport']['intData']));
        $status = "1";
      
      mysql_query("INSERT INTO ".DBPrefix."battlereportsDetails (
      	   	 	              ReportID,
      	   	 	              stats_pid,
      	   	 	              status,
      	   	 	              bestKills,
      	   	 	              bestRecon,
      	   	 	              awardscore,
      	   	 	              support,
      	   	 	              assault,
      	   	 	              engineer,
      	   	 	              accuracy,
      	   	 	              jetcrew,
      	   	 	              helicrew,
      	   	 	              vehiclecrew,
      	   	 	              duration,
      	   	 	              ticket1,
      	   	 	              ticket2,
      	   	 	              ticket3,
      	   	 	              ticket4,
      	   	 	              playersTeam1,
      	   	 	              playersTeam2,
      	   	 	              playersTeam3,
      	   	 	              playersTeam4,
      	   	 	              winner,
      	   	 	              Servername,
      	   	 	              map,
      	   	 	              mapMode,
      	   	 	              topWeapon,
      	   	 	              uWeapons,
      	   	 	              uVehiclespecs,
      	   	 	              uSoldierspecializations,
      	   	 	              uGadgets,
      	   	 	              awards,
      	   	 	              uWeaponaddons,
      	   	 	              elo,
      	   	 	              topVehicle,
      	   	 	              rankSpanPreviousScore,
      	   	 	              pointsNeededNext,
      	   	 	              pointsNeededcurr,
      	   	 	              rankSpanIncrementScore,
      	   	 	              intData
      	   	 	              )
    		                  VALUES (
    		                    '{$datas}',
    		                    '{$ID}',
    		                    '{$status}',
    		                    '{$bestKills}',
    		                    '{$bestRecon}',
    		                    '{$awardscore}',
    		                    '{$support}',
    		                    '{$assault}',
    		                    '{$engineer}',
    		                    '{$accuracy}',
    		                    '{$jetcrew}',
    		                    '{$helicrew}',
    		                    '{$vehiclecrew}',
    		                    '{$duration}',
    		                    '{$ticket1}',
    		                    '{$ticket2}',
    		                    '{$ticket3}',
    		                    '{$ticket4}',
    		                    '{$playerT1}',
    		                    '{$playerT2}',
    		                    '{$playerT3}',
    		                    '{$playerT4}',
    		                    '{$winner}',
    		                    '{$Servername}',
    		                    '{$Map}',
    		                    '{$Mode}',
    		                    '{$topWeapon}',
    		                    '{$uWeapons}',
    		                    '{$uVehiclespecs}',
    		                    '{$uSoldierspecializations}',
    		                    '{$uGadgets}',
    		                    '{$aAwards}',
    		                    '{$uWeaponaddons}',
    		                    '{$elo}',
    		                    '{$topVehicle}',
    		                    '{$rankSpanPreviousScore}',
    		                    '{$pointsNeededNext}',
    		                    '{$pointsNeededcurr}',
    		                    '{$rankSpanIncrementScore}',
    		                    '{$intData}'
    		                  )") or die(mysql_error());
    		        
   }else{
   	$status = "0";
   	mysql_query("INSERT INTO ".DBPrefix."battlereportsDetails (
      	   	 	              ReportID,
      	   	 	              stats_pid,
      	   	 	              status
      	   	 	               )
    		                  VALUES (
    		                    '{$datas}',
    		                    '{$ID}',
    		                    '{$status}'
    		                    )") or die(mysql_error());
   }
}
  
 
