Skip to content

Instantly share code, notes, and snippets.

@thara
Last active June 17, 2019 06:36
Show Gist options
  • Save thara/d780738775e87841ee84 to your computer and use it in GitHub Desktop.
Save thara/d780738775e87841ee84 to your computer and use it in GitHub Desktop.
Matchmaking by Redis & Python
# -*- coding: utf-8 -*-
import random
import redis
import predef
r = redis.StrictRedis(host='localhost', port=6379, db=0)
player_id = random.randint(100, 200)
print("I'm %s" % player_id)
my_join_status = predef.join_status(player_id)
score = random.randint(0, 10000000)
r.set(my_join_status, "")
r.rpush(predef.queue, player_id)
while True:
matching_id = r.get(my_join_status)
if matching_id:
print("I'm matching!!!!")
print("Matching id: %s" % matching_id)
break
# -*- coding: utf-8 -*-
queue = 'matchmaking:queue'
next_matching = 'matchmaking:next'
def room(matching_id):
return 'matchmaking:room:%s' % matching_id
def join_status(player_id):
return 'matchmaking:%s:join_status' % player_id
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import redis
import predef
from tornado import gen, stack_context
from tornado.concurrent import return_future, TracebackFuture
r = redis.StrictRedis(host='localhost', port=6379, db=0)
@gen.engine
def _get_matching_id(player_id, callback):
import random
my_join_status = predef.join_status(player_id)
r.rpush(predef.queue, player_id)
print(my_join_status)
while True:
yield gen.sleep(1)
matching_id = r.get(my_join_status)
if matching_id:
print('player:%s, matching:%s' % (player_id, matching_id))
return callback(matching_id)
class PlayerMatchingHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
player_id = int(self.get_argument('pid'))
matching_id = yield gen.Task(_get_matching_id, player_id)
self.write("I'm matching into room:%s" % matching_id)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", PlayerMatchingHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
# -*- coding: utf-8 -*-
import time
import random
import redis
import predef
ROOM_SIZE = 4
r = redis.StrictRedis(host='localhost', port=6379, db=0)
def init():
r.setnx(predef.next_matching, 1)
r.delete(predef.queue)
for key in r.keys('matchmaking:room:*'):
r.delete(key)
for key in r.keys('matchmaking:*:join_status'):
r.delete(key)
for n in range(0, 100):
r.rpush(predef.queue, n)
def get_next_matching():
return int(r.get(predef.next_matching))
init()
while True:
next_id = get_next_matching()
player_id = r.lpop(predef.queue)
if player_id:
room = predef.room(next_id)
r.rpush(room, player_id)
if ROOM_SIZE <= r.llen(room):
all_members = r.lrange(room, 0, -1)
print(all_members)
for m in all_members:
member_join_status = predef.join_status(int(m))
r.set(member_join_status, next_id)
r.incr(predef.next_matching)
print('Matchig!!!')
print(room)
print(all_members)
else:
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment