Ejemplo n.º 1
0
 def post(self, *args, **kwargs):
     proto = game_pb2.DismissRoomWebRequest()
     proto.ParseFromString(self.request.body)
     table = TableMgr().room_dict.get(proto.room_id)
     if table:
         assert table.room_id == proto.room_id
         assert table.conf.game_type == proto.game_type
         assert table.conf.app_id == proto.app_id
         assert table.owner == proto.owner
         assert table.room_uuid == proto.room_uuid
         table.dismiss_state = True
         # noinspection PyBroadException
         try:
             table.dismiss_room(False)
         except Exception:
             fp = StringIO()
             traceback.print_exc(file=fp)
             table.logger.critical(fp.getvalue())
             table.delete()
             TableMgr().dismiss(proto.room_id)
     proto_back = game_pb2.DismissRoomWebResponse()
     proto_back.code = 0
     proto_back.room_id = proto.room_id
     proto_back.game_type = proto.game_type
     proto_back.app_id = proto.app_id
     proto_back.owner = proto.owner
     self.write(proto.SerializeToString())
Ejemplo n.º 2
0
 def post(self, *args, **kwargs):
     proto = game_pb2.LoadBalanceWebResponse()
     for room_id, table in TableMgr().room_dict.items():
         if not table:
             TableMgr().dismiss(room_id)
             continue
         unit = proto.unit.add()
         unit.room_id = table.room_id
         unit.room_status = 0 if table.state == "InitState" else 1
         unit.room_uuid = table.room_uuid
         unit.owner = table.owner
         unit.game_type = table.conf.game_type
         unit.app_id = table.conf.app_id
         unit.st = int(table.st)
         for player in table.player_dict.keys():
             unit.player.append(player)
     self.write(proto.SerializeToString())
Ejemplo n.º 3
0
 def post(self):
     proto = game_pb2.CreateRoomRequest()
     proto.ParseFromString(self.request.body)
     TableMgr().create(proto.room_id, proto.room_uuid, proto.owner,
                       proto.kwargs)
     proto = game_pb2.CreateRoomResponse()
     proto.code = 1
     self.write(proto.SerializeToString())
Ejemplo n.º 4
0
 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())
Ejemplo n.º 5
0
    def dismiss_room(self, vote=True):

        # 如果是投票解散房间则进入大结算,否则直接推送房主解散命令

        def _dismiss():
            # 弹出大结算
            from state.table_state.settle_for_room import SettleForRoomState
            self.machine.trigger(SettleForRoomState())
            self.request.load_minus()

        if vote and self.state != "InitState":
            # 房间内游戏中投票解散
            _dismiss()

        else:
            # 0-没开始解散 1-投票 2-外部解散或者游戏进行中房主解散
            vote_flag = 2
            if vote:
                # 房间内投票
                vote_flag = 1
            else:
                if self.state == "InitState":
                    # 房主在游戏未开始时解散
                    vote_flag = 0
            if vote_flag == 2:
                # 到这说明游戏在进行中房主解散
                # 在游戏内部已经做了限制不会走到这里
                # 在游戏外部如果游戏进行中不让解散
                # 这里直接返回吧
                # self.dismiss_flag = 2
                # _dismiss()
                return
            else:

                # 游戏未开始时和游戏外房主解散直接返回0,2。
                # 0代表外开始时解散,2代表外部解散或者游戏进行中房主解散
                # 直接返回一个flag就完事了
                proto = game_pb2.DismissRoomResponse()
                proto.code = 1
                proto.flag = vote_flag
                for k, v in self.player_dict.items():
                    send(DISMISS_ROOM, proto, v.session)
                # 这个位置没-1导致房间数量一直上升
                if vote_flag == 1:
                    self.request.load_minus()
        self.logger.info("room {0} dismiss".format(self.room_id))
        from logic.table_manager import TableMgr
        self.request.dismiss_room(self)
        TableMgr().dismiss(self.room_id)
        for player in self.player_dict.values():
            try:
                player.session.close()
            except Exception:
                pass
            player.delete()

        self.delete()
Ejemplo n.º 6
0
 def post(self, *args, **kwargs):
     proto = game_pb2.ExistRoomWebRequest()
     proto.ParseFromString(self.request.body)
     table = TableMgr().room_dict.get(proto.room_id)
     if table:
         flag = True
     else:
         flag = False
     proto = game_pb2.ExistRoomWebResponse()
     proto.flag = flag
     self.write(proto.SerializeToString())
Ejemplo n.º 7
0
    def post(self, *args, **kwargs):
        # proto = game_pb2.BroadcastWebRequest()
        # proto.ParseFromString(self.request.body)
        # message = proto.message
        # ext_info = proto.ext_info
        body = json.loads(self.request.body)
        message = body.get('message')
        ext_info = body.get('ext_info')

        table_mgr = TableMgr()
        table_mgr.broadcast = [message]

        for room_id, table in table_mgr.room_dict.items():
            if not table:
                TableMgr().dismiss(room_id)
                continue
            try:
                table.broadcast_all(message)
            except Exception:
                fp = StringIO()
                traceback.print_exc(file=fp)
                table.logger.critical(fp.getvalue())
Ejemplo n.º 8
0
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()
Ejemplo n.º 9
0
    def dismiss_room(self, vote=True):
        # 如果是投票解散房间则进入大结算,否则直接推送房主解散命令
        def _dismiss():
            from state.table_state.settle_for_room import SettleForRoomState
            self.machine.trigger(SettleForRoomState())
            self.request.load_minus()

        if vote and self.state != "InitState":
            _dismiss()
        else:
            proto = game_pb2.DismissRoomResponse()
            proto.code = 1
            # 0-没开始解散 1-投票 2-游戏内解散
            vote_flag = 2
            if vote:
                vote_flag = 1
            else:
                if self.state == "InitState":
                    vote_flag = 0
            proto.flag = vote_flag
            for k, v in self.player_dict.items():
                send(DISMISS_ROOM, proto, v.session)

            if self.state != "InitState":
                self.dismiss_flag = 2
                _dismiss()

        self.logger.info("room {0} dismiss".format(self.room_id))
        from logic.table_manager import TableMgr
        self.request.dismiss_room(self)
        TableMgr().dismiss(self.room_id)
        for player in self.player_dict.values():
            try:
                player.session.close()
            except Exception:
                pass
            player.delete()

        self.delete()
Ejemplo n.º 10
0
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()  # 启动项目
Ejemplo n.º 11
0
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()
Ejemplo n.º 12
0
def enter_room(string, session):
    # print 'enter_room'
    proto = game_pb2.EnterRoomRequest()
    proto.ParseFromString(string)
    TableMgr().enter(proto.room_id, proto.player, proto.info, session)