<html> <head> <title>Swarm - Vicinity Testing</title> </head> <body> <?php /* Tarim / April 2008 / for Swarm MediaSandbox Project */ /* Tweak these to get the desired effects... */ // Read in $db, $dbuser, $dbpasswd include 'consts.php'; // These are the boundaries on the range _squared_ $InnerRangeSquared = 3; $OuterRangeSquared = 4; // The root and data elements around nearby output $Root = 'document'; $Nearby = 'nearby'; $Device = 'device'; $Time = 'time'; // Uncomment this to see the XML // header("Content-Type: Text/Plain;"); /* Define a few functions */ // Return a get option if it exists, else null function get_opt($key) { return array_key_exists($key, $_GET) ? $_GET[$key] : null; } // Output the results of a query as XML function xml_query($query, $element, $root) { ($result = mysql_query($query)) || die("Select $element: " . mysql_error()); if ($root) echo "<${root}>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo " <${element}> "; foreach ($line as $name=>$value) { echo " <${name}>${value}</${name}>"; } echo " </${element}>\n"; } if ($root) echo "</${root}>\n"; mysql_free_result($result); } /* Start the code for real */ // Connect to database mysql_connect(null, $dbuser, $dbpasswd) || die('Connect database: ' . mysql_error()); mysql_select_db($db) || die('Select database: ' . mysql_error()); // if we are adding a player then insert row into Players and Connections if($player = get_opt('add')) { $name = get_opt('name'); $xpos = get_opt('x'); $ypos = get_opt('y'); mysql_query(" INSERT INTO Players ( Player, Name, Xpos, Ypos ) VALUES (${player}, '${name}', ${xpos}, ${ypos}) ") || die('Add player: ' . mysql_error()); mysql_query(" INSERT INTO Connections ( Player1, Player2 ) SELECT ${player} AS NewPlayer, Players.Player FROM Players WHERE Players.Player <> ${player} ") || die('Add connection: ' . mysql_error()); } // Delete player from players and connections tables if ($player = get_opt('delete')) { mysql_query(" DELETE FROM Players WHERE Players.Player = ${player} ") || die('Delete player: ' . mysql_error()); mysql_query(" DELETE FROM Connections WHERE Connections.Player1 = ${player} OR Connections.Player2 = ${player} ") || die('Delete connection: ' . mysql_error()); } // Update connections table when there are new co-ordinates from a player if (($player = get_opt('update')) || ($player = get_opt('add'))) { $xpos = get_opt('x'); $ypos = get_opt('y'); mysql_query(" UPDATE Players SET Xpos = ${xpos}, Ypos = ${ypos} WHERE Player = $player ") || die('Update player: ' . mysql_error()); mysql_query(" UPDATE (Connections INNER JOIN Players AS Players_1 ON Connections.Player1 = Players_1.Player) INNER JOIN Players AS Players_2 ON Connections.Player2 = Players_2.Player SET Connections.FirstContact = if(isnull(FirstContact), now(), null) WHERE (${player} = Connections.Player1 Or ${player} = Connections.Player2) AND ( (Players_1.Xpos - Players_2.Xpos)*(Players_1.Xpos - Players_2.Xpos) + (Players_1.Ypos - Players_2.Ypos)*(Players_1.Ypos - Players_2.Ypos) < if(isnull(FirstContact), ${InnerRangeSquared}, ${OuterRangeSquared})) = isnull(FirstContact) ") || die('Update connection: ' . mysql_error()); } if ($player = get_opt('update')) { xml_query(" SELECT if($player = Player2, Player1, Player2) AS $Device, FirstContact AS $Time FROM Connections WHERE ($player = Player1 or $player = Player2) and not isnull(FirstContact) ORDER BY Player1, Player2 ", $Nearby, $Root); } else { // Display the complete players table xml_query("SELECT * from Players ORDER BY Player", 'playerdata', 'pre'); // Display the complete connections table xml_query("SELECT * from Connections ORDER BY Player1, Player2", 'connections', 'pre'); } ?> <hr/> <address>Tarim / Vicinity for Swarm</address> </body> </html>