|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | +var crypto = require('crypto'); |
| 4 | +var _ = require('underscore'); |
| 5 | + |
| 6 | +var game; |
| 7 | +var savePath = __dirname + '/data/game.json'; |
| 8 | + |
| 9 | +var start = function(data) { |
| 10 | + if (game && game.title === data.title) { |
| 11 | + game.description = data.description; |
| 12 | + game.input = data.input; |
| 13 | + game.output = data.output; |
| 14 | + game.running = true; |
| 15 | + return save(); |
| 16 | + } |
| 17 | + |
| 18 | + game = { |
| 19 | + title: data.title, |
| 20 | + description: data.description, |
| 21 | + input: data.input, |
| 22 | + output: data.output, |
| 23 | + running: true, |
| 24 | + entries: [] |
| 25 | + }; |
| 26 | + |
| 27 | + save(); |
| 28 | +}; |
| 29 | + |
| 30 | +var stop = function() { |
| 31 | + if (game) game.running = false; |
| 32 | + save(); |
| 33 | +}; |
| 34 | + |
| 35 | +var get = function() { |
| 36 | + return game; |
| 37 | +}; |
| 38 | + |
| 39 | +var addEntry = function(data) { |
| 40 | + var createKey = function() { |
| 41 | + return (Math.round(Math.random() * 100000000000)).toString(36); |
| 42 | + }; |
| 43 | + |
| 44 | + var getGravatarUrl = function(email) { |
| 45 | + var hash = crypto.createHash('md5').update(email).digest('hex'); |
| 46 | + return 'http://www.gravatar.com/avatar/' + hash + '?s=130'; |
| 47 | + }; |
| 48 | + |
| 49 | + var countStrokes = function(file) { |
| 50 | + if (fs.existsSync(file)) { |
| 51 | + var contents = fs.readFileSync(file, 'utf8').replace(/\s/g, ''); |
| 52 | + return contents.length; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + if (!game.running) return { err: 'Game is not running' };; |
| 57 | + |
| 58 | + var entry = _.findWhere(game.entries, { email: data.email }); |
| 59 | + |
| 60 | + if (entry && entry.key !== data.key) { |
| 61 | + return { err: 'This email address is taken' }; |
| 62 | + } |
| 63 | + |
| 64 | + if (!entry) { |
| 65 | + if (!data.email) return { err: 'Enter an email address' }; |
| 66 | + if (!data.team) return { err: 'Enter a team name' }; |
| 67 | + if (!data.file) return { err: 'No file was selected' }; |
| 68 | + |
| 69 | + entry = { |
| 70 | + email: data.email, |
| 71 | + gravatar: getGravatarUrl(data.email), |
| 72 | + team: data.team, |
| 73 | + file: data.file, |
| 74 | + key: createKey(), |
| 75 | + strokes: countStrokes(data.file), |
| 76 | + updated: new Date |
| 77 | + }; |
| 78 | + |
| 79 | + game.entries.push(entry); |
| 80 | + save(); |
| 81 | + return { entry: entry }; |
| 82 | + } |
| 83 | + |
| 84 | + if (entry && entry.key === data.key) { |
| 85 | + entry.updated = new Date; |
| 86 | + entry.file = data.file; |
| 87 | + entry.strokes = countStrokes(data.file); |
| 88 | + save(); |
| 89 | + return { entry: entry }; |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +var setValid = function(key, valid) { |
| 94 | + var entry = _.findWhere(game.entries, { key: key }); |
| 95 | + |
| 96 | + if (entry) { |
| 97 | + entry.valid = valid; |
| 98 | + save(); |
| 99 | + } |
| 100 | +}; |
| 101 | + |
| 102 | +var save = function() { |
| 103 | + fs.writeFileSync(savePath, JSON.stringify(game)); |
| 104 | +}; |
| 105 | + |
| 106 | +var load = function() { |
| 107 | + if (fs.existsSync(savePath)) { |
| 108 | + game = JSON.parse(fs.readFileSync(savePath, 'utf8')); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +load(); |
| 113 | + |
| 114 | +module.exports = { |
| 115 | + addEntry: addEntry, |
| 116 | + setValid: setValid, |
| 117 | + start: start, |
| 118 | + stop: stop, |
| 119 | + get: get |
| 120 | +}; |
0 commit comments