Skip to content

Instantly share code, notes, and snippets.

@holderbaum
Created January 21, 2014 17:32
Show Gist options
  • Save holderbaum/8544380 to your computer and use it in GitHub Desktop.
Save holderbaum/8544380 to your computer and use it in GitHub Desktop.
Competiton ranking for redis
ocal offset = ARGV[1]
local limit = ARGV[2]
local users = redis.call('zrevrange', KEYS[1], offset, offset + limit, 'WITHSCORES')
local result = {}
for i=1,#users,2 do
local user = users[i]
local score = users[i+1]
local min_score = score + 1
local comp_rank = redis.call('zcount', KEYS[1] , min_score, '+inf') + 1
local rank = redis.call('zrevrank', KEYS[1] , user) + 1
-- tmp logging
redis.log(redis.LOG_WARNING, user .. ':' .. score .. ' ' .. rank .. ' ' .. comp_rank)
table.insert(result, user)
table.insert(result, score)
table.insert(result, rank)
table.insert(result, comp_rank)
end
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment