Cupid Matchmaker For Unity Mirror

https://github.com/Sewmina7/CupidServer

https://github.com/Sewmina7/CupidUnity


Inspiration

When I started the development on this project, There was no proper way to start dedicated servers on demand for mirror networked games in unity. Most developers that needs to utilize multiple game room in mirror uses a relay network system where the server actually serves as a relay, The game server will be a players computer.

 What I didn't like about not using dedicated servers is with Dedicated servers you can setup server authoritive player controls and make the game so much secure than the other method.
Take Rocket League for example. Developers of this game has stated that they are using dedicated servers for each game session. Hence the server is not on any players computer, making the game hard to penetrate using client side exploits.


Challenges

- Complexity

- Performance Cost

This approach is much challenging than the simple relay system. You have to start a game's server instance everytime a new match starts. making this costy performance wise as well.

And since you need to run multiple server instances on the server computer, You need to use a different port for each server instance. and need to make sure not to use a port for a new instance which is already in use by another server instance.


Implementation

My approach for this challenge was to write a program that handles matchmaking and server handling.
I chose NodeJS to write this program since its easy to make a public endpoint and lack of boilerplate.

I exposed a port to the public that utilizes a REST API. This API is used by the game's client instance to communicate with this server program.

When a new client press on start matchmaking, client will send a request to the server program using this API Endpoint. Server program will keep the users on a waiting list until there is enough players to start a match. Once there is enough players to start a new match, Program creates a new object for the new match and stores it in a list with start time and players list and a new port number for the new game server instance.

 

Challenge #1

When determining a port number for the new game server instance, I need to make sure that this port is available, open and not in use for another application. 

To overcome this, I made a variable for port range in config file. So you can enter a range of ports that is not in use by another application and keep them open (ex: 4000 : 5000). New match will get a random port number within the range provided.

Since we store the port number for each match in the list in matchmaker program, we can iterate through the list and see if new port is in any of those matches. If not we are good to go, Otherwise, choose a new number and do the check again.

 

Once a port is determined and validated, the program will start a new instance of game server with the new port number passed as an argument. There's a script inside the game server to automatically start server only for mirror using the port number reading from the argument.

Once the game server instance is started, the matchmaker will communicate back to the client and say, 'Hey, we found a new match for you. Use this port number to connect to it' (only the message is not this pretty, It's just data parsed as JSON) . Once the client receives this message, it starts client mode on mirror using the port number it just received, Connecting the client to newly made server instance, Joining the players together.

 

Challenge #2

Technically, if there was no logic in the server instance to close itself once the match is over. Server instances will keep piling up on server, hogging performance and It will eventually freeze it up.

to Overcome this, I've put another variable in config file for TTL (time to live). Matches list on matchmaker program keeps track on program instance of game server started for each new match.
Since we already have the started time of each match, matchmaker will check for expired matches and will kill the server instance forcefully if it haven't closed itself already, and will remove the match from list aswell.


Bonus:

Once the program is completed, I thought for a name, What other name is better than the almighty matchmaker, The Cupid!

Comments

Popular Posts