Making online feature work for your game “technically”

Nuri Yuri

Hi, I saw a lot of people asking on discord about making online feature for their game, like P2P battle or stuff like that. Most of the time this theme ends with: “what server would host it”?
This question is a bit reductive and I’ll some way to technically make online feature without having a dedicated server (for the internal stuff of the online feature).

Before starting
Making online feature for your game is something that will always imply some modification in the engine you’re using. This operation can be easy (open source based engine) or very hard (proprietary based engine).
If you don’t know how to script, you’ll have to learn because you shouldn’t expect the maintainer of the engine you use to implement such features.

How to make online feature work without using any dedicated server
You probably know about P2P (peer to peer), most of the time, it implies having a server in the middle to connect the two peers (often used in tormenting) but in our case it’s not mandatory.
Let me explain: to connect each other one of the peers needs to know the IP address of the other peer, the server is here to tell the peer what’s the IP address of the other peer. In our case, we can just DM the peer using any support to tell him our IP address.

The big issue with P2P is being able to communicate with the other peer. Most of the time, internet user are being a NAT/PAT that is at least configured to block any unwanted traffic from the outside. This prevents anyone to mess with your devices in your network.
There are three solutions to prevent this issue:
  1. Manually open the game port in your router and tell the router to forward it to your computer (you did this if you self-hosted a Minecraft server).
  2. Enable UPnP on your router if it’s not done. UPnP helps the game to find an IGD that could allow the game to automatically open a port for a little amount of time (or forever).
  3. Use software like Hamachi that creates a fake network the game can use to communicate with the peers if they’re in your fake network.

P2P is the approach Pokémon SDK use for its online Trade and online Battle (using UPnP). The only thing the game has to do is being the host or the client and communicates the right data to the other peer.

Useful ref:

Issue related to sharing IP address
Sharing your IP address is not often a good idea because you don’t really knows what people could do with it so to be a little safe, in the ref you’ll see a function called “code” and another one called “decode”. These function transform the IP address + Port couple to a coded number that makes IP and Port not clear for human. It also prevents collision amongst fangames if you change the value of CodeIP and CodePORT. Some series of number are invalid :p

How to make online work without making complicated for the basic players
Some players know nothing about tech, asking them to enable UPnP or to open a port in their router is too much, for that we’ll need to use a server… and also some tech aware players :q
The idea of this part is making a decentralized system which would prevent you from having an expensive VPS, you can just use a cheap web host (requires a db).

With this system there will be various participants:
- The Game
- The web host ("server")
- The proxies (a tech aware player)
- The fallback servers (a tech away player)

What does the Game do?

The game will do various things. At first, it’ll tries to open a port using UPnP to become a proxy if necessary. The result of this operation is not a huge matter; it’ll only help the system to be stronger (adding more proxy to our decentralized system).

After that, the game will connect to the web host, the goal is to send data to the host (opened port) and most importantly fetch the IP address of some proxies. The game will use the proxy to communicate with the players or to use fallback servers.

What’s the exact role of the web host?

Your web host has the same role as the tracker in Torrents; it’ll track all the available proxies and report them to the game that asks for proxies. In short, it’s a directory.
Without the web host we cannot have a stable system but the webhost can be optional if the game knows proxies that never change their IP address and that are always up.

Note: The web host stores the IP address, Port couple inside a database (SQL or anything else as long as it works). The web host can also do other things like community related stuff (achievements, ban, etc…)

What’s the role of the Proxies?

The proxies play the most important role in our decentralized system. They connect two players together by forwarding the packet to them. The only thing the proxy needs to know is the basic information about the players that want to connect together (with who a player wants to connect).
For that we can write a basic protocol that helps the proxy to forward the packets.

Announce protocol:
  1. The game connects to the proxy
  2. The game tells the information about the player (trainer name, trainer id, a secret phrase)
  3. The proxy accepts or rejects the player
Request protocol:
Once the game announced a player to the proxy, it ask the proxy to connect to a specific player.
  1. The game sends the information about the player it wants to connect.
  2. The proxy search the player in its data
  3. The proxy tells if the player exists and is connected
  4. The proxy sends a request to the other player
The Acknowledge protocol:
When a game receives a request to player with another player the game can decide what to do
  1. The proxy sends the information about the player that wants to connect to the game
  2. The game asks its player what he wants to do
  3. a. If the player accepts, the game sends a positive acknowledge to the proxy.
    b. Otherwise the game sends a negative acknowledge to the proxy, connection ends there.
  4. The proxy remove the two players from its database if the acknowledge was positive.
  5. The two game exchange information using the proxy.
Note: When step 5 is done, the game will have to disconnect from the proxy and redo the announce step.
Note 2: The second step can also be skipped by the game if the other player is black listed by the player.

The service protocol:
A game can also not announce the player to the proxy but just request a service, like underground room, GTS or stuff like that.
  1. The game connects to the proxy.
  2. The game sends the service id to the proxy.
  3. The proxy sends a list of corresponding service (IP+Port+Descr).
  4. The game disconnects from the proxy.

The register protocol:
This protocol is for the fallback servers. They announce themselves to the proxy as a specific service (or a set of services) to make the service protocol work.
  1. The fallback server connects to the proxy.
  2. The fallback server sends its service id or set of services id.
  3. The fallback server tells how much time it should be up (24h should be the max limit)
  4. The proxy acknowledges the fallback server.
  5. The fallback server disconnects from the proxy.

What’s the role of the fallback server?
In our decentralized system, the fallback server is in fact the VPS you would want to have to make your specific online feature work (GTS etc…) but it’s hosted by a generous player that knows a little about technology and that has a good internet connection.
It basically runs the code that makes the online feature work, in addition it implement the register protocol to be visible inside the decentralized system.
On this side everything is up to you, just be respectful to the generous player that hosts your fallback server.

Final note
These are some idea inspired by the era of Minecraft servers. I’ve experienced some stuff and I did Network and Telecommunication Studies (I have a degree about that), all this things are possible, for the P2P part I can show you two videos, for the decentralized part I can tell you an example of something that probably works the same : PeerTube. (I just invented the protocol while writing the document so there’s some variation about the way PeerTube works and what I wrote but the thing is that both things are decentralized system.)
P2P trade between a PSDK Game and a neutral PSDK:
Demonstration of Online Battle between two PSDK processes (using the P2P system but locally):

That’s all for me,
If you have questions, I can answer them :p