question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How to make inbox?

Hello everyone,

We want to make an inbox in the game. The player will receive some messages, kind of mission in the game, and they will accept or decline the missions. The messages will be shown respectively on a virtual computer in the game and when they decline a mission, the related message will be deleted. There are 2 approaches we are thinking of, but maybe there is a more optimized way to do this. So, which one would be the best in terms of using the resources?

First of all, every player can receive up to 5 messages.

First approach: Making only 1 read only data and save all the messages in the field as a JSON file. For example;

{
   Message1:{
      Sender: "Mark",
Mission: 10,
IsAccepted: 0 } Message2:{ Sender: "Paul", Mission: 5, IsAccepted: 0 }
Message3:{ Sender: "Tom", Mission: 2, IsAccepted: 1 } }

When the player decline, let's say, the second message, we need to shift all the message, which is a bit problem for us. We don't know how to do that in Cloudscript. A code example would be nice for this approach. The last result should be like this:

{
   Message1:{
      Sender: "Mark",
      Mission: 10,
      IsAccepted: 0
   }
   Message2:{
      Sender: "Tom",
      Mission: 2,
      IsAccepted: 1
   }
}

Second approach: Making 5 different read only data and save all the messages separately as a JSON file. For example;

This approach makes everything more clear, however, when the player declines a mission, we need to shift all the data, which is a bit complex.

Which approach would be the best or is there any other options to implement this feature?

000.jpg (69.6 KiB)
10 |1200

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

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

I would recommend the first approach because it only takes two API calls during the whole process – the first one retrieving the data, the second writing back the data. And, to solve the message shifting problem, I suggest that you store all the messages as a Json Array with a size of 5 to better tackle this:

[   
    {
        Sender: "Mark",
        Mission: 10,
        IsAccepted: 0
     },
     {
        Sender: "Paul",
        Mission: 5,
        IsAccepted: 0
     },
     {
        Sender: "Tom",
        Mission: 2,
        IsAccepted: 1
     },
     null,
     null
  ]

Instead of explicitly specifying Message1, Message2, …, use index to denote it.

Below is a sample script that accesses and removes a certain message:

// a json array with a size of 5
var obj = [   
    {
        Sender: "Mark",
        Mission: 10,
        IsAccepted: 0
     },
     {
        Sender: "Paul",
        Mission: 5,
        IsAccepted: 0
     },
     {
        Sender: "Tom",
        Mission: 2,
        IsAccepted: 1
     },
     null,
     null
  ];
//use index to access a certain message
  function accessEntry(index){
      console.log(obj[index]);
  }
//remove a certain message and shift all the message
  function removeEntry(index){
      obj[index] = null; // set the message to null
      //shift all the messages 
      for (i = index; i < 5; i++){
          if (obj[index+1] == null)
                break;
          else 
          {
            obj[index] = obj[index+1];
            obj[index+1] = null;
          }
         }
      }
  removeEntry(2);
  console.log(obj);

Please let me know if you have any questions regarding this.

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.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

You explained everything clearly. Thank you

0 Likes 0 ·

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.