forked from CoffiDev/nfchero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |