How can I get a random user's id

Hi Guys, I am developing social with this wonderful server engine.

This engine is so cool I can’t help but be satisfied.

However, one of the functions is blocked, is there any function that can randomly call users from the engine? If you get a certain score in our game, you can go to attack a random user’s base. Randomly among users similar to your level.

I’m not sure how to implement this. Can anyone tell me if this is a possible implementation?

Thanks
David

Hi @Gandangf,

I’m really pleased to hear you’re enjoying working with Nakama. Hopefully I can help you with this.

Our server runtime framework does indeed provide a way to get a random selection of users.

Depending on your specific requirements you could either use this as-is on the server side (e.g. in a match handler or leaderboard reset hook), or you could setup an RPC to return it which a client could call.

An example of how you could use it in an RPC is given below:

const getRandomUserRpc : nkruntime.RpcFunction = (ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, payload: string) => {
  let randomUsers = nk.usersGetRandom(1);
  if (randomUsers.length > 0) {
    let selectedRandomUser = randomUsers[0];
    return JSON.stringify({
      user: {
        id: selectedRandomUser.userId,
        username: selectedRandomUser.username
      }
    });
  }

  logger.debug("no users found");

  return JSON.stringify({
    user: null
  });
};

initializer.registerRpc("get_random_user", getRandomUserRpc)

I hope this helps you solve your issue. If you have any further questions please let me know.

Regards,
Tom

oh! Okay. You can do it this way. Thanks for letting me know. But now, I need to select a target with a level similar to the level of the inquiring user, which is the necessary logic. Is it possible to call a random user that meets the conditions?

There is no way to query for users that meet a specific condition using the usersGetRandom function unfortunately. This function is designed to return a set of completely random users from the database.

Can I ask how you are storing player progression, and more specifically levels? Are you using user metadata or the storage engine for example?

As an example, if you’re using user metadata to store the level, you could oversample using the usersGetRandom function (let’s say by asking for 1000 random users) and then filter the list of users in-memory based on checking their metadata for an appropriate level value.

Of course, with this approach you run the risk of not finding a user with an appropriate level in the initial random sample set. So it would be up to you to decide how best to handle that (e.g. re-sample until you find an appropriate user for the player to battle, return a user with a lower level or tell the user no appropriate player was found etc).

Hmm… that’s right. Although we are storing level information in the meta data. Then, as you said, it would be better to take enough user and filter out the best one among them.

Oh, in conclusion, we can’t bring a list of saved meta data, right? For example, can’t we get the full list of user with username “Gandangf”, or user with level 10 stored in metadata?

You can retrieve users by user id or username directly using the client.GetUsersAsync function. But there is no way to query users on their public metadata via the API. You would need to grab the sample set of random users and query their meta data individually.

Okay Thank you very much for your advice! :slight_smile: