|
| 1 | +var express = require('express'), |
| 2 | + router = express.Router(), |
| 3 | + router2 = express.Router(), |
| 4 | + Sequelize = require('sequelize'), |
| 5 | + db = require('../models'); |
| 6 | + |
| 7 | +/** |
| 8 | + * Get a static list of unique Players given a list of Games. |
| 9 | + */ |
| 10 | +function getStaticPlayerList(games) |
| 11 | +{ |
| 12 | + var list = []; |
| 13 | + games.forEach(function(game) { |
| 14 | + if (!game.Players) return; |
| 15 | + game.Players.forEach(function(player) { |
| 16 | + var exists = false; |
| 17 | + list.forEach(function(li) { |
| 18 | + exists = exists || (li.id == player.id); |
| 19 | + }); |
| 20 | + if (!exists) |
| 21 | + list.push({id: player.id, name: player.name, email: player.email}); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + return list; |
| 26 | +} |
| 27 | + |
| 28 | +module.exports = function (app) { |
| 29 | + app.use('/games', router); |
| 30 | + app.use('/game', router2); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Page templates: |
| 35 | + */ |
| 36 | +router2.get('/:id/', function(req, res, next) { |
| 37 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 38 | + // TODO: redirect to index but put the messages in a session (so URL is updated) |
| 39 | + if (!game) res.render('index', {messages: ["Cannot find that Game!"] }); |
| 40 | + else res.render('game', {gameStart: game.when, gameThreshold: game.threshold, gameId: game.id}); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * JSON routes: |
| 46 | + */ |
| 47 | +// See the list of the game history (most recent first) |
| 48 | +// TODO: pagination eventually? |
| 49 | +router.get('/all', function (req, res, next) { |
| 50 | + db.Game.findAll({ order: [['when', 'DESC']]}).then(function (games) { |
| 51 | + // Just return list of id / timestamps |
| 52 | + var ret = {games: []}; |
| 53 | + var details = {}; |
| 54 | + games.forEach(function(game) { |
| 55 | + details = {id: game.id, when: game.when}; |
| 56 | + ret.games.push(details); |
| 57 | + }); |
| 58 | + res.json(ret); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +// See the list of the games for the last week (for the homepage) |
| 63 | +router.get('/last-week', function (req, res, next) { |
| 64 | + // Calculate midnight of the current day |
| 65 | + var midnight = new Date(); |
| 66 | + midnight.setHours(0, 0, 0, 0); |
| 67 | + midnight.setDate(midnight.getDate() - 7); |
| 68 | + var ret = {games: []}; |
| 69 | + db.Game.findAll({ |
| 70 | + where: {when: {$gte: midnight}}, |
| 71 | + include: [db.Player, db.Goal], |
| 72 | + order: [['when', 'DESC']] |
| 73 | + }).then(function (games) { |
| 74 | + |
| 75 | + // For the week, actually return player goals |
| 76 | + // NOTE: To aid in the D3 viz, we ensure that all games have the |
| 77 | + // same list of players, in the same order. So start by building |
| 78 | + // the information for the players list for this week. |
| 79 | + var playerList = getStaticPlayerList(games); |
| 80 | + console.log(playerList); |
| 81 | + |
| 82 | + // Now build the information for the games themselves: |
| 83 | + var details = {}; |
| 84 | + games.forEach(function(game) { |
| 85 | + details = {id: game.id, when: game.when}; |
| 86 | + details.players = []; |
| 87 | + // Instantiate the player list in the same order with the same values |
| 88 | + playerList.forEach(function(player) { |
| 89 | + details.players.push({id: player.id, name: player.name, email: player.email, goals: 0}); |
| 90 | + }); |
| 91 | + game.Players.forEach(function(player){ |
| 92 | + // Count this user's goals |
| 93 | + var goalCount = 0; |
| 94 | + game.Goals.forEach(function(goal) { |
| 95 | + if (goal.PlayerId == player.id) goalCount++; |
| 96 | + }); |
| 97 | + |
| 98 | + // Update the goals for this player (others remain at 0) |
| 99 | + details.players.forEach(function(inner) { |
| 100 | + if (player.id == inner.id) inner.goals = goalCount; |
| 101 | + }); |
| 102 | + }); |
| 103 | + ret.games.push(details); |
| 104 | + }); |
| 105 | + |
| 106 | + res.json(ret); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +// Details for a single game |
| 111 | +router.get('/:id', function (req, res, next) { |
| 112 | + // Return value |
| 113 | + var targetGame = {}; |
| 114 | + |
| 115 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 116 | + if (!game) res.json(targetGame); |
| 117 | + else |
| 118 | + { |
| 119 | + targetGame.when = game.when; |
| 120 | + targetGame.threshold = game.threshold; |
| 121 | + targetGame.players = {}; |
| 122 | + |
| 123 | + // Get Players Scores |
| 124 | + game.getPlayers().then(function(players) { |
| 125 | + // Initialize goals to zero |
| 126 | + players.forEach(function (player) { |
| 127 | + targetGame.players[player.id] = { |
| 128 | + id: player.id, |
| 129 | + name: player.name, |
| 130 | + email: player.email, |
| 131 | + goals: 0 |
| 132 | + }; |
| 133 | + }); |
| 134 | + |
| 135 | + // Now record actual counts |
| 136 | + game.getGoals().then(function(goals) { |
| 137 | + goals.forEach(function(goal) { |
| 138 | + targetGame.players[goal.PlayerId].goals++; |
| 139 | + }); |
| 140 | + |
| 141 | + // Send back data |
| 142 | + res.json(targetGame); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +// Create |
| 150 | +router.post('/create', function(req, res, next) { |
| 151 | + db.Game.create({when: new Date()}).then(function(game) { |
| 152 | + res.json({success: true, id: game.id}); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +/** |
| 157 | + * Helper methods. |
| 158 | + */ |
| 159 | +// Add a Player to an existing game |
| 160 | +router.post('/:id/add/player/:pid', function(req, res, next) { |
| 161 | + console.log('Adding a Player to a Game'); |
| 162 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 163 | + if (!game) res.json({error: 'Invalid Game ID'}); |
| 164 | + else |
| 165 | + { |
| 166 | + db.Player.find({ where: { id: req.params.pid }}).then(function(player) { |
| 167 | + if (!player) res.json({error: 'Invalid Player ID'}); |
| 168 | + else |
| 169 | + { |
| 170 | + game.addPlayer(player).then(function() { |
| 171 | + res.json({success: 'Player ' + player.name + ' added to Game ID ' + game.id}); |
| 172 | + }); |
| 173 | + } |
| 174 | + }); |
| 175 | + } |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +// Remove a player from a game (they must not have any goals) |
| 180 | +router.post('/:id/remove/player/:pid', function(req, res, next) { |
| 181 | + console.log('Removing a Player from a Game'); |
| 182 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 183 | + if (!game) res.json({error: 'Invalid Game ID'}); |
| 184 | + else |
| 185 | + { |
| 186 | + db.Player.find({ where: { id: req.params.pid }}).then(function(player) { |
| 187 | + if (!player) res.json({error: 'Invalid Player ID'}); |
| 188 | + else |
| 189 | + { |
| 190 | + game.getGoals().then(function(goals) { |
| 191 | + var count = 0; |
| 192 | + goals.forEach(function(goal) { |
| 193 | + if (goal.PlayerId == player.id) count++; |
| 194 | + }); |
| 195 | + |
| 196 | + if (count > 0) |
| 197 | + { |
| 198 | + res.json({error: "Cannot remove a Player that has already scored. FINISH THAT GAME!"}); |
| 199 | + } |
| 200 | + else |
| 201 | + { |
| 202 | + game.removePlayer(player).then(function() { |
| 203 | + res.json({success: 'Player ' + player.name + ' removed from Game ID ' + game.id}); |
| 204 | + }); |
| 205 | + } |
| 206 | + }); |
| 207 | + } |
| 208 | + }); |
| 209 | + } |
| 210 | + }); |
| 211 | +}); |
| 212 | + |
| 213 | +// Score a goal |
| 214 | +router.post('/:id/goal/player/:pid', function(req, res, next) { |
| 215 | + console.log('GOOOOOOOOOOAL!'); |
| 216 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 217 | + if (!game) res.json({error: 'Invalid Game ID'}); |
| 218 | + else |
| 219 | + { |
| 220 | + db.Player.find({ where: { id: req.params.pid }}).then(function(player) { |
| 221 | + if (!player) res.json({error: 'Invalid Player ID'}); |
| 222 | + else |
| 223 | + { |
| 224 | + // Verify that the game is still open |
| 225 | + var sql = "SELECT PlayerId, COUNT(PlayerId) AS c FROM Goals WHERE GameId=? GROUP BY PlayerId ORDER BY c DESC"; |
| 226 | + db.sequelize.query(sql, { replacements: [game.id], type: Sequelize.QueryTypes.SELECT }).then(function(rows) { |
| 227 | + if (rows.length > 0 && rows[0].c >= game.threshold) |
| 228 | + { |
| 229 | + res.json({error: 'Game is already closed, Winner (PlayerId): ' + rows[0].PlayerId}); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + // Create the goal |
| 234 | + db.Goal.create({ when: new Date() }).then(function(goal) { |
| 235 | + goal.setPlayer(player); |
| 236 | + goal.setGame(game); |
| 237 | + res.json({success: 'Player ' + player.name + ': GOOOOOOOOOOOOOAL!', id: goal.id}); |
| 238 | + }); |
| 239 | + } |
| 240 | + }); |
| 241 | + } |
| 242 | + }); |
| 243 | + } |
| 244 | + }); |
| 245 | +}); |
| 246 | + |
| 247 | +// Undo a goal (delete) |
| 248 | +router.delete('/:id/goal/:gid', function(req, res, next) { |
| 249 | + console.log('Undoing accidental goal button press...'); |
| 250 | + db.Game.find({ where: { id: req.params.id }}).then(function (game) { |
| 251 | + if (!game) res.json({error: 'Invalid Game ID'}); |
| 252 | + else |
| 253 | + { |
| 254 | + db.Goal.find({ where: { id: req.params.gid }}).then(function(goal) { |
| 255 | + if (!goal) res.json({error: 'Invalid Goal ID'}); |
| 256 | + else |
| 257 | + { |
| 258 | + goal.destroy().then(function() { |
| 259 | + res.json({success: 'Goal undone'}); |
| 260 | + }); |
| 261 | + } |
| 262 | + }); |
| 263 | + } |
| 264 | + }); |
| 265 | +}); |
0 commit comments