publishComposite on meteor.com?


#1

Hi everyone,

This is my first topic here so please be indulgent ;-). I am developing an online strategy game where each player has a personal field of view that can evolve during the game when he conquers more territories (in fact when he drops some cubes on the map, yes that’s kind of an abstract game…). Therefore the server publishes only the tiles owned by the player + somes tiles around them. Players can stack cubes on a tile or erase an opponent cube by dropping one on the concerned tile. If they drop a cube on a tile where there’s just one opponent cube, they become the owner of the cube. I struggled to achieve this reactive publication but I’ve found out the publishComposite package and it was actually quiet simple : it works perfectly on my local server and on my nitrous.io cloud IDE. But when I deploy on meteor.com, there is one case that doesn’t work : when a player drops a cube on a tile where there’s just one opponent cube, the DB is modified but there is no “reaction” on the client side. I assure you it works on my local server. I am kind of confused here so I’m looking for your help ! I put my publish & update code below, do not hesitate to ask me for more details if needed !

 Meteor.publishComposite('map', {
  find: function() {
    var currentUserId = this.userId;
    if(currentUserId) {
      var username = Meteor.users.findOne({_id: currentUserId}).username;
      return Map.find({player: username});
    }
  },
  children: [
    {
      find: function(playerTile) {
        var queryArray = [];
        var x_max = Math.floor(visibility(playerTile.z));
        for(var x=0; x <= x_max; x++) {
          var y_max = x_max-x;
          for(var y=0; y<=y_max; y++) {
            queryArray.push({$and: [{x: playerTile.x+x}, {y: playerTile.y+y}]});
            queryArray.push({$and: [{x: playerTile.x-x}, {y: playerTile.y+y}]});
            queryArray.push({$and: [{x: playerTile.x+x}, {y: playerTile.y-y}]});
            queryArray.push({$and: [{x: playerTile.x-x}, {y: playerTile.y-y}]});
          }
        }
        return Map.find({$or: queryArray});
      }
    }
  ]
});

Meteor.methods({
  'dropCube': function(tile) {
    var currentUserId = Meteor.userId();
    var username = Meteor.users.findOne({_id: currentUserId}).username;
    var existingTile = Map.findOne({x:tile.x, y:tile.y});
    if (existingTile) {
      if(existingTile.player != username) {
        if(existingTile.z > 1) {
          Map.update(existingTile._id, {$set: {z: existingTile.z-1}});
        } else {
          Map.update(existingTile._id, {$set: {player: username}});
        }
      } else {
        Map.update(existingTile._id, {$set: {z: existingTile.z+1}});
      }
    }
    else {
      Map.insert({x: tile.x, y:tile.y, z:1, player: username});
    }
  }
});

#2

Ok guys, I found out the cause of this strange behavior. In fact I just needed to set up oplog support. Since it’s not available on meteor.com, I sign up at Digital Ocean , and follow this tutorial.

See you !