Skip to content

Commit

Permalink
Bot updates for FS25.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatkot committed Nov 12, 2024
1 parent 3ccf8fb commit 38edfea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
47 changes: 39 additions & 8 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
logger = mfs.Logger(__name__, level="DEBUG")

# region constants
GAME_CODES = {
"FS22": "Farming Simulator 22",
"FS25": "Farming Simulator 25",
}
MAP_SIZES = ["2048", "4096", "8192", "16384"]
MAX_HEIGHTS = {
"100": "🍀 For flatlands",
Expand All @@ -29,7 +33,6 @@

# region directories
working_directory = os.getcwd()
map_template = os.path.join(working_directory, "data", "map-template.zip")
maps_directory = os.path.join(working_directory, "maps")
archives_directory = os.path.join(working_directory, "archives")
stats_directory = os.path.join(working_directory, "stats")
Expand Down Expand Up @@ -77,12 +80,13 @@ class Session:
dem_settings (generate.DemSettings): DEM settings.
"""

def __init__(self, telegram_id: int, coordinates: tuple[float, float]):
def __init__(self, telegram_id: int, game_code: str):
self.telegram_id = telegram_id
self.timestamp = int(datetime.now().timestamp())
self.name = f"{self.telegram_id}_{self.timestamp}"
self.map_directory = os.path.join(maps_directory, self.name)
self.coordinates = coordinates
self.game = mfs.Game.from_code(game_code)
self.coordinates = None
self.distance = None
self.max_height = None

Expand All @@ -93,17 +97,17 @@ def run(self) -> tuple[str, str]:
tuple[str, str]: Paths to the preview and the archive.
"""
mp = mfs.Map(
self.game,
self.coordinates,
self.distance,
self.map_directory,
blur_seed=5,
max_height=self.max_height,
map_template=map_template,
logger=logger,
)
mp.generate()
# preview_path = mp.previews()[0]
preview_path = None # Disabled to avoid Docker 137 error (OUT OF MEMORY).
preview_path = None # Disabled to avoid Docker 137 error (OUT OF MEMORY).
archive_path = mp.pack(os.path.join(archives_directory, self.name))
return preview_path, archive_path

Expand Down Expand Up @@ -198,13 +202,38 @@ async def button_coffee(message: types.Message) -> None:

@dp.message_handler(Text(equals=Buttons.GENERATE.value))
async def button_generate(message: types.Message) -> None:
"""Handles the Generate button, registers the coordinates handler.
"""Handles the Generate button, creates inline buttons for the game selection.
Args:
message (types.Message): Message, which triggered the handler.
"""
await log_event(message)

buttons = {}
for game_code, game_name in GAME_CODES.items():
buttons[f"game_code_{game_code}"] = game_name

await bot.send_message(
message.from_user.id,
Messages.CHOOSE_GAME.value,
reply_markup=await keyboard(buttons),
)


@dp.callback_query_handler(text_contains="game_code_")
async def game_choose(message: types.Message) -> None:
"""Handles the catch of the game selection, registers the coordinates handler.
Args:
message (types.Message): Message, which triggered the handler.
"""
await log_event(message)

game_code = message.data.rsplit("_", 1)[-1]

session = Session(message.from_user.id, game_code)
sessions[message.from_user.id] = session

dp.register_message_handler(coordinates)

await bot.send_message(
Expand Down Expand Up @@ -263,8 +292,10 @@ async def coordinates(message: types.Message) -> None:
)
return

telegram_id = message.from_user.id
sessions[telegram_id] = Session(telegram_id, (latitude, longitude))
session = sessions.get(message.from_user.id)
if not session:
return
session.coordinates = (latitude, longitude)

sizes = MAP_SIZES
indicators = ["🟢", "🟢", "🟡", "🔴"]
Expand Down
2 changes: 2 additions & 0 deletions bot/bot_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Messages(Enum):

CANCELLED = "The operation has been cancelled."

CHOOSE_GAME = "Choose the game for which you want to generate a map."

ENTER_COORDINATES = (
"Enter the coordinates of the center of the map\."
"The coordinates are latitude and longitude separated by a comma\.\n\n"
Expand Down

0 comments on commit 38edfea

Please sign in to comment.