From b07aae3d6e59cf2d6007485d34004076eec60e19 Mon Sep 17 00:00:00 2001 From: Raffson Date: Mon, 20 May 2024 15:13:21 +0200 Subject: [PATCH] Allow [start, end] times for Night to traverse midnight Daytime mapping entries like [23, 2] were causing trouble, for which the following fix was implemented... --- game/weather/conditions.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/game/weather/conditions.py b/game/weather/conditions.py index dad1de78..dfcb32ff 100644 --- a/game/weather/conditions.py +++ b/game/weather/conditions.py @@ -4,6 +4,7 @@ import datetime import logging import random from dataclasses import dataclass +from typing import Tuple from game.settings import Settings, NightMissions from game.theater import ConflictTheater, SeasonalConditions @@ -74,11 +75,25 @@ class Conditions: # time constrained to that. DaytimeMap enforces that we have only whole hour # ranges for now, so we don't need to worry about accidentally changing the time # of day by truncating sub-hours. - time = datetime.time( - hour=random.randint(time_range[0].hour, time_range[1].hour) - ) + day, hours = Conditions.random_time_progression(day, time_range) + time = datetime.time(hour=hours) return datetime.datetime.combine(day, time) + @staticmethod + def random_time_progression( + day: datetime.date, time_range: Tuple[datetime.time, datetime.time] + ) -> Tuple[datetime.date, int]: + start, end = time_range[0].hour, time_range[1].hour + if start > end: + end += 24 + hours = random.randint(start, end) + if hours > 23: + day += datetime.timedelta(days=1.0) + hours %= 24 + else: + hours = random.randint(start, end) + return day, hours + @classmethod def generate_weather( cls,