Skip to content

Instantly share code, notes, and snippets.

@yoki123
Created February 6, 2015 04:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoki123/3111aef55e0230b12181 to your computer and use it in GitHub Desktop.
Save yoki123/3111aef55e0230b12181 to your computer and use it in GitHub Desktop.
A simple pvp ranking with python,redis
# -*- coding: utf-8 -*-
import random
import redis
import time
class PvPRanking(object):
def __init__(self):
self._rs = redis.Redis()
self._zset_name = 'host:ranking'
def get_last_score(self):
person = self._rs.zrange(self._zset_name, -1, -1)
if len(person) == 0:
return 1
score = self._rs.zscore(self._zset_name, person[0])
return score
def add_player(self, pid):
is_old = self._rs.zscore(self._zset_name, pid)
if is_old:
raise Exception('player already exist')
score = self.get_last_score()
# new player score = last rank player score + 1
self._rs.zadd(self._zset_name, pid, score + 1)
rank = self._rs.zrank(self._zset_name, pid)
return rank
def delete_player(self, pid):
return self._rs.zrem(self._zset_name, pid)
def exchange_rank(self, pid1, pid2):
with self._rs.pipeline() as pipe:
pipe.zscore(self._zset_name, pid1)
pipe.zscore(self._zset_name, pid2)
score1, score2 = pipe.execute()
with self._rs.pipeline() as pipe:
pipe.zadd(self._zset_name, pid1, score2)
pipe.zadd(self._zset_name, pid2, score1)
pipe.execute()
def ranking(self, start=0, end=100):
return self._rs.zrange(self._zset_name, start, end)
def get_rank(self, pid):
return self._rs.zrank(self._zset_name, pid)
def get_score(self, pid):
return self._rs.zscore(self._zset_name, pid)
if __name__ == '__main__':
p = PvPRanking()
p._rs.flushdb()
num_test = 1000
base_num = 1000000
t1 = time.clock()
for pid in xrange(base_num, base_num + num_test):
p.add_player(pid)
t2 = time.clock()
top100 = p.ranking()
t3 = time.clock()
p.get_rank(base_num + random.randint(0, num_test-1))
t4 = time.clock()
print(top100)
print('init 1000: %f ms' % ((t2 - t1) * 1000))
print('top 100: %f ms' % ((t3 - t2) * 1000))
print('get single rank: %f ms' % ((t4 - t3) * 1000))
pid1, pid2 = base_num + random.randint(0, num_test-1), base_num + random.randint(0, num_test-1)
print('before exchange rank: %d score=%s rank=%s' % (pid1, p.get_score(pid1), p.get_rank(pid1)))
print('before exchange rank: %d score=%s rank=%s' % (pid2, p.get_score(pid2), p.get_rank(pid2)))
p.exchange_rank(pid1, pid2)
print('after exchange rank: %d score=%s rank=%s' % (pid1, p.get_score(pid1), p.get_rank(pid1)))
print('after exchange rank: %d score=%s rank=%s' % (pid2, p.get_score(pid2), p.get_rank(pid2)))
@yoki123
Copy link
Author

yoki123 commented Feb 6, 2015

test result on my pc ,

['1000000', '1000001', '1000002', '1000003', '1000004', '1000005', '1000006', '1000007', '1000008', '1000009', '1000010', '1000011', '1000012', '1000013', '1000014', '1000015', '1000016', '1000017', '1000018', '1000019', '1000020', '1000021', '1000022', '1000023', '1000024', '1000025', '1000026', '1000027', '1000028', '1000029', '1000030', '1000031', '1000032', '1000033', '1000034', '1000035', '1000036', '1000037', '1000038', '1000039', '1000040', '1000041', '1000042', '1000043', '1000044', '1000045', '1000046', '1000047', '1000048', '1000049', '1000050', '1000051', '1000052', '1000053', '1000054', '1000055', '1000056', '1000057', '1000058', '1000059', '1000060', '1000061', '1000062', '1000063', '1000064', '1000065', '1000066', '1000067', '1000068', '1000069', '1000070', '1000071', '1000072', '1000073', '1000074', '1000075', '1000076', '1000077', '1000078', '1000079', '1000080', '1000081', '1000082', '1000083', '1000084', '1000085', '1000086', '1000087', '1000088', '1000089', '1000090', '1000091', '1000092', '1000093', '1000094', '1000095', '1000096', '1000097', '1000098', '1000099', '1000100']
init 1000: 226.556000 ms
top 100: 0.070000 ms
get single rank: 0.064000 ms
before exchange rank: 1000024  score=26.0 rank=24
before exchange rank: 1000183  score=185.0 rank=183
after exchange rank: 1000024  score=185.0 rank=183
after exchange rank: 1000183  score=26.0 rank=24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment