Pergunta

I'm using Google App Engine, so I'm using a Non relational database (NoSQL). My question is:

Which is the best option to modeling a rank (ranking of players) using their scores?

For example, my players are:

Player { String name, int score}

I want to know the rank (position) from a player and also get the top 10 players, but I doubt which is the best way.

Thanks.

Foi útil?

Solução

If your scores are indexed, it's easy to do a datastore query and get players in sorted order. So if you want the top 10 players, that's pretty trivial.

Getting the ranking for an arbitrary player is really hard. Hard enough that I'd say, avoid it if you can, and if you can't, find a hack way around it.

For example, if you have 50,000 players, and PlayerX is ranked 12,345, the only way to know that is query all the players, and check through each of them, keeping count, until you find PlayerX.

One hack might be to store the player ranking in the player entity, and update it with a cron job that runs once every few hours.

Outras dicas

There is a built-in solution in Redis:

First add a few members with a score:

redis>  ZADD myzset 1 "one"
(integer) 1
redis>  ZADD myzset 2 "two"
(integer) 1
redis>  ZADD myzset 3 "three"
(integer) 1

Get the rank of "two":

redis>  ZREVRANK myzset "one"
(integer) 2

(Index starts at 0)

And if you want the current order:

redis>  ZREVRANGE myzset 0 -1
1) "three"
2) "two"
3) "one"

See ZREVRANGE and ZREVRANK in redis documentation.

A suitable representation of this in JSON would be:

"players" : [
    {
        "name" : "John",
        "score" : 15
    },
    {
        "name" : "Swadq",
        "score" : 7
    },
    {
        "name" : "Jane",
        "score" : 22
    }
]

For examples of how to sort this:

You could set up your index.yaml like so:

- kind: Player
  properties:
  -  name: score
     direction: ascending

To get a player's score you just need to make a pass over the players (while keeping a count) and cache the result to speed further searches for that player.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top