How to Handle Game States

In SkillzCrossplatform.cs, you will be able to find functions you can call for your synchronous tournaments, such as:

GetMatchInfo:

  •  Returns a Match object that has details regarding the specific match the user is in.

IsMatchCompleted: 

  • Returns true if a player has started disconnect/shutdown (on reporting score or aborting).

SendData: 

  • Explained in previous section <link here>.

GetConnectedPlayerCount: 

  • Returns num players currently connected to the server.

GetCurrentPlayerId: 

  • Returns current player ID as a long.

GetServerTime: 

  • Returns server time in seconds as double (Prototype, Unreliable).

GetTimeLeftForReconnection: 

  • Returns the actual time a player has left to reconnect if they are disconnected. You’ll need to pass in the tournament player id of the player that lost connection. For the current player pass SkillzCrossPlatform.getCurrentPlayerId.

Step 1:

Determine which player goes first. Since it’s a turn-based synchronous game, you will need to create logic on which player goes first. Here are some ideas:

  1. The first player returned from the Players array could go first.
  2. Use the Skillz Random class to implement a coin toss functionality.

Example:

public static void AssignPlayers()
{
    var players = SkillzCrossPlatform.GetMatchInfo().Players;

    if (players.Count < 2)
    {
       Debug.Log ("UNITY - Non async game");
       return;
    }

    if (players.ElementAt(0).ID < players.ElementAt(1).ID)
    {
       PlayerOne = players.ElementAt (0);
       PlayerTwo = players.ElementAt (1);
    }
    else
    {
       PlayerOne = players.ElementAt (1);
       PlayerTwo = players.ElementAt (0);
    }
}

Step 2: 

Reporting a score is still the same. You’ll just need to call SkillzCrossPlatform.ReportFinalScore(score).

 


 
When the game reaches a finishing state, it is important that both players are able to receive and resolve the final board state before reporting a score. To help with this, the SyncDelegate declares the onMatchCompleted method. This is called once a player has reported a score or aborted the match through the corresponding Skillz API methods.
Recommended: Only verify winning states and finalize the match in onDidReceiveData & onMatchCompleted:
Note: Calls to report or abort a score will send an additional message that will trigger onMatchCompleted for the opponent player.

At this point, you have the tools necessary to develop the vast majority of your synchronous application. Spend time developing the game and data exchange in a manner that will fulfill the needs of the game. It is recommended that you greatly familiarize yourself with the data exchange flow before moving to the following advanced steps.

 

Next Step: Handle Disconnections

Note: Without following the steps to handle disconnections, you may experience frozen game states as the disconnect logic is not yet being handled.