Multiplayer meteor


#1

would anybody be so kind to tell me how you set up a lobby/room for multiplayer ?

do you need a room collection where every game is stored with an id in addition to the users that play that game ?

Where I am lost: how do I initiate multiple game instances ? It is a basic question, by I cannot find the answer.


#2

Check out this simple Game collection schema https://github.com/modweb/wordrama/blob/dev/lib/collections/games.litcoffee

Each game is a document in the collection (has it’s own Mongo id), and each game has an array of players. The player object has the player name and user id. Only users who are a part of the players array are allowed to update the game.


#3

Thank you very much.

This seems a great link and suficient explanations. I will work through it.

My collections are set up as you recommend and that part is working. I am creating a lobby with an individual id and matching url, players, invited players, host etc. All that is working, logged in parties can see an instance of the content of a collection under the specified url.

However, I do not manage to get a new game object started.

I assume that I need a Meteor.method.

Currently the game template is rendered on page load. When the game is selected with a button click, jQuery $(#game).show makes it visible. But it is only one instance.

Below is the key code. How can I transform the code appropriately into a method that I can call to create a game object ?

I tried to put autorun in there in a hope it would react to the new url, but I do not fully understadn how this works and I do not think a new object i created.

Best regards
Eckhart
Template.graphvis.rendered = function () {

console.log("I am rendering");

var nodesCursor = Nodes.find({});
var nodes = nodesCursor.fetch();

graphvis = new GraphVis("#graphvis", nodes);

nodesCursor.observe({
    added: function (doc) {
        graphvis.addNode(doc);
        graphvis.render();
    },
    removed: function (doc) {
        graphvis.removeNode(doc);
        graphvis.render();
    }
});

return this.autorun(function() {
    var data;
    data = Template.currentData();
    if (data.url) {
        return Router.go(data.url);
    }
});

};

function GraphVis(selector, nodes) { game code here }


#4

Posted here as well:

http://stackoverflow.com/questions/32549803/meteor-method-to-start-game-object http://stackoverflow.com/questions/32549803/meteor-method-to-start-game-object


#5

I don’t know if this will help, but check out my head-to-head matching game. It’s designed to only let two players in per game, and a player is locked to a client by an ID saved in localStorage. Make sure to open this link on two devices or browsers: http://gridmatch.meteor.com

In the source, you will see that new games are called via Meteor Method on the server. The initiator’s ID is added to the player array in the game document, and the game will be displayed in the ‘lobby’ until a second ID is added to the player array. Once there are two players, the game is removed from the lobby. https://github.com/StackThis/stackmatch/blob/master/server/lib/methods.js

I hope that is useful.


#6

autorun will only rerun if a computation within the autorun block has been updated, so if your Template.currentData() changes, your autorun block will be executed.

I would create a click handler on the template with the ‘create game’ or ‘start game’ button that calls a meteor method. In the callback of the method call I would reroute the app to the appropriate game route if the creation/start of the game is successful.