From a144d60c562891c864388b551f499029ceeed871 Mon Sep 17 00:00:00 2001 From: MPetrovic Date: Wed, 2 Nov 2011 15:13:16 -0400 Subject: [PATCH] Work on server side of saving game to server. --- srv/db.main.inc | 5 +++++ srv/db.mysql.inc | 41 +++++++++++++++++++++++++++++++++++++++-- srv/save.php | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 srv/db.main.inc diff --git a/srv/db.main.inc b/srv/db.main.inc new file mode 100644 index 000000000..6b160daba --- /dev/null +++ b/srv/db.main.inc @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/srv/db.mysql.inc b/srv/db.mysql.inc index 1714c992a..ecbbe7a79 100644 --- a/srv/db.mysql.inc +++ b/srv/db.mysql.inc @@ -3,7 +3,6 @@ * db.auth.php contains server specific parameters * It should never be made public */ -include('db.auth.php'); $conn = mysql_connect($server, $user, $pass); if (!mysql_select_db($db)) { mysql_create_db($db); @@ -22,7 +21,7 @@ if (!mysql_select_db($db)) { 'id' => array( 'type' => 'int', 'length' => 11, - 'index' => 'primary', + 'index' => 'primary key', 'autoincrement' => false, ), 'data' => array( @@ -34,6 +33,33 @@ if (!mysql_select_db($db)) { createTable('save', $fields); */ function createTable($name, $fields) { + $s = s("SELECT * FROM %s LIMIT 1", $name); + if (!($res = q($s))) { + // table doesn't exist yet + $s = "CREATE TABLE %s (%s)"; + $columns = array(); + foreach ($fields as $f_name => $f) { + $str = $f_name.' '.getType($f['type']); + if ($f['length']) $str.='('.$f['length'].')'; + if ($f['autoincrement']) $str.= ' AUTOINCREMENT'; + if ($f['index'] && $f['index'] != 'no') $str.= ' '.$f['index']; + $columns[] = $str; + } + + $s = s($s, $name, implode(', ', $columns)); + q($s); + } +} + +// shortcut for sprintf +function s() { + $args = func_get_args(); + $format = array_shift($args); + foreach ($args as &$arg) { + if (is_string($arg)) + $arg = mysql_real_escape_string($arg); + } + return vsprintf($format, $args); } /**@ @@ -59,4 +85,15 @@ function f($result) { return mysql_fetch_assoc($result); } +function getType($type) { + static $typeMap = array( + 'int' => 'INT', + 'string' => 'VARCHAR', + 'text' => 'TEXT', + 'float' => 'FLOAT', + ); + + return $typeMap[$type]; +} + ?> \ No newline at end of file diff --git a/srv/save.php b/srv/save.php index b31608711..b3a713376 100644 --- a/srv/save.php +++ b/srv/save.php @@ -1,5 +1,51 @@ array( + 'type' => 'string', + 'length' => 255, + 'index' => 'primary key', + ), + 'player' => array( + 'type' => 'int', + 'length' => 11, + ), + 'ts' => array( + 'type' => 'timestamp', + ), + 'data' => array( + 'type' => 'text', + ), +); +createTable($table, $fields); + +$output = array(); +switch ($_POST['mode']) { + case 'timestamps': + $s = s("SELECT key, ts AS timestamp FROM %s", $table); + $q = q($s); + while ($r = f($q)) { + $output[] = (object)$r; + } + break; + case 'save': + $r = f(q(s("SELECT COUNT(key) AS c FROM %s WHERE key = '%s'", $table, $_POST['key']))); + if ($r['c']) { + // update + q(s("UPDATE %s SET data = '%s', ts = %d WHERE key = '%s'", $table, serialize($_POST['data']), $_POST['timestamp'])); + } + else { + // insert + q(s("INSERT INTO %s VALUE('%s', 0, %d, '%s')", $table, $_POST['key'], $_POST['timestamp'], serialize($_POST['data']))); + } + break; + case 'load': + break; +} + +echo json_encode($output); ?> \ No newline at end of file