Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added inventory shuffle; level 5

  • Loading branch information...
commit 282d141262eb781229ce2bca710a7c83364ffbe4 1 parent e3a5d90
@ednapiranha ednapiranha authored
View
31 config/level4.json
@@ -3,16 +3,16 @@
"enemies":
[
{
- "name": "Siren",
- "avatar_alive": "/enemies/siren-alive.png",
- "avatar_dead": "/enemies/siren-dead.png",
+ "name": "Siree",
+ "avatar_alive": "/enemies/siree-alive.png",
+ "avatar_dead": "/enemies/siree-dead.png",
"battle_messages":
[
{
- "message": "Siren sings a terrible pop song in a high pitch!"
+ "message": "Siree sings a terrible pop song in a high pitch!"
},
{
- "message": "Siren gives you the worst tinnitus!"
+ "message": "Siree gives you the worst tinnitus!"
}
],
"hp": 98,
@@ -22,6 +22,27 @@
"gold_high_range": 63,
"xp_low_range": 12,
"xp_high_range": 16
+ },
+ {
+ "name": "Spidee",
+ "avatar_alive": "/enemies/spidee-alive.png",
+ "avatar_dead": "/enemies/spidee-dead.png",
+ "battle_messages":
+ [
+ {
+ "message": "Spidee whips up a sticky web of gross!"
+ },
+ {
+ "message": "Spidee tricks you into an invisible wall of web!"
+ }
+ ],
+ "hp": 115,
+ "damage_low_range": 35,
+ "damage_high_range": 46,
+ "gold_low_range": 53,
+ "gold_high_range": 70,
+ "xp_low_range": 15,
+ "xp_high_range": 18
}
]
}
View
27 config/level5.json
@@ -0,0 +1,27 @@
+{
+ "location": "Exploding Fireballs",
+ "enemies":
+ [
+ {
+ "name": "Siree",
+ "avatar_alive": "/enemies/siree-alive.png",
+ "avatar_dead": "/enemies/siree-dead.png",
+ "battle_messages":
+ [
+ {
+ "message": "Siree sings a terrible pop song in a high pitch!"
+ },
+ {
+ "message": "Siree gives you the worst tinnitus!"
+ }
+ ],
+ "hp": 140,
+ "damage_low_range": 45,
+ "damage_high_range": 60,
+ "gold_low_range": 68,
+ "gold_high_range": 85,
+ "xp_low_range": 22,
+ "xp_high_range": 26
+ }
+ ]
+}
View
98 lib/game.js
@@ -11,13 +11,13 @@ var multiplier = function(high_range, low_range) {
var enemyDamageGenerator = function(req) {
var job = req.session.job;
- var tools = req.session.tools;
+ var tools = req.session.activeTools;
var tool = tools[req.body.tool];
var speedJobMultiplier = multiplier(job.speed_multiplier_high_range, job.speed_multiplier_low_range);
var magicJobMultiplier = multiplier(job.mp_multiplier_high_range, job.mp_multiplier_low_range);
var moxieJobMultiplier = multiplier(job.moxie_multiplier_high_range, job.moxie_multiplier_low_range);
- var xpMultiplier = req.session.xp + req.session.level;
+ var xpMultiplier = parseInt(req.session.xp, 10) + parseInt(req.session.level, 10);
var toolDamage = multiplier(tool.damage_high_range, tool.damage_low_range);
var toolMagic = multiplier(tool.mp_high_range, tool.mp_low_range);
@@ -33,51 +33,55 @@ var playerDamageGenerator = function(enemy) {
* Requires: web request, db connection
* Returns: resulting user stats during battle
*/
-exports.battle = function(req, enemy, db, callback) {
- var gold = req.session.gold;
- var enemyDamage = 0;
- var enemyHP = parseInt(req.body.enemy_hp, 10) || enemy.hp;
-
- enemyDamage = enemyDamageGenerator(req);
-
- req.session.hp -= playerDamageGenerator(enemy);
- enemyHP -= enemyDamage;
-
- if (req.session.hp < 1) {
- req.session.hp = 0;
-
- } else if (enemyHP < 1) {
- enemyHP = 0;
-
- // enemy is dead, so let's count up the gold
- req.session.gold += multiplier(enemy.gold_high_range, enemy.gold_low_range);
-
- // let's also calculate the xp
- req.session.xp += multiplier(enemy.xp_high_range, enemy.xp_low_range);
-
- /* Calculate to see if player can level up
- * Formula is currently: (x * x + x) * 5
- */
- var currentLevel = (req.session.level * req.session.level + req.session.level) * levelConstant;
- if (currentLevel < req.session.xp) {
- req.session.level += 1;
+exports.battle = function(req, db, callback) {
+ try {
+ var gold = req.session.gold;
+ var enemy = req.session.enemy;
+ var enemyDamage = 0;
+ var enemyHP = parseInt(req.body.enemy_hp, 10) || enemy.hp;
+
+ enemyDamage = enemyDamageGenerator(req);
+ req.session.hp -= playerDamageGenerator(enemy);
+ enemyHP -= enemyDamage;
+
+ if (req.session.hp < 1) {
+ req.session.hp = 0;
+
+ } else if (enemyHP < 1) {
+ enemyHP = 0;
+
+ // enemy is dead, so let's count up the gold
+ req.session.gold += multiplier(enemy.gold_high_range, enemy.gold_low_range);
+
+ // let's also calculate the xp
+ req.session.xp += multiplier(enemy.xp_high_range, enemy.xp_low_range);
+
+ /* Calculate to see if player can level up
+ * Formula is currently: (x * x + x) * 5
+ */
+ var currentLevel = (req.session.level * req.session.level + req.session.level) * levelConstant;
+ if (currentLevel < req.session.xp) {
+ req.session.level += 1;
+ }
}
- }
-
- user.saveStats(req, db, function(err, user) {
- if (err) {
- callback(err);
- } else {
- var result = {
- player_hp: user.hp,
- enemy_hp: enemyHP,
- gold: user.gold,
- enemy_damage: enemyDamage,
- xp: user.xp,
- mp: user.mp
- };
- callback(null, result);
- }
- });
+ user.saveStats(req, db, function(err, user) {
+ if (err) {
+ callback(err);
+ } else {
+ var result = {
+ player_hp: user.hp,
+ enemy_hp: enemyHP,
+ gold: user.gold,
+ enemy_damage: enemyDamage,
+ xp: user.xp,
+ mp: user.mp
+ };
+
+ callback(null, result);
+ }
+ });
+ } catch(err) {
+ console.log('ERROR ', err);
+ }
};
View
81 lib/user.js
@@ -2,6 +2,7 @@
var config = require('../config/defaults');
var jobs = require('../config/jobs');
+var utils = require('./utils');
var tools = require('../config/tools');
var toolObj = {};
toolObj['fist'] = tools['fist'];
@@ -23,22 +24,81 @@ var userInit = function(req) {
gold: verifyStatValue('gold', req.session.gold),
level: verifyStatValue('level', req.session.level),
hp: verifyStatValue('hp', req.session.hp),
- tools: req.session.tools || toolObj,
+ tools: req.session.tools || {},
+ activeTools: req.session.activeTools || toolObj,
xp: verifyStatValue('xp', req.session.xp),
- mp: verifyStatValue('mp', req.session.mp)
+ mp: verifyStatValue('mp', req.session.mp),
+ enemy: {}
};
return user;
};
/* Set user job
- * Requires: job name, db connection
+ * Requires: job name
* Returns: job object
*/
-exports.setJob = function(job, db, callback) {
+exports.setJob = function(job, callback) {
callback(null, jobs[job] || jobs['engineer']);
};
+/* Add active tool
+ * Requires: web request, tool name, db connection
+ * Returns: true if successful, false otherwise
+ */
+exports.addActiveTool = function(req, db, callback) {
+ var toolName = req.body.tool.replace(/\s/, '');
+ var newToolCount = utils.getObjectSize(req.session.activeTools) + 1;
+
+ if (!toolName || (!req.session.activeTools[toolName] && newToolCount > 6)) {
+ callback(null, false);
+
+ } else if (!req.session.activeTools[toolName]) {
+ req.session.activeTools[toolName] = tools[toolName];
+ delete req.session.tools[toolName];
+
+ this.saveStats(req, db, function(err, user) {
+ if (err) {
+ callback(err);
+ } else {
+ callback(null, tools[toolName]);
+ }
+ });
+ }
+};
+
+/* Remove active tool
+ * Requires: web request, tool name, db connection
+ * Returns: tool object if successful, false otherwise
+ */
+exports.removeActiveTool = function(req, toolName, db, callback) {
+ toolName = req.body.tool.replace(/\s/, '');
+ var newToolCount = utils.getObjectSize(req.session.activeTools) - 1;
+
+ if (newToolCount < 1) {
+ callback(null, false);
+ } else {
+ delete req.session.activeTools[toolName];
+ req.session.tools[toolName] = tools[toolName];
+
+ this.saveStats(req, db, function(err, user) {
+ if (err) {
+ callback(err);
+ } else {
+ callback(null, tools[toolName]);
+ }
+ });
+ }
+};
+
+var setHash = function(db, userKey, user, name) {
+ if (typeof user[name] === 'object') {
+ db.hset(userKey, name, JSON.stringify(user[name]), function(err, resp) { });
+ } else {
+ db.hset(userKey, name, user[name], function(err, resp) { });
+ }
+};
+
/* Set user info
* Requires: web request, db connection
* Returns: A hash of the user's current score settings
@@ -53,11 +113,7 @@ exports.saveStats = function(req, db, callback) {
var user = userInit(req);
for (var name in user) {
- if (typeof user[name] === 'object') {
- db.hset(userKey, name, JSON.stringify(user[name]), function(err, resp) { });
- } else {
- db.hset(userKey, name, user[name], function(err, resp) { });
- }
+ setHash(db, userKey, user, name);
}
callback(null, user);
@@ -86,6 +142,7 @@ exports.getStats = function(email, db, callback) {
} else {
userItems.tools = JSON.parse(userItems.tools);
userItems.job = JSON.parse(userItems.job);
+ userItems.activeTools = JSON.parse(userItems.activeTools);
}
callback(null, userItems);
@@ -93,6 +150,10 @@ exports.getStats = function(email, db, callback) {
});
};
+var deleteHash = function(db, userKey, name) {
+ db.hdel(userKey, name, function(err, resp) { });
+};
+
/* Reset user stats
* Requires: email, db connection
* Returns: A hash of the user's current score settings
@@ -101,7 +162,7 @@ exports.resetStats = function(req, db, callback) {
var userKey = 'user:' + req.session.email;
for (var name in req.session) {
- db.hdel(userKey, name, function(err, resp) { });
+ deleteHash(db, userKey, name);
}
callback(null, true);
View
15 lib/utils.js
@@ -0,0 +1,15 @@
+/* Get size
+ * Requires: object
+ * Returns: size
+ */
+exports.getObjectSize = function(obj) {
+ var size = 0;
+
+ for (var key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ size ++;
+ }
+ }
+
+ return size;
+};
View
1  local.json-dist
@@ -4,7 +4,6 @@
"authPort": 3000,
"authUrl": "https://browserid.org",
"session_secret": "secret",
- "session_cookie": "session_your_cookie_name",
"redis_dev": 1,
"redis_test": 2,
"redis_prod": 0
View
0  public/enemies/siren-alive.png → public/enemies/siree-alive.png
File renamed without changes
View
0  public/enemies/siren-dead.png → public/enemies/siree-dead.png
File renamed without changes
View
36 public/javascripts/lib/game.js
@@ -9,6 +9,8 @@ define(['jquery'], function ($) {
var message = $('.battle-message');
var fightAgain = $('.fight-again');
var goldAmount = $('.stats .gold');
+ var currentTools = $('.dashboard .tools ul');
+ var inventory = $('.dashboard .inventory ul');
var updateStats = function(options) {
enemy.data('hp', options.enemy_hp);
@@ -90,6 +92,40 @@ define(['jquery'], function ($) {
goldAmount.text(data.result.gold);
});
}
+ },
+ addTool: function(self) {
+ if (currentTools.find('li').length < 6) {
+ var params = {
+ tool: self.find('span').text()
+ };
+
+ $.post('/add_tool', params, function(data) {
+ if (data.result.tool) {
+ var toolItem = $('<li><img><span></span></li>');
+ toolItem.find('img').attr('src', '/tools/' + data.result.tool.name.replace(/\s/, '_') + '.png');
+ toolItem.find('span').text(data.result.tool.name);
+ currentTools.append(toolItem);
+ inventory.find('span').text(data.result.tool.name).parent().remove();
+ };
+ });
+ }
+ },
+ removeTool: function(self) {
+ if (currentTools.find('li').length > 1) {
+ var params = {
+ tool: self.find('span').text()
+ };
+
+ $.post('/remove_tool', params, function(data) {
+ if (data.result.tool) {
+ var toolItem = $('<li><img><span></span></li>');
+ toolItem.find('img').attr('src', '/tools/' + data.result.tool.name.replace(/\s/, '_') + '.png');
+ toolItem.find('span').text(data.result.tool.name);
+ currentTools.find('span').text(data.result.tool.name).parent().remove();
+ inventory.append(toolItem);
+ };
+ });
+ }
}
};
View
16 public/javascripts/main.js
@@ -48,10 +48,24 @@ define(['jquery', 'game'],
game.refuel();
});
- // Purchasing items
+ // Purchasing tools
var toolItems = $('ul.items');
toolItems.on('click', 'li', function() {
game.buy($(this));
});
+
+ // Adding tools
+ var inventory = $('div.inventory ul');
+
+ inventory.on('click', 'li', function() {
+ game.addTool($(this));
+ });
+
+ // Removing tools
+ var activeTools = $('div.tools ul');
+
+ activeTools.on('click', 'li', function() {
+ game.removeTool($(this));
+ });
});
View
BIN  public/locations/level5-detail.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/locations/level5-preview.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/misc/stats-dash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/scenes/dashboard.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  public/scenes/level5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
87 public/stylesheets/main.css
@@ -178,7 +178,8 @@ p.intro, .game h1, .dashboard h1, .store h1 {
position: relative;
}
-.store .items li span, .detail .tools li span {
+.store .items li span, .detail .tools li span,
+.dashboard .tools li span, .dashboard .inventory li span {
display: block;
font-family: 'LeagueGothicRegular';
font-size: 14px;
@@ -327,16 +328,22 @@ p.intro, .game h1, .dashboard h1, .store h1 {
background-image: url('/scenes/dashboard.jpg');
}
-.dashboard .stats {
+.dashboard .stats, .dashboard .tools, .dashboard .inventory {
position: absolute;
+ background-image: url('/misc/stats-dash.png');
+ box-shadow: inset #000 1px 1px 5px;
color: #eee;
- left: 50px;
- top: 105px;
+ left: 10px;
font-size: 18px;
width: 480px;
- height: 445px;
overflow: auto;
text-shadow: #000 1px 1px 1px;
+ padding: 30px;
+ border-radius: 8px;
+}
+
+.dashboard .stats {
+ top: 55px;
}
.stats li {
@@ -348,6 +355,34 @@ p.intro, .game h1, .dashboard h1, .store h1 {
width: 110px;
}
+.dashboard .tools {
+ top: 350px;
+}
+
+.dashboard .tools li {
+ float: left;
+ margin: 15px 12px 5px 0px;
+}
+
+.dashboard .inventory h2 {
+ margin-bottom: 10px;
+}
+
+.dashboard .inventory {
+ top: 55px;
+ left: 565px;
+ text-align: center;
+ width: 170px;
+ padding-left: 25px;
+ height: 401px;
+ overflow: auto;
+}
+
+.dashboard .inventory li {
+ float: left;
+ margin: 10px 10px 30px 10px;
+}
+
a.button {
border-radius: 3px;
text-decoration: none;
@@ -432,14 +467,14 @@ img.enemy, img.player {
img.enemy {
position: absolute;
- top: 130px;
- left: 230px;
+ top: 180px;
+ left: 280px;
}
img.player {
position: absolute;
- top: 130px;
- left: 10px;
+ top: 180px;
+ left: 20px;
}
img.enemy.dead, img.player.dead {
@@ -449,8 +484,8 @@ img.enemy.dead, img.player.dead {
img.action {
position: absolute;
- top: 40px;
- left: 30px;
+ top: 100px;
+ left: 65px;
display: none;
}
@@ -460,8 +495,8 @@ ol.tools {
display: block;
border-radius: 8px;
position: absolute;
- top: 400px;
- left: 10px;
+ top: 460px;
+ left: 20px;
box-shadow: inset #000 1px 1px 5px;
border: 1px solid #111;
}
@@ -479,10 +514,28 @@ ol.tools li, .store .items li {
height: 60px;
}
+.dashboard .inventory ul {
+ display: block;
+ float: left;
+ width: 180px;
+}
+
+.dashboard .tools li, .dashboard .inventory li {
+ border: 4px solid #fff;
+ border-radius: 40px;
+ cursor: pointer;
+ width: 60px;
+ height: 60px;
+}
+
.detail ol.tools li span {
text-shadow: #eee 0px 0px 3px;
}
+.dashboard .tools li span, .dashboard .inventory li span {
+ text-shadow: none;
+}
+
.detail ol.tools li:hover span {
color: #fff;
text-shadow: none;
@@ -593,3 +646,11 @@ a.fight-again:hover {
.level4.detail .wrapper {
background-image: url('/locations/level4-detail.jpg');
}
+
+.level5 .wrapper {
+ background-image: url('/locations/level5-preview.jpg');
+}
+
+.level5.detail .wrapper {
+ background-image: url('/locations/level5-detail.jpg');
+}
View
4 routes/auth.js
@@ -21,7 +21,7 @@ module.exports = function(app, db, nconf, isLoggedIn) {
// Logout
app.get('/logout', isLoggedIn, function(req, res) {
- req.session.reset();
- res.redirect('/?logged_out=1', 303);
+ req.session.destroy();
+ res.redirect('/', 303);
});
};
View
31 routes/index.js
@@ -24,6 +24,32 @@ module.exports = function(app, db, isLoggedIn, hasJob, hasNoJob, sufficientLevel
});
});
+ app.post('/add_tool', isLoggedIn, function(req, res) {
+ user.addActiveTool(req, db, function(err, tool) {
+ req.session.activeTools[tool.name.replace(/\s/, '_')] = tool;
+
+ var data = {};
+ data.result = {
+ tool: tool
+ };
+
+ res.json(data);
+ });
+ });
+
+ app.post('/remove_tool', isLoggedIn, function(req, res) {
+ user.removeActiveTool(req, db, function(err, tool) {
+ delete req.session.activeTools[tool.name.replace(/\s/, '_')];
+
+ var data = {};
+ data.result = {
+ tool: tool
+ };
+
+ res.json(data);
+ });
+ });
+
app.get('/reset', isLoggedIn, function(req, res) {
user.resetStats(req, db, function(err, user) {
res.redirect('/logout', 303);
@@ -74,7 +100,6 @@ module.exports = function(app, db, isLoggedIn, hasJob, hasNoJob, sufficientLevel
});
});
- // This is temporary until we actually have store functionality
app.get('/refuel', isLoggedIn, resetEnemy, function(req, res) {
if (req.session.gold >= 10) {
var data = { result: {} };
@@ -105,7 +130,7 @@ module.exports = function(app, db, isLoggedIn, hasJob, hasNoJob, sufficientLevel
});
app.post('/job', isLoggedIn, hasNoJob, resetEnemy, function(req, res) {
- user.setJob(req.body.job, db, function(err, job) {
+ user.setJob(req.body.job, function(err, job) {
req.session.job = job;
user.saveStats(req, db, function(err, user) {
@@ -146,7 +171,7 @@ module.exports = function(app, db, isLoggedIn, hasJob, hasNoJob, sufficientLevel
var config = require('../config/level' + level);
var result = {};
- game.battle(req, req.session.enemy, db, function(err, result) {
+ game.battle(req, db, function(err, result) {
res.json({
result: result
});
View
11 settings.js
@@ -14,15 +14,8 @@ module.exports = function(app, configurations, express) {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
- app.use(clientSessions({
- cookieName: nconf.get('session_cookie'),
- secret: nconf.get('session_secret'), // MUST be set
- // true session duration:
- // will expire after duration (ms)
- // from last session.reset() or
- // initial cookieing.
- duration: 24 * 60 * 60 * 1000 * 35 // 5 weeks
- }));
+ app.use(express.cookieParser());
+ app.use(express.session({ secret: nconf.get('session_secret') }));
app.use(app.router);
app.use(function(req, res, next) {
res.status(403);
View
9 test/test.game.js
@@ -60,7 +60,8 @@ describe('game', function() {
describe('battle', function() {
it('fights an enemy and both HPs are less', function(done) {
req.body.enemy_hp = 20;
- game.battle(req, enemy, db, function(err, result) {
+ req.session.enemy = enemy;
+ game.battle(req, db, function(err, result) {
should.exist(result);
result.player_hp.should.not.equal(50);
result.enemy_hp.should.not.equal(20);
@@ -71,7 +72,8 @@ describe('game', function() {
it('fights an enemy and player wins', function(done) {
enemy.hp = 0;
req.body.enemy_hp = 0;
- game.battle(req, enemy, db, function(err, result) {
+ req.session.enemy = enemy;
+ game.battle(req, db, function(err, result) {
should.exist(result);
result.gold.should.not.equal(0);
result.xp.should.not.equal(1);
@@ -83,7 +85,8 @@ describe('game', function() {
it('fights an enemy and player loses', function(done) {
enemy.hp = req.body.enemy_hp = 50;
req.session.hp = 0;
- game.battle(req, enemy, db, function(err, result) {
+ req.session.enemy = enemy;
+ game.battle(req, db, function(err, result) {
should.exist(result);
result.player_hp.should.equal(0);
result.enemy_hp.should.not.equal(0);
View
4 test/test.user.js
@@ -63,7 +63,7 @@ describe('user', function() {
});
it('sets the user job if correctly defined', function(done) {
- user.setJob('engineer', db, function(err, job) {
+ user.setJob('engineer', function(err, job) {
should.exist(job);
job.should.equal(jobs['engineer']);
done();
@@ -82,7 +82,7 @@ describe('user', function() {
});
it('sets the user job if incorrectly defined', function(done) {
- user.setJob('engineer-nonexistent', db, function(err, job) {
+ user.setJob('engineer-nonexistent', function(err, job) {
should.exist(job);
job.name.toLowerCase().should.equal('engineer');
done();
View
20 views/game_dashboard.jade
@@ -34,3 +34,23 @@ block content
h3 Reset
span End game (you will lose all stats)
a(href='/reset', class='button') Reset!
+
+ .tools
+ h2 Current Battle Tools (6 maximum)
+ ul
+ - for(var name in session.activeTools) {
+ - var toolName = session.activeTools[name].name.replace(/\s/, '_');
+ li
+ img(src='/tools/#{toolName}.png')
+ span= session.activeTools[name].name
+ - }
+
+ .inventory
+ h2 Inventory
+ ul
+ - for(var name in session.tools) {
+ - var toolName = session.tools[name].name.replace(/\s/, '_');
+ li
+ img(src='/tools/#{toolName}.png')
+ span= session.tools[name].name
+ - }
View
6 views/game_detail.jade
@@ -13,11 +13,11 @@ block content
a(href='/detail/#{level}', class='fight-again') fight again!
ol.tools(data-level='#{level}')
- - for(var name in session.tools) {
- - var toolName = session.tools[name].name.replace(/\s/, '_');
+ - for(var name in session.activeTools) {
+ - var toolName = session.activeTools[name].name.replace(/\s/, '_');
li(data-tool='#{toolName}')
img(src='/tools/#{toolName}.png')
- span= session.tools[name].name
+ span= session.activeTools[name].name
- }
if session.hp > 0
Please sign in to comment.
Something went wrong with that request. Please try again.