Sign In Register

How can we help you today?

Start a new topic
Answered

Creating my own Async pvp system

Hello, I am trying to achieve a system to match player working like this:


when the player register i add a field in a collection:

 

//[PVP]
     var pvp_opponentsCollection = Spark.runtimeCollection("pvp_opponents");
     var playerPvpEntry = pvp_opponentsCollection.findOne({"playerId" : Spark.getPlayer().getPlayerId()});
     if(playerPvpEntry === null)
     {
        playerProgressionEntry = {};
        playerProgressionEntry.playerId = Spark.getPlayer().getPlayerId();
        playerProgressionEntry.attacks_availlable = 3;
        Spark.setScriptData("pvp",  playerProgressionEntry.attacks_availlable);
    }

 so each player will have 3 attacks availlable (mean they can be attacked 3 time)


then i create a event "find_opponent", this event is my simplified matchmaking. for now what i want to do is sort it by attack_availlable and take the first one save its id and decrease its attack_availlable so im going to always take the player which have the most attack_availlable (most active)


problem is i do not know how to sort and take the first one from the DB it it exist?

 

 var pvp_opponentsCollection = Spark.runtimeCollection("pvp_opponents");
 //pvp_opponentsCollection.sort by the field "pvp_opponents"
 //take the first one if its us take second one
 //save the id
 var opponentID = 123;
 var opponentPlayer = Spark.loadPlayer(opponentID);
 Spark.setScriptData("opponent_name", opponentPlayer.getDisplayName());
 Spark.setScriptData("opponent_base", opponentPlayer.getScriptData("base_data"));

 

==== thats for the first part ===


then after that when i have my player id i want to find him and get his script data where i saved his base. is it possible to get the script data of an other player?




Best Answer

Okay, so i left brackets out for the mongoDB in cloud code.
It should run now.


 var opponentID = pvp_opponentsCollection.findOne().sort({"attacks_availlable" : 1}).playerId;


i just realized i didnt save my collection, it seem to be something like these but not sure which one i should use:


        pvp_opponentsCollection.insert(playerPvpEntry);

        pvp_opponentsCollection.save(playerPvpEntry);

Hey Damien,

If you can get the ID of the opponent, you can use Spark.LoadPlayer(); to load the opponent and the use player.getScriptData.

Does this help?
Sean

 

yes sean i already did this,  in the second part of the cloud code im stuck on the two commented line i dont know how to sort the collection by order ascending for the field attacks_availlable then how to take the first one ? cannot find anything in the docs about these if its possible ?

 You can sort your query using...


collection.find({ ..query... }).sort( "field" : 1)


where 1 is ascending order and -1 is descending order.


You can get the first element from this using the 'next()' method on the cursor...


var doc = cursor.next();

im a bit confuse i tried this but it tell me an error when i try to save:


// ====================================================================================================

 var pvp_opponentsCollection = Spark.runtimeCollection("pvp_opponents");

 //pvp_opponentsCollection.sort by the field "attacks_availlable"

 var opponentID = pvp_opponentsCollection.find().sort( "attacks_availlable" : 1).next().playerId;

okay, try collection.findOne().sort("attacks_availlable" : 1).playerId;

 

when i try to save line 10 give me this error: sun.org.mozilla.javascript.internal.EvaluatorException: missing ) after argument list (#10)



line 10 is  var opponentID = pvp_opponentsCollection.findOne().sort("attacks_availlable" : 1).playerId;

Hey Damien,

Ive gone through your collection in the games in your portal and i cannot find any collection called 'pvp_opponents' so this wont return anything.
can you let me know where is code is so i can check it for myself?

Thanks,
Sean

 

its in authentication response :


 //[PVP]

     var pvp_opponentsCollection = Spark.runtimeCollection("pvp_opponents");

     var playerPvpEntry = pvp_opponentsCollection.findOne({"playerId" : Spark.getPlayer().getPlayerId()});

     if(playerPvpEntry === null)

     {

        //JSON documents can be parsed into Mongo documents directly

        playerProgressionEntry = {};

        //add the player's id to the document so we can find it in the future

        playerProgressionEntry.playerId = Spark.getPlayer().getPlayerId()

        //create a new empty object to store each levels progression

        playerProgressionEntry.attacks_availlable = 3;

        pvp_opponentsCollection.insert(playerPvpEntry);

       // pvp_opponentsCollection.save(playerPvpEntry);

        Spark.setScriptData("pvp", playerProgressionEntry.attacks_availlable);

    }

then in find_opponent event


the game is called WolfQuest

Answer

Okay, so i left brackets out for the mongoDB in cloud code.
It should run now.


 var opponentID = pvp_opponentsCollection.findOne().sort({"attacks_availlable" : 1}).playerId;

Has that answered your question Damien? 

Regards, Patrick. 

Login to post a comment