Both player answer at same time

Hi, I have a trivia game, where the user has to answer the name of the image and they win and update ui for themselves and a raise event for the other player. Problem is when both the users are answering at the same time both are doing that and the functions are running twice. Can someone help? I tried getting the time when they pressed through custom properties which has reduced the error chance but still not solved

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Afterlife1707,

    I think this concurrency problem is better solved via CAS (Check-And-Swap or Compare-And-Set).
    This is done via ExpectedProperties parameters in SetCustomProperties calls.
    You set the key/value pairs of ExpectedProperties Hashtable matching the custom properties in their state before changing them, so the first client who change them succeeds and the second one fails.
  • Hi @JohnTube,
    Sorry for the late reply. I tried this using Custom room properties, still if its at the exact same time, they both execute it and both succeed. Is there any other way?
  • Would like to edit the above reply:
    I did try it and I'm struggling with the implementation. Is there any reference or example I can refer from?
    I think I'm not doing it the right way and it might work.
    Thanks
  • JohnTube
    JohnTube ✭✭✭✭✭
    Maybe you start sharing the code you have tried and how it fails.
    We don't have an example of CAS.
  • Afterlife1707
    edited July 2021
    ExitGames.Client.Photon.Hashtable CheckAnsweredProps = new ExitGames.Client.Photon.Hashtable();
    ExitGames.Client.Photon.Hashtable ExpectedProps = new ExitGames.Client.Photon.Hashtable();
    	
    	//in the awake method I set the default values
    	
    	    CheckAnsweredProps["HasAnyoneAnswered"] = false;
                PhotonNetwork.CurrentRoom.SetCustomProperties(CheckAnsweredProps);
                SetDefaultExpectedProp();
    			
    	//body of the function SetDefaultExpectedProp
        void SetDefaultExpectedProp()
        {
            ExpectedProps["ExpectedDefaultValue"] = false;
            PhotonNetwork.CurrentRoom.SetCustomProperties(ExpectedProps);
        }
    
    //below is the function called where a player answers correctly, so both players can call this at same time
    
    	bool success = (bool)PhotonNetwork.CurrentRoom.CustomProperties["HasAnyoneAnswered"];
    	if (!success)
            {
                SetAnsweredTrue();
    	    //rest of function
            }
    
    	//
    	 public void SetAnsweredTrue()
        {
            CheckAnsweredProps["HasAnyoneAnswered"] = true;
            PhotonNetwork.CurrentRoom.SetCustomProperties(CheckAnsweredProps, ExpectedProps);
        }
    


    If the CheckAnsweredProps hashtable has a value of true already, it shouldn't run right, as the expected value is false. Again, I'm not sure about the implementation.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited July 2021
    well I would do it in a simple way and use the same property key:
    	//body of the function SetDefaultExpectedProp
        void SetDefaultExpectedProp() // or init this during room creation
        {
            ExpectedProps["HasAnyoneAnswered"] = false;
            PhotonNetwork.CurrentRoom.SetCustomProperties(ExpectedProps);
        }
    
    public void SetAnsweredTrue()
        {
            CheckAnsweredProps["HasAnyoneAnswered"] = true;
            ExpectedProps["HasAnyoneAnswered"] = false;
            PhotonNetwork.CurrentRoom.SetCustomProperties(CheckAnsweredProps, ExpectedProps);
        }
    

    for your code you forgot to change ExpectedProperty value:
    public void SetAnsweredTrue()
        {
            CheckAnsweredProps["HasAnyoneAnswered"] = true;
            CheckAnsweredProps["ExpectedDefaultValue"] = true;
            PhotonNetwork.CurrentRoom.SetCustomProperties(CheckAnsweredProps, ExpectedProps);
        }
    
  • Hi, I tried it but they are both calling it at the same time, and both are succeeding. Hence both the players are winning.
  • Hi @JohnTube, can you think of any other way to resolve this issue? Sorry for bothering you so much, been stuck on this for some time now.
  • JohnTube
    JohnTube ✭✭✭✭✭
    hi @Afterlife1707,

    make sure both clients are joined to the same room.
    using the same ExpectedProperties from two clients to change those same properties will result in an error in one of the two clients.
  • Hi @JohnTube,
    So this solution won't work for a draw case right? What I'm doing now is after answering, the time when they answered is stored in the custom properties, then I wait for 0.5 secs and check the other player's time when they answered If they haven't answered yet there is a default value. So we compare the time and then can check who answered first. This is working for now but I'm not sure if it is completely error free or a good solution. Let me know what you think
  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @Afterlife1707,

    Yes you could use this approach.

    There is no perfect approach.