Exemplo n.º 1
0
def start_chat(conn, username, trans=None):
    """first part of message interchange for starting a one-to-one
    conversation"""
    user = ConnectedUser.__users__.get(username)
    sender = conn.data['user']

    if sender is user:
        conn.send(UserError.startchat_cannot_chat_with_yourself(), trans)
        return

    chat = Chat()

    def callback(accepted):
        if accepted:
            conn.send(UserOk.startchat_accepted(chat.id, sender.db_tuple.name, username), trans)
        else:
            sender.exitChat(chat)
            conn.send(UserOk.startchat_not_accepted(), trans)

    if user is not None:
        sender.enterChat(chat)
        user.callIntoChat(sender, chat.id, callback)
    else:
        conn.send(UserError.user_not_connected(), trans)
Exemplo n.º 2
0
def start_game(conn, username, color,roomname, size=None, trans=None, start_now=False):
    """first part of message interchange for starting a game
    
    color: 'white' or 'black'  (game starter will get this color)
    
    # TODO: faltan otras opciones de juego a configurar
    """
    sender = conn.data['user']
    
    if username == "GNUGo":
        start_now=True
    else:
        user = ConnectedUser.__users__.get(username)

        if sender is user:
            conn.send(UserError.startgame_cannot_play_with_yourself(), trans)
            return
    
        if user is None:
            conn.send(UserError.user_not_connected(), trans)
            return

    game = Game()
    
    def call_into_callback(accepted):
        if accepted:
            conn.send(UserOk.startgame_accepted(game.id), trans)
            sender.enterGame(game)
            room = sender.rooms.get(roomname)
            room.add_game(conn, white, black, size, game.id)
        else:
            game.destroy(clear_all_traces=True)
            conn.send(UserOk.startgame_not_accepted(), trans)
    
    def game_init_done_callback():
        if game.id is None:
            conn.send(GameError.init_failed(), trans)
        else:
            if start_now:
                sender.enterGame(game)
                if username != "GNUGo":
                    user.enterGame(game)
                room = sender.rooms.get(roomname)
                room.add_game(conn, white, black, size, game.id)
            else:
                user.callIntoGame(sender, game.id, call_into_callback)
    
    if color == 'white':
        white = sender.db_tuple.name
        if username != "GNUGo":
            black = user.db_tuple.name
        else:
            black = username
    else:
        black = sender.db_tuple.name
        if username != "GNUGo":
            white = user.db_tuple.name
        else:
            white = username
        
    kwarg = {}
    
    if isinstance(size, int):
        kwarg['size'] = size
    
    game_config = GameConfig(white, black, roomname, **kwarg)
    
    game.initialize(game_init_done_callback, game_config=game_config)