Giter Club home page Giter Club logo

leaderboard-python's Introduction

leaderboard

Leaderboards backed by Redis in Python.

Builds off ideas proposed in http://www.agoragames.com/blog/2011/01/01/creating-high-score-tables-leaderboards-using-redis/.

Installation

pip install leaderboard

Make sure your redis server is running! Redis configuration is outside the scope of this README, but check out the Redis documentation.

Usage

Creating a leaderboard

Be sure to require the leaderboard library:

from leaderboard.leaderboard import Leaderboard

Create a new leaderboard or attach to an existing leaderboard named 'highscores':

highscore_lb = Leaderboard('highscores')

Defining leaderboard options

The default options are as follows:

    DEFAULT_PAGE_SIZE = 25
    DEFAULT_REDIS_HOST = 'localhost'
    DEFAULT_REDIS_PORT = 6379
    DEFAULT_REDIS_DB = 0
    DEFAULT_MEMBER_DATA_NAMESPACE = 'member_data'
    DEFAULT_GLOBAL_MEMBER_DATA = False
    ASC = 'asc'
    DESC = 'desc'
    MEMBER_KEY = 'member'
    MEMBER_DATA_KEY = 'member_data'
    SCORE_KEY = 'score'
    RANK_KEY = 'rank'

You would use the option, order=Leaderboard.ASC, if you wanted a leaderboard sorted from lowest-to-highest score. You may also set the order option on a leaderboard after you have created a new instance of a leaderboard. The various ..._KEY options above control what data is returned in the hash of leaderboard data from calls such as leaders or around_me. Finally, the global_member_data option allows you to control whether optional member data is per-leaderboard (False) or global (True).

Ranking members in the leaderboard

Add members to your leaderboard using rank_member:

for index in range(1, 11):
  highscore_lb.rank_member('member_%s' % index, index)

You can call rank_member with the same member and the leaderboard will be updated automatically.

Get some information about your leaderboard:

highscore_lb.total_members()
10

highscore_lb.total_pages()
1

Get some information about a specific member(s) in the leaderboard:

highscore_lb.score_for('member_4')
4.0

highscore_lb.rank_for('member_4')
7

highscore_lb.rank_for('member_10')
1

Retrieving members from the leaderboard

Get page 1 in the leaderboard:

highscore_lb.leaders(1)

[{'member': 'member_10', 'score': 10.0, 'rank': 1}, {'member': 'member_9', 'score': 9.0, 'rank': 2}, {'member': 'member_8', 'score': 8.0, 'rank': 3}, {'member': 'member_7', 'score': 7.0, 'rank': 4}, {'member': 'member_6', 'score': 6.0, 'rank': 5}, {'member': 'member_5', 'score': 5.0, 'rank': 6}, {'member': 'member_4', 'score': 4.0, 'rank': 7}, {'member': 'member_3', 'score': 3.0, 'rank': 8}, {'member': 'member_2', 'score': 2.0, 'rank': 9}, {'member': 'member_1', 'score': 1.0, 'rank': 10}]

Add more members to your leaderboard:

for index in range(50, 96):
  highscore_lb.rank_member('member_%s' % index, index)

highscore_lb.total_pages()
3

Get an "Around Me" leaderboard page for a given member, which pulls members above and below the given member:

highscore_lb.around_me('member_53')

[{'member': 'member_65', 'score': 65.0, 'rank': 31}, {'member': 'member_64', 'score': 64.0, 'rank': 32}, {'member': 'member_63', 'score': 63.0, 'rank': 33}, {'member': 'member_62', 'score': 62.0, 'rank': 34}, {'member': 'member_61', 'score': 61.0, 'rank': 35}, {'member': 'member_60', 'score': 60.0, 'rank': 36}, {'member': 'member_59', 'score': 59.0, 'rank': 37}, {'member': 'member_58', 'score': 58.0, 'rank': 38}, {'member': 'member_57', 'score': 57.0, 'rank': 39}, {'member': 'member_56', 'score': 56.0, 'rank': 40}, {'member': 'member_55', 'score': 55.0, 'rank': 41}, {'member': 'member_54', 'score': 54.0, 'rank': 42}, {'member': 'member_53', 'score': 53.0, 'rank': 43}, {'member': 'member_52', 'score': 52.0, 'rank': 44}, {'member': 'member_51', 'score': 51.0, 'rank': 45}, {'member': 'member_50', 'score': 50.0, 'rank': 46}, {'member': 'member_10', 'score': 10.0, 'rank': 47}, {'member': 'member_9', 'score': 9.0, 'rank': 48}, {'member': 'member_8', 'score': 8.0, 'rank': 49}, {'member': 'member_7', 'score': 7.0, 'rank': 50}, {'member': 'member_6', 'score': 6.0, 'rank': 51}, {'member': 'member_5', 'score': 5.0, 'rank': 52}, {'member': 'member_4', 'score': 4.0, 'rank': 53}, {'member': 'member_3', 'score': 3.0, 'rank': 54}, {'member': 'member_2', 'score': 2.0, 'rank': 55}]

Get rank and score for an arbitrary list of members (e.g. friends) from the leaderboard:

highscore_lb.ranked_in_list(['member_1', 'member_62', 'member_67'])

[{'member': 'member_1', 'score': 1.0, 'rank': 56}, {'member': 'member_62', 'score': 62.0, 'rank': 34}, {'member': 'member_67', 'score': 67.0, 'rank': 29}]

Retrieve members from the leaderboard in a given score range:

highscore_lb.members_from_score_range(4, 19)

[{'member': 'member_10', 'score': 10.0, 'rank': 47}, {'member': 'member_9', 'score': 9.0, 'rank': 48}, {'member': 'member_8', 'score': 8.0, 'rank': 49}, {'member': 'member_7', 'score': 7.0, 'rank': 50}, {'member': 'member_6', 'score': 6.0, 'rank': 51}, {'member': 'member_5', 'score': 5.0, 'rank': 52}, {'member': 'member_4', 'score': 4.0, 'rank': 53}]

Retrieve a single member from the leaderboard at a given position:

highscore_lb.member_at(4)

{'member': 'member_92', 'score': 92.0, 'rank': 4}

Retrieve a range of members from the leaderboard within a given rank range:

highscore_lb.members_from_rank_range(1, 5)

[{'member': 'member_95', 'score': 95.0, 'rank': 1}, {'member': 'member_94', 'score': 94.0, 'rank': 2}, {'member': 'member_93', 'score': 93.0, 'rank': 3}, {'member': 'member_92', 'score': 92.0, 'rank': 4}, {'member': 'member_91', 'score': 91.0, 'rank': 5}]

Optional member data notes

If you use optional member data, the use of the remove_members_in_score_range or remove_members_outside_rank methods will leave data around in the member data hash. This is because the internal Redis method, zremrangebyscore, only returns the number of items removed. It does not return the members that it removed.

Leaderboard request options

You can pass various options to the calls leaders, all_leaders, around_me, members_from_score_range, members_from_rank_range and ranked_in_list. Valid options are:

  • with_member_data - true or false to return the optional member data.
  • page_size - An integer value to change the page size for that call.
  • members_only - true or false to return only the members without their score and rank.
  • sort_by - Valid values for sort_by are score and rank.

Conditionally rank a member in the leaderboard

You can pass a function to the rank_member_if method to conditionally rank a member in the leaderboard. The function is passed the following 5 parameters:

  • member: Member name.
  • current_score: Current score for the member in the leaderboard. May be nil if the member is not currently ranked in the leaderboard.
  • score: Member score.
  • member_data: Optional member data.
  • leaderboard_options: Leaderboard options, e.g. 'reverse': Value of reverse option
def highscore_check(self, member, current_score, score, member_data, leaderboard_options):
  if (current_score is None):
    return True
  if (score > current_score):
    return True
  return False

highscore_lb.rank_member_if(highscore_check, 'david', 1337)
highscore_lb.score_for('david')

1337.0

highscore_lb.rank_member_if(highscore_check, 'david', 1336)
highscore_lb.score_for('david')

1337.0

highscore_lb.rank_member_if(highscore_check, 'david', 1338)
highscore_lb.score_for('david')

1338.0

Ranking a member across multiple leaderboards

highscore_lb.rank_member_across(['highscores', 'more_highscores'], 'david', 50000, { 'member_name': 'david' })

Alternate leaderboard types

The leaderboard library offers 3 styles of ranking. This is only an issue for members with the same score in a leaderboard.

Default: The Leaderboard class uses the default Redis sorted set ordering, whereby different members having the same score are ordered lexicographically. As per the Redis documentation on Redis sorted sets, "The lexicographic ordering used is binary, it compares strings as array of bytes."

Tie ranking: The TieRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score are given the same rank. For example, members in a leaderboard with the associated scores would have the ranks of:

| member     | score | rank |
-----------------------------
| member_1   | 50    | 1    |
| member_2   | 50    | 1    |
| member_3   | 30    | 2    |
| member_4   | 30    | 2    |
| member_5   | 10    | 3    |

The TieRankingLeaderboard accepts one additional option, ties_namespace (default: ties), when initializing a new instance of this class. Please note that in its current implementation, the TieRankingLeaderboard class uses an additional sorted set to rank the scores, so please keep this in mind when you are doing any capacity planning for Redis with respect to memory usage.

Competition ranking: The CompetitionRankingLeaderboard subclass of Leaderboard allows you to define a leaderboard where members with the same score will have the same rank, and then a gap is left in the ranking numbers. For example, members in a leaderboard with the associated scores would have the ranks of:

| member     | score | rank |
-----------------------------
| member_1   | 50    | 1    |
| member_2   | 50    | 1    |
| member_3   | 30    | 3    |
| member_4   | 30    | 3    |
| member_5   | 10    | 5    |

Performance Metrics

You can view performance metrics for the leaderboard library at the original Ruby library's page.

Ports

The following ports have been made of the leaderboard gem.

Officially supported:

Unofficially supported (they need some feature parity love):

Contributing to leaderboard

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the version or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011-2018 Ola Mork, David Czarnecki. See LICENSE.txt for further details.

leaderboard-python's People

Contributors

blafountain avatar czarneckid avatar graylinkim avatar hobthross avatar mikeszahaj avatar msaroufim avatar phrawger avatar vbabiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaderboard-python's Issues

Filtering Members on the basis of Member Data

I have many filters in my leaderboards i.e. around 15. I cannot make separate leaderboard against each filter combination. Is there any way I can filter members according to my choice? The only filter which I was able to find is members in a list. How can I do if I want to find if member is of specific type for example?

Custom Redis object/connection options

It is possible to customize the redis_connection, but what about the connection_pool?

I would like to use the decode_responses from ConnectionPool of redis-py option:

redis.from_url(redis_url, decode_responses=True)

Leaderboard('...', ?????)

Leaderboard('...', redis_connection=redis.from_url(redis_url, decode_responses=True))????
Leaderboard('...', connection=redis.from_url(redis_url, decode_responses=True))????

Should not be a good idea let the decode_responses=True as default in the leaderboard lib?

Thanks in advance.

How to rank based on criteria?

I'm using leaderboard-python for creating leaderboard.
Global rank is coming out fine but I wish to rank users based on their country.
Do I have to create different leaderboard for each country or is there any filtering mechanism for this?

P.S. I have very little idea of Redis, so explanation with sample code would be very much appreciated.

ranked_in_list returns incorrect ranks if some members dont exist

Use CompetitionLeaderboard and i have a set of friends - some of whom may or may not be part of the Leaderboard. When i call ranked_in_list with the complete list of my friends, it returns incorrect ranks for some of the friends who are part of the Leaderboard.

ResponseError: Command # 1 (ZADD unittesting_leaderboard (-: (DEFAULT: ), 32000) 1) of pipeline caused error: value is not a double

Hi, I'm seeing this error log when calling rank_member on an existing member.

I should mention this is when running unit tests on a dev machine.

The values instance.user.id and instance.score evaluate to 2 and 0 respectively (both int).

Traceback (most recent call last):
...
  File "<removed>", line 43, in update_leaderboard
    lb.rank_member(instance.user.id, instance.score)
  File "C:\<removed>\site-packages\leaderboard\__init__.py", line 92, in rank_member
    self.rank_member_in(self.leaderboard_name, member, score, member_data)
  File "C:\<removed>\site-packages\leaderboard\__init__.py", line 106, in rank_member_in
    pipeline.execute()
  File "C:\<removed>\site-packages\redis\client.py", line 2154, in execute
    return execute(conn, stack, raise_on_error)
  File "C:\<removed>\site-packages\redis\client.py", line 2069, in _execute_transaction
    self.raise_first_error(commands, response)
  File "C:\<removed>\site-packages\redis\client.py", line 2105, in raise_first_error
    raise r
ResponseError: Command # 1 (ZADD unittesting_leaderboard (-: (DEFAULT: ), 32000) 1) of pipeline caused error: value is not a double

I tried the same command from within the shell (on the same database), and no error occurs. What might be the cause?

Thanks in advance,

How to configure it for around 1,00,000 users

I have to show realtime leaderboard for 1,00,000 users , so How can I implement it in my Django webapp ,

If I am insert my data through Loop it will take too much time and load , so Can you please tell me what will the best approach for this
Here you can Check my code
Models.py

`class Score(models.Model):
name=models.CharField(max_length=211)
score=models.IntegerField()

def str(self):
return self.name
`

Views.py

def Index(request):
a=Score.objects.all()
highscore_lb=CompetitionRankingLeaderboard('sdfs')
for index in a:
highscore_lb.rank_member(index.name, index.score)
b=highscore_lb.all_leaders()
return render(request,'index.html',{'b':b})

how to configure Redis leaderborads with redis cluster endpoint

Hi,

we are using Redis with a single node, its working fine for us.

when we configured the same with cluster primary endpoint, xxxx.xxx.clustercfg.aps1.cache.amazonaws.com we are getting an error like

    628         if isinstance(response, ResponseError):
--> 629             raise response
    630         return response
    631 

ResponseError: MOVED 1896 10.0.0.27:6379

when I am using 10.0.0.27 everything working fine.

but the point here is to use the primary endpoint DNS value, irrespective of the ip, can you suggest us the correct way to use the cluster endpoint

StrictRedis not considered when passing connection argument to Leaderboard

If you pass Leaderboard(connection=redis.StrictRedis()) it will not consider it since you do a type introspection for only redis.Redis in the initialize function.

It took a while for me to notice that you were not re-using my connection so you might want to either raise an error, document the behavior or re-use the connection_pool only.

expire_leaderboard_at effects on global_member_data

Hi,
We have decided to use your awesome package to replace our postgres one. During the testing period, we have encountered an issue regarding time-based leaderboards. When I use expire_leaderboard_at on our leaderboard which uses global member data, it sets the same expiration date on the global member data too, which is not what we desire since other keys use this data too.
As I tracked the code, this method "_member_data_key" returns the member data key for "expire_leaderboard_at" which causes the global member data to be deleted.
Is there a workaround to prevent this behavior or is it in your plan to develop this feature?

Sorting Competition Leaderboard by rank ( for a given set of users )

For ranked_in_list API in CompetitionLeaderboard if any user id doesn't exist in a leaderboard and 'sort_by' = 'rank' is used, then the API throws an exception ( 'rank' key not found ). Whereas it works if the 'sort_by' = 'score' is used.

The bug is 'rank' is not initialised to None - unlike 'score' which by default is initialised to None.
This can be fixed with the following:

data[self.RANK_KEY] = None in line 100 or so as shown here:

        score = responses[index * 2 + 1]
        if score is not None:
            score = float(score)
        data[self.SCORE_KEY] = score

        data[self.RANK_KEY] = None # This is the additional line

        if self.order == self.ASC:
            data[self.RANK_KEY] = self.redis_connection.zcount(
                leaderboard_name, '-inf', "(%s" % str(float(data[self.SCORE_KEY])))
        else:
            #print "SCORE_KEY - " + str(data[self.SCORE_KEY])
            if data[self.SCORE_KEY] != None:
                    data[self.RANK_KEY] = self.redis_connection.zcount(
                        leaderboard_name, "(%s" % str(float(data[self.SCORE_KEY])), '+inf')

around_me_in does't work right on python 3.4

When using python 3.4 and requesting a leaderboard with a page_size=1 it returns the incorrect member due to the way python 3 handles division on line 1007 in leaderboard/leaderboard.py. The work around was to cast to int, so that fixed it for any page size.

starting_offset = reverse_rank_for_member - (page_size / 2)

should be

starting_offset = reverse_rank_for_member - int(page_size / 2)

Breaking changes in Redis-Py 3+

If you install the latest version of Redis Python Client (3.1.0 at this time) you will receive this error when trying to run the sample code and use the rank_member function for the first time:
AttributeError: 'str' object has no attribute 'items'

This is because of breaking changes that occurred between Redis Client v2.x to v3.x
https://github.com/andymccurdy/redis-py#upgrading-from-redis-py-2x-to-30

This specific issue can be solved as follows, but I'm not sure if other parts may have broken:
https://stackoverflow.com/questions/53553009/not-able-to-insert-data-using-zaddsorted-set-in-redis-using-python

As a super quick fix, you can use redis python client v2.10.6

To install that version of redis run the following:
pip install redis==2.10.6

member_at returns a member for ranks below 1

I have a leaderboard with 2994 members in it.
lb.member_at(0) should return None, I believe, but it returns {'member': '7694', 'score': 2352.43, 'rank': 25}
Accordingly, lb.member_at(-1) returns {'member': '15488', 'score': 2392.103, 'rank': 24}

Is this behaviour normal? Should I control this on my side?

Incorrect behavior of members_from_rank_range for CompetitionRankingLeaderboard

If a leaderboard has multiple rows for the same rank, then the members_from_rank_range does not return correct value. e.g.

'Person 1' with 120 points is ranked 1
'Person 2' with 120 points is ranked 1
'Person 3' with 112 points is ranked 3

Then
a) invoking members_from_rank_range(1, 1) - returns only 1 item ( expecting 2 )
b) invoking members_from_rank_range(2, 2) - returns 1 item ( expecting none )

Ranking members with the same score

Unless I missed it in the docs, is there a way to force ranks to be the same when members have the same score?

>>> for index in range(1, 11):
...    highscore_lb.rank_member('member_%s' % index, index)

 >>> highscore_lb.rank_member('member_%s' % 100, 10.0)
 >>> highscore_lb.leaders(1)
 [{'member': 'member_100', 'score': 10.0, 'rank': 1}, {'member': 'member_10', 'score': 10.0, 'rank': 2}, ...]

As you can see, the first two members have an equal score, but different ranks. Is there a way to have them assigned the same rank?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.