Last active
April 26, 2016 01:48
-
-
Save justinoboyle/94f9b849493fdd03b4e28ff378cfa1a0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var exports = module.exports = {}; | |
var easytime = require('../util/easytime.js'); | |
var Chance = require('chance'); | |
var chance = new Chance(); | |
exports.gameStates = { | |
acceptingBets: 0, | |
waitingToRoll: 1, | |
rolling: 2 | |
} | |
exports.gameColors = { | |
red: 0, | |
black: 1, | |
green: 2 | |
} | |
exports.invGameColors = ["red", "black", "green"]; | |
exports.gameInstance = { | |
forcedWinner: false, // Debug, remove this in a final game! | |
gameState: exports.gameStates["acceptingBets"], | |
timeStarted: Date.now(), | |
timeUntilNext: easytime.sec(30), | |
lastPoll: Date.now(), | |
timeBetweenPolls: easytime.sec(1), | |
currentBets: [], | |
winner: undefined, | |
updateGameTask: function () { | |
if (exports.gameInstance.gameState === exports.gameStates["acceptingBets"]) { | |
exports.gameInstance.timeUntilNext -= easytime.sec(1); | |
if (exports.gameInstance.timeUntilNext <= 0) { | |
exports.gameInstance.gameState = exports.gameStates["waitingToRoll"]; | |
exports.gameInstance.timeUntilNext = easytime.sec(3); | |
} | |
return exports.gameInstance.scheduleUpdateTask(); | |
} | |
if (exports.gameInstance.gameState === exports.gameStates["waitingToRoll"]) { | |
exports.gameInstance.timeUntilNext -= easytime.sec(1); | |
if (exports.gameInstance.timeUntilNext <= 0) { | |
exports.gameInstance.gameState = exports.gameStates["rolling"]; | |
exports.gameInstance.winner = exports.gameInstance.pickWinner(), | |
exports.gameInstance.timeUntilNext = easytime.sec(15); | |
} | |
return exports.gameInstance.scheduleUpdateTask(); | |
} | |
if (exports.gameInstance.gameState === exports.gameStates["rolling"]) { | |
exports.gameInstance.timeUntilNext -= easytime.sec(1); | |
if (exports.gameInstance.timeUntilNext <= 0) { | |
exports.gameInstance.timeUntilNext = easytime.sec(30); | |
exports.gameInstance.timeStarted = Date.now(); | |
exports.gameInstance.currentBets = []; | |
exports.gameInstance.winner = undefined; | |
exports.gameInstance.gameState = exports.gameStates["acceptingBets"]; | |
} | |
return exports.gameInstance.scheduleUpdateTask(); | |
} | |
}, | |
scheduleUpdateTask: function () { | |
exports.gameInstance.lastPoll = Date.now(); | |
return setTimeout(exports.gameInstance.updateGameTask, exports.gameInstance.timeBetweenPolls); | |
}, | |
pickWinner: function () { | |
// Debug, remove this in a final game! | |
if (exports.gameInstance.forcedWinner) | |
return exports.gameInstance.forcedWinner; | |
return Math.floor(chance.random() * (4)); | |
} | |
}; | |
exports.gameInstance.scheduleUpdateTask(); | |
exports.api = function (request, callback) { | |
if (request.getCurrentGame) { | |
var prepared = { | |
gameState: exports.gameInstance.gameState, | |
timeStarted: exports.gameInstance.timeStarted, | |
timeUntilNext: exports.gameInstance.timeUntilNext, | |
lastPoll: exports.gameInstance.lastPoll, | |
nextPoll: exports.gameInstance.lastPoll + exports.gameInstance.timeBetweenPolls, | |
timeBetweenPolls: exports.gameInstance.timeBetweenPolls, | |
currentBets: [], | |
} | |
return callback({ success: true, currentGame: prepared }); | |
} | |
callback({ success: false }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment