Skip to content

Commit

Permalink
Merge pull request btouellette#29 from btouellette/master
Browse files Browse the repository at this point in the history
adding last modified time to gamestates
  • Loading branch information
btouellette committed May 10, 2018
2 parents 751a010 + 82a0cd5 commit 503b06f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/gameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ module.exports = function(server, sessionStore) {
if(gamestate && gamestate.userIsInGame(currentUser)) {
// add the message to the gamestate, trimming to 200 characters and limiting message array length to 500
message = message.substr(0,200);
Gamestate.findByIdAndUpdate(gameID, { $push: { messages: { $each: [{ username: currentUser.username, message: message}], $slice: -500 }}}, function(err, gamestate) {
Gamestate.findByIdAndUpdate(gameID, { $set: { lastModified: new Date() }, $push: { messages: { $each: [{ username: currentUser.username, message: message}], $slice: -500 }}}, function(err, gamestate) {
gamestate.populate('players.user', function(err, gamestate) {
// get distinct list of user IDs in the game
var distinctUserIDs = gamestate.players.map(function(player) { return player.user._id; }).filter(function(value, index, self) {
Expand Down
5 changes: 4 additions & 1 deletion app/models/gamestate.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ var gamestateSchema = mongoose.Schema({
x: Number,
y: Number,
playerIndex: Number // which player placed this tile
}]
}],
lastModified: { type: Date, default: new Date() }
});

function getMeepleFlagFromType(meepleType) {
Expand Down Expand Up @@ -791,6 +792,8 @@ gamestateSchema.methods.drawTile = function(callback, autocomplete) {
for (var key in groupedPlacements) {
gamestate.activeTile.validPlacements.push(groupedPlacements[key]);
}
gamestate.lastModified = new Date();
gamestate.markModified('lastModified');
if(autocomplete) {
callback(null, gamestate);
return;
Expand Down
2 changes: 1 addition & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function(passport) {
} else {
console.log('user found: ' + user.username);
}
user.populate('activeGames friends', 'players.user players.active started finished username unusedTiles', function(err, user) {
user.populate('activeGames friends', 'players.user players.active started finished username unusedTiles lastModified', function(err, user) {
if(err) {
console.log('passport deserialize populate user err: ' + err);
} else {
Expand Down
2 changes: 2 additions & 0 deletions views/game.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@
//TODO: add a message to the game chat when a tile is discarded
var userGames = <%- JSON.stringify(user.activeGames) %>;
userGames.sort(function(a,b) {return (a.lastModified > b.lastModified) ? 1 : ((b.lastModified > a.lastModified) ? -1 : 0);} );
var userFriends = <%- JSON.stringify(user.friends) %>;
var userID = '<%- user._id %>';
var socket = io.connect('<%- process.env.SOCKET_URL %>');
Expand Down

0 comments on commit 503b06f

Please sign in to comment.