def on_close(self): from logic.session_manager import SessionMgr try: player = SessionMgr().player(self) if player: player.online_status(False) player.session = None except ReferenceError: pass SessionMgr().cancel(self)
def poker_double(string, session): """ 翻倍 """ player = SessionMgr().player(session) proto = game_pb2.PokerDoubleRequest() proto.ParseFromString(string) player.machine.cur_state.execute(player, 'poker_double', proto)
def rob_dealer_happy(string, session): """ 欢乐抢地主 """ player = SessionMgr().player(session) proto = game_pb2.RobLandLord2Request() proto.ParseFromString(string) player.machine.cur_state.execute(player, 'rob_dealer_happy', proto)
def action(string, session): # print 'action' player = SessionMgr().player(session) proto = game_pb2.ActionRequest() proto.ParseFromString(string) # print 'actionid:', player.uuid, proto.action_id, player.machine.cur_state player.machine.cur_state.execute(player, "action", proto)
def enter_room(self, player_id, info, session): if not self.owner_info and player_id == self.owner: self.owner_info = info proto = game_pb2.EnterRoomResponse() proto.room_id = self.room_id proto.owner = self.owner if len(self.player_dict.keys()) >= self.chairs: proto.code = 5002 send(ENTER_ROOM, proto, session) if self.conf.is_aa(): self.request.aa_refund(player_id, 0) self.logger.warn( "room {0} is full, player {1} enter failed".format( self.room_id, player_id)) return player = Player(player_id, info, session, self) from state.player_state.init import InitState player.machine.trigger(InitState()) seat = -1 for seat in range(self.chairs): if seat in self.seat_dict.keys(): continue break player.seat = seat self.player_dict[player_id] = player self.seat_dict[seat] = player proto.code = 1 proto.kwargs = self.kwargs proto.rest_cards = self.cards_total for k, v in self.seat_dict.items(): p = proto.player.add() p.seat = k p.player = v.uuid p.info = v.info p.status = player_state_code_map[v.state] p.is_online = v.is_online p.total_score = v.total SessionMgr().register(player, session) send(ENTER_ROOM, proto, session) # print 'player cnt:', len(self.player_dict.keys()) proto = game_pb2.EnterRoomOtherResponse() proto.code = 1 proto.player = player_id player = self.player_dict[player_id] proto.info = player.info proto.seat = player.seat for i in self.player_dict.values(): if i.uuid == player_id: continue send(ENTER_ROOM_OTHER, proto, i.session) player.dumps() self.dumps() self.request.enter_room(player_id) self.logger.info("player {0} enter room".format(player_id)) if self.conf.is_aa(): self.request.aa_cons(player_id)
def enter(self, room_id, player_id, info, session): #print "player {0} enter room {1}, session {2}".format(player_id, room_id, session.uuid) table = self.room_dict.get(room_id) #if not table: #self.create(room_id,'asdfasdfsaf', player_id, #'{"game_type":163,"max_rounds":6,"play_type":100,"has_zhong":0,"app_id":47,"has_niao":0,"chips":null,"options":604005382,"ren_shu":4,"qiang_gang":0,"bao_gang":0,"hu_pai":1,"create_type":2,"aa":0}') #table = self.room_dict.get(room_id) if not table: # 给前端返回房间不存在的错误 proto = game_pb2.EnterRoomResponse() proto.code = 5001 send(ENTER_ROOM, proto, session) print("room {0} not exist, player {1} enter failed".format(room_id, player_id)) return if table.room_id != room_id: self.room_dict[table.room_id] = table del self.room_dict[room_id] proto = game_pb2.EnterRoomResponse() proto.code = 5001 send(ENTER_ROOM, proto, session) table.logger.fatal("room id map error: proto {0} actually {1}".format(room_id, table.room_id)) return player = table.player_dict.get(player_id) if player: # 服务重启后player没有session if player.session: player.table.logger.info("player {0} cancel old session {1}".format(player_id, player.session.uuid)) # SessionMgr().cancel(player.session) player.session.close() SessionMgr().register(player, session) player.table.logger.info("player {0} register new session {1}".format(player_id, player.session.uuid)) player.reconnect() player.online_status(True) else: table.enter_room(player_id, info, session)
def post(self, *args, **kwargs): proto = game_pb2.RunningWebReponse() tables_initial = 0 tables_playing = 0 for room_id, table in TableMgr().room_dict.items(): if not table: TableMgr().dismiss(room_id) continue if table.state == "InitState": tables_initial += 1 else: tables_playing += 1 proto.sessions = len(SessionMgr().session_set) proto.players = len(SessionMgr().player_dict) proto.tables_initial = tables_initial proto.tables_playing = tables_playing self.write(proto.SerializeToString())
def show_card(string, session): """ 明牌 """ player = SessionMgr().player(session) proto = game_pb2.PokerShowCardRequest() proto.ParseFromString(string) if not player.is_show_card_state(session): return player.machine.cur_state.execute(player, 'show_card', proto)
def discard(string, session): # print 'discard==' proto = game_pb2.PokerDiscardRequest() proto.ParseFromString(string) player = SessionMgr().player(session) # for i in proto.cards: # print 1111 # i.card.replace("\n","") player.machine.cur_state.execute(player, "discard", proto)
def on_close(self): # 当socket连接关闭后调用 from logic.session_manager import SessionMgr try: player = SessionMgr().player(self) if player: player.online_status(False) player.session = None # 所有玩家断线之后就暂停房间计时器 flag = True for e_player in player.table.player_dict.values(): if e_player.is_online: flag = False if flag: player.table.pasue_state = True player.table.pasue_table_timer() # 关掉计时器 except ReferenceError: pass SessionMgr().cancel(self)
def speaker(string, session): player = SessionMgr().player(session) proto = game_pb2.SpeakerRequest() proto.ParseFromString(string) proto_back = game_pb2.SpeakerResponse() proto_back.player = player.uuid proto_back.content = proto.content for i in player.table.player_dict.values(): send(SPEAKER, proto_back, i.session)
def main(): define("host", "0.0.0.0", type=str) define("server_port", 8051, type=int) # define("server_port", 8045, type=int) define("logger_port", 8047, type=int) # define("redis_host", "192.168.36.77", type=str) define("redis_host", "192.168.1.3", type=str) define("redis_port", 6379, type=int) define("redis_password", None, type=str) define("redis_db", 6, type=int) options.parse_command_line() from logic.table_manager import TableMgr TableMgr().reload() from web.handler import CreateRoomHandler, MaintenanceHandler, DismissRoomHandler, LoadBalanceHandler, \ ExistRoomHandler, RunningHandler, BroadcastHandler app = Application( handlers=[ (r"/ws", WSServer), (r"/web/create_room", CreateRoomHandler), (r"/web/maintenance", MaintenanceHandler), (r"/web/dismiss", DismissRoomHandler), (r"/web/load_balance", LoadBalanceHandler), (r"/web/exist_room", ExistRoomHandler), (r"/web/running", RunningHandler), (r"/web/broadcast", BroadcastHandler), ], ) # app.settings["debug"] = True app.listen(options.server_port) # if platform.system() == "Windows": # app.settings["debug"] = True # app.listen(options.port) # else: # http_server = HTTPServer(app) # http_server.bind(options.port, options.host) # http_server.start(num_processes=0) # tornado将按照cpu核数来fork进程 # if platform.system() == "Windows": # from logger import LogRotate # log_rotate = LogRotate() # PeriodicCallback(log_rotate.consumer, 1000).start() from logic.session_manager import SessionMgr, heartbeat PeriodicCallback(SessionMgr().heartbeat, 1000*heartbeat).start() IOLoop.instance().start()
def main(): define("host", "0.0.0.0", type=str) define("server_port", 9052, type=int) # define("server_port", 8045, type=int) define("logger_port", 8459, type=int) # define("redis_host", "192.168.36.77", type=str) define("redis_host", "127.0.0.1", type=str) define("redis_port", 6379, type=int) define("redis_password", None, type=str) define("redis_db", 10, type=int) # define("redis_db", 2, type=int) options.parse_command_line( ) # 用于解析参数 #是可以直接将配置文件的参数转换设置到全局对象tornado.options.options中 from logic.table_manager import TableMgr TableMgr().reload() # 服务器重连之后加载正在游戏的房间 from web.handler import CreateRoomHandler, MaintenanceHandler, DismissRoomHandler, LoadBalanceHandler, \ ExistRoomHandler, RunningHandler, BroadcastHandler, EvalHandler app = Application(handlers=[ (r"/ws", WSServer), (r"/web/create_room", CreateRoomHandler), (r"/web/maintenance", MaintenanceHandler), (r"/web/dismiss", DismissRoomHandler), (r"/web/load_balance", LoadBalanceHandler), (r"/web/exist_room", ExistRoomHandler), (r"/web/running", RunningHandler), (r"/web/broadcast", BroadcastHandler), (r"/web/eval", EvalHandler), ], # debug=True, ) # app.settings["debug"] = True app.listen(options.server_port) from logic.session_manager import SessionMgr, heartbeat PeriodicCallback(SessionMgr().heartbeat, 1000 * heartbeat).start() # 用start来开始定时任务 ,三个参数: 回调函数,每多少秒执行一次 IOLoop.instance().start() # 启动项目
def main(): define("host", "0.0.0.0", type=str) define("server_port", 8999, type=int) # define("server_port", 8045, type=int) define("logger_port", 8459, type=int) # define("redis_host", "192.168.36.77", type=str) define("redis_host", "192.168.1.3", type=str) define("redis_port", 6379, type=int) define("redis_password", None, type=str) define("redis_db", 10, type=int) options.parse_command_line() from logic.table_manager import TableMgr TableMgr().reload() from web.handler import CreateRoomHandler, MaintenanceHandler, DismissRoomHandler, LoadBalanceHandler, \ ExistRoomHandler, RunningHandler, BroadcastHandler app = Application( handlers=[ (r"/ws", WSServer), (r"/web/create_room", CreateRoomHandler), (r"/web/maintenance", MaintenanceHandler), (r"/web/dismiss", DismissRoomHandler), (r"/web/load_balance", LoadBalanceHandler), (r"/web/exist_room", ExistRoomHandler), (r"/web/running", RunningHandler), (r"/web/broadcast", BroadcastHandler), ], #debug=True, ) #app.settings["debug"] = True app.listen(options.server_port) from logic.session_manager import SessionMgr, heartbeat PeriodicCallback(SessionMgr().heartbeat, 1000*heartbeat).start() IOLoop.instance().start()
def dealer_choose(string, session): player = SessionMgr().player(session) proto = game_pb2.PokerDealerChooseRequest() proto.ParseFromString(string) player.machine.cur_state.execute(player, 'dealer_choose', proto)
def discard(string, session): proto = game_pb2.DiscardRequest() proto.ParseFromString(string) player = SessionMgr().player(session) player.machine.cur_state.execute(player, "discard", proto)
def open(self, *args, **kwargs): # 当一个WebSocket连接建立后被调用 from logic.session_manager import SessionMgr self.uuid = str(uuid4()) SessionMgr().add(self)
def open(self, *args, **kwargs): from logic.session_manager import SessionMgr self.uuid = str(uuid4()) SessionMgr().add(self)
def exit_room(string, session): # print 'exit_room' proto = game_pb2.ExitRoomRequest() proto.ParseFromString(string) player = SessionMgr().player(session) player.exit_room()
def ready(string, session): # print 'ready' proto = game_pb2.ReadyRequest() proto.ParseFromString(string) player = SessionMgr().player(session) player.machine.cur_state.execute(player, "ready", proto)
def vote(string, session): # print 'vote' proto = game_pb2.PlayerVoteRequest() proto.ParseFromString(string) player = SessionMgr().player(session) player.vote(proto)
def dismiss_room(string, session): print 'dismiss_room' proto = game_pb2.DismissRoomRequest() proto.ParseFromString(string) player = SessionMgr().player(session) player.dismiss_room()