示例#1
0
def CreateMatchDetails(matchid, json_data=None):
    """This creates a MatchDetails object by matchid. 
    If a MatchDetails object already exists, it is deleted and recreated.
    WARNING: This method is volatile, and will always delete a conflicting duplicate match. See ``GetMatchDetails`` for a non-volatile lookup method.
    
    Args:
        matchid (int): Valid MatchID to parse.
        json_data (dict): Instead of asking WebAPI for json, you can provide your own. 
        
    Returns:
        The newly created MatchDetails object.
    
    """
    bulk_create = []
    account_list = []
    match_details = None
    MatchDetails.objects.filter(
        match_id=matchid).all().delete()  # Delete all previous MatchDetails.
    try:
        if not json_data:
            try:
                json_data = GetMatchDetailsJson(matchid)
            except urllib2.HTTPError, e:
                if e.code == 401:  # Unauthorized to view lobby. Return None
                    MatchHistoryQueue.objects.filter(
                        match_id=matchid).all().delete()  # Remove from queue.
                    transaction.commit(
                    )  # Make sure the deletion goes through before raising error.
                    raise SteamAPIError("This lobby is password protected.")
                else:
                    raise
        json_player_data = json_data['players']
        match_details = MatchDetails.from_json_response(json_data)
        match_details.save()
        json_picks_bans_data = json_data.get('picks_bans', False)
        if json_picks_bans_data:
            picks_bans_bulk_create = []
            for json_picks_bans in json_picks_bans_data:
                picks_bans_bulk_create.append(
                    MatchPicksBans.from_json_response(match_details,
                                                      json_picks_bans))
            MatchPicksBans.objects.bulk_create(picks_bans_bulk_create)
        for json_player in json_player_data:
            bulk_player = MatchDetailsPlayerEntry.from_json_response(
                match_details, json_player)
            if bulk_player:
                bulk_create.append(bulk_player)
                account_list.append(
                    convertAccountNumbertoSteam64(
                        json_player.get('account_id', None)))
        GetPlayerNames(
            account_list
        )  # Loads accounts into db for FK constraints. TODO: Re-work me? Disable FK constraints entirely?
        if match_details != None and len(bulk_create) > 0:
            match_details.matchdetailsplayerentry_set.bulk_create(bulk_create)
        MatchHistoryQueue.objects.filter(match_id=matchid).all().delete()
        transaction.commit()
示例#2
0
文件: tasks.py 项目: Lrouger/opendota
def poll_match_history_queue():
    """Celery task that handles the constant background loading of matches.
    
    This task will first empty the MatchHistoryQueue, or look for more matches if nothing in queue.
    
    If there is no work at all, it will refresh old MatchDetails according to staleness.
    
    Returns True if work was handled; False if there was an error; None if no work to be done.
    """
    lock_id = "poll_match_history_queue_lock"
    success_value = True
    acquire_lock = lambda: cache.add(lock_id, "true", LOCK_EXPIRE)
    release_lock = lambda: cache.delete(lock_id)

    if acquire_lock():
        logger.debug("Queue locked.")
        queue_object = None
        force_refresh = False
        try:
            if cache.get(lock_id + '_time') == None:
                GetMatchHistory()
                logger.debug("Ran out of work. Attempting more from history..")
                cache.set(lock_id + '_time', True, LOCK_EXPIRE)
            else:
                try:
                    queue_object = MatchHistoryQueue.objects.latest()
                    logger.debug("Got work from MatchHistoryQueue")
                except ObjectDoesNotExist:
                    queue_object = None
                if queue_object == None:
                    queue_object = MatchDetails.get_refresh()
                    if queue_object:
                        force_refresh = True
                        logger.debug("Got work from stale MatchDetails.")
            if queue_object:
                logger.debug("Attempting to retreive match_id: " +
                             str(queue_object.pk))
                GetMatchDetails(queue_object.pk, force_refresh=force_refresh)
                logger.debug("Retreived and set match_id: " +
                             str(queue_object.pk))
            else:
                logger.debug("No work to be done. Sleeping.")
                success_value = None
        except Exception, e:
            success_value = False
            logger.error(traceback.format_exc())
            logger.error("Error creating object.")
        finally:
示例#3
0
def CreateMatchDetails(matchid, json_data=None):
    """This creates a MatchDetails object by matchid. 
    If a MatchDetails object already exists, it is deleted and recreated.
    WARNING: This method is volatile, and will always delete a conflicting duplicate match. See ``GetMatchDetails`` for a non-volatile lookup method.
    
    Args:
        matchid (int): Valid MatchID to parse.
        json_data (dict): Instead of asking WebAPI for json, you can provide your own. 
        
    Returns:
        The newly created MatchDetails object.
    
    """
    bulk_create = []
    account_list = []
    match_details = None
    MatchDetails.objects.filter(match_id=matchid).all().delete() # Delete all previous MatchDetails.
    try:
        if not json_data:
            try:
                json_data = GetMatchDetailsJson(matchid)
            except urllib2.HTTPError, e:
                if e.code == 401: # Unauthorized to view lobby. Return None
                    MatchHistoryQueue.objects.filter(match_id=matchid).all().delete() # Remove from queue.
                    transaction.commit() # Make sure the deletion goes through before raising error.
                    raise SteamAPIError("This lobby is password protected.")
                else:
                    raise
        json_player_data = json_data['players']
        match_details = MatchDetails.from_json_response(json_data)
        match_details.save()
        json_picks_bans_data = json_data.get('picks_bans', False)
        if json_picks_bans_data:
            picks_bans_bulk_create = []
            for json_picks_bans in json_picks_bans_data:
                picks_bans_bulk_create.append(MatchPicksBans.from_json_response(match_details, json_picks_bans))
            MatchPicksBans.objects.bulk_create(picks_bans_bulk_create)
        for json_player in json_player_data:
            bulk_player = MatchDetailsPlayerEntry.from_json_response(match_details, json_player)
            if bulk_player:
                bulk_create.append(bulk_player)
                account_list.append(convertAccountNumbertoSteam64(json_player.get('account_id', None)))
        GetPlayerNames(account_list) # Loads accounts into db for FK constraints. TODO: Re-work me? Disable FK constraints entirely?
        if match_details != None and len(bulk_create) > 0: 
            match_details.matchdetailsplayerentry_set.bulk_create(bulk_create)
        MatchHistoryQueue.objects.filter(match_id=matchid).all().delete()
        transaction.commit()
示例#4
0
def poll_match_history_queue():
    """Celery task that handles the constant background loading of matches.
    
    This task will first empty the MatchHistoryQueue, or look for more matches if nothing in queue.
    
    If there is no work at all, it will refresh old MatchDetails according to staleness.
    
    Returns True if work was handled; False if there was an error; None if no work to be done.
    """
    lock_id = "poll_match_history_queue_lock"
    success_value = True
    acquire_lock = lambda: cache.add(lock_id, "true", LOCK_EXPIRE)
    release_lock = lambda: cache.delete(lock_id)
    
    if acquire_lock():
        logger.debug("Queue locked.")
        queue_object = None
        force_refresh = False
        try:
            if cache.get(lock_id + '_time') == None:
                GetMatchHistory()
                logger.debug("Ran out of work. Attempting more from history..")
                cache.set(lock_id + '_time', True, LOCK_EXPIRE)
            else:
                try:
                    queue_object = MatchHistoryQueue.objects.latest()
                    logger.debug("Got work from MatchHistoryQueue")
                except ObjectDoesNotExist:
                    queue_object = None
                if queue_object == None:
                        queue_object = MatchDetails.get_refresh()
                        if queue_object:
                            force_refresh = True
                            logger.debug("Got work from stale MatchDetails.")
            if queue_object:
                logger.debug("Attempting to retreive match_id: " + str(queue_object.pk))
                GetMatchDetails(queue_object.pk, force_refresh=force_refresh)
                logger.debug("Retreived and set match_id: " + str(queue_object.pk))
            else:
                logger.debug("No work to be done. Sleeping.")
                success_value = None
        except Exception, e:
            success_value = False
            logger.error(traceback.format_exc())
            logger.error("Error creating object.")
        finally:
示例#5
0
def GetLatestMatches():
    """Returns the last 500 matches (sorted by match_seq_num) that were parsed into MatchDetails. 
    """
    return MatchDetails.exclude_low_priority().filter(
        lobby_type=0).order_by('-match_seq_num')[:500]
示例#6
0
def GetLatestMatches():
    """Returns the last 500 matches (sorted by match_seq_num) that were parsed into MatchDetails. 
    """
    return MatchDetails.exclude_low_priority().filter(lobby_type=0).order_by('-match_seq_num')[:500]