# Match

All the stuff are controlled by the mothership server which is base on TCP but all data in the matchs are controlled by the child server. All communications inside a match is based on RUDP protocol. RUDP is much faster than the TCP, and it's excellent for Fast-paced like FPS games and MMO games.

# Send Package

For sending data you should use SendReliableData method and data should be json.

    JSONObject dataJson = new JSONObject();
    dataJson.Add("matchId", matchId);
    dataJson.Add("userId" , userId);
    dataJson.Add("matchData", _vector3.ToString());

    client.ChildServer.SendReliableData(dataJson);

WARNING

matchId and userId is required in each packet.

# OnReceivePacket Event

You can receive other players match data in this event.

    client.ChildServer.OnReceivePacket = (data) =>
    {
        JSONNode jsonRes = JSON.Parse(data);
        Debug.Log(jsonRes);
    };

# Save Match Data

You can save the important state of the match in the child server. We want to use this information when some users have a problem joining the game or their connection has a problem. After they request to join the match we give them this information. Here we need id for objects, this id can be user id or other objects in the game. For example, we can save the user's cloth or their weapons or the last position of that user.

    JSONObject json = new JSONObject();
    json.Add("code", 1);
    json.Add("position", "xyz");
    json.Add("weapon", "wid_1");
    json.Add("set", "sid_3");

    client.ChildServer.SaveMatchData(matchId, anyId, json);

# Send Match State

We need some rules for implementing the end of the match. We use these rules to figure out how should we finish the match. All rules must be set in the dashboard, The rule must have matchMode, key, and value. If we want to determine the winner we should send data to the server. The data contains action and team . For example, we have a rule like if a player killed 10 players we consider that player as a winner. Each time player killed a player we send that data to the server by SendMatchState method. The result will appear in OnEnd event

    client.ChildServer.SendMatchState(matchId, userId, userTeam, action);

If we have a Free For All match we consider a player as a winner if our match is a Team Deathmatch we consider that team as the winner.

WARNING

The action must be float or string

TIP

In some matches, we can set a rule like if a player destroyed an enemy base, we consider that player or team as the winner. In that case we can set the rule as string ID in the dashboard and send string action to the server from client.

# Check Match Status

If the user left the game for any reason and wants to join again we can check if the match is in progress or not and then the user can join the game. We just need to send match ID to the server.

    client.MatchController.IsMatchInProgressStatus(matchId, (res) =>
        { Debug.Log(res.Message + " " + res.Data); });

# Join To Match

Users can join the match by knowing the match id and using JoinToMatch method. After the user joined to the match server send all match data in response.

    client.MatchController.JoinToMatch(matchId, (res) =>
    {
        Debug.Log(res.Message);
        Debug.Log(res.Data);
    });

# Leave Match

User can leave match by using LeaveMatch method.

    client.MatchController.LeaveMatch(matchId, (res) => { Debug.Log(res.Message); });

# OnEnd Event

After the match is ended, we receive the end message.

    client.MatchController.OnEnd = (res) =>
    {
        Debug.Log(res.Message);
    };