Multiplayer game logic?

Hey everyone, im trying to make a multiplayer game. Once 2 player reach a certain page I want both of them to be routed to an instance of the “game” page, but it seems that I cannot do it and I need some help with it.

the code:

GLOBAL:

Rooms = new Mongo.Collection('rooms');
var usersArray =[];

SERVER SIDE:

  userCounter: function(sessions){
   usersArray.push(sessions);
  
  var usersConnected = usersArray.length;

  if(usersConnected > 1){ 

    var randomGen = Math.floor(Math.random() * 9999999) + 1;
    Rooms.insert({
      users: usersArray,
      roomnumber: randomGen
    });

    var players = Rooms.findOne({
      roomnumber: randomGen
    });

    var play = players.users;
    console.log(players);
    


    usersArray =[]; 

    return players;

}

}`

CLIENT SIDE:

Template.GameLayout.onRendered(function () {
var ses = Session.get("loggedIn");

Meteor.call('userCounter', ses, function(error, players){

  console.log(players);

/*
if(error && error.error === "noArray"){
  console.log(error);
  console.log("I have error");
} else {
  if(fullArray){
  /* fullArray = [[randomNumber],[userSessionID]]
  var usersArray = fullArray[1];
  var randomGen = fullArray[0];

  console.log('step2');
  
}

}*/
}) 

once 2 users are hitting the “game” page I want an event to be executed for both of them (or atleast a console.log), can anyone direct me to an answer for this issue?
Thanks in advance, Max.

Hey Max.

It would be nice if you could place the code indicators correctly so that all the code is in the code block, right now it is painful to read.

Also, there seems to be things missing from the server side, like where is the “userCounter” method defined? Is it in a Meteor.methods?
Where does usersArray come from in the first line here? Where does fullArray come from on the client?

Edited, thanks for showing interest

Well, I can tell you that having a plain array for current users on your server won’t work as expected. For one, it will reset every time your server restarts, when players are really still connected. You need to place them somewhere in a collection, like a Lobby collection, that tracks users without rooms. Or you could have a field in your user collection that says which room the user belongs to. To find the number of users not in a room, you just count the ones without a roomId field, for example.

You could also have a “connect” method in your “certain page”'s onCreated function, so that you register with the server when you enter it. The connect method places your user in a lobby, and checks if there are enough people to start a game. If there is, you move them from the lobby to the game.

If you users are subscribed to their current status, they should be automatically aware that they are in a game.

Ok, I made some code changes, when 2 users are in the main lobby room (onRender) boths of them are registered to “Room” collection with a unique “randomRoomId” field, now I want the server to fire an event to the client so both players will be routed to “GameRoom” with “randomRoomId”(which is gonna be used as a GameRoom instance). My question is how can I do that code-wise?.

You don’t fire the event from the server. You publish the number of players in your Room publication and on the client, fire the redirection in autorun.

I’ll copypaste my yesterday’s Slack answer.

Let’s say you’re making a scrabble game and have to notify the users about the number of players at the table or play a sound when the number of players reaches 4.

You have your table publication to which all users sitting at the particular table are subscribed. Then you have autorun set that checks if number of players is equal to 4 and plays a sound if yes.

Lastly, you have method.call which changes the number of players at the table on the server when new player comes to the table.

Try to think of it the other way around. Instead of the server telling the clients to change page, you subscribe the client to their room, and use an observe (http://docs.meteor.com/#/full/observe) on the room collection to watch for changes. When the client gets a room, you route yourself to the game room.

// Client
Meteor.subscribe("myRooms");

Room.find().observe({
   added: function (doc) {
       FlowRouter.go("/gameroom/" + doc.id);
   }
});

Optionally, if you place each player in a room before the room is full, you can observe changed instead, checking if a fullor active variable in the room is set to true.

isnt Room.find() is serverside only?

Not if you define it outside a client or server forlder, like both/collections/room.js