Permalink
Browse files

updated communitcatipon w/ frontend.

  • Loading branch information...
1 parent fe3b15f commit f894fd93ecd2deba8173513b195dfe36255084ec Seye Ojumu committed Aug 27, 2011
Showing with 40 additions and 20 deletions.
  1. +6 −3 app.coffee
  2. +6 −3 app.js
  3. +6 −3 game.coffee
  4. +13 −2 game.js
  5. +3 −5 static/js/client.coffee
  6. +6 −4 static/js/client.js
View
@@ -26,8 +26,10 @@ game.init()
io.sockets.on "connection", (socket) ->
+ console.log "connected"
+
player = game.create_player()
- game.add_player player.id
+ game.add_player player
socket.emit "gamestate", game.get_state()
socket.on "update", (data) ->
@@ -37,8 +39,9 @@ io.sockets.on "connection", (socket) ->
game.rem_player player.id
loopt = () ->
- console.log "loopt"
- io.sockets.emit "gamestate", game.tick()
+ state = game.tick()
+ console.log "loopt", state
+ io.sockets.emit "gamestate", state
setInterval loopt, game.SPEED
View
@@ -19,8 +19,9 @@
game.init();
io.sockets.on("connection", function(socket) {
var player;
+ console.log("connected");
player = game.create_player();
- game.add_player(player.id);
+ game.add_player(player);
socket.emit("gamestate", game.get_state());
socket.on("update", function(data) {
return console.log(data);
@@ -30,8 +31,10 @@
});
});
loopt = function() {
- console.log("loopt");
- return io.sockets.emit("gamestate", game.tick());
+ var state;
+ state = game.tick();
+ console.log("loopt", state);
+ return io.sockets.emit("gamestate", state);
};
setInterval(loopt, game.SPEED);
app.listen(8000);
View
@@ -4,7 +4,7 @@ _ = require("underscore")
players = {}
directions = {}
-FPS = 1
+FPS = .5
SPEED = 1000 / FPS
defaults =
@@ -32,7 +32,7 @@ rem_player = (pid) ->
delete players[pid]
get_state = () ->
- _.values players
+ (x.coords for x in _.values players)
tick = () ->
for pid, player in players
@@ -51,4 +51,7 @@ set_direction = (player) ->
exports.init = init
exports.SPEED = SPEED
exports.tick = tick
-
+exports.create_player = create_player
+exports.add_player = add_player
+exports.rem_player = rem_player
+exports.get_state = get_state
View
@@ -3,7 +3,7 @@
_ = require("underscore");
players = {};
directions = {};
- FPS = 1;
+ FPS = .5;
SPEED = 1000 / FPS;
defaults = {
snakes: {
@@ -41,7 +41,14 @@
return delete players[pid];
};
get_state = function() {
- return _.values(players);
+ var x, _i, _len, _ref, _results;
+ _ref = _.values(players);
+ _results = [];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ x = _ref[_i];
+ _results.push(x.coords);
+ }
+ return _results;
};
tick = function() {
var pid, player, _len;
@@ -63,4 +70,8 @@
exports.init = init;
exports.SPEED = SPEED;
exports.tick = tick;
+ exports.create_player = create_player;
+ exports.add_player = add_player;
+ exports.rem_player = rem_player;
+ exports.get_state = get_state;
}).call(this);
@@ -16,9 +16,7 @@ $ ->
# Start socket.io
- #socket = io.connect()
- #io.sockets.on 'connection', (socket) ->
- # console.log 'connected'
+ socket = io.connect()
- #socket.on 'news', (data) ->
- # console.log data
+ socket.on "gamestate", (data) ->
+ console.log data
View
@@ -1,9 +1,8 @@
(function() {
$(function() {
- var box, height, row, width, x, y, _results;
+ var box, height, row, socket, width, x, y;
width = 20;
height = 10;
- _results = [];
for (x = 1; 1 <= height ? x <= height : x >= height; 1 <= height ? x++ : x--) {
row = $("#row-template").clone();
for (y = 1; 1 <= width ? y <= width : y >= width; 1 <= width ? y++ : y--) {
@@ -14,8 +13,11 @@
}
row.removeAttr('id');
row.show();
- _results.push($(".game-board").append(row));
+ $(".game-board").append(row);
}
- return _results;
+ socket = io.connect();
+ return socket.on("gamestate", function(data) {
+ return console.log(data);
+ });
});
}).call(this);

0 comments on commit f894fd9

Please sign in to comment.