Sign In Register

How can we help you today?

Start a new topic
Answered

Random Challenges

Hello,

I was using another BaaS in my games, which provide a random challenge(match) functionality. I'm new with GameSparks and I don't know to do this.

Basically I want to allow the user to create a challenge without inviting anyone, later another user can "pick" this challenge by creating a new challenge. 


Is that possible?


Thanks


Best Answer

Alright I just wanted to share my final solution that I go working now so that maybe this can help other people who are in the same boat.


So instead of trying to hijack the ChallengeRequest as I initially was doing, I remembered about Events.


So I just created a new Event called ChallengeRandomPlayer, and then I basically took all of my cloud code that I was trying to run in the CreateChallengeRequest code, and moved it over there.  I don't have to pass anything to the backend either, I simply just call the event via LogEventRequest from the front end, and pass in my challenge string as the event key.


Then it runs my code that starts the random challenge after getting a random playerId from my randomPlayerMap.


After the challenge is created, I then get the challenge data from a GetChallengeRequest, since all I have from the CreateChallengeRequest is the challengeInstanceId.  So I pass that to the GetChallengeRequest and send that off.


Once I get the data back, I simply get the challenged data from the challenge object on the GetChallengeResponse, and took out the ExternalID.FB, name, and id.  I set them to the scriptdata, and then on the response on my client code, I now have all of the info to let the user know that they have sent a random challenge and to whom they sent it.


Hopefully this information will help out some other people.


I am wondering about this myself.


Is there anyway to send a random challenge request to all players in the system, including those not on your friends list?


Or to send it to ONLY players that are not on your friends list?

Hi Andrew,


You could create a document that stores every playerId in an array. Then when you want to challenge someone, load the document and loop through the array until you find someone, either randomly or by any other means.


We don't support looping through the playerCollection just yet as this contains a lot of data per user, when your game becomes incredibly popular this could become quite slow and resource intensive.


Shane

Thanks Shane.


Yeah last night I messed with creating another collection in the DB called randomPlayerMap.


Then, in the authentication request, I'm checking if that ID exists in that collection, and if not, I add it with a key of the size of collection + 1.  So basically I have a map of the player ID's that just maps to a key that increments starting from 1.


Then I created some cloud code that will generate a random number between 1 and the size of the collection, and get that item based on that key.  I then have some logic to check if that ID is the current users ID, someone on his friends list, or someone he has already sent a challenge to.  If it is any of that, then it will fetch a new random ID.  


By "random" I wanted it to be someone that is random and NOT on their friends list, which is why I added that logic.


The next area I somewhat got stuck in was how to actually hook all of this together.  It was late last night and I didn't get too far because I was tired, but what I was trying to do was send a CreateChallengeRequest to a unique playerId that I am naming something like 'random-invite-player'.  And then in the cloud code, in the code for CreateChallengeRequest, I'm checking for the specific playerId being passed, and when it finds it, it hits all of the logic to find a random playerId from the collection, and then in the cloud code, sends a challenge request to that person.


However, when I do this, the code from the front end comes back as having an error because it the 'random-invite-player' playerId does not exist.  But in the cloud code, it's sending the random invite.  


That is as far as I got, and I'm wondering if there is a better way to do this.  This seems somewhat hacky so I wasn't sure what the best route to go was.  I also wanted to pass some player data back to the front end after getting the random playerId.  Again, I didn't look more into this just because it was late, but I was going to try and see if there is a way that I can set the response that goes back to the front end, to the response that I get from calling the request from the cloud code.

Answer

Alright I just wanted to share my final solution that I go working now so that maybe this can help other people who are in the same boat.


So instead of trying to hijack the ChallengeRequest as I initially was doing, I remembered about Events.


So I just created a new Event called ChallengeRandomPlayer, and then I basically took all of my cloud code that I was trying to run in the CreateChallengeRequest code, and moved it over there.  I don't have to pass anything to the backend either, I simply just call the event via LogEventRequest from the front end, and pass in my challenge string as the event key.


Then it runs my code that starts the random challenge after getting a random playerId from my randomPlayerMap.


After the challenge is created, I then get the challenge data from a GetChallengeRequest, since all I have from the CreateChallengeRequest is the challengeInstanceId.  So I pass that to the GetChallengeRequest and send that off.


Once I get the data back, I simply get the challenged data from the challenge object on the GetChallengeResponse, and took out the ExternalID.FB, name, and id.  I set them to the scriptdata, and then on the response on my client code, I now have all of the info to let the user know that they have sent a random challenge and to whom they sent it.


Hopefully this information will help out some other people.

Hi Andrew, thanks for sharing your solution, sorry for the delay, but releasing the game for multiple platform has been a difficult task..

Here is my solution:

1 - Player A calls the event GetRandomMatches event. Which verifies in the free matches collection if there is an available challenge. That collection just contains the challenger id and challenge id.

2.A - If find a record, Player A calls JoinChallengeRequest.

3.A - Player A calls StartMatch event that removes from the free matches collection that entry.

2.B - No record, Player A calls CreateChallengeRequest.

3.B - Player A calls UpdateMatch event, which create in the free matches collection an entry, with Player A user id and the challenge id.

4 - GetChallengeRequest is called to get more info.



_______________________

Otto Lopes - Programmer 

Play Race for your Life - https://www.facebook.com/raceforyourlife

png
Login to post a comment