Permalink
Browse files

Merged upstream master into my master

  • Loading branch information...
csmeder committed Apr 1, 2012
2 parents a28c830 + 83282fa commit 8b8e20e4cd07b3d4c9d8ce4dd448f476b9bff79c
Showing with 435 additions and 262 deletions.
  1. +4 −0 Makefile
  2. +2 −0 main.js
  3. +13 −4 package.json
  4. +24 −0 static/js/helper.js
  5. +34 −0 static/js/helper_test.js
  6. +241 −0 static/js/simpleClient.js
  7. +47 −17 static/js/world.js
  8. +49 −0 static/js/world_test.js
  9. +1 −241 static/simpleClient.html
  10. +5 −0 test/Readme.md
  11. +15 −0 test/test.world.js
View
@@ -0,0 +1,4 @@
+test:
+ @./node_modules/.bin/mocha --ui tdd
+
+.PHONY: test
View
@@ -39,6 +39,8 @@ io.sockets.on('connection', function (socket) {
world.init();
socket.broadcast.emit('playerCount', {players: world.howManyPlayers()});
+ socket.emit('update', {"playaData": world.getGameState()});
+
socket.on('move', function(data) {
// Update where this player wants to "move"
data.id = socket.id;
View
@@ -1,8 +1,17 @@
{
"name": "NodeWorld",
"version": "0.0.1",
- "dependencies":{
- "socket.io": "0.8.7",
- "express": "2.5.8"
- }
+ "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/bnowel/NodeWorld.git"
+ },
+ "dependencies" :
+ { "socket.io" : "0.8.7"
+ , "express" : "2.5.8"
+ },
+ "devDependencies":
+ {
+ "mocha" : "1.0.0",
+ "asyncjs" : "0.0.6"
+ }
}
View
@@ -0,0 +1,24 @@
+var helper = {
+ getArrayElementIndexByPrpertyValue : function(arrIn, prop, val){
+ var i = -1,
+ len = arrIn.length;
+ if (len!=0){
+ for (i=0;i<len;i++){
+ var el = arrIn[i];
+ if (el.hasOwnProperty(prop) && el[prop] === val){
+ return i;
+ }
+ }
+ if (i === len){
+ return -1;
+ }
+ }
+ return i;
+ }
+}
+
+if (typeof exports !== 'undefined') {
+ if (typeof module !== 'undefined' && module.exports) {
+ exports = module.exports = helper;
+ }
+}
View
@@ -0,0 +1,34 @@
+var assert = require('assert')
+ ,helper = require("../../static/js/helper")
+
+if (typeof module !== "undefined" && module === require.main) {
+ var suiteExports = {};
+ var suite = function(a, fun){
+ fun();
+ }
+
+ var test = function(txt, testfun){
+ suiteExports["test "+ txt] = testfun;
+ }
+
+
+}
+
+var testArr = [
+ {},
+ {name:"findme"}
+];
+
+suite('Helper', function() {
+ test('find index of test elem with name "findme"', function() {
+ assert.equal(helper.getArrayElementIndexByPrpertyValue(testArr, "name", "findme"), 1);
+ });
+ test('find no elem with name:"dontfindme"', function() {
+ assert.equal(helper.getArrayElementIndexByPrpertyValue(testArr, "name", "dontfindme"), -1);
+ });
+});
+
+if (typeof module !== "undefined" && module === require.main) {
+ module.exports = suiteExports;
+ require("asyncjs").test.testcase(module.exports).exec()
+}
View
@@ -0,0 +1,241 @@
+
+// Array Remove - By John Resig (MIT Licensed)
+Array.prototype.remove = function(from, to) {
+ var rest = this.slice((to || from) + 1 || this.length);
+ this.length = from < 0 ? this.length + from : from;
+ return this.push.apply(this, rest);
+};
+
+function initCanvas() {
+ var canvas = document.getElementById("board");
+
+ return canvas.getContext("2d");
+}
+function drawSquare(c, pos, color) {
+ if (color) {
+ c.fillStyle = color;
+ } else {
+ c.fillStyle = "#FF0000";
+ }
+ c.fillRect(pos.x, pos.y, 19, 19);
+ //console.log("drawSquare");
+ //console.log(pos);
+}
+
+ $(function() {
+ var playas = [],
+ board = initCanvas();
+
+ //drawSquare(board, {x: 0, y: 0});
+ function worldToScreen(pos) {
+ return { x: pos.x * 20, y: pos.y * 20 };
+ }
+
+ function getNewPlaya(deets) {
+ //console.log(deets);
+ return $.extend({}, {id: 0, position: {x:0, y:0}, name: "", icon: $('<span class="icon"/>')}, deets);
+ }
+ function getPlaya(id) {
+ for (var i = 0, l = playas.length; i < l; i++) {
+ if (playas[i].id === id) {
+ return playas[i];
+ }
+ }
+ }
+ function killPlaya(id) {
+ for (var i = 0, l = playas.length; i < l; i++) {
+ if (playas[i].id === id) {
+ playas[i].icon.remove();
+ playas.remove(i);
+ return;
+ }
+ }
+ }
+ function updatePosition(newPos) {
+ var playa = getPlaya(newPos.id);
+
+ if (playa) {
+ playa.position = newPos.position;
+ }
+ else {
+ playa = getNewPlaya(newPos);
+ //$('body').append(playa.icon);
+ }
+
+ playa.icon.css({top: playa.position.y, left: playa.position.x});
+ playas.push(playa);
+ }
+
+
+ /*
+ * Functions to handle broadcasts
+ */
+
+ var socket = io.connect('/');
+
+ // playerJoined
+ socket.on('playerJoined', function(data) {
+ chat_console(data.name + " joined", data.color);
+ $('.js-colorBlock').css("background-color", data.color);
+ });
+
+ // updateName
+ socket.on('updateName', function(data) {
+ chat_console(data.oldName + " changed name to " + data.name, data.color);
+ });
+
+ // move
+ socket.on('move', function(data) {
+ updatePosition(data);
+ });
+
+ // chat
+ socket.on('chat' , function(data){
+ $chatLog = $("#chat-log");
+
+ //Check if we are at the bottom of the chat log
+ var atBottom = $chatLog[0].scrollHeight - $chatLog.scrollTop() <= $chatLog.outerHeight();
+
+ $("#chat-log").append($('<div/>').text(data.msg).prepend($('<span>').css("color",data.color).text(data.name + ": ")));
+
+ if(atBottom) { //if we were at the bottom of the chat log
+ //scroll chat log to new bottom
+ $chatLog.scrollTop($chatLog[0].scrollHeight);
+ }
+ });
+
+ // update positions
+ socket.on('update', function(data) {
+ //console.log(data.playaData[0].pos.x);
+ console.log(data);
+ for (var i=0, l=data.playaData.length; i < l; i++) {
+ // convert world to screen
+ var sp = worldToScreen(data.playaData[i].pos);
+ drawSquare(board, sp, data.playaData[i].color);
+ }
+
+ });
+
+ // playerDied
+ socket.on('playerDied', function(data) {
+ console.log("Player " + data.id + " died");
+ chat_console(data.name + " died", data.color);
+ });
+
+ // kill player
+ socket.on('died', function(data) {
+ //console.log('Player died: ' + data.name + JSON.stringify(data));
+ killPlaya(data.id);
+ });
+
+ // reset game
+ socket.on('resetGame', function() {
+ chat_console("game reset recieved", "#575757");
+
+ //clear board
+ board.clearRect(0, 0, 500, 500);
+ socket.emit("rejoinGame");
+ });
+
+
+ // rejoin game
+ socket.on('rejoinGame', function(data) {
+ chat_console(data.name + " rejoined game", data.color);
+
+ });
+ /*
+ * debug
+ */
+
+ // playerCount
+ socket.on('playerCount', function(data) {
+ console.log("player Count: " + data.players);
+ });
+
+ /*
+ * Helper functions
+ */
+
+ function chat_console(console_msg, color) {
+ color = color || '#ddd';
+ $("#chat-log").append($('<div/>').prepend($('<span>').css("color",color).text(console_msg)));
+ }
+
+ function bindSubmitOnEnter($txtBox, $submitButton) {
+ $txtBox.keypress(function(e){
+ if(e.which == 13){
+ $submitButton.triggerHandler("click");
+ }
+ });
+ }
+
+
+ /*
+ * View config
+ */
+
+ bindSubmitOnEnter($('#name'), $('#save'));
+
+ $("#save").click(function() {
+ var $name = $('#name'),
+ stuff = $.trim( $name.val() ),
+ words = stuff.split(" ");
+
+ if (stuff === "") {
+ $name.css({'background-color': '#faa', 'border-color': '#944'});
+ }
+ else if (words[0] != "god") {
+ socket.emit('name', { name: $name.val() });
+ $("#joined #joined-name").text( $name.val() );
+ $("#join").hide();
+ $("#joined").show();
+ }
+ else {
+ socket.emit('god', {message:words[1],arg1:words[2]})
+ }
+ });
+
+ $("#name").focus(function() {
+ $(this).css({'background-color': '', 'border-color': ''});
+ });
+
+ $("#edit-name").click(function() {
+ $("#joined").hide();
+ $("#join").show().focus();
+ });
+
+ bindSubmitOnEnter($('#chat-msg'), $('#send-chat-msg'));
+
+ $("#send-chat-msg").click(function() {
+ var messageFromPlayer = $("#chat-msg").val();
+ if(messageFromPlayer != "") {
+ socket.emit('chat', {message: messageFromPlayer});
+ }
+ $("#chat-msg").val("");
+ });
+
+ $("#reset-game").click(function() {
+ socket.emit('resetGame');
+ chat_console("game reset sent", "#444");
+ });
+
+ /*
+ * Game controls
+ */
+
+ // Only triggered once per press. If we want to know if they are holding it down
+ // we should look at keypress.
+ $(window).keydown(function(e) {
+ var dir;
+
+ switch (e.keyCode) {
+ case 37: dir = 'w'; break;
+ case 38: dir = 'n'; break;
+ case 39: dir = 'e'; break;
+ case 40: dir = 's'; break;
+ default: return;
+ }
+ socket.emit('dir', {'dir': dir});
+ });
+ });
+
Oops, something went wrong.

0 comments on commit 8b8e20e

Please sign in to comment.