implemented movement updates & room descriptions
[mudd.git] / mud.php
diff --git a/mud.php b/mud.php
index 74607f3..ed063dc 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,45 +134,73 @@ 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, 'poll' => $this->poll());
     }
 
     private function yell($msg) {
+        if (!$msg) {
+            $this->error('400', 'expected `msg` field');
+            return;
+        }
         $this->insert(
             'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
             'ssii', $msg, 'yell', $this->player->room, $this->player->id);
     }
 
-//    private function tell($msg) {
-//        // lookup dest
-//        $this->insert(
-//            'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
-//            'ssii', $msg, 'tell', $this->player->room, $this->player->id);
-//    }
-
     private function say($msg) {
+        if (!$msg) {
+            $this->error('400', 'expected `dest` field');
+            return;
+        }
         $this->insert(
             'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
             'ssii', $msg, 'say', $this->player->room, $this->player->id);
     }
 
-    private function move() {
-        // ahhhhhhhhhhhhhhhhhhhh
+    private function tell($dest, $msg) {
+        if (!$dest || !$msg) {
+            $this->error('400', 'expected `dest` and `msg` fields');
+            return;
+        }
+        $player = $this->query('SELECT id FROM players where name = ?', 's', $dest);
+        if (!($player && $player['id'])) {
+            $this->error('400', "could not find a player with `name` == $dest");
+            return;
+        }
+        $this->insert(
+            'INSERT INTO messages (message,type,destination,source) VALUES(?,?,?,?)',
+            'ssii', $msg, 'say', $player['id'], $this->player->id);
+    }
+
+    private function move($direction) {
+        $directions = array(
+            'north' => array('x' => $this->player->x,   'y' => $this->player->y-1),
+            'east'  => array('x' => $this->player->x+1, 'y' => $this->player->y),
+            'south' => array('x' => $this->player->x,   'y' => $this->player->y+1),
+            'west'  => array('x' => $this->player->x-1, 'y' => $this->player->y),
+        );
+        if ($directions[$direction]) {
+            $rooms = $this->query(
+                'SELECT id, state, description FROM rooms WHERE x = ? and y = ?',
+                'ii', $directions[$direction]['x'], $directions[$direction]['y']);
+            if ($rooms[0] && !$rooms[0]['state']) {
+                $this->update('UPDATE players SET room = ? WHERE players.id = ?',
+                    'si', $rooms[0]['id'], $this->player->id);
+                return array('description' => $rooms[0]['description']);
+            } else {
+                $this->error(403, 'your path is blocked');
+            }
+        } else
+            $this->error(400, 'expected `direction` field with value {north|east|south|west}');
     }
 
     private function poll() {
@@ -179,8 +215,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) {
@@ -197,22 +237,21 @@ class mud extends model {
         switch ($cmd) {
             case 'start':
                 $this->response($this->universe->serialize());
-//                $this->response($this->universe->serialize($_GET['restart']));
                 break;
             case 'join':
                 $this->response($this->join());
                 break;
             case 'move':
-                $this->response(array('valid' => $this->move()));
+                $this->response($this->move($_GET['direction']));
                 break;
             case 'tell':
-                $this->tell();
+                $this->tell($_GET['msg']);
                 break;
             case 'yell':
                 $this->yell($_GET['msg']);
                 break;
             case 'say':
-                $this->say();
+                $this->say($_GET['dest'], $_GET['msg']);
                 break;
             case 'tell':
                 $this->tell();
@@ -220,10 +259,6 @@ class mud extends model {
             case 'poll':
                 echo json_encode($this->poll());
                 break;
-//            case 'respawn':
-//                session_destroy()
-//                session_start()
-//                $this->response($this->join());
             default:
                 $this->error(400, 'Unknown command');
         }