<?php

$regionStatement = $RUNTIME['PDO']->prepare("SELECT regions.RegionName AS Name, regions.RegionUUID AS UUID, regions.RegionImage AS Image, regions.RegionOwner AS OwnerID, users.UserName AS OwnerName, onlinetimes.RegionLastSeen AS LastSeen, onlinetimes.RegionOnlineUser AS OnlineCount, regions.RegionVersion AS RegionVersion, regions.RegionHostname AS Hostname, regions.RegionPort AS Port FROM regions INNER JOIN onlinetimes ON regions.RegionUUID = onlinetimes.RegionUUID INNER JOIN users ON regions.RegionOwner = users.UserUUID ORDER BY RAND() LIMIT 10");
$regionStatement->execute(array()); 

while($row = $regionStatement->fetch())
{
    if(ping($row['Hostname'], $row['Port']))
    {
        $data = json_decode(getDataFromHTTP("http://".$row['Hostname'].":".$row['Port']."/RegionData", ""), true);

        //Cleanup
        $ObjectsCleanupStatement = $RUNTIME['PDO']->prepare("SELECT * FROM parcels WHERE ParcelRegionUUID = ?");
        $ObjectsCleanupStatement->execute(array($row['UUID'])); 
        while($deleteRow = $ObjectsCleanupStatement->fetch())
        {
            $QueueStatement = $RUNTIME['PDO']->prepare("DELETE FROM objects WHERE ObjectParcelUUID = ?");
            $QueueStatement->execute(array($deleteRow['ParcelUUID'])); 
        }

        $QueueStatement = $RUNTIME['PDO']->prepare("DELETE FROM parcels WHERE ParcelRegionUUID = ?");
        $QueueStatement->execute(array($row['UUID'])); 

        //Add Parcels
        foreach ($data['Parcels'] as $Parcel) 
        {
            //Add User to DB if not exist
            $QueueStatement = $RUNTIME['PDO']->prepare("INSERT IGNORE INTO users VALUES (?, ?, ?)");
            $QueueStatement->execute(array($Parcel['Owner']['UUID'], $Parcel['Owner']['Name'], "")); 

            //Add Parcel to DB if not exist
            $QueueStatement = $RUNTIME['PDO']->prepare("INSERT IGNORE INTO parcels VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            $QueueStatement->execute(array($Parcel['Parent'], $Parcel['UUID'], $Parcel['Name'], $Parcel['Description'], $Parcel['Sale'], $Parcel['InSearch'], $Parcel['Price'], $Parcel['Owner']['UUID'], $Parcel['Image'], $Parcel['Bitmap'], $Parcel['Traffic'])); 
        }

        //Add Objects
        if(count($data['Objects']) != 0)
        {
            $rawSQLString = "INSERT IGNORE INTO objects VALUES ";
            $rawSQLData = array();
            foreach ($data['Objects'] as $Object) 
            {
                $rawSQLString .= " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?),";
                array_push($rawSQLData, $Object['UUID'], $Object['Parent'], $Object['Name'], $Object['Description'], $Object['Image'], $Object['InSearch'], $Object['Sale'], $Object['Price'], $Object['Copy'], $Object['Position'], $Object['Owner']['UUID']);

                if(isset($Object['Textures']) && count($Object['Textures']) != 0)
                {
                    $rawTextureSQLString = "INSERT IGNORE INTO textures VALUES ";
                    $rawTextureSQLData = array();
                    foreach ($Object['Textures'] as $Texture) 
                    {
                        $rawTextureSQLString .= " (?, ?),";
                        array_push($rawTextureSQLData, $Object['UUID'], $Texture);
                    }

                    $QueueImageStatement = $RUNTIME['PDO']->prepare(rtrim($rawTextureSQLString, ","));
                    $QueueImageStatement->execute($rawTextureSQLData); 
                }
            }

            $QueueStatement = $RUNTIME['PDO']->prepare(rtrim($rawSQLString, ","));
            $QueueStatement->execute($rawSQLData); 
        }
    }
}

?>