Skip to content

Commit

Permalink
Updated vector and validation (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanAEckelberg authored Jan 28, 2024
1 parent c66ff3c commit c1058a1
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 58 deletions.
2 changes: 1 addition & 1 deletion game/controllers/movement_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def handle_actions(self, action: ActionType, client: Player, world: GameBoard):
case _: # default case
return

temp_vec: Vector = Vector.add_vectors(avatar_pos, pos_mod)
temp_vec: Vector = avatar_pos.add_to_vector(pos_mod)
# if tile is occupied return
temp: Tile = world.game_map[temp_vec.y][temp_vec.x]
while hasattr(temp.occupied_by, 'occupied_by'):
Expand Down
2 changes: 1 addition & 1 deletion game/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def tick(self):
# If thread is no longer alive, mark it as non-functional, preventing it from receiving future turns
if thr.is_alive():
client.functional = False
client.error = TimeoutError(f'{client.id} failed to reply in time and has been dropped.')
client.error = f'{client.id} failed to reply in time and has been dropped.'
print(client.error)

# Also check to see if the client had created an error and save it
Expand Down
25 changes: 9 additions & 16 deletions game/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
from game.config import ALLOWED_MODULES


def verify_code(filename, already_string=False):
def verify_code(filename: str) -> ([], bool, bool):
"""
This file is used to verify a client's code. It helps to prevent certain attempts at purposefully interfering with the
code and competition.
"""
contents = None
with open(filename, 'r') as f:
contents = f.read()

if already_string:
contents = filename
else:
with open(filename, 'r') as f:
contents = f.read()

contents = contents.split('\n')
contents = contents.splitlines()

illegal_imports = list()
uses_open = False
Expand All @@ -30,25 +25,23 @@ def verify_code(filename, already_string=False):
break

# Check for illegal keywords
if 'from' in token:
if 'from' == token:
module = line[line.index('from') + 1]
if module not in ALLOWED_MODULES:
illegal_imports.append(module)
else:
break
elif 'import' in token:
elif 'import' == token:
module = line[line.index('import') + 1]
if module not in ALLOWED_MODULES:
illegal_imports.append(module)
else:
break
if 'open' in token:
uses_open = True
uses_open = 'open(' in token

if 'print' in token:
uses_print = True
uses_print = 'print(' in token

return (illegal_imports, uses_open, uses_print)
return illegal_imports, uses_open, uses_print

def verify_num_clients(clients, set_clients, min_clients, max_clients):
res = None
Expand Down
33 changes: 18 additions & 15 deletions game/utils/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,31 @@ def y(self, y: int) -> None:
self.__y = y

@staticmethod
def add_vectors(vector_1: 'Vector', vector_2: 'Vector') -> 'Vector':
new_x: int = vector_1.x + vector_2.x
new_y: int = vector_1.y + vector_2.y
return Vector(new_x, new_y)
def from_xy_tuple(xy_tuple: Tuple[int, int]) -> 'Vector':
return Vector(*xy_tuple)

def add_to_vector(self, other_vector: Self) -> None:
self.x += other_vector.x
self.y += other_vector.y
@staticmethod
def from_yx_tuple(yx_tuple: Tuple[int, int]) -> 'Vector':
return Vector(*yx_tuple[::-1])

def add_to_vector(self, other_vector: Self) -> 'Vector':
return Vector(
self.x + other_vector.x,
self.y + other_vector.y
)

def add_x_y(self, x: int, y: int) -> None:
self.x += x
self.y += y
def add_x_y(self, x: int, y: int) -> 'Vector':
return self.add_to_vector(Vector(x, y))

def add_x(self, x: int) -> None:
self.x += x
def add_x(self, x: int) -> 'Vector':
return self.add_to_vector(Vector(x))

def add_y(self, y: int) -> None:
self.y += y
def add_y(self, y: int) -> 'Vector':
return self.add_to_vector(Vector(y=y))

def as_tuple(self) -> Tuple[int, int]:
"""Returns (x: int, y: int)"""
return (self.x, self.y)
return self.x, self.y

def to_json(self) -> dict:
data = super().to_json()
Expand Down
2 changes: 1 addition & 1 deletion visualizer/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def render(self) -> None:
:return: None
"""
text = Text(self.screen, f'{self.turn_number} / {self.turn_max}', 48)
text.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().midtop), Vector(0, 50)).as_tuple()
text.rect.center = Vector(*self.screen.get_rect().midtop).add_y(50).as_tuple()
text.render()
self.playback.playback_render()

Expand Down
9 changes: 3 additions & 6 deletions visualizer/templates/menu_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ def __init__(self, screen: pygame.Surface):
self.results_button: Button = Button(screen, 'Exit', lambda: False, font_size=24, padding=10)

# the next two variables shouldn't be type hinted. The center is a tuple of two ints (i.e., tuple[int, int])
self.start_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(0, 100)).as_tuple()
self.start_button.rect.center = Vector(*self.screen.get_rect().center).add_y(100).as_tuple()

self.results_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(0, 100)).as_tuple()
self.results_button.rect.center = Vector(*self.screen.get_rect().center).add_y(100).as_tuple()

def start_events(self, event: pygame.event) -> Any:
"""
Expand Down Expand Up @@ -87,8 +85,7 @@ class Basic(MenuTemplate):
def __init__(self, screen: pygame.Surface, title: str):
super().__init__(screen)
self.title: Text = Text(screen, title, 48) # creates the title using the Text class; refer to text.py
self.title.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center), # the center of the title
Vector(0, -100)).as_tuple()
self.title.rect.center = Vector(*self.screen.get_rect().center).add_y(-100).as_tuple()

self.winning_team_name: Text = Text(screen, '', 0) # name is determined by endgame results

Expand Down
27 changes: 9 additions & 18 deletions visualizer/templates/playback_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,15 @@ def __init__(self, screen: pygame.Surface):
self.fastest_speed_button: Button = Button(self.screen, '4x', lambda: PlaybackButtons.FASTEST_SPEED_BUTTON,
font_size=18)

self.prev_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(-80, 225)).as_tuple()
self.pause_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(0, 225)).as_tuple()
self.next_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(80, 225)).as_tuple()
self.start_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(-80, 275)).as_tuple()
self.save_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(0, 275)).as_tuple()
self.end_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(80, 275)).as_tuple()
self.normal_speed_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(-80, 325)).as_tuple()
self.fast_speed_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(0, 325)).as_tuple()
self.fastest_speed_button.rect.center = Vector.add_vectors(Vector(*self.screen.get_rect().center),
Vector(80, 325)).as_tuple()
self.prev_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(-80, 225).as_tuple()
self.pause_button.rect.center = Vector(*self.screen.get_rect().center).add_y(225).as_tuple()
self.next_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(80, 225).as_tuple()
self.start_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(-80, 275).as_tuple()
self.save_button.rect.center = Vector(*self.screen.get_rect().center).add_y(275).as_tuple()
self.end_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(80, 275).as_tuple()
self.normal_speed_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(-80, 325).as_tuple()
self.fast_speed_button.rect.center = Vector(*self.screen.get_rect().center).add_y(325).as_tuple()
self.fastest_speed_button.rect.center = Vector(*self.screen.get_rect().center).add_x_y(80, 325).as_tuple()

def playback_render(self) -> None:
"""
Expand Down

0 comments on commit c1058a1

Please sign in to comment.