Example #1
0
def route_add_division_machine_team(division_id,division_machine_id,team_id):            
    db = db_util.app_db_handle(current_app)
    tables = db_util.app_db_tables(current_app)                            
    division_machine = fetch_entity(tables.DivisionMachine,division_machine_id)        
    team = fetch_entity(tables.Team,team_id)
    if division_machine.team_id or division_machine.player_id:
        raise Conflict('The machine is already being played')
    if check_player_team_can_start_game(current_app,division_machine,team=team) is False:
        raise BadRequest('Player can not start game - either no tickets or already on another machine')    
    if tables.DivisionMachine.query.filter_by(player_id=team.players[0].player_id).first() or tables.DivisionMachine.query.filter_by(player_id=team.players[1].player_id).first():            
        raise BadRequest('Team can not start game - one player is playing on another machine')            
    set_token_start_time(current_app,None,division_machine,team_id=team_id)    
    division_machine.team_id=team.team_id
    tables.db_handle.session.commit()
    players_to_alert = []
    removed_queues = []
    for player in team.players:
        queue = tables.Queue.query.filter_by(player_id=player.player_id).first()        
        if queue:
            players_to_alert = players_to_alert + get_player_list_to_notify(player.player_id,queue.division_machine)            
        removed_queue = remove_player_from_queue(current_app,player,commit=True)
        removed_queues.append(removed_queue)
        db.session.commit()
    if all(item is not None and item is not False for item in removed_queues) and len(players_to_alert) > 0:    
        push_notification_message = "The queue for %s has changed!  Please check the queue to see your new position." % queue.division_machine.machine.machine_name
        send_push_notification(push_notification_message, players=players_to_alert)
        
    return jsonify({'data':division_machine.to_dict_simple()})
Example #2
0
def route_add_division_machine_player(division_id,division_machine_id,player_id):            
    db = db_util.app_db_handle(current_app)
    tables = db_util.app_db_tables(current_app)                            
    division_machine = fetch_entity(tables.DivisionMachine,division_machine_id)        
    player = fetch_entity(tables.Player,player_id)
    if division_machine.division.active is False:
        raise Conflict("Division is not active.")                
    if division_machine.removed is True:
        raise Conflict("Machine is not active.  You have been very naughty.")        
    if player.active is False:
        raise Conflict('Player is not active.  Please see the front desk.')        
    if division_machine.player_id or division_machine.team_id:
        raise Conflict('Machine is already being played')
    if check_player_team_can_start_game(current_app,division_machine,player) is False:
        raise BadRequest('Player can not start game - either no tickets or already on another machine')
    if len(player.teams) > 0:
        if tables.DivisionMachine.query.filter_by(team_id=player.teams[0].team_id).first():            
            raise BadRequest('Player can not start game - his team is playing on another machine')        
    set_token_start_time(current_app,player,division_machine,commit=False)    
    create_audit_log_ex(current_app, "Game Started",
                        user_id=current_user.user_id,
                        player_id=player.player_id,
                        division_machine_id=division_machine.division_machine_id,                        
                        commit=False)

    division_machine.player_id=player.player_id
    ##db.session.commit()
    queue = tables.Queue.query.filter_by(player_id=player.player_id).first()
    players_to_alert = []
    players_in_other_queue = []
    if queue:
        players_to_alert = get_player_list_to_notify(player.player_id,queue.division_machine)
        players_in_other_queue = get_players_in_queue_after_player(player.player_id)
    removed_queue = remove_player_from_queue(current_app,player,commit=True)
    if removed_queue and removed_queue is not False:
        create_audit_log_ex(current_app, "Player removed from queue",
                            user_id=current_user.user_id,
                            player_id=player.player_id,
                            division_machine_id=removed_queue.division_machine_id,
                            description="Player removed from queue %s by getting started on %s" % (removed_queue.division_machine.machine.machine_name,division_machine.machine.machine_name),
                            commit=False)

    for player_in_other_que in players_in_other_queue:
        create_audit_log_ex(current_app, "Other player removed from queue",
                            user_id=current_user.user_id,
                            player_id=player_in_other_que['player_id'],
                            division_machine_id=player_in_other_que['division_machine_id'],
                            description="Player moved up on queue %s due to removal of player %s" % (player_in_other_que['division_machine']['division_machine_name'],player.first_name+" "+player.last_name),
                            commit=False)
        
    if removed_queue is not None and removed_queue is not False and len(players_to_alert) > 0:        
        push_notification_message = "The queue for %s has changed!  Please check the queue to see your new position." % queue.division_machine.machine.machine_name
        send_push_notification(push_notification_message, players=players_to_alert)
    db.session.commit()
    return jsonify({'data':division_machine.to_dict_simple()})
def route_remove_player_from_queue(player_id):
    db = db_util.app_db_handle(current_app)
    tables = db_util.app_db_tables(current_app)
    player = fetch_entity(tables.Player, player_id)
    queue = tables.Queue.query.filter_by(player_id=player_id).first()
    if queue is None:
        raise BadRequest('Player is not in any queues')
    division_machine = queue.division_machine
    try:
        queues_to_lock = tables.Queue.query.with_for_update().filter_by(
            division_machine_id=division_machine.division_machine_id).all()
        players_to_alert = get_player_list_to_notify(player.player_id,
                                                     division_machine)
        remove_result = remove_player_from_queue(current_app,
                                                 player,
                                                 commit=False)
        new_queue = get_queue_from_division_machine(division_machine, True)
        if len(players_to_alert) > 0:
            push_notification_message = """
            The queue for %s has changed! Check the queue to see your new position.
            """ % division_machine.machine.machine_name
            send_push_notification(push_notification_message,
                                   players=players_to_alert)
        queuer = get_username_or_player_name(current_user.user_id)
        create_audit_log_ex(
            current_app,
            "Player removed from queue",
            user_id=current_user.user_id,
            player_id=player.player_id,
            division_machine_id=division_machine.division_machine_id,
            description="Player removed from queue for %s by %s" %
            (division_machine.machine.machine_name, queuer),
            commit=False)

        db.session.commit()
    except Exception as e:
        db.session.commit()
        raise e
    return jsonify({
        'data': {
            division_machine.division_machine_id: {
                'queues': new_queue
            }
        }
    })
def bump_player_down_queue(division_machine_id):
    db = db_util.app_db_handle(current_app)
    tables = db_util.app_db_tables(current_app)
    division_machine = fetch_entity(tables.DivisionMachine,
                                    division_machine_id)
    if len(division_machine.queue) > 0:
        #queue = division_machine.queue[0]
        queue = tables.Queue.query.filter_by(
            division_machine_id=division_machine_id, parent_id=None).first()
    else:
        queue = None
    allow_bump = True
    if queue and queue.bumped:
        allow_bump = False
    if len(queue.queue_child) == 0:
        allow_bump = False
    player_id = queue.player_id
    player = fetch_entity(tables.Player, player_id)

    with db.session.no_autoflush:
        try:
            queues_to_lock = tables.Queue.query.with_for_update().filter_by(
                division_machine_id=division_machine.division_machine_id).all(
                )
            if allow_bump:
                child_queue = queue.queue_child[0]
                if child_queue and len(child_queue.queue_child) > 0:
                    grand_child = child_queue.queue_child[0]
                else:
                    grand_child = None
                queue.queue_child.remove(child_queue)
                if grand_child:
                    child_queue.queue_child.remove(grand_child)
                new_queue = tables.Queue(
                    division_machine_id=division_machine_id,
                    player_id=player_id,
                    bumped=True)
                db.session.add(new_queue)
                child_queue.queue_child.append(new_queue)
                if grand_child:
                    new_queue.queue_child.append(grand_child)
                db.session.delete(queue)
                create_audit_log_ex(
                    current_app,
                    "Player bumped down queue",
                    user_id=current_user.user_id,
                    player_id=player.player_id,
                    division_machine_id=division_machine.division_machine_id,
                    description="Player bumped down queue for %s by %s" %
                    (division_machine.machine.machine_name,
                     current_user.username),
                    commit=False)

                create_audit_log(
                    "Player Bumped",
                    datetime.datetime.now(),
                    "",
                    user_id=current_user.user_id,
                    player_id=player_id,
                    division_machine_id=division_machine.division_machine_id,
                    commit=False)
            else:
                if queue:
                    players_to_alert = get_player_list_to_notify(
                        player.player_id, queue.division_machine)
                else:
                    players_to_alert = []
                create_audit_log_ex(
                    current_app,
                    "Player removed from queue due to bumping",
                    user_id=current_user.user_id,
                    player_id=player.player_id,
                    division_machine_id=division_machine.division_machine_id,
                    description="Player removed from queue for %s by %s" %
                    (division_machine.machine.machine_name,
                     current_user.username),
                    commit=False)

                remove_player_from_queue(current_app, player, commit=False)

                if len(players_to_alert) > 0:
                    push_notification_message = "The queue for %s has changed!  Please check the queue to see your new position." % queue.division_machine.machine.machine_name
                    send_push_notification(push_notification_message,
                                           players=players_to_alert)
            if player.bump_count:
                player.bump_count = player.bump_count + 1
            else:
                player.bump_count = 1
            print "about to commit bump..."
            db.session.commit()
            return_queue = get_queue_from_division_machine(
                division_machine, True)
            #db.session.commit()
            return jsonify(
                {'data': {
                    division_machine_id: {
                        'queues': return_queue
                    }
                }})
        except Exception as e:
            db.session.commit()
            print "poop...%s" % e
            raise Conflict(
                "Something went wrong while bumping player down queue - please try again."
            )
def add_other_player_to_queue():
    db = db_util.app_db_handle(current_app)
    tables = db_util.app_db_tables(current_app)
    queue_data = json.loads(request.data)
    division_machine = fetch_entity(tables.DivisionMachine,
                                    queue_data['division_machine_id'])

    if division_machine.division.active is False:
        raise BadRequest('Division is not active')
    if division_machine.removed is True:
        raise BadRequest(
            'Machine has been removed - you have been very naughty')
    if division_machine.player_id is None and len(division_machine.queue) == 0:
        raise BadRequest(
            'Oops - player finished playing this game while you were queuing.  See a scorekeeper to start game'
        )
    #player = fetch_entity(tables.Player,queue_data['player_id'])
    player = fetch_entity(tables.Player, queue_data['other_player_id'])
    if player.active is False:
        raise BadRequest("Player is not active - please see front desk")
    if 'other_player_pin' not in queue_data or player.pin != int(
            queue_data['other_player_pin']):
        raise BadRequest('Invalid player id and player pin')
    if player.division_machine:
        raise BadRequest("Can't queue - player  is already playing a machine")
    if len(player.teams) > 0:
        if tables.DivisionMachine.query.filter_by(
                team_id=player.teams[0].team_id).first():
            raise BadRequest(
                "Can't queue - player's team is on another machine")
    if check_player_team_can_start_game(current_app, division_machine,
                                        player) is False:
        raise BadRequest("Can't queue - player has no tokens")
    queue = tables.Queue.query.filter_by(player_id=player.player_id).first()
    if queue and queue.division_machine_id == division_machine.division_machine_id:
        return jsonify({'data': queue.to_dict_simple()})
    players_to_alert = []

    with db.session.no_autoflush:
        try:
            queues_to_lock = tables.Queue.query.with_for_update().filter_by(
                division_machine_id=division_machine.division_machine_id).all(
                )
            if queue:
                queues_to_lock = tables.Queue.query.with_for_update(
                ).filter_by(division_machine_id=queue.division_machine.
                            division_machine_id).all()
                players_to_alert = get_player_list_to_notify(
                    player.player_id, queue.division_machine)
            else:
                players_to_alert = []
            removed_queue = remove_player_from_queue(current_app,
                                                     player,
                                                     commit=False)
            if removed_queue is not None and removed_queue is not False and len(
                    players_to_alert) > 0:
                push_notification_message = "The queue for %s has changed!  Please check the queue to see your new position." % queue.division_machine.machine.machine_name
                send_push_notification(push_notification_message,
                                       players=players_to_alert)
            new_queue = create_queue(current_app,
                                     queue_data['division_machine_id'],
                                     queue_data['other_player_id'])
            queuer = get_username_or_player_name(current_user.user_id)
            create_audit_log_ex(
                current_app,
                "Player added other player to queue",
                user_id=current_user.user_id,
                player_id=player.player_id,
                division_machine_id=division_machine.division_machine_id,
                description="Player added to queue for %s by %s" %
                (division_machine.machine.machine_name, queuer),
                commit=False)
            db.session.commit()

            return jsonify({'data': new_queue.to_dict_simple()})
            #return jsonify({'data':None})
        except Exception as e:
            db.session.commit()
            print "poop %s" % e
            raise e