Skip to content

Commit

Permalink
extract rand_int function
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-alvarez committed Feb 20, 2016
1 parent 4b34833 commit 8b9c479
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions js/stores/StoriesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ StoriesStore.prototype.bossSays = function () {
return this._boss.says;
};

function rand_item(array) { return array[Math.floor(Math.random() * array.length)]; }
function rand_int(start, end) { return Math.floor(Math.random() * (end - start)) + start; }
function rand_item(array) { return array[rand_int(0, array.length)]; }
StoriesStore.prototype._generateNewDead = function () {
var stories = [];
var used = [];
Expand All @@ -44,7 +45,7 @@ StoriesStore.prototype._generateNewDead = function () {
consequence: rand_karma(this._consequences),
});
}
var fate = karma + Math.floor(Math.random() * 10) - 5 >= 0 ? HEAVEN : HELL;
var fate = karma + rand_int(-5, +5) >= 0 ? HEAVEN : HELL;
return {
name: "Some guy",
stories: stories,
Expand Down Expand Up @@ -87,7 +88,7 @@ StoriesStore.prototype.handle = function (event) {
case Actions.ACTION_DATA_LOADED:
var data = event.data;
function toObject (obj, array) {
array.forEach(function(p) { obj[p] = 0; });
array.forEach(function(p) { obj[p] = rand_int(-2, +2); });
}
toObject(STORIES_STORE._reasons, data["reasons"]);
toObject(STORIES_STORE._actions, data["actions"]);
Expand All @@ -100,7 +101,7 @@ StoriesStore.prototype.handle = function (event) {
break;
case Actions.ACTION_SELECT_FATE:
STORIES_STORE._updateBoss(event.dead.expects, event.fate);
STORIES_STORE._updateStories(event.dead.stories, event.fate == HELL ? -1 : 1);
STORIES_STORE._updateStories(event.dead.stories, event.fate == HELL ? -1 : +1);
STORIES_STORE._dead = STORIES_STORE._generateNewDead();
break;
case Actions.ACTION_GAME_RESTART:
Expand Down

0 comments on commit 8b9c479

Please sign in to comment.