WebFund 2013F: Tutorial 9

From Soma-notes
Revision as of 14:26, 11 November 2013 by Soma (talk | contribs) (→‎Code)
Jump to navigation Jump to search

In this tutorial you will be modifying adventure-ajax-demo, an updated version of Simple Adventure that now uses asynchronous background requests (AJAX) to navigate between rooms and update the page.

First, grab the code, either with modules or without. Then unzip it and get it running as usual. (If you download the version with modules, you shouldn't need to run npm install.)

Do the following:

  • Create a room, the Holodeck, which is just connected to sick bay.
  • Get to the secret room by editing and re-sending a post request.
  • Modify the server code so that attempts to get to non-existent rooms instead send you to the Void.
  • Modify the server code so that if the password "openSesame" is included in the post request, it allows users to enter the secret room. If users give the wrong password or no password at all, they should get redirected to the void.
  • [BONUS] Modify the server so only allows users to exit using valid exits for the current room.

Code

package.json

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.4.0",
    "jade": "*",
    "stylus": "*",
    "validator": "*"
   }
}

app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('COMP2406 rules!'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));


// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/game', routes.game);
app.get('/getContents', routes.getContents);
app.post('/start', routes.start);
app.post('/quit', routes.quit);
app.post('/doAction', routes.doAction);

http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

routes/index.js

public/javascripts/adventure.js

rooms.js

views/index.jade

views/layout.jade

views/room.jade