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

Memberdata allowing javascript objects #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 36 additions & 8 deletions lib/leaderboard.js

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

11 changes: 11 additions & 0 deletions spec/leaderboard_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ describe 'Leaderboard', ->
reply.should.equal('Optional member data')
done())

it 'should allow you to store and retrieve optional member data as object', (done) ->
data = {
index: 3,
data: 'Optional member data'
}
@leaderboard.rankMember('member', 1, data, (reply) -> )

@leaderboard.memberDataFor('member', (reply) ->
reply.should.deepEqual(data)
done())

it 'should allow you to update optional member data', (done) ->
@leaderboard.rankMember('member', 1, 'Optional member data', (reply) -> )

Expand Down
30 changes: 25 additions & 5 deletions src/leaderboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ class Leaderboard
###
rankMemberIn: (leaderboardName, member, score, memberData = null, callback) ->
transaction = @redisConnection.multi()
data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData
transaction.zadd(leaderboardName, score, member)
transaction.hset(this.memberDataKey(leaderboardName), member, memberData) if memberData?
transaction.hset(this.memberDataKey(leaderboardName), member, data) if memberData?
transaction.exec((err, reply) ->
callback(reply) if callback)

Expand All @@ -126,10 +127,11 @@ class Leaderboard
# @param member_data [String] Optional member data.
###
rankMemberAcross: (leaderboardNames, member, score, memberData = null, callback) ->
data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData
transaction = @redisConnection.multi()
for leaderboardName in leaderboardNames
transaction.zadd(leaderboardName, score, member)
transaction.hset(this.memberDataKey(leaderboardName), member, memberData) if memberData?
transaction.hset(this.memberDataKey(leaderboardName), member, data) if memberData?
transaction.exec((err, reply) ->
callback(reply) if callback)

Expand Down Expand Up @@ -222,8 +224,9 @@ class Leaderboard
# @return String of optional member data.
###
memberDataForIn: (leaderboardName, member, callback) ->
@redisConnection.hget(this.memberDataKey(leaderboardName), member, (err, reply) ->
callback(reply))
@redisConnection.hget(this.memberDataKey(leaderboardName), member, (err, reply) =>
result = if this.isJsonData(reply) then JSON.parse(reply) else reply
callback(result))

###
# Update the optional member data for a given member in the leaderboard.
Expand All @@ -244,7 +247,8 @@ class Leaderboard
# @param callback Optional callback for result of call.
###
updateMemberDataFor: (leaderboardName, member, memberData, callback) ->
@redisConnection.hset(this.memberDataKey(leaderboardName), member, memberData, (err, reply) ->
data = if typeof memberData is 'object' then JSON.stringify(memberData) else memberData
@redisConnection.hset(this.memberDataKey(leaderboardName), member, data, (err, reply) ->
callback(reply) if callback)

###
Expand Down Expand Up @@ -1078,4 +1082,20 @@ class Leaderboard
memberDataKey: (leaderboardName) ->
"#{leaderboardName}:#{@memberDataNamespace}"

###
# Checks a string if encoded json
#
# @param value [String] String to be checked.
#
# @return boolean of string is json
###
isJsonData: (value) ->
try
JSON.parse(value);
catch e
return false
return true



module.exports = Leaderboard