Exemple #1
0
def get_season_based_on_fetch_time_and_match(fetch_time, match):
    """
    Mach is a really special snowflake, it can return curr when it is prev right after season switch but never prev
    instead of curr (since if a new ladder is referred the player_ladder is up to date). This code will trust that
    prev/curr is correct and let code outside make a sanity check and keep it as nyd if this fails.

    Returns the ladder's season based on fetch_time and match.
    """

    if match not in ('curr', 'prev'):
        raise Exception("match '%s' unexpected" % match)

    fetch_season = Season.get_active(fetch_time)

    if fetch_season.near_start(fetch_time):
        # May be season before if another region caused the season switch.
        fetch_valid = -1

    elif fetch_season.is_open() or fetch_season.near_end(fetch_time):
        # We may actually be in the next season.
        fetch_valid = 1
    else:

        # We are mid season so it is correct.
        fetch_valid = 0

    if match == 'prev':
        fetch_season = fetch_season.get_prev()

    return fetch_season, fetch_valid
Exemple #2
0
def get_season_based_on_join_times(first_join, last_join):
    """
    Get season based off first and last join times only.

    Return <season, value>, valid will be 0 if sure, -1 if could be season before +1 if could be season after (if
    both joins are to close to boundary).

    Season boundaries will not be exact (also join time is in unknown time zone), since we switch autimatically they
    will be on the date after switch or event two days after because of different time zones. Also ladders have been
    created well before season start some time.

    If both times are near a boundary the last_join will be used against the stored value on the season (this will make
    it more likely to fall in the first season). If there is player ladder data it will sort this out, if there is not
    it may stay wrong.

    NOTE: Join times can be off by serveral days, like 3-4 days. If both joins are too close to the same season end
    valid will be -1 or +1 and join season could be off by one season.

    """

    context = "first_join %s, last_join %s" % (first_join, last_join)

    if last_join < first_join:
        raise Exception("%s, last_join is before first_join, this is strange" %
                        context)

    first_season = Season.get_active(first_join)
    last_season = Season.get_active(last_join)

    context += ", first_season %s, last_season %d" % (first_season.id,
                                                      last_season.id)

    if first_season.id == last_season.id:
        # Same season matched, this should be the most common case.

        if first_season.is_open() and not (first_season.near_start(first_join) or last_season.near_start(last_join))\
                or not first_season.is_open() and first_season.near_end(first_join):
            # Compact join period to the end of season, insecure, could be season after.
            return last_season, 1

        if last_season.near_start(last_join):
            # Compact join period to the start of season, insecure, could be season before.
            return last_season, -1

        # One/both join in the middle or first in beginning and last in end, secure.
        return last_season, 0

    if first_season.id + 1 == last_season.id:
        # End points in different seasons, prefer determine by last_join, but it depends on how valid it is.
        if last_season.near_start(last_join):
            # Last end is unsure.
            if first_season.near_end(first_join):
                # First end is also unsure, pick last_season, but may be season before.
                return last_season, -1

            # First end is sure, pick first season.
            return first_season, 0

        if first_season.near_end(first_join):
            # First end is unsure, pick last season.
            return last_season, 0

        # This means both are sure but in different seasons.
        raise CatError(
            "%s, can't determine season this ladder seems to span full seasons"
            % context)

    if first_season.id + 2 == last_season.id \
       and first_season.near_end(first_join) \
       and last_season.near_start(last_join):
        # Both ends are insecure, so it has to be season in the middle.
        return first_season.get_next(), 0

    raise CatError(
        "%s, this must be buggy, seasons wrong order or too far apart" %
        context)