Structure of a matchmaking system

Hi,
I am developing a simple matchmaking system. I want to match the last two people in the queue with each other.
My proposed algorithm is that As soon as the count of people reaches 2, the second person fills a global variable (suppose matchId ) with a value greater than 0.
The first player who was waiting and checking for the matchId value to be greater than 0 now gets the value and joins the match.
Is it possible to implement this structure in teal (I am using Pyteal)?
If yes, how would you suggest the waiting mechanism for the first person?

You can just have a global variable which is either empty if no player is waiting or which contains the address of the player waiting.
The first player will make the smart contract set this variable to its address.
The second player will make the smart contract reset the variable and create a matchId.

If you want multiple match concurrently, you may want to store matchId in local storage instead.

thanks but how the first player can get the matchId now? how second player tells the matchId to first player?

You can set up the smart contract as follows:

  • Global storage: one []byte variable waitingPlayer
  • Local storage: one []byte variable partner

Suppose Alice and Bob want to play with address ALICE_ADDRESS and BOB_ADDRESS.

  1. Alice calls the smart contract. We assume that Alice is not in a game so her local storage variable partner is empty. Since waitingPlayer is empty, the smart contract set waitingPlayer=ALICE_ADDRESS
  2. Bob calls the smart contract. We assume that Bob is not in a game so his local storage variable partner is empty. Since waitingPlayer is not empty, the smart contract does the following:
    a. Set Alice’s partner to BOB_ADDRESS.
    b. Set Bob’s partner to ALICE_ADDRESS.
    c. Set waitingPlayer to empty

Now Alice and Bob have in their local storage the address of their partner, which is sufficient to run a game.

If you want to have match IDs, you can also have a global variable lastMatchID that you increment. When a match is found, you increment this gobal variable and store its value in another local variable matchID of Alice and Bob. But I think in most cases you don’t need that.

Thanks for your explanation, so when Bob calls the smart contract, the smart contract inserts a value into Alice’s local storage variable?
My first thought was to solve it this way, but I read the below sentence in the documentation and realized that the smart contract cannot write to Alice’s wallet local storage when Bob calls it.

This sentences :
In order to read or manipulate an account’s local state, that account must be present in the application call transaction’s Txn.accounts array.
https://pyteal.readthedocs.io/en/latest/state.html

Are both bob and Alice exist in the Txn.accounts?

I tried your suggested algorithm and got the following error on localPut when i call the smart contract for second player (Bob):
logic eval error: invalid Account reference
Here’s the code (I wrote a comment where I received the error):

      scratchWaitinglayer.store(App.globalGet(Bytes("waitingPlayer"))),
        If(scratchWaitinglayer.load() == Bytes(""),  # no player in Queue
           Seq([
               App.globalPut(Bytes("waitingPlayer"), Txn.sender()),
           ])
           ,  # a player is in Queue
           Seq([
               App.localPut(Int(0), Bytes("partner"), scratchWaitinglayer.load()), #Got error here
               App.localPut(scratchWaitinglayer.load(),Bytes("partner"),Txn.sender()),
               App.globalPut(Bytes("waitingPlayer"), Bytes("")),
           ])
           ),

The application call sender is implicitly in Txn.accounts at index 0.
You indeed need Alice’s address to Txn.accounts.

The error you see is probably due to the fact that Alice’s address is not included in Txn.accounts.

Thank you, I included Alice’s address when sending the transaction, and the problem has been resolved