irrigator
irrigator

Reputation: 251

Meteor realtime game - match two players according to their score?

I want to build a realtime quiz game which randomly matches two players (according to their winning rate if they are logged in). I've read through the book Discover Meteor and have a basic understanding of the framework, but I just have no idea of how to implement the matching part. Anyone know how to do that?

Upvotes: 0

Views: 304

Answers (3)

Peppe L-G
Peppe L-G

Reputation: 8345

If you add the package peppelg:random-opponent-matcher to your application, you can match together opponents like this:

On the server, you need to have an instance of RandomOpponentMatcher like this:

new RandomOpponentMatcher('my-matcher', {name: 'fifo'}, function(user1, user2){
    // Create the match/game they should play.
})

The function you pass to RandomOpponentMatcher will get called when two users been matched to play against each other. In it, you'll probably want to create the match the users should play against each other (this package does only match opponents together, it does not contain any functionality for playing games/matches).

On the client, you need to create an instance of RandomOpponentMatcher as well, but you only pass the name to it (the same name as you used on the server):

myMatcher = new RandomOpponentMatcher('my-matcher')

Then when the users is logged in and which to be matched with a random opponent, all you need to do is to call the add method. For example:

<template name="myTemplate">
    <button class="clickMatchesWithOpponent">Match me with someone!</button>
</template>
Template.myTemplate.events({
    'click .clickMatchesWithOpponent': function(event, template){
        myMatcher.add()
    }
})

When two different logged in users has clicked on the button, the function you passed to RandomOpponentMatcher on the server will get called.

Upvotes: 0

looshi
looshi

Reputation: 1236

if you want to match users who have scores close to each other, you can do something like this : mongodb - Find document with closest integer value

The Meteor code for those Mongo queries is very similar, but there are some subtle differences that are kind of tricky. In Meteor, it would look something like this :

  SP  // "selected player" = the User you want to match someone up with
 var score = SP.score; // selected player's score

  var queryLow = {score: {$lte:score},_id:{$ne:SP._id}};
  var queryHigh = {score:{$gte:score},_id:{$ne:SP._id}};

  // "L" is the player with the closest lower score
  var L=Players.findOne(queryLow,{sort:{score:-1},limit:1});

  // "H" is the player with the closest higher score
  var H=Players.findOne(queryHigh,{sort:{score:1},limit:1});

so, now you have references to the players with scores right above and right below the 'selected player'. In terms of making it random, perhaps start with a simple algorithm like "match me with the next available player who's score is closest" , then if it's too predictable and boring you can throw some randomness into the algorithm.

you can view the above Meteor code working live here http://meteorpad.com/pad/4umMP4iY8AkB9ct2d/ClosestScore and you can Fork it and mess about with the queries to see how it works. good luck! Meteor is great, I really like it.

Upvotes: 1

Eric T
Eric T

Reputation: 946

One implementation might be as follows:

A user somehow triggers a 'looking for game' event that sets an attribute on user.profile.lookingForGame to true. The event then makes a call to a server side Meteor method which queries for all other online users looking for games.

From there you it really depends on how you want to handle users once they 'match'.

To determine all online users, try using the User Status package: https://github.com/mizzao/meteor-user-status

Once added, any online user will have an attribute in the profile object of 'online'. You can use this to query for all online users.

Upvotes: 0

Related Questions