Esempio n. 1
0
    def __init__(self, host='', port=36709, name=None, ui = ConsoleUI):
        
        self.host = host
        self.port = port
        self.ui = ui(self.send_chat)

        if name is None:
            self.name = validate_name(self.ui.get_player_name())
        else:
            self.name = validate_name(name)


        self.m_handlers = defaultdict(lambda: self.handle_default)
        self.m_handlers['join'] = self.handle_join
        self.m_handlers['chat'] = self.ui.show_chat
        self.m_handlers['ante'] = self.handle_ante
        self.m_handlers['deal'] = self.handle_deal
        self.m_handlers['turn'] = self.handle_turn
        self.m_handlers['stat'] = self.handle_stat
        self.m_handlers['endg'] = self.handle_endg
        self.m_handlers['exit'] = self.handle_exit
        self.all_messages = self.m_handlers.keys()
        self.server = s.socket(s.AF_INET, s.SOCK_STREAM)
        
        self.watched_socks = [self.server]
        if self.ui.chat_at_stdin:
            self.watched_socks.append(sys.stdin)
        
        signal.signal(signal.SIGINT, self.exit)


        #logging stuff
        self.logger = logging.getLogger('blackjack')
        self.logger.setLevel(logging.DEBUG)

        # create file handler which logs even debug messages
        fh = logging.FileHandler('client.log')
        fh.setLevel(logging.DEBUG)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        # create formatter and add it to the handlers
        format_style = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
        formatter = logging.Formatter(colors.DIM + format_style + colors.ENDC)
        formatter_no_color = logging.Formatter(format_style)
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        # add the handlers to the logger
        self.logger.addHandler(fh)
        self.logger.addHandler(ch)


        self.logger.info('about to make chat handler')
        c_handler = ChatHandler(self.server.sendall)
        c_handler.setLevel(logging.INFO)
        c_handler.setFormatter(formatter_no_color)
        self.logger.addHandler(c_handler)
        self.game_in_progress = False
        self.players = False
        self.location='lobby'