Master server changes to add ID to schema.

This commit is contained in:
Matthew Bowra-Dean
2010-05-27 01:10:45 +12:00
parent e7c7a117a8
commit 1839655eb5
3 changed files with 82 additions and 78 deletions

View File

@@ -1,15 +1,19 @@
<?php <?php
header( 'Content-type: text/plain' );
if ($db = sqlite_open('openra.db', 0666, $e)) try
{ {
echo 'sqlite_open ok.'; $db = new PDO('sqlite:openra.db');
sqlite_query( $sb, 'DROP TABLE servers' ); echo 'Connection to DB established.\n';
sqlite_query( $db, 'CREATE TABLE servers (name varchar(255), address varchar(255), players integer, state integer, ts integer, map varchar(255), mods varchar(255))' ); if ($db->query('DROP TABLE servers'))
sqlite_close( $db ); echo 'Dropped table.\n';
} $schema = 'CREATE TABLE servers (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar(255),
else address varchar(255) UNIQUE, players integer, state integer, ts integer, map varchar(255), mods varchar(255))';
{ if ($db->query($schema))
echo $e; echo 'Created table.';
} $db = null;
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?> ?>

View File

@@ -1,28 +1,26 @@
<?php <?php
header( 'Content-type: text/plain' ); header( 'Content-type: text/plain' );
if (!($db = sqlite_open( 'openra.db', 0666, $e ))) try
{ {
echo 'Database error: ', $e; $db = new PDO('sqlite:openra.db');
return; $stale = 60 * 5;
} $result = $db->query('SELECT * FROM servers WHERE (' . time() . ' - ts < ' . $stale . ')');
foreach ( $result as $row )
$stale = 60 * 5; {
$result = sqlite_query( $db, 'SELECT * FROM servers WHERE (' . time() . ' - ts < ' . $stale . ')' ); echo "Game@" . $row['id'] . ":\n";
echo "\tName: " . $row['name'] . "\n";
$rows = sqlite_fetch_all( $result, SQLITE_ASSOC ); echo "\tAddress: " . $row['address'] . "\n";
echo "\tState: " . $row['state'] . "\n";
$n = 0; echo "\tPlayers: " . $row['players'] . "\n";
foreach( $rows as $a ) { echo "\tMap: " . $row['map'] . "\n";
echo "Game@" . $n++ . ":\n"; echo "\tMods: " . $row['mods'] . "\n";
echo "\tName: " . $a['name'] . "\n"; echo "\tTTL: " . ($stale - (time() - $row['ts'])) . "\n";
echo "\tAddress: " . $a['address'] . "\n"; }
echo "\tState: " . $a['state'] . "\n"; $db = null;
echo "\tPlayers: " . $a['players'] . "\n"; }
echo "\tMap: " . $a['map'] . "\n"; catch (PDOException $e)
echo "\tMods: " . $a['mods'] . "\n"; {
echo "\tTTL: " . ($stale - (time() - $a['ts'])) . "\n"; echo $e->getMessage();
} }
sqlite_close( $db );
?> ?>

View File

@@ -1,38 +1,40 @@
<?php <?php
header( 'Content-type: text/plain' );
if (!($db = sqlite_open( 'openra.db', 0666, $e ))) try
{ {
echo 'Database error: ', $e; $db = new PDO('sqlite:openra.db');
return; $addr = $_SERVER['REMOTE_ADDR'] . ':' . $_REQUEST['port'];
}
$insert = $db->prepare('INSERT OR REPLACE INTO servers
$addr = $_SERVER['REMOTE_ADDR'] . ':' . $_REQUEST['port']; (name, address, players, state, ts, map, mods)
$prune = 'DELETE FROM servers WHERE address = \'' . sqlite_escape_string( $addr ) . '\''; VALUES (:name, :addr, :players, :state, :time, :map, :mods)');
echo $prune . "\n\n"; $insert->bindValue(':name', $_REQUEST['name'], PDO::PARAM_STR);
$insert->bindValue(':addr', $addr, PDO::PARAM_STR);
sqlite_exec( $db, $prune ); $insert->bindValue(':players', $_REQUEST['players'], PDO::PARAM_INT);
$insert->bindValue(':state', $_REQUEST['state'], PDO::PARAM_INT);
$q = 'INSERT INTO servers VALUES ('. $insert->bindValue(':time', time(), PDO::PARAM_INT);
'\'' . sqlite_escape_string( $_REQUEST['name'] ) . '\', '. $insert->bindValue(':map', $_REQUEST['map'], PDO::PARAM_STR);
'\'' . sqlite_escape_string( $addr ) . '\', '. $insert->bindValue(':mods', $_REQUEST['mods'], PDO::PARAM_STR);
sqlite_escape_string( $_REQUEST['players'] ) . ', '.
sqlite_escape_string( $_REQUEST['state'] ) . ', '. $insert->execute();
time() . ', '.
'\'' . sqlite_escape_string( $_REQUEST['map'] ) . '\', '. if (isset( $_REQUEST['new']))
'\'' . sqlite_escape_string( $_REQUEST['mods'] ) . '\')'; {
$select = $db->prepare('SELECT id FROM servers WHERE address = :addr');
echo $q; $select->bindValue(':addr', $addr, PDO::PARAM_STR);
if (!sqlite_exec( $db, $q )) $select->execute();
{
echo 'Error in query:' . sqlite_error_string( sqlite_last_error() ); echo (int)$select->fetchColumn();
}
$games = file_get_contents("../games.txt");
sqlite_close( $db ); file_put_contents("../games.txt", $games + 1);
}
if (isset( $_REQUEST['new']))
{ $db = null;
$games = file_get_contents("../games.txt"); }
file_put_contents("../games.txt", $games + 1); catch (PDOException $e)
} {
echo $e->getMessage();
}
?> ?>