Skip to content

Instantly share code, notes, and snippets.

@Solmen
Last active August 29, 2015 14:24
Show Gist options
  • Save Solmen/2f93e71277e701a3b2a0 to your computer and use it in GitHub Desktop.
Save Solmen/2f93e71277e701a3b2a0 to your computer and use it in GitHub Desktop.
jack: function(req, res) {
if (req.isSocket && req.body.hasOwnProperty('gameId') && req.body.hasOwnProperty('pNum') && req.body.hasOwnProperty('thiefId') && req.body.hasOwnProperty('victimId') && req.body.hasOwnProperty('jackId') && req.body.hasOwnProperty('targetId')) {
Game.findOne(req.body.gameId).populate('players').populate('deck').populate('scrap').exec(function(error, game) {
if (error || !game) {
console.log("Game " + req.body.gameId + " not found for jack");
res.send(404);
} else {
Player.find([req.body.thiefId, req.body.victimId]).populate('hand').populate('points').populate('runes').exec(function(erro, players) {
if (erro || !players[0] || !players[1]) {
console.log("Can't find players for jack");
res.send(404);
} else {
//Sort players returns an array of the two players sorted according to their pNum attribute
//This is used to determine who is player0 and who is player1
var playerSort = sortPlayers(players);
Card.findOne(req.body.targetId).populate('attachments').exec(function(err, target) {
if (err || !target) {
console.log("Card " + req.body.targetId + " not found for jack");
res.send(404);
} else {
var yourTurn = req.body.pNum === game.turn % 2;
if (yourTurn) {
//Player models have a frozenId attribute corresponding to one card that they are not allowed to play on a given turn
var cardIsFrozen = playerSort[req.body.pNum].frozenId === req.body.jackId;
if (!cardIsFrozen) {
playerSort[(req.body.pNum + 1) % 2].points.remove(target.id);
playerSort[req.body.pNum].points.add(target.id);
playerSort[req.body.pNum].hand.remove(req.body.jackId);
target.attachments.add(req.body.jackId);
playerSort[req.body.pNum].frozenId = null;
game.turn++;
game.save(function(er, savedGame) {
target.save(function(e, savedTarget) {
//This save is the weird one. When the first player to be saved is the one playing
//the jack from their hand, the log is correct, but the server is wrong
playerSort[0].save(function(e6, savedP0) {
console.log("\nsavedP0: ");
//If player0 played the jack, this log
//properly shows the target card was added to
//player0's points collection
//In that case, this log is INCONSISTENT
//WITH THE SERVER, which doesn't have
//the new card added to the points collection
console.log(savedP0);
playerSort[0].save(function(e7, savedP1) {
console.log("\nsavedP1: ");
//This log is also always right, but the server
//actually agrees with what is logged for this player
console.log(savedP1);
//publishUpdate always has the right data, since savedP0 and savedP1
//are always right, even though the server is wrong if req.body.pNum === 0
Game.publishUpdate(game.id, {
change: 'jack',
game: savedGame,
players: [savedP0, savedP1],
thief: req.body.pNum,
targetCard: savedTarget
});
});
});
});
});
}
}
res.send({
jack: true,
yourTurn: yourTurn,
frozen: cardIsFrozen
});
}
});
}
});
}
});
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment