OJD Commit: Fix node id reset
[eidogo-ojd.git] / php / ojd_progressive.php
blob16597e29294930945946fd280a5a7e85d00a6116
1 <?php
3 require("db.php");
4 require("json.php");
6 mysql_connect(DB_HOST, DB_USER, DB_PASS);
7 mysql_select_db(DB_NAME);
9 $sgf_start = array();
11 $id = (int)$_REQUEST['id'];
12 if (!$id) {
13 $id = 0; // show first tree by default
14 $sgf_start = array(
15 'GM' => 1, # Go
16 'FF' => 3, # SGFv3
17 'VW' => null, # whole board visible
18 'SO' => 'The Depths of Internet', # source
19 'EV' => null, # event
20 'PC' => null, # place
21 'SZ' => 19, # board size
22 'HA' => 0, # no handi
23 'ST' => 0, # variation display contorl
24 'DT' => null, # date
25 'KM' => 0, # komi
26 'GN' => 'Open Joseki Dictionary (until someone thinks of a cooler name)',
27 'CP' => 'Public Domain for now',
28 'GC' => 'Surely this is gonna be the next big thing in Go!');
32 function get_sgf_nodes($version)
34 $sgf_query = mysql_query("SELECT code, value FROM sgf_node WHERE tnode = '$version'");
35 $nodes = array('_V' => $version);
36 while ($node = mysql_fetch_assoc($sgf_query)) {
37 if ($nodes[$node['code']]) {
38 if (is_array($nodes[$node['code']])) {
39 array_push($nodes[$node['code']], $node['value']);
40 } else {
41 $nodes[$node['code']] = array($nodes[$node['code']], $node['value']);
43 } else {
44 $nodes[$node['code']] = $node['value'];
47 return $nodes;
51 $tree_query = mysql_query("SELECT tn.id AS id, tn.parent AS parent, tna.id AS version, tna.editorial AS editorial
52 FROM tree_node AS tn
53 LEFT JOIN tree_node_archive AS tna ON tna.id = tn.version
54 WHERE IFNULL(tn.parent, 0) = '".mysql_real_escape_string($id)."'");
55 $trees = array();
56 if (!$tree_query) {
57 echo "Error loading game data.";
58 } else {
59 while ($node = mysql_fetch_assoc($tree_query)) {
60 $tree['id'] = $node['id'];
61 $tree['parent'] = $node['parent']; if ($tree['parent'] == null) $tree['parent'] = 0;
62 $tree['nodes'] = array(array_merge($sgf_start, array_merge(array('_I' => $node['id'], 'C' => $node['editorial']), get_sgf_nodes($node['version']))));
63 $tree['trees'] = array();
64 $trees[] = $tree;
66 $json = new Services_JSON();
67 echo $json->encode(array(
68 "id" => $id,
69 "nodes" => array(),
70 "trees" => $trees,
71 ));