Sign In Register

How can we help you today?

Start a new topic

MatchData

I've found very little about the functionality of matchData. What I'm trying to do is store a couple small pieces of data in matchData. From reading this Q&A post I've figured out much of what I need, but I'm stumped on this:


Here's my desired data:

"matchData": { "seats": [ { "player_id": "58a08a760a45b30567f848ab", "seat": 5 }, {"player_id": "blah", "seat": 3 ] , "dealer": "58a08a760a45b30567f848ab"}

 So two keys at the root level ("seats", which itself can contain many nodes within it, and "dealer", which is a single).

Through cloudcode I'm using the MatchFoundMessage to check the matchdata for "dealer", and if it doesn't exist, it adds it with a random participant of the match. That much works.


I have an Event-bound script that adds "seats" nodes to the MatchData. The first time it's called, it replaces everything in MatchData with the new seat entry. Subsequent seat entries get added to the "Seats" collection just fine. Borrowing heavily from the previous link, my Event script looks like this:


 

var seatNumberA = Spark.getData().seatNumber;
var matchIDA = Spark.getData().matchID;
var theMatch = Spark.getMultiplayer().loadMatch(matchIDA);
var seatNode = theMatch.getMatchData().seats;

var playerSending = Spark.getPlayer().getPlayerId();

if(seatNode === undefined)
{
    theMatch.setMatchData({"seats":[{"player_id": playerSending, "seat": seatNumberA}]});
}
else
{
    if(!contains(seatNode, playerSending))
    {
        seatNode.push({"player_id":playerSending, "seat":seatNumberA})
        theMatch.setMatchData({
            "seats" : seatNode
        })
    }
}

var playersToSendTo = Spark.getMultiplayer().loadMatch(matchIDA).getParticipants();

for (var i = 0; i<playersToSendTo.length;i++)
{
    Spark.message("playerSeat").setPlayerIds(playersToSendTo[i].getPlayer().getPlayerId()).setMessageData({"playerID":playerSending, "playerSeat":seatNumberA}).send();
};

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i].player_id === obj) {
            return true;
        }
    }
    return false;
}

 

I can't figure out how to NOT overwrite the already-existing "Dealer" node in MatchData when adding the first "Seat" node. I tried doing

 

var baseNode = theMatch.getMatchData();

 and pushing "seats" to baseNode, then setting MatchData, but I got an error stating that push isn't a function for baseNode.


1 Comment

Hi Jeff,


Apologies for the delayed response. What'll you need to do here is get the matchdata object with getMatchData(). Then make all of the required changes and set the object back with setMatchData(yourObject). If you need more control you could create a collection and use the matchId as an id for the document and store all of your match information there. You could then query and update it using mongo.


Regards,

Liam

Login to post a comment