Skip to content

Commit

Permalink
Work on server side of saving game to server.
Browse files Browse the repository at this point in the history
  • Loading branch information
MPetrovic committed Nov 2, 2011
1 parent 3f62706 commit a144d60
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
5 changes: 5 additions & 0 deletions srv/db.main.inc
@@ -0,0 +1,5 @@
<?php

require_once('db.auth.inc');
require_once('db.'.$dbType.'.inc');
?>
41 changes: 39 additions & 2 deletions srv/db.mysql.inc
Expand Up @@ -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);
Expand All @@ -22,7 +21,7 @@ if (!mysql_select_db($db)) {
'id' => array(
'type' => 'int',
'length' => 11,
'index' => 'primary',
'index' => 'primary key',
'autoincrement' => false,
),
'data' => array(
Expand All @@ -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);
}

/**@
Expand All @@ -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];
}

?>
48 changes: 47 additions & 1 deletion srv/save.php
@@ -1,5 +1,51 @@
<?php
include('db.mysql.inc');
$db = mysql_real_escape_string($_POST['game']);
include('db.main.inc');

$table = 'save';
$fields = array(
'key' => 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);
?>

0 comments on commit a144d60

Please sign in to comment.