pick a name when you join
authorDylan Lloyd <dylan@dylansserver.com>
Fri, 4 Oct 2013 07:18:44 +0000 (07:18 +0000)
committerDylan Lloyd <dylan@dylansserver.com>
Fri, 4 Oct 2013 07:18:44 +0000 (07:18 +0000)
index.php
mud.js
mud.php
style.css

index 8e8360e..1b6fa10 100644 (file)
--- a/index.php
+++ b/index.php
@@ -8,10 +8,11 @@
   </head>
   <body>
     <canvas width='450px' height='450px' id='universe'></canvas>
-    <div id="controls">
-        <input id='chat'><div class='button' id='submit'>submit</div>
-        <div id='log'></div>
+    <div id='controls'>
+        <input id='chat'>
         <div class='button' id='join'>join</div>
+        <div class='button' id='submit'>submit</div>
+        <div id='log'></div>
     </div>
   </body>
 </html>
diff --git a/mud.js b/mud.js
index 1fee512..2384832 100644 (file)
--- a/mud.js
+++ b/mud.js
@@ -105,8 +105,8 @@ $(document).ready(function(){
             }
         }
 
-        this.join = function() {
-            $.getJSON(endpoint, { 'cmd' : 'join' }, function(json) {
+        this.join = function(name) {
+            $.getJSON(endpoint, { 'cmd' : 'join', 'name' : name }, function(json) {
                 self.player = new player(json.x, json.y, json.id);
                 setInterval(self.poll, 1000);
             });
@@ -172,13 +172,23 @@ $(document).ready(function(){
         });
         $('#chat').keydown(function(e) {
             if (e.which == '13') {
-                $('#submit').click();
+                typeof self.player == 'undefined' ?
+                    $('#join').click() : $('#submit').click();
             } else if (e.which >= 37 && e.which <= 40)
                 e.stopPropagation();
         });
         $('#join').click(function() {
-            mud.join();
-            $(this).fadeOut('slow');
+            var chat = $('#chat');
+            if (chat.val() == '') {
+                chat.css('border-color', 'red');
+                return;
+            }
+            mud.join(chat.val());
+            chat.css('border-color', 'black').val('');
+            $(this).fadeOut('slow', function() {
+                $('#log, #submit').fadeIn('slow')
+                    .css('display', 'inline-block');
+            });
         });
 
         $(document).keydown(function(e) {
diff --git a/mud.php b/mud.php
index 74607f3..7306bd0 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 {
@@ -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);
     }
 
     private function yell($msg) {
@@ -150,13 +152,6 @@ class mud extends model {
             '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) {
         $this->insert(
             'INSERT INTO messages (message,type,room,source) VALUES(?,?,?,?)',
@@ -164,7 +159,6 @@ class mud extends model {
     }
 
     private function move() {
-        // ahhhhhhhhhhhhhhhhhhhh
     }
 
     private function poll() {
@@ -197,7 +191,6 @@ 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());
@@ -220,10 +213,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');
         }
index bfc06fe..dba9f27 100644 (file)
--- a/style.css
+++ b/style.css
@@ -35,11 +35,15 @@ body {
     margin-left: 25px;
 }
 
+#submit {
+    display: none;
+}
+
 #log {
+    display: none;
     margin: 10px 3px 15px 3px;
     padding: 5px 0;
     border: 3px solid black;
-    display: inline-block;
     width: 370px;
     height: 150px;
     overflow-y: auto;