show other folk in the world
[mudd.git] / mud.php
diff --git a/mud.php b/mud.php
index 9e347a3..7dae853 100644 (file)
--- a/mud.php
+++ b/mud.php
@@ -49,6 +49,14 @@ abstract class model {
         return $this->db->insert_id;
     }
 
+    public function update() {
+        $args = func_get_args();
+        $statement = $this->db->prepare(array_shift($args));
+        call_user_func_array(array($statement, 'bind_param'), &$args);
+        $statement->execute();
+        return $this->db->insert_id;
+    }
+
 }
 
 class universe {
@@ -57,7 +65,7 @@ class universe {
     public $rooms = array();
 
     public function __construct($db) {
-        $rooms = $db->query("SELECT id,x,y,state FROM rooms");
+        $rooms = $db->query('SELECT id,x,y,state FROM rooms');
         if ($rooms) {
             foreach ($rooms as $room) {
                 $this->rooms[$room['x']][$room['y']] =
@@ -126,22 +134,16 @@ class mud extends model {
     }
 
     private function join() {
-        $x = 0;
-        $y = 0;
-        if (isset($_SESSION['id'])) {
-            $x = $this->player->x;
-            $y = $this->player->y;
-            $id = $this->player->id;
-        } else {
-            do {
-                $x = rand(0, count($this->universe->rooms)-1);
-                $y = rand(0, count($this->universe->rooms[0])-1);
-            } while ($this->universe->rooms[$x][$y]['state']);
-            $id = $this->insert('INSERT INTO players (name,room) VALUES(?,?)',
-                                'si', 'majuscule', $this->universe->rooms[$x][$y]['id']);
-            $_SESSION['id'] = $id;
-        }
-        return array('x' => $x, 'y' => $y, 'id' => $id);
+        $x = $y = 0;
+        do {
+            $x = rand(0, count($this->universe->rooms)-1);
+            $y = rand(0, count($this->universe->rooms[0])-1);
+        } while ($this->universe->rooms[$x][$y]['state']);
+        $id = $this->insert('INSERT INTO players (name,room) VALUES(?,?)',
+                            'si', $_GET['name'], $this->universe->rooms[$x][$y]['id']);
+        $_SESSION['id'] = $id;
+        //$others = $this->query('SELECT id, room FROM players WHERE id != ?', 'i', $this->player->id);
+        return array('x' => $x, 'y' => $y, 'id' => $id, 'name' => $name, 'poll' => $this->poll());
     }
 
     private function yell($msg) {
@@ -150,6 +152,15 @@ class mud extends model {
             'ssii', $msg, 'yell', $this->player->room, $this->player->id);
     }
 
+    private function say($msg) {
+        $this->insert(
+            'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
+            'ssii', $msg, 'say', $this->player->room, $this->player->id);
+    }
+
+    private function move() {
+    }
+
     private function poll() {
         $time = isset($_SESSION['last_polled']) ? $_SESSION['last_polled'] : 0;
         $messages = $this->query(
@@ -162,8 +173,12 @@ class mud extends model {
                 . ' OR (type = "tell" AND destination = ?)'
                 . ' OR (type = "say" AND messages.room = ?))',
             'iii', $time, $this->player->id, $this->player->room);
+        $players = $this->query(
+            'SELECT players.id, name, x ,y '
+            . 'FROM players JOIN rooms '
+            . 'ON players.room = rooms.id');
         $_SESSION['last_polled'] = time();
-        return $messages;
+        return array('messages' => $messages, 'players' => $players);
     }
 
     public function response($content) {