Sign In Register

How can we help you today?

Start a new topic
Answered

Multiplayer Card Game Tutorial

Hello guys,

I know this topic has been discussed a lot, but I want to check the best solution for my case.

I finished doing a game similar to hearthstone. The single mode is working fine.

I want to add the multiplayer feature now. Details are:

- User selects multiplayer mode, and waits till another player joins ( its only 2 players max)

- When another player joins, the game starts.

- At the start of the game, server sends the same array of integers to both players

- After player 1 plays a card, the device sends the card ID to the server, which in turns forwards this id to player 2 devide.

- AI logic in player two device will play a card depending on the received card ID.


I passed by your tutorials but I am a bit confused. Shall I use a match or challenge option? It should be real time based right?


Is there any example on how to use  real-time scripts. I didnt get this part.


Thank you your help! 



Best Answer

Hi Mohamad!


No your game does not need realtime services if it is a card game. You will be using a mixture of Matches and Challenges, they compliment each other. We have a realtime tutorials on our docs, that give you examples of how to use realtime scripts. Here's an example using unity, but you can see how realtime scripts are used for logic and input and out of data: https://docs.gamesparks.com/tutorials/real-time-services/clock-synchronization-and-network-programming.html

The Hearthstone tutorial on our docs website walks you through setting up a multiplayer card game similar to Hearthstone. (https://docs.gamesparks.com/tutorials/multiplayer/hearthstone-example/).

The tutorial is a little complex, and lengthy and assumes you have a basic understanding of Cloud Code but it answers all your questions above. The Async messages are very fast, and exactly what you need. You'd use matches to match up your players, and add extra logic to your 'match found message' cloud code which automatically sets up a challenge and moves the sequence on until the ChallengeStarted Message initialises your game for your players. The challenge is where you'd save your game's logic and manage the data of the players. 

Here's a script from the tutorial that initialises the game when the challenge has been started (In ChallengeStarted message cloud code):


//Declare challenge

var chal = Spark.getChallenge(Spark.getData().challenge.challengeId);

//Player IDs

var challengerId = chal.getChallengerId();

var challengedId = chal.getChallengedPlayerIds()[0];

//Initiation of the challenge settings through the challenger(similar to host)

if(Spark.getPlayer().getPlayerId() === challengerId){

    //Construct the play field JSON - Used for the playing field

    var playField = {};

    playField[challengerId] = {};

    playField[challengedId] = {};

    //Construct the current hand JSON - Used for the cards in the player's hands

    var currentHand = {};

    currentHand[challengerId] = {};

    currentHand[challengedId] = {};

    //Construct player details

    var playerStats = {};

    playerStats[challengerId] = {"overallMana": 1, "currentMana": 1, "playerHealth": 30, "cardsPulled": 0, "hasPulled": true, "tauntProtection":false }

    playerStats[challengedId] = {"overallMana": 1, "currentMana": 1, "playerHealth": 30, "cardsPulled": 0, "hasPulled": true, "tauntProtection":false }

    //Pull three cards for each player

    for(var i = 0; i < 3; i++){

        //First Id

        var pId = challengerId;

        require("pullCardModule");

        //second Id

        var pId = challengedId;

        require("pullCardModule");

    }

    //Save the contructed JSONs against the challenge's scriptData

    chal.setScriptData("playField",playField);

    chal.setScriptData("currentHand", currentHand);

    chal.setScriptData("playerStats", playerStats)

}


You'd then use the playerIDs and ChallengeID to pull those objects created and manage your game's data using ChallengeEvent which take in the unique ChallengeID to refer to that challenge.


Here's quick answers to your questions, more detail in the tutorials:


- User selects multiplayer mode, and waits till another player joins ( its only 2 players max)

      -Matchmaking request (Set player max to 2 players when creating the match on the platform)

- When another player joins, the game starts.

      -Game will automatically start when another player is matched provided what's above is done

- At the start of the game, server sends the same array of integers to both players

       -When the match is found you can fire off logic by editing the cloud code for 'MatchFoundMessage'. This logic will create a challenge and invite the second player, then                  automatically accepts it for them. When the challenge is started the code in 'ChallengeStarted Message' will fire off, you can have all your logic there similar to the example above. more detail in the tutorial.

- After player 1 plays a card, the device sends the card ID to the server, which in turns forwards this id to player 2 devide.

       -Players can use ChallengeEvents to interact with a challenge. A player will send data about their card to the server, and in turn the server to send scriptMessages to other players.

- AI logic in player two device will play a card depending on the received card ID.

       -When the scriptMessage reaches the client (Message Listeners/Handlers) you will have code in your game which acts upon that data. (Card is moved from player hand to player            field will result in a scriptMessage to your game which in turn reflect it in the frontend.) This is done through pullCard, pushCard and playCard functions in our tutorial.


I understand this is daunting at first, but bit by bit it will start making more sense. We are always here to help. If you have any more questions, please fire away.


Cheers, 
Omar


Answer

Hi Mohamad!


No your game does not need realtime services if it is a card game. You will be using a mixture of Matches and Challenges, they compliment each other. We have a realtime tutorials on our docs, that give you examples of how to use realtime scripts. Here's an example using unity, but you can see how realtime scripts are used for logic and input and out of data: https://docs.gamesparks.com/tutorials/real-time-services/clock-synchronization-and-network-programming.html

The Hearthstone tutorial on our docs website walks you through setting up a multiplayer card game similar to Hearthstone. (https://docs.gamesparks.com/tutorials/multiplayer/hearthstone-example/).

The tutorial is a little complex, and lengthy and assumes you have a basic understanding of Cloud Code but it answers all your questions above. The Async messages are very fast, and exactly what you need. You'd use matches to match up your players, and add extra logic to your 'match found message' cloud code which automatically sets up a challenge and moves the sequence on until the ChallengeStarted Message initialises your game for your players. The challenge is where you'd save your game's logic and manage the data of the players. 

Here's a script from the tutorial that initialises the game when the challenge has been started (In ChallengeStarted message cloud code):


//Declare challenge

var chal = Spark.getChallenge(Spark.getData().challenge.challengeId);

//Player IDs

var challengerId = chal.getChallengerId();

var challengedId = chal.getChallengedPlayerIds()[0];

//Initiation of the challenge settings through the challenger(similar to host)

if(Spark.getPlayer().getPlayerId() === challengerId){

    //Construct the play field JSON - Used for the playing field

    var playField = {};

    playField[challengerId] = {};

    playField[challengedId] = {};

    //Construct the current hand JSON - Used for the cards in the player's hands

    var currentHand = {};

    currentHand[challengerId] = {};

    currentHand[challengedId] = {};

    //Construct player details

    var playerStats = {};

    playerStats[challengerId] = {"overallMana": 1, "currentMana": 1, "playerHealth": 30, "cardsPulled": 0, "hasPulled": true, "tauntProtection":false }

    playerStats[challengedId] = {"overallMana": 1, "currentMana": 1, "playerHealth": 30, "cardsPulled": 0, "hasPulled": true, "tauntProtection":false }

    //Pull three cards for each player

    for(var i = 0; i < 3; i++){

        //First Id

        var pId = challengerId;

        require("pullCardModule");

        //second Id

        var pId = challengedId;

        require("pullCardModule");

    }

    //Save the contructed JSONs against the challenge's scriptData

    chal.setScriptData("playField",playField);

    chal.setScriptData("currentHand", currentHand);

    chal.setScriptData("playerStats", playerStats)

}


You'd then use the playerIDs and ChallengeID to pull those objects created and manage your game's data using ChallengeEvent which take in the unique ChallengeID to refer to that challenge.


Here's quick answers to your questions, more detail in the tutorials:


- User selects multiplayer mode, and waits till another player joins ( its only 2 players max)

      -Matchmaking request (Set player max to 2 players when creating the match on the platform)

- When another player joins, the game starts.

      -Game will automatically start when another player is matched provided what's above is done

- At the start of the game, server sends the same array of integers to both players

       -When the match is found you can fire off logic by editing the cloud code for 'MatchFoundMessage'. This logic will create a challenge and invite the second player, then                  automatically accepts it for them. When the challenge is started the code in 'ChallengeStarted Message' will fire off, you can have all your logic there similar to the example above. more detail in the tutorial.

- After player 1 plays a card, the device sends the card ID to the server, which in turns forwards this id to player 2 devide.

       -Players can use ChallengeEvents to interact with a challenge. A player will send data about their card to the server, and in turn the server to send scriptMessages to other players.

- AI logic in player two device will play a card depending on the received card ID.

       -When the scriptMessage reaches the client (Message Listeners/Handlers) you will have code in your game which acts upon that data. (Card is moved from player hand to player            field will result in a scriptMessage to your game which in turn reflect it in the frontend.) This is done through pullCard, pushCard and playCard functions in our tutorial.


I understand this is daunting at first, but bit by bit it will start making more sense. We are always here to help. If you have any more questions, please fire away.


Cheers, 
Omar

Hi Omar!


Thank you for your reply!


I went through the tutorials and things are clearer now.


I have one issue regarding this point:


- After player 1 plays a card, the device sends the card ID to the server, which in turns forwards this id to player 2 devide.

       -Players can use ChallengeEvents to interact with a challenge. A player will send data about their card to the server, and in turn the server to send scriptMessages to other players.


How to create the scriptMessage and what listener should I include in the Unity Code? 


Besides that, everything is clear now. Thank you

Login to post a comment