Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 100
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ jobs:

- name: Run pylint
run: pylint src --rcfile=.pylintrc --fail-under=9.5

- name: Run flake8
run: flake8 src test

- name: Check formatting
run: black --check src test

- name: Run mypy
run: mypy
2 changes: 1 addition & 1 deletion .github/workflows/close_invalid_prs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
if: github.head_ref == 'master'
with:
comment: |
Hi there! :wave: This PR is targeting the `master` branch, which is not allowed. Please target the `develop` branch instead. Thanks!
Hi there! :wave: This PR is targeting the `master` branch, which is not allowed. Please target the `dev` branch instead. Thanks!
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["test"]

[tool.mypy]
mypy_path = "src"
explicit_package_bases = true
files = ["src"]
1 change: 1 addition & 0 deletions src/controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module contains the FastAPI application and the endpoints for the floor generation.
"""

import secrets

from fastapi import FastAPI, Path
Expand Down
11 changes: 5 additions & 6 deletions src/floors/floor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
It also provides methods to save and load the floor as a json file
and contains the seed it was created with.
"""

import json
from typing import List, Tuple, TypeVar, Optional
from typing import List, Tuple, Optional

import numpy as np

Expand All @@ -18,8 +19,6 @@
from rooms.room import Room
from rooms.teleport_room import TeleportRoom

T = TypeVar("T", bound=Room)


class Floor:
"""
Expand All @@ -33,7 +32,7 @@ def __init__(self, height: int, width: int, seed: str):
@param height: height for the floor
@param width: width for the floor
"""
self._rooms: List[T] = []
self._rooms: List[Room] = []
self._floor_grid = np.zeros((height, width))
self._width: int = width
self._height: int = height
Expand Down Expand Up @@ -271,7 +270,7 @@ def has_boos_room_as_neighbour(self, coordinates: Tuple[int, int]) -> bool:
new_coordinates
):
room = self._get_room(new_coordinates)
if room.get_type() == RoomType.BOSS_ROOM:
if room is not None and room.get_type() == RoomType.BOSS_ROOM:
return True
return False

Expand All @@ -295,7 +294,7 @@ def has_special_room_as_neighbour(self, coordinates: Tuple[int, int]) -> bool:
new_coordinates
):
room = self._get_room(new_coordinates)
if room.get_type().is_special():
if room is not None and room.get_type().is_special():
return True
return False

Expand Down
12 changes: 10 additions & 2 deletions src/floors/tkinter_floor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import threading
import time
from typing import List, cast

from floors.floor import Floor
from rooms.room import Room
Expand Down Expand Up @@ -67,13 +68,20 @@ def from_floor(cls, floor: Floor, canvas, name: str):
) # pylint: disable=protected-access
return tkinter_floor

def _tkinter_rooms(self) -> List[TkinterRoom]:
"""
Returns the rooms of the floor, which are all tkinter rooms.
@return: rooms
"""
return cast(List[TkinterRoom], self._rooms)

def draw(self) -> None:
"""
Draws the floor with all the rooms on the canvas.
"""
self.stop_drawing()
self._canvas.delete("all")
for room in self._rooms:
for room in self._tkinter_rooms():
room.draw(self._canvas)

def draw_thread(self) -> None:
Expand All @@ -94,7 +102,7 @@ def draw_step_by_step(self) -> None:
Draws the floor step by step.
"""
self._canvas.delete("all")
sorted_rooms = sorted(self._rooms, key=lambda r: r.get_id())
sorted_rooms = sorted(self._tkinter_rooms(), key=lambda r: r.get_id())
self._is_drawing = True
for room in sorted_rooms:
if not self._is_drawing:
Expand Down
4 changes: 3 additions & 1 deletion src/generators/floor_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import secrets
import tkinter as tk
from collections import deque
from typing import Deque

from utils import globals as my_globals

from floors.tkinter_floor import TkinterFloor
Expand All @@ -20,7 +22,7 @@ def __init__(self, output_file_name: str) -> None:
Creates a new floor manager.
@param output_file_name: name of the output file
"""
self._floors = deque()
self._floors: Deque[TkinterFloor] = deque()
self.current_floor_index = -1
self.current_floor_name = tk.StringVar()
self.current_floor_name.set(output_file_name)
Expand Down
10 changes: 5 additions & 5 deletions src/generators/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import random
from collections import deque
from datetime import datetime
from typing import List, Optional, TypeVar, Tuple
from typing import List, Optional, Tuple

from floors.floor import Floor
from rooms.room import Room
from utils import util_functions, globals as my_globals, room_type
from utils.direction import Direction
from utils.room_type import RoomType

T = TypeVar("T", bound=Floor)


class Generator:
"""
Expand All @@ -45,7 +43,9 @@ def __init__(
self._seed = seed
self._output_file_name = output_file_name
self._output_file_path = output_file_path
self._floor: T = Floor(my_globals.FLOOR_WIDTH, my_globals.FLOOR_HEIGHT, seed)
self._floor: Floor = Floor(
my_globals.FLOOR_WIDTH, my_globals.FLOOR_HEIGHT, seed
)
self._random = random.Random(seed)

def to_json(self, indent: int) -> str:
Expand Down Expand Up @@ -159,7 +159,7 @@ def mark_dead_ends(self) -> list:
Searches for dead ends in the floor and marks them ands returns their indices in a list.
@return: indices of all dead ends
"""
dead_end_indices = []
dead_end_indices: List[int] = []
floor = self._floor
for i in range(len(floor.get_rooms())):
room = floor.get_rooms()[i]
Expand Down
2 changes: 2 additions & 0 deletions src/generators/tkinter_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TkinterGenerator(Generator):
Tkinter version of the generator.
"""

_floor: TkinterFloor

def __init__(
self, seed: str, output_file_name: str, output_file_path: str, stage_id: int = 2
):
Expand Down
5 changes: 2 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ def main(seed: str, output: str, ui: bool, floor_id):
print("No output folder found. Creating specific folder")

if ui:
generator = TkinterGenerator(
TkinterGenerator(
seed, my_globals.DEFAULT_FLOOR_NAME, output_folder, floor_id
)
generator.run()
).run()
else:
generator = Generator(seed=seed, output_file_name=output, stage_id=floor_id)
generator.generate()
Expand Down
7 changes: 4 additions & 3 deletions src/rooms/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
A room has a type, coordinates and doors.
"""

from typing import List

from utils import globals as my_globals
from utils.globals import DoorFace, MAX_DOOR_AMOUNT
from utils.room_type import RoomType
Expand All @@ -27,7 +29,7 @@ def __init__(self, x: int, y: int, room_id: int, room_type: RoomType):
self._y: int = y
self._id: int = room_id
self._type = room_type
self._doors = []
self._doors: List[DoorFace] = []

def __getitem__(self, index: int) -> int:
"""
Expand All @@ -40,8 +42,7 @@ def __getitem__(self, index: int) -> int:
raise ValueError(str(index) + "is not a valid index")
if index == 0:
return self._x
if index == 1:
return self._y
return self._y

def to_json(self, indent: int) -> str:
"""
Expand Down
1 change: 1 addition & 0 deletions src/rooms/teleport_room.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Teleport room module.
"""

from rooms.room import Room
from utils.room_type import RoomType

Expand Down
2 changes: 1 addition & 1 deletion src/rooms/tkinter/legend_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import tkinter as tk

import src.utils.globals as my_globals
import utils.globals as my_globals
from utils import util_functions
from utils.room_type import RoomType, room_colors, room_names

Expand Down
1 change: 1 addition & 0 deletions src/rooms/tkinter/tkinter_room.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module for TkinterRoom class.
"""

import tkinter as tk
from rooms.room import Room
from utils import util_functions, globals as my_globals, room_type
Expand Down
1 change: 1 addition & 0 deletions src/utils/globals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This file contains all the global constants used in the project.
"""

import os
from enum import Enum

Expand Down
31 changes: 6 additions & 25 deletions src/utils/util_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module contains utility functions for the floor generator.
"""

import os
import random
import math
Expand Down Expand Up @@ -28,11 +29,7 @@ def get_picture_for_room_type(room_type: RoomType):
# Join the project root directory with the relative path to get the absolute path
image_path = os.path.join(project_root, "resources", "room_icons", image_file)
try:
return ImageTk.PhotoImage(
Image.open(
image_path
).resize((32, 32))
)
return ImageTk.PhotoImage(Image.open(image_path).resize((32, 32)))
except FileNotFoundError:
return None

Expand Down Expand Up @@ -67,32 +64,16 @@ def rgb2hex(r, g, b):


def add_direction_to_coordinates(
direction: Direction, coordinates: Tuple[int, int]
direction: Direction, coordinates: Tuple[int, int]
) -> Tuple[int, int]:
"""
Calculates the room according to the given room and direction
@param direction: direction for the room to calculate
@param coordinates: start point for the calculation
@return: calculated point
"""
result = None
if direction is Direction.UP:
result = coordinates[0], coordinates[1] - 1
if direction is Direction.DOWN:
result = coordinates[0], coordinates[1] + 1
if direction is Direction.LEFT:
result = coordinates[0] - 1, coordinates[1]
if direction is Direction.RIGHT:
result = coordinates[0] + 1, coordinates[1]
if direction is Direction.UP_LEFT:
result = coordinates[0] - 1, coordinates[1] - 1
if direction is Direction.UP_RIGHT:
result = coordinates[0] + 1, coordinates[1] - 1
if direction is Direction.DOWN_LEFT:
result = coordinates[0] - 1, coordinates[1] + 1
if direction is Direction.DOWN_RIGHT:
result = coordinates[0] + 1, coordinates[1] + 1
return result
delta_x, delta_y = direction.value
return coordinates[0] + delta_x, coordinates[1] + delta_y


json_file_options: dict = {
Expand All @@ -112,5 +93,5 @@ def calculate_distance(cord1, cord2) -> int:
Calculates the distance between two points.
"""
return (cord1[0] - cord2[0]) * (cord1[0] - cord2[0]) + (cord1[1] - cord2[1]) * (
cord1[1] - cord2[1]
cord1[1] - cord2[1]
)
13 changes: 10 additions & 3 deletions test/generator_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This file contains the test cases for the generator class.
"""

import json
import os
import random
Expand Down Expand Up @@ -33,14 +34,18 @@ def _reachable_coordinates(floor):
while queue:
current = queue.popleft()
neighbour_coordinates = [
(current[0] + dx, current[1] + dy) for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1))
(current[0] + dx, current[1] + dy)
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1))
]
if isinstance(current, TeleportRoom):
connected_room = rooms_by_id[current.get_connected_room_id()]
neighbour_coordinates.append((connected_room[0], connected_room[1]))
for room in rooms:
room_coordinates = (room[0], room[1])
if room_coordinates in neighbour_coordinates and room_coordinates not in seen:
if (
room_coordinates in neighbour_coordinates
and room_coordinates not in seen
):
seen.add(room_coordinates)
queue.append(room)
return seen, coordinates
Expand Down Expand Up @@ -124,7 +129,9 @@ def test_add_special_rooms_assigns_item_and_shop_room(self):
self.assertEqual(RoomType.ITEM_ROOM, floor.get_rooms()[1].get_type())
self.assertEqual(RoomType.SHOP_ROOM, floor.get_rooms()[2].get_type())

def test_add_special_rooms_stops_when_there_are_fewer_dead_ends_than_special_rooms(self):
def test_add_special_rooms_stops_when_there_are_fewer_dead_ends_than_special_rooms(
self,
):
floor = self._generator._floor
floor.add_room(2, 2, RoomType.START_ROOM)
floor.add_room(1, 2, RoomType.DEAD_END)
Expand Down
2 changes: 1 addition & 1 deletion test/room_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_set_cord(self):

def test_get_cord_with_exception(self):
with self.assertRaises(ValueError, msg="20 is not a valid index"):
var = self._room[20]
_ = self._room[20]


if __name__ == "__main__":
Expand Down
Loading