
<?
  require 'at_dbconnect.php';
  require 'at_delete_expired.php';
  require 'at_archive_moves.php';
  //look for cookie
  $mymagnet=-1;
  $mymagnet_x=0;
  $mymagnet_y=0;
  if($_COOKIE['magnet'])
  {
     $mymagnet=$_COOKIE['magnet'];
  }
  $magnet_count=0;
  $magnet_id="";
  $magnet_x="";
  $magnet_y="";
  
  $ts=time();
  // Request all non-expired magnets
  $result = mysql_query(
                "SELECT ID FROM tblMagnets ".
                "WHERE UNIX_TIMESTAMP(Expires)>$ts ");

  if (!$result)
  {
      echo("error=4");
      exit();
  }
  while ( $row = mysql_fetch_array($result) )
  {
      $magid = $row["ID"];
      //select last move for each magnet
      $moved=mysql_query(
                 "SELECT ID, Magnet, Xpos, Ypos, Symbol FROM tblMoves,(SELECT MAX(MovedAt) AS lastmove FROM tblMoves GROUP BY Magnet) AS dismags WHERE tblMoves.MovedAt = dismags.lastmove AND Magnet=$magid");
      if (!$moved)
      {
         echo("error=4");
         exit();
      }
      
      while ( $row2 = mysql_fetch_array($moved) )
      {
         $magnet = $row2["Magnet"];
         $xpos   = $row2["Xpos"];
         $ypos   = $row2["Ypos"];
         $symbol   = $row2["Symbol"];
         if($magnet!=$mymagnet||count($row2)<2)
         {
             //echo("<P>$magnet - $xpos - $ypos - $symbol</P>");
             $magnet_count++;
             $magnet_id=$magnet_id.$magnet.",";
             $magnet_x=$magnet_x.$xpos.",";
             $magnet_y=$magnet_y.$ypos.",";

         }
         else
         {
             $mymagnet_x=$xpos;
             $mymagnet_y=$ypos;
         }
      }
   }
   $magnet_id=rtrim($magnet_id,",");
   $magnet_x=rtrim($magnet_x,",");
   $magnet_y=rtrim($magnet_y,",");
   echo("start=1&mymagnet=$mymagnet&mymagnet_x=$mymagnet_x&mymagnet_y=$mymagnet_y&magnet_count=$magnet_count&magnet_id=$magnet_id&magnet_x=$magnet_x&magnet_y=$magnet_y&end=1");

?>

