class Dealer(object): def __init__(self, exchange, host, port): self.exchange = exchange self.host = host self.port = port self.room_list = {} def init_database(self): database.init_database() info = DealerInfo.find(exchange=self.exchange) if not info: info = DealerInfo.new(self.exchange) else: print "WARNING, dealer %s already in database" % self.exchange info.rooms = 0 self.info = info def on_queue_bound(self, frame): self.channel.basic_consume(consumer_callback=self.on_message, queue=self.queue_name, no_ack=True) def on_queue_declared(self, frame): self.queue_name = frame.method.queue self.channel.queue_bind(exchange=self.exchange, queue=self.queue_name, routing_key='dealer', callback=self.on_queue_bound) def on_exchange_declared(self, frame): self.channel.queue_declare(auto_delete=True, durable=False, exclusive=False, callback=self.on_queue_declared) def on_channel_open(self, channel): self.channel = channel self.channel.exchange_declare(exchange=self.exchange, type='topic', auto_delete=True, durable=False, callback=self.on_exchange_declared) def on_connected(self, connection): connection.channel(self.on_channel_open) def start(self): credentials = pika.PlainCredentials('guest', 'guest') param = pika.ConnectionParameters(self.host, port=self.port, virtual_host="/", credentials=credentials) self.connection = TornadoConnection(param, on_open_callback=self.on_connected) self.connection.set_backpressure_multiplier(100000) def cmd_action(self, args): print "action in dealer %d" % args["action"] if args["room_id"] in self.room_list: current_room = self.room_list[args["room_id"]] current_room.user_action(args) def cmd_chat(self, args): print args room = self.room_list[args['room']] room.chat(args['user'], args['seat'], args['content']) def cmd_sit(self, args): print "sit received" source = args['source'] private_key = args['private_key'] stake = args['stake'] user = User.find(_id=args['user_id']) print args['user_id'] current_room = self.room_list[args["room_id"]] (status, msg) = current_room.sit(user, int(args["seat"]), source, private_key, stake) if status: message = {"status": "success"} else: message = {"status": "failed", "msg": msg} self.channel.basic_publish(exchange=self.exchange, routing_key=source, body=json.dumps(message)) def broadcast(self, routing_key, msg): self.channel.basic_publish(exchange=self.exchange, routing_key=routing_key, body=json.dumps(msg)) def cmd_enter(self, args): routing_key = args['source'] user_id = args['user_id'] if args['room_id'] not in self.room_list: #self.cmd_create_room(args) message = {'status': 'failed'} else: current_room = self.room_list[args["room_id"]] message = { 'status': 'success', "room": current_room.to_listener(user_id) } self.channel.basic_publish(exchange=self.exchange, routing_key=routing_key, body=json.dumps(message)) #current_room.resend_last_next_message() def cmd_create_room(self, args): print "creating room" blind = int(args["blind"]) max_stake = int(args["max_stake"]) min_stake = int(args["min_stake"]) max_player = int(args["max_player"]) routing_key = args['source'] roomType = int(args["roomType"]) newRoom = Room.new(self.exchange, blind, max_player, max_stake, min_stake, roomType) self.room_list[newRoom.id] = GameRoom(newRoom, args["user_id"], self, max_player, blind, min_stake, max_stake) print newRoom message = {"room_id": newRoom.id} self.channel.basic_publish(exchange=self.exchange, routing_key=routing_key, body=json.dumps(message)) self.info.rooms += 1 def on_message(self, channel, method, header, body): print "ON_MESSAGE!" obj = json.loads(body) print body method = getattr(self, "cmd_" + obj['method']) method(obj) def close(self): self.connection.close() def handle_init_file(self, fname, isDebug): info = Room.find_all(exchange=self.exchange) for room in info: room.remove() # if os.path.isfile(fname): print len(fname) print os.getcwd() with open(fname) as f: for line in f: if line[0] == '#': continue (roomType, blind, max_stake, min_stake, max_player) = (int(x) for x in line.strip().split(',')) newRoom = Room.new(self.exchange, blind, max_player, max_stake, min_stake, roomType) self.room_list[newRoom.id] = GameRoom(newRoom, 1, self, max_player, blind, min_stake, max_stake) self.info.rooms += 1 print newRoom
class Tester(object): def __init__(self,queue,exchange,host='localhost',port=5672): user1 = User(1,1) user2 = User(2,2) user3 = User(3,3) self.queue = queue self.exchange =exchange self.users = [user1]#,user2,user3] self.pKeys = {} self.boundQueue = 0 def on_queue_bound(self, frame): self.boundQueue -= 1 print "BOUND",self.boundQueue if self.boundQueue == 0: print "Start consume!!" self.channel.basic_consume(consumer_callback=self.on_message, queue=self.queue, no_ack=True) else: return for user in self.users: self.channel.basic_publish(exchange='dealer_exchange_1', routing_key="dealer", body=json.dumps({'method':'enter','source':'IAMGOD', "room_id":1, "user_id":user.user_id})) self.channel.basic_publish(exchange='dealer_exchange_1', routing_key="dealer", body=json.dumps({'method':'sit','source':'IAMGOD','user_id':1, "room_id":1, "seat":1,"private_key":self.users[0].private_key, "stake":100})) # self.channel.basic_publish(exchange='dealer_exchange_1', # routing_key="dealer", # body=json.dumps({'method':'sit','source':'IAMGOD','user_id':2, "room_id":1, "seat":2,"private_key":self.users[1].private_key, "stake":100})) # # self.channel.basic_publish(exchange='dealer_exchange_1', # routing_key="dealer", # body=json.dumps({'method':'sit','source':'IAMGOD','user_id':3, "room_id":1, "seat":3,"private_key":self.users[2].private_key, "stake":500})) def on_queue_declared(self, frame): for user in self.users: self.boundQueue += 1 self.channel.queue_bind(exchange='dealer_exchange_1', queue=self.queue,routing_key=user.private_key, callback=self.on_queue_bound ) self.boundQueue += 1 self.channel.queue_bind(exchange='dealer_exchange_1', queue=self.queue,routing_key="IAMGOD", callback=self.on_queue_bound ) broadcast_key = "broadcast_%s_%d.testing" % ( exchange, room_id) self.boundQueue += 1 self.channel.queue_bind(exchange='dealer_exchange_1', queue=self.queue,routing_key=broadcast_key, callback=self.on_queue_bound ) def on_exchange_declared(self,frame): self.channel.queue_declare(queue = self.queue, auto_delete = True, durable = True, exclusive = False, callback=self.on_queue_declared, arguments = {"x-expires":15000}) def on_channel_open(self,channel): self.channel = channel self.channel.exchange_declare(exchange = self.exchange, type = 'topic', auto_delete = True, durable = False, callback=self.on_exchange_declared ) def on_connected(self,connection): connection.channel(self.on_channel_open) def start(self): credentials = pika.PlainCredentials('guest', 'guest') param = pika.ConnectionParameters(host="localhost", port=5672, virtual_host="/", credentials=credentials) self.connection = TornadoConnection(param, on_open_callback=self.on_connected) self.connection.set_backpressure_multiplier(100000) def on_message(self,ch, method, properties, body): print " [x] %r:%r" % (method.routing_key, json.loads(body),) msg = json.loads(body) # if 'Cards in hand' in msg: self.pKeys[method.routing_key] = 1 # if len(self.pKeys) == 1: self.pKeys = {} self.channel.basic_publish(exchange='dealer_exchange_1', routing_key="dealer", body=json.dumps({'method':'action','action':8,'user_id':1, "room_id":1, "private_key":self.users[0].private_key, "amount":20}))
class Dealer(object): def __init__(self,exchange,host,port): self.exchange = exchange self.host = host self.port = port self.room_list = {} def init_database(self): database.init_database() info = DealerInfo.find(exchange = self.exchange) if not info: info = DealerInfo.new(self.exchange) else: print "WARNING, dealer %s already in database" % self.exchange info.rooms = 0 self.info = info def on_queue_bound(self, frame): self.channel.basic_consume( consumer_callback=self.on_message, queue=self.queue_name, no_ack=True) def on_queue_declared(self, frame): self.queue_name = frame.method.queue self.channel.queue_bind( exchange = self.exchange, queue = self.queue_name, routing_key = 'dealer', callback=self.on_queue_bound) def on_exchange_declared(self,frame): self.channel.queue_declare( auto_delete = True, durable = False, exclusive = False, callback = self.on_queue_declared) def on_channel_open(self,channel): self.channel = channel self.channel.exchange_declare(exchange = self.exchange, type = 'topic', auto_delete = True, durable = False, callback=self.on_exchange_declared ) def on_connected(self,connection): connection.channel(self.on_channel_open) def start(self): credentials = pika.PlainCredentials('guest', 'guest') param = pika.ConnectionParameters(self.host, port=self.port, virtual_host="/", credentials=credentials) self.connection = TornadoConnection(param, on_open_callback=self.on_connected) self.connection.set_backpressure_multiplier(100000) def cmd_action(self, args): print "action in dealer %d" % args["action"] if args["room_id"] in self.room_list: current_room = self.room_list[args["room_id"]] current_room.user_action(args) def cmd_chat(self, args): print args room = self.room_list[args['room']] room.chat(args['user'], args['seat'], args['content']) def cmd_sit(self, args): print "sit received" source = args['source'] private_key = args['private_key'] stake = args['stake'] user = User.find(_id=args['user_id']) print args['user_id'] current_room = self.room_list[args["room_id"]] (status, msg) = current_room.sit(user, int(args["seat"]), source, private_key,stake) if status: message = {"status": "success" } else: message = {"status": "failed", "msg": msg} self.channel.basic_publish( exchange = self.exchange, routing_key = source, body = json.dumps(message)) def broadcast(self, routing_key, msg): self.channel.basic_publish( exchange = self.exchange, routing_key = routing_key, body = json.dumps(msg)) def cmd_enter(self,args): routing_key = args['source'] user_id = args['user_id'] if args['room_id'] not in self.room_list: #self.cmd_create_room(args) message = {'status':'failed'} else: current_room = self.room_list[args["room_id"]] message = {'status':'success', "room":current_room.to_listener(user_id)} self.channel.basic_publish( exchange = self.exchange, routing_key = routing_key, body = json.dumps(message)) #current_room.resend_last_next_message() def cmd_create_room(self, args): print "creating room" blind = int(args["blind"]) max_stake = int(args["max_stake"]) min_stake = int(args["min_stake"]) max_player = int(args["max_player"]) routing_key = args['source'] roomType = int(args["roomType"]) newRoom = Room.new(self.exchange,blind,max_player, max_stake, min_stake,roomType) self.room_list[newRoom.id] = GameRoom( newRoom, args["user_id"], self, max_player,blind,min_stake,max_stake) print newRoom message = {"room_id":newRoom.id} self.channel.basic_publish( exchange = self.exchange, routing_key = routing_key, body = json.dumps(message)) self.info.rooms += 1 def on_message(self, channel, method, header, body): print "ON_MESSAGE!" obj = json.loads(body) print body method = getattr(self,"cmd_" + obj['method']) method(obj) def close(self): self.connection.close() def handle_init_file(self,fname,isDebug): info = Room.find_all(exchange = self.exchange) for room in info: room.remove() # if os.path.isfile(fname): print len(fname) print os.getcwd() with open(fname) as f: for line in f: if line[0] == '#': continue (roomType,blind,max_stake,min_stake,max_player) = ( int(x) for x in line.strip().split(',')) newRoom = Room.new(self.exchange,blind,max_player, max_stake, min_stake,roomType) self.room_list[newRoom.id] = GameRoom( newRoom, 1, self, max_player, blind, min_stake, max_stake) self.info.rooms += 1 print newRoom