How To Post A List Of Data Into Gamespark Runtime

2019-09-13 01:08发布

I got a problem how to implement gamesparks cloud code. I want to make a add posting button, the data will save to gamesparks runtime collection. So far what i have done just make a save button one data for only one for each player. But i need to make a list of player data, so one player can save multiplayer data to runtime collection. Below is my cloud code gamesparks :

    Spark.setScriptData("player_Data", currentPlayer); // return the player via script-data
var playerDataList = Spark.runtimeCollection("playerNewspaper"); // this will get the collection of player data
  var playerID = Spark.getPlayer().getPlayerId(); // first we get the id of the current player
  var playerUsername =Spark.getPlayer().getUserName();
  var newspaper = Spark.getData().newspaper;

  var currentPlayer = {
  "playerID": playerID,
  "playerUsername": playerUsername,
  "player_newspaper": newspaper

  }; // we construct a new player from the data we are about to input into the player data

  playerDataList.insert({
  "playerID": playerID
  }, //Looks for a doc with the id of the current player
  {
  "$set": currentPlayer
  }, // Uses the $set mongo modifier to set old player data to the current player data
  true, // Create the document if it does not exist (upsert)
  false // This query will only affect a single object (multi)
  );

This cloud code only save one data for one player. When i save the data with same ID player it is not worked.

Below is noSql data preview : and below is my noSql preview data :

{
 "_id": {
  "$oid": "5850dfdb135f38fb1edbf28c"
 },
 "playerID": "57bffe76b6a70d6e2a3855b7",
 "playerUsername": "dennis",
 "player_newspaper": "{\"ID\":\"57bffg77b6a70d6e2a3855b7\",\"Username\":\"Dennis\",\"itemName\":\"Egg\",\"Comment\":\"Promo Telur 1 Butir cuman murah\",\"Date\":\"12/14/2016\"}"
}

I want to make a list of data posting by player. Although the posting is a same player of not.

This list posting data then i will use to query random 5 data and show it to unity i call it newspaper.

How to do it ?

Thanks Dennis

1条回答
时光不老,我们不散
2楼-- · 2019-09-13 01:53

Finally i got the solution myself.

I just realize this is a mongodb dan javascript.

So in mongodb if you want to insert a data like mysql here to do :

For Add Data :

var playerDataList = Spark.runtimeCollection("playerNewspaper"); // this will get the collection of player data
    var playerID = Spark.getPlayer().getPlayerId(); // first we get the id of the current player
    var playerUsername =Spark.getPlayer().getUserName();
    var newspaper = Spark.getData().newspaper;

    var currentPlayer = {
        "playerID": playerID,
        "playerUsername": playerUsername,
        "player_newspaper": newspaper

    }; // we construct a new player from the data we are about to input into the player data

    playerDataList.insert(
    {
      "newspaper" : currentPlayer
    } // Uses the $set mongo modifier to set old player data to the current player data
    );

For Load Data With random 5 Item :

var playerData = Spark.runtimeCollection("playerNewspaper"); // get the collection data
var currentPlayer = playerData.find().limit(5).skip(Math.random() * playerData.count());

Spark.setScriptData("player_Newspaper", currentPlayer); // return the player via script-data

That's All

查看更多
登录 后发表回答