<?
//connect to database
require 'at_dbconnect.php';
//get parameters from URL
$magnet=$_GET['magnet'];
$xpos=$_GET['xpos'];
$ypos=$_GET['ypos'];
$symbol=$_GET['symbol'];
//create timestamp
$moveTime=time();

$get_latest=mysql_query("SELECT ID, UNIX_TIMESTAMP(MovedAt) AS lastseconds FROM tblMoves,(SELECT MAX(MovedAt) AS lastmove FROM tblMoves GROUP BY Magnet) AS dismags WHERE tblMoves.MovedAt = dismags.lastmove AND Magnet=$magnet");
if (!$get_latest) {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
    }
    while ( $row = mysql_fetch_array($get_latest) ) {
      $magnetid = $row["ID"];
      $lastmoved = $row["lastseconds"];
    }
//update if less than 5 minutes from last move
if (($moveTime-$lastmoved)<300)
{
   $add_move="UPDATE tblMoves SET Magnet=$magnet, Xpos=$xpos, Ypos=$ypos, Symbol=$symbol, MovedAt=FROM_UNIXTIME($moveTime)";
   $add_move.=" WHERE ID=$magnetid";
   if (mysql_query($add_move))
   {
      echo "Move added successfully";
   }
   else
   {
       echo("<P>Error adding move: " .
       mysql_error() . "</P>");
   }
}
//otherwise create a new entry for tracking purposes
else
{

    $add_move="INSERT INTO tblMoves (Magnet, Xpos, Ypos, Symbol, MovedAt)";
    $add_move.= " VALUES ($magnet, $xpos, $ypos, $symbol, FROM_UNIXTIME($moveTime))";
    if (mysql_query($add_move))
    {
       echo "Move added successfully";
    }
    else
    {
        echo("<P>Error adding move: " .
        mysql_error() . "</P>");
    }
}
?>

