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
C
Customer Support
said
almost 5 years ago
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);
Customer Support
said
about 5 years ago
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
d
damien delmarle
said
about 5 years ago
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 ?
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;
Customer Support
said
almost 5 years ago
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
d
damien delmarle
said
almost 5 years ago
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
damien delmarle
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:
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?
==== 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?
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;
- Oldest First
- Popular
- Newest First
Sorted by Oldest Firstdamien delmarle
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);
Customer Support
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
damien delmarle
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 ?
Customer Support
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();
damien delmarle
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;
Customer Support
damien delmarle
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;
Customer Support
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
damien delmarle
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);
}
damien delmarle
then in find_opponent event
the game is called WolfQuest
Customer Support
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;
Customer Support
Has that answered your question Damien?
Regards, Patrick.
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2485 topics