Пример #1
0
def choose_current_weather():
    """
    Picks a new emit for the current weather conditions, and locks it in.
    :return: The emit to use.
    """

    weather_type = get_weather_type()
    weather_intensity = get_weather_intensity()

    emit = pick_emit(weather_type, intensity=weather_intensity)
    while not emit:
        # Just in case there's no available weather for a given
        # target intensity of during our current season/time;
        # we'll advance the weather until we do have something.
        season, time = gametime.get_time_and_season()
        logger.log_err(
            "Weather: No available weather for type {} intensity {} during {} {}".format(
                weather_type, weather_intensity, season, time
            )
        )
        weather_type, weather_intensity = advance_weather()
        emit = pick_emit(weather_type, intensity=weather_intensity)

    ServerConfig.objects.conf(key="weather_last_emit", value=emit)
    return emit
Пример #2
0
def advance_weather():
    """
    Advances the weather by one 'step', towards our target weather and intensity.
    If we have met our target, pick a new one for the next run.
    :return: Current weather ID as an integer, current weather intensity as an integer
    """
    if ServerConfig.objects.conf('weather_locked', default=False):
        return get_weather_type(), get_weather_intensity()

    target_weather = ServerConfig.objects.conf('weather_type_target',
                                               default=None)
    target_intensity = ServerConfig.objects.conf('weather_intensity_target',
                                                 default=None)

    season, time = gametime.get_time_and_season()

    if not target_weather:
        new_weather = random_weather(season=season)
        target_weather = new_weather.id
        set_weather_type(target_weather)

    if not target_intensity:
        target_intensity = randint(1, 10)
        set_weather_intensity(target_intensity)

    current_weather = ServerConfig.objects.conf('weather_type_current',
                                                default=1)
    current_intensity = ServerConfig.objects.conf('weather_intensity_current',
                                                  default=1)

    if current_weather != target_weather:
        current_intensity -= randint(1, 6)
        if current_intensity <= 0:
            current_intensity = abs(current_intensity)
            if current_intensity == 0:
                current_intensity = 1
            current_weather = target_weather
    else:
        if current_intensity < target_intensity:
            current_intensity += randint(1, 4)
            if current_intensity > target_intensity:
                current_intensity = target_intensity
        else:
            current_intensity -= randint(1, 4)
            if current_intensity < target_intensity:
                current_intensity = target_intensity

    set_weather_type(current_weather)
    set_weather_intensity(current_intensity)

    if current_weather == target_weather and current_intensity == target_intensity:
        # We hit our target.  Let's pick a new one and return.
        new_weather = random_weather(season=season)
        target_weather = new_weather.id
        set_weather_target_type(target_weather)
        target_intensity = randint(1, 10)
        set_weather_target_intensity(target_intensity)

    return current_weather, current_intensity
Пример #3
0
def weather_emits(weathertype, season=None, time=None, intensity=5):
    """
    Return all emits matching the given values.
    :param weathertype: The type of weather to use, a WeatherType object
    :param season: The season (summer, spring, autumn, winter)
    :param time: The time (morning, afternoon, evening, night)
    :param intensity: The intensity of weather to pick an emit for, from 1 to 10
    :return: A QuerySet of matching WeatherEmit objects
    """
    if not season:
        season, _ = gametime.get_time_and_season()
    season = season.lower()

    if not time:
        _, time = gametime.get_time_and_season()
    time = time.lower()

    qs = WeatherEmit.objects.filter(weather=weathertype)
    qs = qs.filter(intensity_min__lte=intensity, intensity_max__gte=intensity)
    if season == 'spring':
        qs = qs.filter(in_spring=True)
    elif season == 'summer':
        qs = qs.filter(in_summer=True)
    elif season == 'autumn' or season == 'fall':
        qs = qs.filter(in_fall=True)
    elif season == 'winter':
        qs = qs.filter(in_winter=True)

    if time == 'night':
        qs = qs.filter(at_night=True)
    elif time == 'morning':
        qs = qs.filter(at_morning=True)
    elif time == 'afternoon':
        qs = qs.filter(at_afternoon=True)
    elif time == 'evening':
        qs = qs.filter(at_evening=True)

    return qs
Пример #4
0
 def get_time_and_season(self):
     """
     Calculate the current time and season ids.
     """
     return gametime.get_time_and_season()