Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom data keys #11

Merged
merged 2 commits into from
Jan 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions lib/leaderboard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 30 additions & 7 deletions spec/leaderboard_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ describe 'Leaderboard', ->
@redisConnection = redis.createClient(6379, 'localhost')

beforeEach ->
@leaderboard = new Leaderboard('highscores')
@leaderboard = new Leaderboard('highscores', Leaderboard.DEFAULT_OPTIONS)

afterEach ->
@redisConnection.flushdb()
Expand All @@ -27,6 +27,29 @@ describe 'Leaderboard', ->

done()

it 'should allow you to set custom keys for member, score, rank and member_data', (done) ->
options =
'pageSize': Leaderboard.DEFAULT_PAGE_SIZE
'reverse': false
'memberKey': 'member_custom'
'rankKey': 'rank_custom'
'scoreKey': 'score_custom'
'memberDataKey': 'member_data_custom'


@leaderboard = new Leaderboard('highscores', options)

for index in [0...Leaderboard.DEFAULT_PAGE_SIZE + 1]
@leaderboard.rankMember("member_#{index}", index, "member_data_#{index}", (reply) -> )

@leaderboard.leaders(1, {'with_member_data': true}, (reply) ->
reply.length.should.equal(25)
reply[0]['member_custom'].should.equal('member_25')
reply[0]['score_custom'].should.equal(25)
reply[0]['rank_custom'].should.equal(1)
reply[0]['member_data_custom'].should.equal('member_data_25')
done())

it 'should allow you to disconnect the Redis connection', (done) ->
@leaderboard.disconnect()
done()
Expand Down Expand Up @@ -540,9 +563,9 @@ describe 'Leaderboard', ->
done()

it 'should allow you to merge leaderboards', (done) ->
foo = new Leaderboard('foo')
bar = new Leaderboard('bar')
foobar = new Leaderboard('foobar')
foo = new Leaderboard('foo', Leaderboard.DEFAULT_OPTIONS)
bar = new Leaderboard('bar', Leaderboard.DEFAULT_OPTIONS)
foobar = new Leaderboard('foobar', Leaderboard.DEFAULT_OPTIONS)

foo.rankMember('foo_1', 1, null, (reply) ->
foo.rankMember('foo_2', 2, null, (reply) ->
Expand All @@ -556,9 +579,9 @@ describe 'Leaderboard', ->
done())))))))

it 'should allow you to intersect leaderboards', (done) ->
foo = new Leaderboard('foo')
bar = new Leaderboard('bar')
foobar = new Leaderboard('foobar')
foo = new Leaderboard('foo', Leaderboard.DEFAULT_OPTIONS)
bar = new Leaderboard('bar', Leaderboard.DEFAULT_OPTIONS)
foobar = new Leaderboard('foobar', Leaderboard.DEFAULT_OPTIONS)

foo.rankMember('foo_1', 1, null, (reply) ->
foo.rankMember('foo_2', 2, null, (reply) ->
Expand Down
33 changes: 21 additions & 12 deletions src/leaderboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Leaderboard
DEFAULT_OPTIONS =
'pageSize': @DEFAULT_PAGE_SIZE
'reverse': false
'memberKey': 'member'
'rankKey': 'rank'
'scoreKey': 'score'
'memberDataKey': 'member_data'

###
# Default Redis host: localhost
Expand Down Expand Up @@ -45,6 +49,10 @@ class Leaderboard
@pageSize = options['pageSize']
if @pageSize == null || @pageSize < 1
@pageSize = Leaderboard.DEFAULT_PAGE_SIZE
@memberKeyOption = options['memberKey'] || 'member'
@rankKeyOption = options['rankKey'] || 'rank'
@scoreKeyOption = options['scoreKey'] || 'score'
@memberDataKeyOption = options['memberDataKey'] || 'member_data'

@redisConnection = redisOptions['redis_connection']

Expand Down Expand Up @@ -487,18 +495,19 @@ class Leaderboard
transaction.zrank(leaderboardName, member)
else
transaction.zrevrank(leaderboardName, member)
transaction.exec((err, replies) ->

transaction.exec((err, replies) =>
if replies
scoreAndRankData = {}
if replies[0]?
scoreAndRankData['score'] = parseFloat(replies[0])
scoreAndRankData[@scoreKeyOption] = parseFloat(replies[0])
else
scoreAndRankData['score'] = null
scoreAndRankData[@scoreKeyOption] = null
if replies[1]?
scoreAndRankData['rank'] = replies[1] + 1
scoreAndRankData[@rankKeyOption] = replies[1] + 1
else
scoreAndRankData['rank'] = null
scoreAndRankData['member'] = member
scoreAndRankData[@rankKeyOption] = null
scoreAndRankData[@memberKeyOption] = member
callback(scoreAndRankData))

###
Expand Down Expand Up @@ -764,19 +773,19 @@ class Leaderboard
for member, index in members
do (member) =>
data = {}
data['member'] = member
data[@memberKeyOption] = member
unless options['members_only']
data['rank'] = replies[index * 2] + 1
data[@rankKeyOption] = replies[index * 2] + 1
if replies[index * 2 + 1]
data['score'] = parseFloat(replies[index * 2 + 1])
data[@scoreKeyOption] = parseFloat(replies[index * 2 + 1])
else
data['score'] = null
data['rank'] = null
data[@scoreKeyOption] = null
data[@rankKeyOption] = null

# Retrieve optional member data based on options['with_member_data']
if options['with_member_data']
this.memberDataForIn leaderboardName, member, (memberdata) =>
data['member_data'] = memberdata
data[@memberDataKeyOption] = memberdata
ranksForMembers.push(data)
# Sort if options['sort_by']
if ranksForMembers.length == members.length
Expand Down