Permalink
Browse files

added inventory shuffle; level 5

  • Loading branch information...
Jen Fong-Adwent
Jen Fong-Adwent committed Jul 27, 2012
1 parent e3a5d90 commit 282d141262eb781229ce2bca710a7c83364ffbe4
View
@@ -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
@@ -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
@@ -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
@@ -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,13 +142,18 @@ 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);
}
});
};
+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
@@ -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
@@ -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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.

0 comments on commit 282d141

Please sign in to comment.