1v1 matchmaking

Hi, I'm new to PUN so my question is basic concept of PUN.
I'm working on a game that has 1v1 battle. For matchmaking I try to make something by my knowledge. First, both players enter the lobby after entering they have quick match button that by clicking on it one of them create room and another one join to that room by JoinRandomRoom. The first problem is if both players click on quick match button almost at the same time both of them creating room, What is the solution for this problem ? .

Piece of code :


void Start()
{
if (!PhotonNetwork.connected)
{
print("Connecting to server ...");
PhotonNetwork.ConnectUsingSettings("0.1");
}
}

private void OnConnectedToMaster()
{
print("Connected to master");
PhotonNetwork.automaticallySyncScene = true;
PhotonNetwork.JoinLobby(TypedLobby.Default);
}

private void OnJoinedLobby()
{
print("Joined lobby");
JoinedLobby();
}

private void OnPhotonRandomJoinFailed()
{
print("Random join failed");
RoomOptions roomOptions = new RoomOptions { IsVisible = true, IsOpen = true, MaxPlayers = 2 };
PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
}

private void OnCreatedRoom()
{
print("Created room");
}

private void OnJoinedRoom()
{
print("Joined room " + PhotonNetwork.room.Name);
JoinedRoom();
}

public void JoinedLobby()
{
if (onLobbyJoined != null)
{
onLobbyJoined();
}
}

public void JoinedRoom()
{
if (onRoomJoined != null)
{
onRoomJoined();
}
}


Second, When another player joins the room that created by master client it is notified by OnPhotonPlayerConnected after that I'm loading scene by PhotonNetwork.LoadLevel for master client and because of PhotonNetwork.automaticallySyncScene another player scene loads.

Piece of code :


public void LoadLevel()
{
if (!PhotonNetwork.isMasterClient)
return;
PhotonNetwork.LoadLevel(1);
}

private void OnPhotonPlayerConnected(PhotonPlayer player)
{
LoadLevel();
}


Third, wait for both players to load their scene after that by triggering event, tell both players to hide their "Waiting for opponent" message.

Piece of code :


private void OnSceneFinishedLoading(Scene scene, LoadSceneMode mode)
{
if (scene.name == "Main")
{
photonView.RPC("RPC_LoadedGameScene", PhotonTargets.MasterClient, PhotonNetwork.player);
}
}

[PunRPC]
private void RPC_LoadedGameScene(PhotonPlayer photonPlayer)
{
playersInGame++;
if (playersInGame == PhotonNetwork.playerList.Length)
{
print("All players are in the game scene");
photonView.RPC("RPC_NotifyAllPlayersLoadedGameScene", PhotonTargets.All);
//photonView.RPC("RPC_CreatePlayer", PhotonTargets.All);
}
}

[PunRPC]
private void RPC_NotifyAllPlayersLoadedGameScene()
{
if (onAllPlayersLoadedGameScene != null)
onAllPlayersLoadedGameScene();
}


The problem is "Waiting for opponent" message doesn't disappear at the same time, I want to know if it is normal or not and RPC send event trigger at the same time, if it isn't what is the solution ?.


After that I want to have a count down timer in my game, I use this piece of code for timing:


public class TimerCountDown : MonoBehaviour
{
public int duration;

private int startTime;
private int storedStartTime;
private int storedDuration;
private Text timerText;

private void Awake()
{
PlayerNetwork.onAllPlayersLoadedGameScene += TimeInitialization;

timerText = transform.GetChild(0).GetComponent();
}

// Use this for initialization
void Start()
{

}

public void Update()
{
if (storedDuration != 0 && storedDuration != 0)
{
int timerDuration = (PhotonNetwork.ServerTimestamp - storedStartTime) / 1000;
int currentTimer = storedDuration - timerDuration;

timerText.text = currentTimer.ToString();
}
}

public void OnPhotonCustomRoomPropertiesChanged(Hashtable propertiesThatChanged)
{
if (propertiesThatChanged.ContainsKey("startTime") && propertiesThatChanged.ContainsKey("duration"))
{
storedStartTime = (int)propertiesThatChanged["startTime"];
storedDuration = (int)propertiesThatChanged["duration"];
}
}

public void TimeInitialization()
{
startTime = PhotonNetwork.ServerTimestamp;
Hashtable ht = new Hashtable() { { "startTime", startTime }, { "duration", duration } };
PhotonNetwork.room.SetCustomProperties(ht);
}
}


The problem is, there is a little difference in their timer. What is the solution ?

Comments

  • Hi @ATHellboy,

    The first problem is if both players click on quick match button almost at the same time both of them creating room, What is the solution for this problem ?


    I guess there isn't a better solution than the one you already have implemented, at least what I would say when taking a look at the code above.

    The problem is "Waiting for opponent" message doesn't disappear at the same time, I want to know if it is normal or not and RPC send event trigger at the same time, if it isn't what is the solution ?


    Not sure if I understand the problem correctly, so please correct me if I misunderstood it: In this case it is correct, that the message doesn't disappear at the same time because you use PhotonTargets.All as parameter. This means that the RPC is sent to the server to notify each client except the client who actually sends the RPC. Means that the sender processes the RPC immediately while the other clients are still waiting for the RPC to get forwarded to them. Instead you can use PhotonTargets.AllViaServer as parameter. This way the sender doesn't process the RPC immediately and waits for the notification from the server, too. Please not that you possibly still won't have a simultaneously disappearing message on all clients, because of different latency for each client.

    Another option you might have here, is to use the Player Properties which work similar to the Room Properties you already use.

    The problem is, there is a little difference in their timer. What is the solution ?


    If I get things right, each player calls the TimeInitialization function and furthermore each player adds a different 'startTime' to the Room Properties. Again, please correct me if I'm wrong. To fix this, only the MasterClient should set the 'startTime' in the Room Properties. Besides that it should work the way it is.
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @ATHellboy,

    Thank you for choosing Photon!

    I will talk about your first question only,

    The first problem is if both players click on quick match button almost at the same time both of them creating room, What is the solution for this problem ?


    As my colleague @Christian_Simon explained, this is a "known issue" and happens when you are manually testing your game with few players at an early stage. Once you have more players to do matchmaking you will not notice this.
  • ATHellboy
    ATHellboy
    edited November 2017
    Hi @Christian_Simon and @JohnTube ,

    Thanks for replying, I left thinking about first problem for myself too, I think more players can solve this problem too and if player creates room and no one join it in specific time I can ignore current matchmaking and searching for opponent again.

    For second problem, thanks for solution it solved my problem. But I have a question about PhotonTargets, what is the difference between All and Others ?
  • But I have a question about PhotonTargets, what is the difference between All and Others ?


    You can take a look at the comments made for this enum. It is implemented in the Enums.cs script which you can find in Assets\Photon Unity Networking\Plugins\PhotonNetwork\ directory.

    All: Sends the RPC to everyone else and executes it immediately on this client.
    Others: Sends the RPC to everyone else. This client does not execute the RPC.