Advertisement
Guest User

game_calendar.py

a guest
Feb 12th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. import datetime
  2.  
  3. def ordinal(n: int) -> str:
  4.     # ...existing code...
  5.     if 10 <= n % 100 <= 20:
  6.         suffix = "th"
  7.     else:
  8.         suffix = {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")
  9.     return f"{n}{suffix}"
  10.  
  11. class GameCalendar:
  12.     """
  13.    Manage the game calendar and date operations.
  14.    """
  15.     def __init__(self, start_year: int = 1950) -> None:
  16.         self.base_date = datetime.date(start_year, 1, 1)
  17.         self.current_date = self.base_date
  18.         self.recruited_today: bool = False
  19.  
  20.     @property
  21.     def day(self) -> str:
  22.         return self.current_date.strftime("%A")
  23.  
  24.     @property
  25.     def date(self) -> int:
  26.         return self.current_date.day
  27.  
  28.     @property
  29.     def month(self) -> str:
  30.         return self.current_date.strftime("%B")
  31.  
  32.     @property
  33.     def year(self) -> int:
  34.         return self.current_date.year
  35.  
  36.     @property
  37.     def ordinal_date(self) -> str:
  38.         return ordinal(self.current_date.day)
  39.  
  40.     @property
  41.     def day_of_week(self) -> str:
  42.         return self.current_date.strftime("%A")
  43.  
  44.     @property
  45.     def date_str(self) -> str:
  46.         return f"{self.current_date.strftime('%A')}, {self.ordinal_date} {self.current_date.strftime('%B %Y')}"
  47.    
  48.     @property
  49.     def numeric_date(self) -> str:
  50.         """
  51.        Return the numeric date as ddmmyyyy.
  52.        For example: 4th January 1950 -> "04011950"
  53.        """
  54.         return self.current_date.strftime("%d%m%Y")
  55.  
  56.     @property
  57.     def total_days(self) -> int:
  58.         return (self.current_date - self.base_date).days
  59.  
  60.     def get_date_from_total_days(self, total_days: int) -> str:
  61.         new_date = self.base_date + datetime.timedelta(days=total_days)
  62.         return f"{new_date.strftime('%A')}, {ordinal(new_date.day)} {new_date.strftime('%B %Y')}"
  63.  
  64.     def next_day(self) -> None:
  65.         self.current_date += datetime.timedelta(days=1)
  66.         self.recruited_today = False  
  67.         from pricing import update_daily_prices
  68.         update_daily_prices(self.total_days)
  69.         from deliveries import process_deliveries
  70.         process_deliveries()
  71.  
  72.     def reset_game_state(self, loaded_game=None) -> None:
  73.         # ...existing code...
  74.         if loaded_game:
  75.             day = loaded_game.get("date", self.base_date.day)
  76.             month = loaded_game.get("month", self.base_date.strftime("%B"))
  77.             year = loaded_game.get("year", self.base_date.year)
  78.             try:
  79.                 new_date = datetime.datetime.strptime(f"{day} {month} {year}", "%d %B %Y").date()
  80.             except Exception:
  81.                 new_date = self.base_date
  82.             self.current_date = new_date
  83.             self.recruited_today = loaded_game.get("recruited_today", False)
  84.         else:
  85.             self.current_date = self.base_date
  86.             self.recruited_today = False
  87.  
  88. game_calendar = GameCalendar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement