Forum Multiplayer Making a "matchmaking" button

Discussion and help relating to the PlayerIO Multiplayer API.

Making a "matchmaking" button

Postby Farzher » May 28th, 2011, 3:34 am

I haven't had any luck on making this work. All I want is a simple matchmaking system that just says "searching for opponent" when you press it.
When two people are "searching for opponent" it will match them and throw them in the same room so they can play. Any ideas?

I'm using AS3 client, C# server.
Last edited by Henrik on November 2nd, 2012, 1:15 pm, edited 1 time in total.
Reason: Stickied
Farzher
 
Posts: 11
Joined: August 18th, 2010, 7:49 pm

Re: Making a "matchmaking" button

Postby Benjaminsen » May 28th, 2011, 10:05 am

I have personally solved this before by having a matchmaking multiplayer room service.

When when a user joins this room, and there is already a user connected to it. Send them both a random room id to join and disconnect them from the match making room.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Making a "matchmaking" button

Postby Farzher » May 28th, 2011, 7:03 pm

How do you send them both to the same random room from the server side code? Looks like you can only join rooms from the client, do you have to send a message to their clients telling them what room to join?
Farzher
 
Posts: 11
Joined: August 18th, 2010, 7:49 pm

Re: Making a "matchmaking" button

Postby Henrik » May 28th, 2011, 7:59 pm

server -> player1: create and join a room
player1: creates and joins a room.
player1 -> server: I created room with id: ...
server -> player2: join room with id: ...
server: disconnect player1 and player2
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Making a "matchmaking" button

Postby Farzher » May 29th, 2011, 9:53 am

Is there any way to generate a unique room id from server side? Then I could just send that ID to each player and disconnect them.
Farzher
 
Posts: 11
Joined: August 18th, 2010, 7:49 pm

Re: Making a "matchmaking" button

Postby Oliver » May 31st, 2011, 9:49 am

How about just making a random string? Either by using the Random class or perhaps System.Guid.NewGuid().ToString() ?

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Making a "matchmaking" button

Postby Farzher » June 1st, 2011, 8:07 pm

Alright, there's a small chance I could get the ID of an existing room though, that would be bad haha.

Here's the solved code for anyone looking to make this themselves:
Code: Select all
[RoomType("matchmaking")]
public class GameCode : Game<Player>
{
    public override void GameStarted() { }
    public override void GameClosed() { }

    public override void UserJoined(Player player)
    {
        Player opponent = null;
        if (PlayerCount >= 2)
        {
            foreach (Player p in Players)
            {
                if (p != player)
                {
                    opponent = p;
                    break;
                }
            }
        }

        if (opponent != null)
        {
            string id = randomString(20);

            opponent.Send("gameFound", id);
            player.Send("gameFound", id);

            opponent.Disconnect();
            player.Disconnect();
        }
    }

    public override void UserLeft(Player player) { }


    private static string randomString(int length)
    {
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
        char[] chars = new char[length];
        Random random = new Random();

        for (int i = 0; i < length; i++)
        {
            chars[i] = allowedChars[random.Next(0, allowedChars.Length)];
        }

        return new string(chars);
    }
}


When you get the "gameFound" message on the client, join a game room with the passed id.
Farzher
 
Posts: 11
Joined: August 18th, 2010, 7:49 pm

Re: Making a "matchmaking" button

Postby WindieGames » June 2nd, 2011, 4:34 am

I highly doubt you will run into the room issue you're talking about. Just generate the room number in the millions or go the semi more complicated route and design a class that comes up with a unique text string like "DE5fZ21BliAwqCa8".

edit: Which you've done so I'm not sure why you said it. :P
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: Making a "matchmaking" button

Postby wanara » December 4th, 2012, 11:13 am

I made the solution suggested by Benjaminsen and Henrik and I'm wondering :

when I'm disconnecting players from the match making room, is there a way to know that the client side disconnect event is called because of being kicked out of the room or because connection to the server has been lost ?
wanara
 
Posts: 18
Joined: November 19th, 2012, 4:20 pm

Re: Making a "matchmaking" button

Postby dreamora » December 4th, 2012, 5:28 pm

you can send a reason along the disconnect or you could simply send a message before sending the disconnect. As TCP is in order and reliable, the user would definitely get the reason for disconnect before getting the disconnect.

If the client didn't get a reason it would be a disconnect due to connection lose.
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: Making a "matchmaking" button

Postby jgames » February 8th, 2013, 8:08 pm

Hi, I'm having a bit of a problem with my matchmaking system.
I use this code for matchmaking:

Code: Select all
public override void UserJoined(Player player) {
        Player opponent = null;
        if (PlayerCount >= 2) {
            foreach (Player p in Players) {
                  if (p != player) {
                    opponent = p;
                    break;
                }
            }
        }

        if (opponent != null) {
         Console.WriteLine("Made Match: " + player.ConnectUserId+ " vs. " + opponent.ConnectUserId);
            
            string id = randomString(20);
            
            opponent.Send("gameFound", id);
            player.Send("gameFound", id);
            opponent.Disconnect();
            player.Disconnect();
        }
    }


However quite often it happens that the players get disconnected without them receiving the "gameFound" message.

What I thought about is that I could just send a gameFound message and disconnect them when they started a game. The problem with this is that they would still be connected to the matchmaking room and thus still searching for new games to join, when they already found one and thus creating players that get matched with players that already found a partner. Anyone know how to solve this problem?
jgames
 
Posts: 25
Joined: September 18th, 2012, 11:36 am

Re: Making a "matchmaking" button

Postby henshouse » February 9th, 2013, 8:43 pm

I'm also having the same problem as jgames (above me).

Basically, when I first startup the dev server and connect two clients, all works as it should. Then if I disconnect the two clients (w/o restarting the dev server) and reconnect with 2 different clients, I can get the matchmaking room to find a random room id for the 2 players and disconnect them from the matchmaking room but for some reason neither of the clients receive the "Game Found" message, thus neither connect to the game room.

I've repeated this process several times, always with the same result. 1st & 2nd connectors = good, any client after that = bad.
henshouse
 
Posts: 4
Joined: August 6th, 2010, 4:40 am

Re: Making a "matchmaking" button

Postby Benjaminsen » February 15th, 2013, 1:28 pm

Could you guys upload a small example that always replicate this issue?
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Making a "matchmaking" button

Postby henshouse » February 16th, 2013, 5:00 am

No worries, it must be a development server issue (possibly specific to Mac/Mono). I've since uploaded my files to and use the real server to test and it works 100% of the time now.
henshouse
 
Posts: 4
Joined: August 6th, 2010, 4:40 am

Re: Making a "matchmaking" button

Postby jgames » February 16th, 2013, 10:16 am

Yeah I haven't had this problem on the real server. Only on the development server. (Mac/Mono)
jgames
 
Posts: 25
Joined: September 18th, 2012, 11:36 am

Re: Making a "matchmaking" button

Postby JimLion » June 22nd, 2014, 1:56 pm

Nice, thanks guys. I will definitely be looking into using some of this code for my own multiplayer game lobby :)

One quation for you guys using Mac / Mono. When you make a new C# project what kind of project is it?
JimLion
 
Posts: 73
Joined: June 17th, 2014, 3:35 am

Re: Making a "matchmaking" button

Postby AquamentosGames » July 10th, 2014, 1:28 pm

JimLion wrote:Nice, thanks guys. I will definitely be looking into using some of this code for my own multiplayer game lobby :)

One quation for you guys using Mac / Mono. When you make a new C# project what kind of project is it?


For server-side code you want to create a 'Library'. That outputs as a .dll you can upload to the server.

I want to point out that the thread you asked this question is over a year old, and your question wasn't related to the topic. You would have been better off posing this as a new thread (probably in the 'Unity' section if you are using MonoDevelop).
AquamentosGames
 
Posts: 27
Joined: December 9th, 2011, 12:44 am

Re: Making a "matchmaking" button

Postby JimLion » September 26th, 2014, 4:02 am

Hi. I know this thread is 4 years old, but I have a question that continues from it. Farzher made an awesome C# room class for the matchmaking room that will bring the two users into the game room, but how do you match players up? When a user clicks the button togo to matchmaking, how do you know which matchmaking room they join? Is there only one big matchmaking room or lots of them with 2 player max?
JimLion
 
Posts: 73
Joined: June 17th, 2014, 3:35 am

Re: Making a "matchmaking" button

Postby fpanettieri » October 8th, 2014, 7:41 pm

Sorry for the necro, but I think I can answer that.

You create a service room that acts as a lobby.
Then when people attempts to make a game, you use an internal system (I'm planning on implementing ELO per player).

If you need more fine grained control, you can make multiple lobbies with some auxiliar flag like the league level, based on ELO range.

0 - 800 => bronze
800 - 1200 => silver
1200 - 1600 => gold
1600+ => elite

This is entirely game-dependent, on your game + matchmaking system.

When two players matches your "game conditions" for a valid game, you disconnect them both, and send them to a private room where they play.
fpanettieri
 
Posts: 1
Joined: May 13th, 2014, 3:45 am

Re: Making a "matchmaking" button

Postby JimLion » December 11th, 2014, 9:47 pm

Hey, I am using this method except the GotMessage function is never being triggered in my matchmaking room (it works in the lobby room). Are you guys putting both rooms in the same DLL? Have you ever experienced this weird problem where one room doesn't get any messages??
JimLion
 
Posts: 73
Joined: June 17th, 2014, 3:35 am

Re: Making a "matchmaking" button

Postby JimLion » January 7th, 2015, 3:11 am

@golddty What are you even talking about?

I posted earlier about a problem I was having, but it's working now. I'm not sure what I was doing wrong before, but I have both the lobby room and the matchmaking room in the same DLL. It works fine on the development server and prod. Chyeah.
JimLion
 
Posts: 73
Joined: June 17th, 2014, 3:35 am

Re: Making a "matchmaking" button

Postby milalhjovich » June 30th, 2016, 11:26 am

I have personally solved this before by having a matchmaking multiplayer room service.

When when a user joins this room, and there is already a user connected to it. Send them both a random room id to join and disconnect them from the match making room.
milalhjovich
 
Posts: 3
Joined: June 30th, 2016, 10:26 am

Re: Making a "matchmaking" button

Postby sally » July 27th, 2016, 1:58 am

I am making Roblox I do not know match making thing what should I do for Roblox game guys give me a plan if you give me plan I will be happy so plz give give me a plan plz Roblox is a good game yeah!
sally
 
Posts: 1
Joined: July 27th, 2016, 1:54 am

Re: Making a "matchmaking" button

Postby MoDDiB » October 24th, 2016, 9:43 am

Why not a more efficient and logical way to achieve matchmaking ?

-Players joins a room called "Game" with CreateJoinRoom
-Serverside when the right number of players is achieved allow us to close this room (overriding AllowUserJoin isn't enough)
-When a new player joins the room called "Game" with CreateJoinRoom it creates a new room because there's no room available.

I'm sure you have everything to allow this easily.
This will prevent us from creating useless room preventing complexity, long connection time and useless bandwith usage.
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Making a "matchmaking" button

Postby Henrik » October 24th, 2016, 10:11 pm

You can already do what you describe using ListRooms. The client can get a list of all rooms lacking players, pick one, and put the player in the room. When the room has enough players, the game will start, and by changing a RoomData property you can make the room not appear in the ListRoom searches clients are making.

Many games use a Lobby system where you simply list out rooms and let the user join one that needs players, instead of the game choosing one for you.

But matchmaking rooms allow for more functionality, you can apply criteria to the users so you get similar users together, and you can expand your criteria until you find enough players to start a game, etc. It's a trade-off, and each game can pick the model that suits it best.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Making a "matchmaking" button

Postby MoDDiB » October 24th, 2016, 10:33 pm

Using ListRooms means I will let the client decides the match he will play, by modifying the code he will be able to easily hacks the matchmaking.

But matchmaking rooms allow for more functionality, you can apply criteria to the users so you get similar users together, and you can expand your criteria until you find enough players to start a game, etc. It's a trade-off, and each game can pick the model that suits it best.

I can find similar users by setting the approriate room name, and I could set the right number of players if I was able to close this room.
I've already coded the matchmaking system : it's really slower and harder than what I described.
Seeing how your service rooms work I guess it wouldn't be hard to do and would save you a lot of people asking for matchmaking.
Don't you find it simpler ?
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Making a "matchmaking" button

Postby Henrik » October 24th, 2016, 11:21 pm

I'm sorry, I don't understand your solution?

You're asking for a way for users joining a room with a special name to be put into the first best actual room that lacks players? If so, you've pretty much just eliminated the ListRoom step from my simpler example, but you would now need another system serverside that would keep the rules for these special rooms?

I don't see the huge benefits to this, or am I understanding it wrong?
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: Making a "matchmaking" button

Postby MoDDiB » October 25th, 2016, 9:11 am

Just a way to "close" a server.
It should work for any room, not a special one.

- 5 Players join a roomId "Game" with CreateJoinRoom
- Serverside I decide I have enough player so I "close" the server
- the next player which try to join a roomId "Game" with CreateJoinRoom will now join a new server because the last one is closed.

Easy, efficient.
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Making a "matchmaking" button

Postby MoDDiB » November 10th, 2018, 9:28 am

Henry1122 wrote:At the point when a client joins this room and there is as of now a client associated with it, send them both an irregular room id to join and detach them from the match making room...

There's a lot of alternatives, all of them take longer for the players and take much more resources, my solution is elegant and seems easy to add serverside.....
MoDDiB
 
Posts: 30
Joined: July 13th, 2014, 8:41 pm

Re: Making a "matchmaking" button

Postby Zwick » March 17th, 2019, 8:22 pm

This code is what we use in our game (two players matchmaking with AI fallback http://thebuttongame.io):

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlayerIO.GameLibrary;

namespace TheButton
{
    [RoomType("Matchmaking")]
    class Matchmaking : GameBase
    {
        public int aiDelay;

        public override void GameStarted()
        {
            aiDelay = int.Parse(RoomData["aiDelay"]);
            aiDelay = aiDelay > 0 ? aiDelay : 5000;

            AddTimer(matchmakingAttempt, 1000);
        }

        private void matchmakingAttempt()
        {
           var enumerator = Players.GetEnumerator();
           while (enumerator.MoveNext())
           {
               Player player1 = enumerator.Current;
               player1.MatchmakingAttempts++;
               if (player1.MatchmakingAttempts * 1000 > aiDelay)
               {
                   matchmakeAIPlayer(player1);
                   continue;
               }

               if (!player1.Matchmaked)
               {
                   Player player2 = findOpponentTo(player1);
                   if (player2 != null)
                   {
                       matchmake(player1, player2);
                   }
               }
           }
        }

        private void matchmake(Player player1, Player player2)
        {
            Console.WriteLine("Made Match: " + player1.Username + " vs. " + player2.Username);

            string id = "room" + M.RandomString(20);

            player1.Send("gameFound", id, "Classic");
            player2.Send("gameFound", id, "Classic");

            player1.Disconnect();
            player2.Disconnect();

            player1.Matchmaked = true;
            player2.Matchmaked = true;
        }

        private void matchmakeAIPlayer(Player player)
        {
            // matchmake with AI after AI_DELAY

            player.Matchmaked = true;

            string id = "room" + M.RandomString(20);
            player.Send("gameFound", id, "ClassicAI");
        }

        private Player findOpponentTo(Player player)
        {
           var enumerator = Players.GetEnumerator();
           while (enumerator.MoveNext())
           {
               Player opponent = enumerator.Current;
               if (player.Username != opponent.Username && !opponent.Matchmaked)
               {
                   return opponent;
               }
           }
           return null;
        }

        [DebugAction("Matchmake AI NOW", DebugAction.Icon.Time)]
        public void matchmakeAIPlayerNow()
        {
            matchmakeAIPlayer(GetOpponentTo(null));
        }
    }
}


It solves few edgecases (ie. matchmaking one player twice, etc.).
Zwick
 
Posts: 19
Joined: December 2nd, 2012, 3:35 pm

Next

Return to Multiplayer



cron