Skip to content

Commit

Permalink
Adding initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FuzzYspo0N committed Jul 11, 2012
1 parent d25dfc0 commit 71218a9
Show file tree
Hide file tree
Showing 8 changed files with 1,907 additions and 0 deletions.
130 changes: 130 additions & 0 deletions app.js
@@ -0,0 +1,130 @@
/* Copyright (c) 2012 Sven "FuzzYspo0N" Bergström
http://underscorediscovery.com
MIT Licensed. See LICENSE for full license.
Usage : node simplest.app.js
*/

var
gameport = process.env.PORT || 4004,

io = require('socket.io'),
express = require('express'),
UUID = require('node-uuid'),

verbose = false,
app = express.createServer();

/* Express server set up. */

//The express server handles passing our content to the browser,
//As well as routing users where they need to go. This example is bare bones
//and will serve any file the user requests from the root of your web server (where you launch the script from)
//so keep this in mind - this is not a production script but a development teaching tool.

//Tell the server to listen for incoming connections
app.listen( gameport );

//Log something so we know that it succeeded.
console.log('\t :: Express :: Listening on port ' + gameport );

//By default, we forward the / path to index.html automatically.
app.get( '/', function( req, res ){
res.sendfile( __dirname + '/simplest.html' );
});


//This handler will listen for requests on /*, any file from the root of our server.
//See expressjs documentation for more info on routing.

app.get( '/*' , function( req, res, next ) {

//This is the current file they have requested
var file = req.params[0];

//For debugging, we can track what files are requested.
if(verbose) console.log('\t :: Express :: file requested : ' + file);

//Send the requesting client the file.
res.sendfile( __dirname + '/' + file );

}); //app.get *


/* Socket.IO server set up. */

//Express and socket.io can work together to serve the socket.io client files for you.
//This way, when the client requests '/socket.io/' files, socket.io determines what the client needs.

//Create a socket.io instance using our express server
var sio = io.listen(app);

//Configure the socket.io connection settings.
//See http://socket.io/
sio.configure(function (){

sio.set('log level', 0);

sio.set('authorization', function (handshakeData, callback) {
callback(null, true); // error first callback style
});

});

//Enter the game server code. The game server handles
//client connections looking for a game, creating games,
//leaving games, joining games and ending games when they leave.
game_server = require('./simplest.game.server.js');

//Socket.io will call this function when a client connects,
//So we can send that client looking for a game to play,
//as well as give that client a unique ID to use so we can
//maintain the list if players.
sio.sockets.on('connection', function (client) {

//Generate a new UUID, looks something like
//5b2ca132-64bd-4513-99da-90e838ca47d1
//and store this on their socket/connection
client.userid = UUID();

//tell the player they connected, giving them their id
client.emit('onconnected', { id: client.userid } );

//now we can find them a game to play with someone.
//if no game exists with someone waiting, they create one and wait.
game_server.findGame(client);

//Useful to know when someone connects
console.log('\t socket.io:: player ' + client.userid + ' connected');


//Now we want to handle some of the messages that clients will send.
//They send messages here, and we send them to the game_server to handle.
client.on('message', function(m) {

game_server.onMessage(client, m);

}); //client.on message

//When this client disconnects, we want to tell the game server
//about that as well, so it can remove them from the game they are
//in, and make sure the other player knows that they left and so on.
client.on('disconnect', function () {

//Useful to know when soomeone disconnects
console.log('\t socket.io:: client disconnected ' + client.userid + ' ' + client.game_id);

//If the client was in a game, set by game_server.findGame,
//we can tell the game server to update that game state.
if(client.game && client.game.id) {

//player leaving a game should destroy that game
game_server.endGame(client.game.id, client.userid);

} //client.game_id

}); //client.on disconnect

}); //sio.sockets.on connection
33 changes: 33 additions & 0 deletions client.js
@@ -0,0 +1,33 @@
//A global for our game root.
var game = {};

//When loading, we store references to our
//drawing canvases, and initiate a game instance.
window.onload = function(){

//Create our game instance.
game = new game_core();

//Fetch the viewports
game.debugview = document.getElementById('debug-viewport');
game.viewport = document.getElementById('viewport');

//Adjust their sizes
game.viewport.width = game.world.width;
game.debugview.width = game.world.width;

game.viewport.height = game.world.height;
game.debugview.height = game.world.height;

//Fetch the rendering contexts
game.ctx = game.viewport.getContext('2d');
game.debugctx = game.debugview.getContext('2d');

//Set the draw style for the font
game.ctx.font = '11px "Helvetica"';
game.debugctx.font = '11px "Helvetica"';

//Finally, start the loop
game.update( new Date().getTime() );

}

0 comments on commit 71218a9

Please sign in to comment.