Skip to content

Commit

Permalink
game
Browse files Browse the repository at this point in the history
  • Loading branch information
vic committed Oct 10, 2015
1 parent 63c9b0a commit ba0b1bd
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@ var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');

var session = require('express-session');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
app.use(session({
genid: function (req) {
return require('node-uuid').v4();
},
secret: 'never-fight-christians'
}));

// uncomment after placin your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
//app.use('/', routes);
//app.use('/users', users);

app.use('/game', require('./routes/game'))

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@
"cookie-parser": "~1.3.4",
"debug": "~2.1.1",
"express": "~4.12.2",
"express-session": "^1.11.3",
"jade": "~1.9.2",
"lodash": "^3.10.1",
"mongoose": "^4.1.10",
"morgan": "~1.5.1",
"node-uuid": "^1.4.3",
"serve-favicon": "~2.2.0"
},
"devDependencies": {
"nodemon": "^1.7.1"
}
}
89 changes: 89 additions & 0 deletions routes/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var _ = require('lodash');

var express = require('express');
var router = express.Router();

var mongoose = require('../models/models');

var game_template = require('./game_template');

var games = {};

router.get('/hola', function(req, res, next) {
destroyGame(req);
createNewGame(req, res, function() {});
res.send(req.game);
});

router.use(createNewGame);

router.get('/:tag', function(req, res, next) {
var tagNumber = parseInt(req.params.tag);

var game = req.game;
var card = game.deck[tagNumber];

if (card) {

if (card.type === 'minion' || card.type === 'boss') {
attack(game.player, card);
if (card.health > 0) {
attack(card, game.player);
} else {
game.deck[tagNumber] = null;
}

if (game.player.health < 0) {
destroyGame(req);
res.send({game: 'over'});
}
}

if (card.type === 'potion') {
if (game.maxHealth !== game.player.health) {
game.player.health = Math.min(
game.maxHealth, card.health + game.player.health);
game.deck[tagNumber] = null;
}
}

if (card.type === 'weapon') {
game.player.weapon = card;
game.deck[tagNumber] = null;
}
}


console.log("====================> ", game);

res.send(game);
});


function attack(attacker, to) {
if (attacker.weapon) {
attacker.weapon.duration -= 1;
to.health -= attacker.weapon.damage;
if (attacker.weapon.duration < 1) {
attacker.weapon = null;
}
} else {
to.health -= attacker.damage;
}
}


function destroyGame(req) {
var sessionId = req.sessionID;
games[sessionId] = null;
}

function createNewGame(req, res, next) {
var sessionId = req.sessionID;
if (!games[sessionId]) {
games[sessionId] = _.cloneDeep(game_template);
}
req.game = games[sessionId];
next();
}
module.exports = router;
42 changes: 42 additions & 0 deletions routes/game_template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var game = {
maxHealth: 10,
time: 10,
player: {
health: 10,
damage: 1,
weapon: null,
},
deck: [
{
type: 'potion',
health: 5
},
{
type: 'weapon',
damage: 2,
duration: 3
},
{
type: 'minion',
health: 4,
damage: 2
},
{
type: 'minion',
health: 4,
damage: 2,
weapon: {
type: 'weapon',
damage: 4,
duration: 2
}
},
{
type: 'boss',
health: 40,
damage: 20
}
]
}

module.exports = game;

0 comments on commit ba0b1bd

Please sign in to comment.