Permalink
Browse files

websockets are functional!

removed the ghost's limited vision (it was client side based only anyway)
fixed some small bugs
  • Loading branch information...
1 parent a257b7c commit 360762f96414cbaedcf449847f14682be1dcd778 @ttasterisco committed May 7, 2012
View
3 dartman-client/client.dart
@@ -22,8 +22,9 @@ class Client {
WEB_SOCKET.on.message.add((m) {
Map response = JSON.parse(m.data);
String cmd = response["command"];
+ print("response data: ${response["data"]}");
if(RESPONSE_CALLBACKS.containsKey(cmd))
- RESPONSE_CALLBACKS[cmd](JSON.parse(response["data"]));
+ RESPONSE_CALLBACKS[cmd](response["data"]);
else
print("invalid command from server ${cmd}");
});
View
10 dartman-client/dartmanClient.dart
@@ -25,7 +25,7 @@ class Dartman {
}
void login(String pn, String pt) {
- this.PLAYER_NAME = pn;
+ PLAYER_NAME = pn;
CLIENT.connect(pn, pt);
}
@@ -62,16 +62,12 @@ class Dartman {
void drawBoard() {
print("drawing map");
- Player player = this.PLAYERS[this.PLAYER_NAME];
-
- if(player.type.compareTo("dartman") == 0) GAME_WORLD.drawMap(this.PLAYERS[this.PLAYER_NAME].GetPoint(), 100);
- else GAME_WORLD.drawMap(this.PLAYERS[this.PLAYER_NAME].GetPoint(), RADIUS);
+ GAME_WORLD.drawMap();
print("drawing players");
PLAYERS.forEach((k, v) {
print("x: ${v.x}, y: ${v.y}, type: ${v.type}");
- if(player.type.compareTo("dartman") == 0) GAME_WORLD.draw_character(v.x, v.y, v.type[0], this.PLAYERS[this.PLAYER_NAME].GetPoint(), 100);
- else GAME_WORLD.draw_character(v.x, v.y, v.type[0], this.PLAYERS[this.PLAYER_NAME].GetPoint(), RADIUS);
+ GAME_WORLD.drawCharacter(v.x, v.y, v.type[0]);
});
}
}
View
2 dartman-client/dartmanClient.html
@@ -15,7 +15,7 @@ <h2 id="status">dart is not running</h2>
<br />
<label>Username</label>
- <input type="text" name="player_name"></input>
+ <input type="text" name="player_name" value="p1"></input>
<label>Character</label>
<select id="player_type">
<option value="dartman">Dartman</option>
View
77 dartman-client/grid.dart
@@ -6,83 +6,72 @@ class Grid {
int GAME_MAP_WIDTH;
List<List<int>> GAME_MAP;
- CanvasElement canvas;
- CanvasRenderingContext2D context;
+ CanvasElement CANVAS;
+ CanvasRenderingContext2D CONTEXT;
- Grid(List<List<int>> game_map) {
- GAME_MAP = game_map;
+ Grid(List<List<int>> gameMap) {
+ GAME_MAP = gameMap;
//grid width and height
GAME_MAP_HEIGHT = GAME_MAP.length * CELL_WIDTH ;
GAME_MAP_WIDTH = GAME_MAP[0].length * CELL_HEIGHT;
- canvas = document.query("#canvas");
- context = canvas.getContext("2d");
+ CANVAS = document.query("#canvas");
+ CONTEXT = CANVAS.getContext("2d");
- drawMap(new Point(0,0), 0);
+ drawMap();
}
- void drawMap(Point centralPoint, int radius) {
- draw_grid(centralPoint, radius);
- draw_walls(centralPoint, radius);
+ void drawMap() {
+ drawGrid();
+ drawWalls();
}
void clear() {
- context.clearRect(0, 0, GAME_MAP_WIDTH+100, GAME_MAP_HEIGHT+100);
+ CONTEXT.clearRect(0, 0, GAME_MAP_WIDTH+100, GAME_MAP_HEIGHT+100);
}
- void draw_grid(Point center, int radius) {
+ void drawGrid() {
clear();
// draw column lines
for (var x = 0; x <= GAME_MAP_HEIGHT; x += CELL_WIDTH) {
- context.moveTo(0.5 + x, 0);
- context.lineTo(0.5 + x, GAME_MAP_HEIGHT + 0);
+ CONTEXT.moveTo(0.5 + x, 0);
+ CONTEXT.lineTo(0.5 + x, GAME_MAP_HEIGHT + 0);
}
// draw column rows
for (var x = 0; x <= GAME_MAP_WIDTH; x += CELL_HEIGHT) {
- context.moveTo(0, 0.5 + x);
- context.lineTo(GAME_MAP_WIDTH, 0.5 + x);
+ CONTEXT.moveTo(0, 0.5 + x);
+ CONTEXT.lineTo(GAME_MAP_WIDTH, 0.5 + x);
}
- context.strokeStyle = "black";
- context.stroke();
+ CONTEXT.strokeStyle = "black";
+ CONTEXT.stroke();
}
- void draw_walls(Point center, int radius) {
- for (var x = 0; x < GAME_MAP.length; x++) {
- for (var y = 0; y < GAME_MAP[0].length; y++) {
- if(GAME_MAP[y][x] == 1) {
- draw_wall(y, x, center, radius);
- }
- }
- }
+ void drawWalls() {
+ for (var x = 0; x < GAME_MAP.length; x++)
+ for (var y = 0; y < GAME_MAP[0].length; y++)
+ if(GAME_MAP[y][x] == 1)
+ drawWall(y, x);
}
- void draw_wall(int cell_x, int cell_y, Point center, int radius) {
- if((Math.pow(cell_x - center.x, 2) > Math.pow(radius,2)) || Math.pow(cell_y - center.y, 2) > Math.pow(radius, 2)) {
- return;
- }
-
- var draw_coords = calculate_draw_position(cell_x, cell_y);
+ void drawWall(int cellX, int cellY) {
+ var drawCoords = calculateDrawPosition(cellX, cellY);
- context.fillRect(draw_coords[0], draw_coords[1], CELL_WIDTH, CELL_HEIGHT);
+ CONTEXT.fillRect(drawCoords[0], drawCoords[1], CELL_WIDTH, CELL_HEIGHT);
}
- void draw_character(int cell_x, int cell_y, String text, Point center, int radius) {
- if((Math.pow(cell_x - center.x, 2) > Math.pow(radius,2)) || Math.pow(cell_y - center.y, 2) > Math.pow(radius, 2)) {
- return;
- }
-
- var draw_coords = calculate_draw_position(cell_x, cell_y);
+ void drawCharacter(int cellX, int cellY, String text) {
+ var drawCoords = calculateDrawPosition(cellX, cellY);
- context.fillText(text, draw_coords[0] + (CELL_WIDTH/2) - (text.length*2), draw_coords[1] + (CELL_HEIGHT/2) + (text.length*2));
+ CONTEXT.fillText(text, drawCoords[0] + (CELL_WIDTH/2) - (text.length*2), drawCoords[1] + (CELL_HEIGHT/2) + (text.length*2));
}
- List<int> calculate_draw_position(int cell_x, int cell_y) {
- int draw_x = cell_x * CELL_WIDTH;
- int draw_y = GAME_MAP_HEIGHT - ((cell_y + 1) * CELL_HEIGHT);
+ List<int> calculateDrawPosition(int cellX, int cellY) {
+ int drawX = cellX * CELL_WIDTH;
+ int drawY = GAME_MAP_HEIGHT - ((cellY + 1) * CELL_HEIGHT);
- return [draw_x, draw_y];
+ return [drawX, drawY];
}
}
View
61 dartman-server/dartmanServer.dart
@@ -17,6 +17,7 @@ class Server {
final PORT = 8080;
HttpServer SERVER;
Map<int, RemoteClient> CONNECTIONS;
+ Map<int, String> TOKENS_PLAYERS;
int TOKEN = 0;
// game state
@@ -27,6 +28,7 @@ class Server {
Server() {
// game initializations
GAME = new GameState(GAME_MATRIX_SIZE_X, GAME_MATRIX_SIZE_Y);
+ TOKENS_PLAYERS = new Map<int, String>();
// server initializations
SERVER = new HttpServer();
@@ -43,37 +45,55 @@ class Server {
}
// request handlers
+ sendResponse(WebSocketConnection c, String cmd, var response) {
+ c.send(JSON.stringify({"command":cmd, "data":response}));
+ }
+
connect(Map params) {
print("add player to game state");
GAME.addPlayer(params["player_name"], params["player_type"]);
+ TOKENS_PLAYERS[params["token"]] = params["player_name"];
- print("return json response with the game world");
- return GAME.getWorld();
+ print("send response with the game world");
+ sendResponse(params["connection"], "connected", GAME.getWorld());
+
+ print("and a new message with the players");
+ sendPlayers();
}
move(Map params) {
print("update player coords");
var pos = GAME.updatePlayer(params["player_name"], params["direction"]);
if(pos["moved"]) {
- print("notify all players that world has changed");
- CONNECTIONS.forEach((token, remoteClient) {
- remoteClient.CLIENT_CONNECTION.send(JSON.stringify({"command":"refreshed", "data":GAME.getPlayers()}));
- });
+ print("sending new player information");
+ sendPlayers();
}
}
disconnect(Map params) {
- print("remove player from game state");
- CONNECTIONS.remove(params["token"]);
- GAME.removePlayer(params["player_name"]);
-
- return "OK";
+ print("remove player from game & server state");
+ closed(params["token"]);
}
closed(int token) {
// remove token's associated player from the state
+ String playerName = TOKENS_PLAYERS.remove(token);
+ print("player ${playerName}, ${token} has left");
CONNECTIONS.remove(token);
+ GAME.removePlayer(playerName);
+
+ print("notify all players that a player has left");
+ sendPlayers();
+ }
+
+ sendPlayers() {
+ print("notify all players that world has changed");
+ if(!CONNECTIONS.isEmpty()) {
+ CONNECTIONS.forEach((token, remoteClient) {
+ remoteClient.CLIENT_CONNECTION.send(JSON.stringify({"command":"refreshed", "data":GAME.getPlayers()}));
+ });
+ }
}
}
@@ -90,36 +110,29 @@ class RemoteClient {
CLIENT_CONNECTION.onMessage = (message) {
Map params = JSON.parse(message);
params["token"] = CLIENT_TOKEN;
+ params["connection"] = CLIENT_CONNECTION;
- var response;
- String cmd = params["command"];
- switch(cmd) {
+ switch(params["command"]) {
case "connect":
- response = callbacks[0](params);
- cmd = "connected";
+ callbacks[0](params);
break;
case "move":
callbacks[1](params);
break;
case "disconnect":
- response = callbacks[3](params);
- cmd = "disconnected";
- break;
- default:
- response = null;
+ callbacks[2](params);
break;
}
-
- if(response!=null) CLIENT_CONNECTION.send(JSON.stringify({"command":cmd, "data":response}));
};
CLIENT_CONNECTION.onClosed = (int status, String reason) {
print('closed with $status for $reason');
- callbacks[4](CLIENT_TOKEN);
+ callbacks[3](CLIENT_TOKEN);
};
CLIENT_CONNECTION.onError = (e) {
print('Error was $e');
+ callbacks[3](CLIENT_TOKEN);
};
}
}
View
13 dartman-server/gameState.dart
@@ -54,18 +54,18 @@ class GameState {
if(!PLAYERS.containsKey(n)) return {"x":0, "y":0, "moved":false};
Player p = PLAYERS[n];
- bool moved;
+ bool moved = false;
switch(d) {
- case "0":
+ case 0:
moved = movePlayerUp(n);
break;
- case "1":
+ case 1:
moved = movePlayerDown(n);
break;
- case "2":
+ case 2:
moved = movePlayerLeft(n);
break;
- case "3":
+ case 3:
moved = movePlayerRight(n);
break;
}
@@ -109,7 +109,8 @@ class GameState {
if(player.x == nextX && player.y == nextY) {
Player dead = player.type.compareTo("dartman") == 0 ? player : p;
removePlayer(dead.name);
- return false;
+
+ if(p.type == "dartman") return false;
}
}

0 comments on commit 360762f

Please sign in to comment.