jshemas/PingPong — pull request #63 by tandrewnichols (2013-09-21 02:55:38+00:00)
Title: WIP: Change redPlayer/bluePlayer to winner/loser

Makes it a little easier to work with the data.

This is a little bigger project that may require multiple parties. Here are the changes I've identified for sure:

1. Adjust the Match schema (in mongoose) to use winner and loser. We can also use "ref" to populate the players, rather than manually setting them as we are now (i.e. get rid of redPlayerDetails and bluePlayerDetails).
2. Change the actual mongo schema with something like `db.matches.update({}, {$rename: {redPlayer: "winner", bluePlayer: "loser"}});` but we have to figure out how to calculate who was the winner and loser of each match. I'm pretty sure we can do this in mongo . . . just gotta figure out how.
3. Change winner/loser (formerly redPlayer/bluePlayer) types to ObjectIds using: `db.matches.find().forEach(function(doc){db.matches.update({_id:doc._id}, {$set: {winner: new ObjectId(doc.winner), loser: new ObjectId(doc.loser)}}); });`
4. Change all queries against matches to use .populate('winner loser'), which functions like a SQL join, looking up the player by id.
5. Change all code references to redPlayer and bluePlayer ( ! )
6. Determine winner and loser when adding a new match.