Search Unity

Authoritative server for card game logic

Discussion in 'Multiplayer' started by lapouet, Jul 11, 2017.

  1. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    Hi everyone !
    So I'm making a CCG (collectible card game) on Unity with Unity Multiplayer.

    First, some context :
    My model is obviously Hearthstone (HS), in terms of gameplay, but also in terms of networking.
    In HS, clients are only remote controllers that tell the server what the client wants to do, an display what's happening on the Server. The server is therefore doing all the logic part, like authorizing the player to make his move, calculating the damage, the activation of special effects...
    You have a simplified representation of how the server part is working : here.

    And what I've already done :
    For the moment I coded both the server and the client code in the same scene.
    I used the newtork GUI so I can choose if I want the instance of my game to be a server or a client (or even a host). If I choose server, nothing really happen until I choose to be a client on two other instances (when 2 clients enter the room), which start the game.
    I've got a some GameObjects that are "server only" that help the networking and logic parts (with attached NetworkingBehaviour scripts). I have a player prefab which is instanciated for each player joining the room. And all the other GameObjets are basic Monobehaviour objects.

    I've coded the game in a way where the logic part is only made on the server instance of the game, and the results are send to clients via RPC. For example when a player draw a card, I send a RpcDrawACard, and if the player that draw is the local client of the game instance, the card is drawn by the "bottom hero" (and visible), if the player is not the local client of the game instance, the card is drawn by the "top hero" (and not visible yet).
    For the player actions I used Command functions. The client Player says what he wants to do, the Player on the server check if he can do that : if he can the actions are made and displayed on both clients screen via Rpc...

    Now my problems :
    When I use the Matchmaker, so that 2 players can play via internet, I can't choose if I want to be a server or a client. If I understand well, when you create a room with the matchmaker, you are the host of the game. And that's a real problem for my game, because I need the logic part to be separated (avoid cheating, the game continue event if one player disconnect). I need the server to be authoritative, the clients always need to go through it to interact.
    Thus, I don't even know if my method of using the same scene is accurate, because the logic code would be stored in the compilated client game.
    Lastly, I would like to have a system that can store the cards of the players on a server, so they need to login to have access to their own collection.

    Some leads I have :

    As I said before, I'm a real noob, but I did some research.
    I found Photon cloud and PUN, which does basically the same thing that Unity Multiplayer.
    But somewhere I read that the photon rooms where hosted online, so wouldn't it better than what I'm currently using ?
    I also read that It wasn't enough, and that I needed to use Photon Server, and to write the logic for it and not in unity...
    For player profiles and collection, I heard about Playfab. I saw that it's compatible with unity and photon, do you think it's a good way to achieve what I want ?
    For the moment I store my cards in Scriptable objects but if I want to store them on a server wouldn't I need to "convert" them into text files with all their informations ?

    As you can see I know what I want but I'm a bit lost on how to do it, I really need some help ! :)
     
  2. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    That is right: when using Unity Services, there is no authoritative server handling the logic of the game. The player creating a game is the host, meaning it acts like the server and a client at the same time. Being able to host UNET-based games on a dedicated server setup is precisely the reason why I developed Master Server Kit. It allows you to have your UNET-based game running on a dedicated server of your own without changes to your game's code (other than the matchmaking calls). Additionally, it provides player authentication and player data storage facilities, which are usually needed in this scenario.

    Your approach to authoritative networking for a CCG is sound. I fundamentally do the same thing in CCG Kit (but with plain network messages instead). When using Unity Services, the host player runs the server-only object handling the logic of the game; when using Master Server Kit, there is an independent Unity instance acting as the authoritative game server which has the server-only object and is running on a dedicated server instead.
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Dedicated Authoritive servers can be run on UNET with no issues. We do that in our title.
    No limitation. And I don't think Unity Services requires you to do NetworkManager.singleton.StartHost();
    Don't see why you couldn't just StartServer.
    And if you want the game code separated from server code you could simply make two different Unity Projects. But I prefer to keep it in one.
     
    Deleted User likes this.
  4. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    Of course they can. I never said they could not. :)

    StartServer will only get you so far if you want to have something equivalent to Unity Services but running on a dedicated server instead, though, considering you will need to write your own matchmaking system. The point of my post was precisely to point out the existence of an asset that can help in the scenario described by the OP if one does not want to write one from scratch himself.
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Fair enough. I just got the impression when you wrote:
     
  6. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    No worries at all! I should probably have put more emphasis on the matchmaking side of things. :)
     
  7. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    @Spelltwine-Games thx for your answer. I already checked on your project but didn't see that you managed to make an independant server holding the game logic. So is it a Unity instance that run the code ? Isn't it a bit heavy if there is a lost of players ? Anyway, your Master Server Kit seems to be the best "immediate" solution for my game.

    @TwoTen hi ! I don't want to make a LAN game nor a P2P game, and I just re-checked Unet documentation, that's all you can do with it (out of the box). Maybe you're thinking about something I missed, so if you could be more specific it would be nice :)

    I'm sorry , my english is not perfect so I might not be clear. I know you can make an "Authoritative" server, but either it's LAN (and working great with one server and two clients) or the server is one client (one host and one client). I would like the same thing that can be done with LAN but online, and of course with some kind of matchmaking sytem.

    edit : missed three posts ^^ thx for your answers, definitly learning a lot !
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    To start a non lan game non p2p (A Dedicated Server). Just replace StartHost with StartServer on the NetworkManager (If you are using HLAPI)
     
  9. gamevanilla

    gamevanilla

    Joined:
    Dec 28, 2015
    Posts:
    968
    That is right: each spawned game server instance is an independent Unity instance. You will always run them in headless mode (meaning no graphics nor input), but they are certainly more heavyweight than a standalone console application would be. An official server library for UNET is on the roadmap which may also help with this in the future.
     
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    But if you need a console application or something alike with UNET. You can still do that. Just import the Unet DLL.
    But ofcourse you won't be able to utilize the physics and other Unity things
     
  11. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    @TwoTen That's it ? How did i missed that. Yeah I'm using HLAPI for now for testing purposes. I will still need some system that can automatically do that for a lot of concurrent players.
    @Spelltwine-Games okay didin't know about the headless mode, so I guess it's really ok with it. And it seems very quick to adapt to an already working project.
    @TwoTen The good part is that I don't need any Unity related things on the server because there is no need for physic synchronization at all between the clients.
     
  12. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    My game only uses dedicated headless linux servers. And if you want a console application with UNET: https://bitbucket.org/Unity-Technologies/networking
     
  13. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    Maybe you solved my problem but my "noob-level" in networking needs a moment to process all these new information :)
     
  14. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Well, make sure you concider everything.
    Writing your own Console App with Unet might take a while. But it will be VERY efficient.
    Running a dedicated server with Unity instance is a easier but also a bit heavier (Concider your estimated playerbase). But then you don't have any of the sweet options that @Spelltwine-Games package and other similar (if there are any) offer. They give you authentification and other neat things that I personally have to deal with myself.

    Concider what works best for you.
     
  15. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    You're absolutely right. You both gave me two perfectly suitable solutions with their pros and cons.
    I also still consider Photon Server (I don't know if you're familiar with it).
    Thanks a lot.

    edit : @TwoTen how do you manage the matchmaking with the headless server hosted unity instances ? I didn't try it yet, but this tutorial seems perfect, so I would have to launch a new "server" game on the server each time 2 players want to fight?
     
    Last edited: Jul 11, 2017
  16. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
  17. Deleted User

    Deleted User

    Guest

    Just use preprocessor directives, so you could make a defines #if Client #if Server, so the Server code would not be compiled on Client build and vice versa.

    https://wobesdev.wordpress.com/2016/10/10/preprocessor-directives/




    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkManagerCustom : NetworkManager
    6. {
    7.     string login;
    8.     string password;
    9.  
    10.     void Awake()
    11.     {
    12.         NetworkManager.singleton = this;
    13.     }
    14.  
    15.     public override void OnServerDisconnect(NetworkConnection conn)
    16.     {
    17.         GameObject player = conn.playerControllers[0].gameObject;
    18.         Save(player);
    19.     }
    20.  
    21. #if Server
    22.  
    23.     //server save system
    24.     void Save(GameObject player)
    25.     {
    26.         login = "DatabaseLogin";
    27.         password = "DatabasePassword";
    28.  
    29.         //save...
    30.     }
    31.  
    32. #endif
    33.  
    34. }

    Client standalone build decompilation:

     
    Last edited by a moderator: Jul 13, 2017
    RogDolos and TwoTen like this.
  18. lapouet

    lapouet

    Joined:
    Aug 28, 2013
    Posts:
    8
    Amazing trick. I will definitely use that.
    But you got me thinking : is it possible to protect the code from a decompilation like the one you're using in your example ?
     
    Deleted User likes this.
  19. Deleted User

    Deleted User

    Guest

    Yes, there is obfuscation tools simply called "Obfuscator" that could encrypt your code.

    For example:

     
    Last edited by a moderator: Jul 13, 2017
  20. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    What obfuscator have you had the most success with and with what config? To not interfear with Mono or UI methods. Or are you decorating the code with ignores?
     
  21. Deleted User

    Deleted User

    Guest

    Now I have the defines system. But I'll look into Obfuscations, because I do not want cheaters to make a hacks such as wallhack or aimbot which is always client-sided. So obfuscation would hurt their eyes.

    I've seen some obfuscators for Unity on Asset Store, they should work okay.