Beispiel #1
0
def register(session, username, password, fullname, role):
    try:
        dbuser = DBUser(username, password, fullname)
        session.add(dbuser)
        dbuser.role = session.query(Role).filter(Role.name == role).one()
        session.commit()
        response = UserOk.register()
    except IntegrityError:
        session.rollback()
        response = UserError.register_username_taken()
    except:
        session.rollback()
        response = UserError.register_failed()

    return response
Beispiel #2
0
def _login_callback(user_tuple, conn, trans=None):
    if user_tuple is not None:
        try:
            cu = ConnectedUser(user_tuple, conn)
        except ValueError:
            # this user is already logged in
            conn.send(UserError.user_is_already_logged_in(), trans)
            return

        conn.data['authenticated'] = True
        conn.data['user'] = cu

        cu.enterRoom(conn.factory.main_room)

        response = UserOk.login()
    else:
        response = UserError.login_failed()

    conn.send(response, trans)
Beispiel #3
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)
Beispiel #4
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)