question

ryandj avatar image
ryandj asked

Help speeding up Cloud script so is under limit

I'm making a draw type game where people can enter the draw multiple times.

im storing who has entered the draw in the leader board and the stat is equal to how many times they have entered.

then to select the winner I'm getting stats for 1 entries 2 entries to 5 using segments

then depending on how many draw entries they have add there playfab id to an array so a random number generator can choose the winner.

this is currently a very heavy function and take 7.5second to run with a couple playing in the leaderboard when the game goes live can have running this up to 10,000 people in which I know would take fair to long. is there another way I may be Able to do this.

handlers.SelectDraw = function () {
  // Add ball 1 to arrray
  var Ball1 = server.GetPlayersInSegment({
      SegmentId: "CF97420512AAE133",
    //  MaxBatchSize: 10000
    });
  var numberOfPlayer1 = Ball1["ProfilesInSegment"];
  Ball1 = Ball1["PlayerProfiles"];


  // Add ball 2 to arrray
  var Ball2 = server.GetPlayersInSegment({
     SegmentId: "38C895EDDA30DB84",
  //   MaxBatchSize: 10000
   });
   var numberOfPlayer2 = Ball2["ProfilesInSegment"];
   Ball2 = Ball2["PlayerProfiles"];


   // Add ball 3 to arrray
   var Ball3 = server.GetPlayersInSegment({
      SegmentId: "CFA21AA983CDB04F",
  //    MaxBatchSize: 10000
    });
    var numberOfPlayer3 = Ball3["ProfilesInSegment"];
    Ball3 = Ball3["PlayerProfiles"];


    // Add ball 4 to arrray
    var Ball4 = server.GetPlayersInSegment({
       SegmentId: "8CE6BCE86F9FE2A",
    //   MaxBatchSize: 10000
     });
     var numberOfPlayer4 = Ball4["ProfilesInSegment"];
     Ball4 = Ball4["PlayerProfiles"];




     // Add ball 5 to arrray
     var Ball5 = server.GetPlayersInSegment({
        SegmentId: "A7A41C04E501348C",
    //    MaxBatchSize: 10000
      });
      var numberOfPlayer5 = Ball5["ProfilesInSegment"];
      Ball5 = Ball5["PlayerProfiles"];
      numberOfPlayer = (numberOfPlayer1 + numberOfPlayer2 + numberOfPlayer3 + numberOfPlayer4 + numberOfPlayer5);
      var EnteredPlayers = [numberOfPlayer];
        for (var i = 0; i < numberOfPlayer; i++)
  {
    if(i < numberOfPlayer1){
      var PlayerId = Ball1[i];
      PlayerId = PlayerId["PlayerId"];
      EnteredPlayers[i] = PlayerId.toString();
    }
    if(i < numberOfPlayer2){
      var PlayerId = Ball2[i];
      PlayerId = PlayerId["PlayerId"];
      EnteredPlayers[i+1] = PlayerId.toString();
      EnteredPlayers[i+2] = PlayerId.toString();
    }
    if(i < numberOfPlayer3){
      var PlayerId = Ball3[i];
      PlayerId = PlayerId["PlayerId"];
      EnteredPlayers[i+3] = PlayerId.toString();
      EnteredPlayers[i+4] = PlayerId.toString();
      EnteredPlayers[i+5] = PlayerId.toString();
    }
    if(i < numberOfPlayer4){
      var PlayerId = Ball4[i];
      PlayerId = PlayerId["PlayerId"];
      EnteredPlayers[i+6] = PlayerId.toString();
      EnteredPlayers[i+7] = PlayerId.toString();
      EnteredPlayers[i+8] = PlayerId.toString();
      EnteredPlayers[i+9] = PlayerId.toString();
    }
    if(i < numberOfPlayer5){
      var PlayerId = Ball5[i];
      PlayerId = PlayerId["PlayerId"];
    EnteredPlayers[i+10] = PlayerId.toString();
    EnteredPlayers[i+11] = PlayerId.toString();
    EnteredPlayers[i+12] = PlayerId.toString();
    EnteredPlayers[i+13] = PlayerId.toString();
    EnteredPlayers[i+14] = PlayerId.toString();
    }
  }


        var winner =  EnteredPlayers[Math.floor(Math.random() * (EnteredPlayers.length) + 0)];


        var playerDrawResult = server.SetTitleData({
          "Key": "Current Draw",
          "Value": winner
        });
}

Leaderboards and Statisticsscheduled tasks
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ryandj avatar image
ryandj answered

Sorted it realized that call a segment of peoples stats is a heavy thing to call and take a long time so instead I used segment all players and get stats for all players in that got all the code down from 7.8 seconds down to 0.3

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·

You can also implement a tool for your scenario on an external secure server to handle these heavy tasks.

0 Likes 0 ·
ryandj avatar image
ryandj answered

After some testing I have found that running 5 get players in segments takes 6 second is there anyway to speed this up

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.