Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

websockets are functional!

removed the ghost's limited vision (it was client side based only anyway)
fixed some small bugs
  • Loading branch information...
commit 360762f96414cbaedcf449847f14682be1dcd778 1 parent a257b7c
ttasterisco authored May 07, 2012
3  dartman-client/client.dart
@@ -22,8 +22,9 @@ class Client {
22 22
     WEB_SOCKET.on.message.add((m) {
23 23
       Map response = JSON.parse(m.data);
24 24
       String cmd = response["command"];
  25
+      print("response data: ${response["data"]}");
25 26
       if(RESPONSE_CALLBACKS.containsKey(cmd))
26  
-        RESPONSE_CALLBACKS[cmd](JSON.parse(response["data"]));
  27
+        RESPONSE_CALLBACKS[cmd](response["data"]);
27 28
       else
28 29
         print("invalid command from server ${cmd}");
29 30
     });
10  dartman-client/dartmanClient.dart
@@ -25,7 +25,7 @@ class Dartman {
25 25
   }
26 26
   
27 27
   void login(String pn, String pt) {
28  
-    this.PLAYER_NAME = pn;
  28
+    PLAYER_NAME = pn;
29 29
     CLIENT.connect(pn, pt);
30 30
   }
31 31
   
@@ -62,16 +62,12 @@ class Dartman {
62 62
   
63 63
   void drawBoard() {
64 64
     print("drawing map");
65  
-    Player player = this.PLAYERS[this.PLAYER_NAME];
66  
-    
67  
-    if(player.type.compareTo("dartman") == 0) GAME_WORLD.drawMap(this.PLAYERS[this.PLAYER_NAME].GetPoint(), 100);
68  
-    else GAME_WORLD.drawMap(this.PLAYERS[this.PLAYER_NAME].GetPoint(), RADIUS);
  65
+    GAME_WORLD.drawMap();
69 66
     
70 67
     print("drawing players");
71 68
     PLAYERS.forEach((k, v) {
72 69
       print("x: ${v.x}, y: ${v.y}, type: ${v.type}");
73  
-      if(player.type.compareTo("dartman") == 0) GAME_WORLD.draw_character(v.x, v.y, v.type[0], this.PLAYERS[this.PLAYER_NAME].GetPoint(), 100);
74  
-      else GAME_WORLD.draw_character(v.x, v.y, v.type[0], this.PLAYERS[this.PLAYER_NAME].GetPoint(), RADIUS);
  70
+      GAME_WORLD.drawCharacter(v.x, v.y, v.type[0]);
75 71
     });
76 72
   }
77 73
 }
2  dartman-client/dartmanClient.html
@@ -15,7 +15,7 @@ <h2 id="status">dart is not running</h2>
15 15
 	<br />
16 16
 	
17 17
 	<label>Username</label>
18  
-	<input type="text" name="player_name"></input>
  18
+	<input type="text" name="player_name" value="p1"></input>
19 19
 	<label>Character</label>
20 20
 	<select id="player_type">
21 21
 		<option value="dartman">Dartman</option>
77  dartman-client/grid.dart
@@ -6,83 +6,72 @@ class Grid {
6 6
   int GAME_MAP_WIDTH;
7 7
   
8 8
   List<List<int>> GAME_MAP;
9  
-  CanvasElement canvas;
10  
-  CanvasRenderingContext2D context;
  9
+  CanvasElement CANVAS;
  10
+  CanvasRenderingContext2D CONTEXT;
11 11
   
12  
-  Grid(List<List<int>> game_map) {
13  
-    GAME_MAP = game_map;
  12
+  Grid(List<List<int>> gameMap) {
  13
+    GAME_MAP = gameMap;
14 14
     
15 15
     //grid width and height
16 16
     GAME_MAP_HEIGHT = GAME_MAP.length * CELL_WIDTH ;
17 17
     GAME_MAP_WIDTH = GAME_MAP[0].length * CELL_HEIGHT;
18 18
     
19  
-    canvas = document.query("#canvas");
20  
-    context = canvas.getContext("2d");
  19
+    CANVAS = document.query("#canvas");
  20
+    CONTEXT = CANVAS.getContext("2d");
21 21
     
22  
-    drawMap(new Point(0,0), 0);
  22
+    drawMap();
23 23
   }
24 24
  
25  
-  void drawMap(Point centralPoint, int radius) {
26  
-    draw_grid(centralPoint, radius);
27  
-    draw_walls(centralPoint, radius);
  25
+  void drawMap() {
  26
+    drawGrid();
  27
+    drawWalls();
28 28
   }
29 29
   
30 30
   void clear() {
31  
-    context.clearRect(0, 0, GAME_MAP_WIDTH+100, GAME_MAP_HEIGHT+100);
  31
+    CONTEXT.clearRect(0, 0, GAME_MAP_WIDTH+100, GAME_MAP_HEIGHT+100);
32 32
   }
33 33
   
34  
-  void draw_grid(Point center, int radius) {
  34
+  void drawGrid() {
35 35
     clear();
36 36
     // draw column lines
37 37
     for (var x = 0; x <= GAME_MAP_HEIGHT; x += CELL_WIDTH) {
38  
-        context.moveTo(0.5 + x, 0);
39  
-        context.lineTo(0.5 + x, GAME_MAP_HEIGHT + 0);
  38
+        CONTEXT.moveTo(0.5 + x, 0);
  39
+        CONTEXT.lineTo(0.5 + x, GAME_MAP_HEIGHT + 0);
40 40
     }
41 41
 
42 42
     // draw column rows
43 43
     for (var x = 0; x <= GAME_MAP_WIDTH; x += CELL_HEIGHT) {
44  
-        context.moveTo(0, 0.5 + x);
45  
-        context.lineTo(GAME_MAP_WIDTH, 0.5 + x);
  44
+        CONTEXT.moveTo(0, 0.5 + x);
  45
+        CONTEXT.lineTo(GAME_MAP_WIDTH, 0.5 + x);
46 46
     }
47 47
 
48  
-    context.strokeStyle = "black";
49  
-    context.stroke();
  48
+    CONTEXT.strokeStyle = "black";
  49
+    CONTEXT.stroke();
50 50
   }
51 51
   
52  
-  void draw_walls(Point center, int radius) {
53  
-    for (var x = 0; x < GAME_MAP.length; x++) {
54  
-      for (var y = 0; y < GAME_MAP[0].length; y++) {
55  
-        if(GAME_MAP[y][x] == 1) {
56  
-          draw_wall(y, x, center, radius);
57  
-        }
58  
-      }
59  
-    }
  52
+  void drawWalls() {
  53
+    for (var x = 0; x < GAME_MAP.length; x++)
  54
+      for (var y = 0; y < GAME_MAP[0].length; y++)
  55
+        if(GAME_MAP[y][x] == 1)
  56
+          drawWall(y, x);
60 57
   }
61 58
   
62  
-  void draw_wall(int cell_x, int cell_y, Point center, int radius) {
63  
-    if((Math.pow(cell_x - center.x, 2) > Math.pow(radius,2)) || Math.pow(cell_y - center.y, 2) > Math.pow(radius, 2)) {
64  
-      return; 
65  
-    }
66  
-    
67  
-    var draw_coords = calculate_draw_position(cell_x, cell_y);
  59
+  void drawWall(int cellX, int cellY) {
  60
+    var drawCoords = calculateDrawPosition(cellX, cellY);
68 61
     
69  
-    context.fillRect(draw_coords[0], draw_coords[1], CELL_WIDTH, CELL_HEIGHT);
  62
+    CONTEXT.fillRect(drawCoords[0], drawCoords[1], CELL_WIDTH, CELL_HEIGHT);
70 63
   }
71 64
   
72  
-  void draw_character(int cell_x, int cell_y, String text, Point center, int radius) {
73  
-    if((Math.pow(cell_x - center.x, 2) > Math.pow(radius,2)) || Math.pow(cell_y - center.y, 2) > Math.pow(radius, 2)) {
74  
-      return; 
75  
-    }
76  
-    
77  
-    var draw_coords = calculate_draw_position(cell_x, cell_y);
  65
+  void drawCharacter(int cellX, int cellY, String text) {
  66
+    var drawCoords = calculateDrawPosition(cellX, cellY);
78 67
     
79  
-    context.fillText(text, draw_coords[0] + (CELL_WIDTH/2) - (text.length*2), draw_coords[1] + (CELL_HEIGHT/2) + (text.length*2));
  68
+    CONTEXT.fillText(text, drawCoords[0] + (CELL_WIDTH/2) - (text.length*2), drawCoords[1] + (CELL_HEIGHT/2) + (text.length*2));
80 69
   }
81 70
     
82  
-  List<int> calculate_draw_position(int cell_x, int cell_y) {
83  
-    int draw_x = cell_x * CELL_WIDTH;
84  
-    int draw_y = GAME_MAP_HEIGHT - ((cell_y + 1) * CELL_HEIGHT);
  71
+  List<int> calculateDrawPosition(int cellX, int cellY) {
  72
+    int drawX = cellX * CELL_WIDTH;
  73
+    int drawY = GAME_MAP_HEIGHT - ((cellY + 1) * CELL_HEIGHT);
85 74
     
86  
-    return [draw_x, draw_y];
  75
+    return [drawX, drawY];
87 76
   }
88 77
 }
61  dartman-server/dartmanServer.dart
@@ -17,6 +17,7 @@ class Server {
17 17
   final PORT = 8080;
18 18
   HttpServer SERVER;
19 19
   Map<int, RemoteClient> CONNECTIONS;
  20
+  Map<int, String> TOKENS_PLAYERS;
20 21
   int TOKEN = 0;
21 22
   
22 23
   // game state
@@ -27,6 +28,7 @@ class Server {
27 28
   Server() {
28 29
     // game initializations
29 30
     GAME = new GameState(GAME_MATRIX_SIZE_X, GAME_MATRIX_SIZE_Y);
  31
+    TOKENS_PLAYERS = new Map<int, String>();
30 32
     
31 33
     // server initializations
32 34
     SERVER = new HttpServer();
@@ -43,12 +45,20 @@ class Server {
43 45
   }
44 46
   
45 47
   // request handlers
  48
+  sendResponse(WebSocketConnection c, String cmd, var response) {
  49
+    c.send(JSON.stringify({"command":cmd, "data":response}));
  50
+  }
  51
+
46 52
   connect(Map params) {
47 53
     print("add player to game state");
48 54
     GAME.addPlayer(params["player_name"], params["player_type"]);
  55
+    TOKENS_PLAYERS[params["token"]] = params["player_name"];
49 56
     
50  
-    print("return json response with the game world");
51  
-    return GAME.getWorld();
  57
+    print("send response with the game world");
  58
+    sendResponse(params["connection"], "connected", GAME.getWorld());
  59
+    
  60
+    print("and a new message with the players");
  61
+    sendPlayers();
52 62
   }
53 63
   
54 64
   move(Map params) {
@@ -56,24 +66,34 @@ class Server {
56 66
     var pos = GAME.updatePlayer(params["player_name"], params["direction"]);
57 67
     
58 68
     if(pos["moved"]) {
59  
-      print("notify all players that world has changed");
60  
-      CONNECTIONS.forEach((token, remoteClient) {
61  
-        remoteClient.CLIENT_CONNECTION.send(JSON.stringify({"command":"refreshed", "data":GAME.getPlayers()}));
62  
-      });
  69
+      print("sending new player information");
  70
+      sendPlayers();
63 71
     }
64 72
   }
65 73
   
66 74
   disconnect(Map params) {
67  
-    print("remove player from game state");
68  
-    CONNECTIONS.remove(params["token"]);
69  
-    GAME.removePlayer(params["player_name"]);
70  
-    
71  
-    return "OK";
  75
+    print("remove player from game & server state");
  76
+    closed(params["token"]);
72 77
   }
73 78
   
74 79
   closed(int token) {
75 80
     // remove token's associated player from the state
  81
+    String playerName = TOKENS_PLAYERS.remove(token);
  82
+    print("player ${playerName}, ${token} has left");
76 83
     CONNECTIONS.remove(token);
  84
+    GAME.removePlayer(playerName);
  85
+    
  86
+    print("notify all players that a player has left");
  87
+    sendPlayers();
  88
+  }
  89
+  
  90
+  sendPlayers() {
  91
+    print("notify all players that world has changed");
  92
+    if(!CONNECTIONS.isEmpty()) {
  93
+      CONNECTIONS.forEach((token, remoteClient) {
  94
+        remoteClient.CLIENT_CONNECTION.send(JSON.stringify({"command":"refreshed", "data":GAME.getPlayers()}));
  95
+      });
  96
+    }
77 97
   }
78 98
 }
79 99
 
@@ -90,36 +110,29 @@ class RemoteClient {
90 110
     CLIENT_CONNECTION.onMessage = (message) {
91 111
       Map params = JSON.parse(message);
92 112
       params["token"] = CLIENT_TOKEN;
  113
+      params["connection"] = CLIENT_CONNECTION;
93 114
       
94  
-      var response;
95  
-      String cmd = params["command"];
96  
-      switch(cmd) {
  115
+      switch(params["command"]) {
97 116
         case "connect":
98  
-          response = callbacks[0](params);
99  
-          cmd = "connected";
  117
+          callbacks[0](params);
100 118
           break;
101 119
         case "move":
102 120
           callbacks[1](params);
103 121
           break;
104 122
         case "disconnect":
105  
-          response = callbacks[3](params);
106  
-          cmd = "disconnected";
107  
-          break;
108  
-        default:
109  
-          response = null;
  123
+          callbacks[2](params);
110 124
           break;
111 125
       }
112  
-      
113  
-      if(response!=null) CLIENT_CONNECTION.send(JSON.stringify({"command":cmd, "data":response}));
114 126
     };
115 127
     
116 128
     CLIENT_CONNECTION.onClosed = (int status, String reason) {
117 129
       print('closed with $status for $reason');
118  
-      callbacks[4](CLIENT_TOKEN);
  130
+      callbacks[3](CLIENT_TOKEN);
119 131
     };
120 132
           
121 133
     CLIENT_CONNECTION.onError = (e) {
122 134
       print('Error was $e');
  135
+      callbacks[3](CLIENT_TOKEN);
123 136
     };
124 137
   }  
125 138
 }
13  dartman-server/gameState.dart
@@ -54,18 +54,18 @@ class GameState {
54 54
     if(!PLAYERS.containsKey(n)) return {"x":0, "y":0, "moved":false};
55 55
 
56 56
     Player p = PLAYERS[n];
57  
-    bool moved;
  57
+    bool moved = false;
58 58
     switch(d) {
59  
-      case "0":
  59
+      case 0:
60 60
         moved = movePlayerUp(n);
61 61
         break;
62  
-      case "1":
  62
+      case 1:
63 63
         moved = movePlayerDown(n);
64 64
         break;
65  
-      case "2":
  65
+      case 2:
66 66
         moved = movePlayerLeft(n);
67 67
         break;
68  
-      case "3":
  68
+      case 3:
69 69
         moved = movePlayerRight(n);
70 70
         break;
71 71
     }
@@ -109,7 +109,8 @@ class GameState {
109 109
         if(player.x == nextX && player.y == nextY) {
110 110
           Player dead = player.type.compareTo("dartman") == 0 ? player : p;
111 111
           removePlayer(dead.name);
112  
-          return false;
  112
+          
  113
+          if(p.type == "dartman") return false;
113 114
         }
114 115
     }
115 116
     

0 notes on commit 360762f

Please sign in to comment.
Something went wrong with that request. Please try again.