Skip to content

Commit

Permalink
Add a common package
Browse files Browse the repository at this point in the history
Signed-off-by: yzamir <kobi.zamir@gmail.com>
  • Loading branch information
yaacov committed Sep 12, 2023
1 parent d41e363 commit 1b8a343
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/random-driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
very good driver but the implementation is very elegant.
"""
import random
from rose.client.game import obstacles, actions # NOQA
from rose.common import obstacles, actions # NOQA

driver_name = "Random Driver"

Expand Down
10 changes: 5 additions & 5 deletions rose/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# quay.io/rose/rose-common
# Use Red Hat Universal Base Image (UBI) with Python
FROM registry.access.redhat.com/ubi8/python-38
# with:
# /app/rose/common package
# PYTHONPATH "${PYTHONPATH}:/app"
FROM quay.io/rose/rose-common

# Set the working directory in the Docker container
WORKDIR /app/rose/client

# Copy the local package files to the container's workspace
COPY ./__init__.py /app/rose/__init__.py
COPY . /app/rose/client

# Install the Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Add the rose client package to the python path
ENV PYTHONPATH "${PYTHONPATH}:/app"

# Set the command to run the main.py file when the container launches
ENTRYPOINT ["python", "main.py", "--listen", "0.0.0.0"]
CMD ["--driver", "./mydriver.py", "--port", "8081"]
4 changes: 2 additions & 2 deletions rose/client/mydriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
very good driver but the implementation is very elegant.
"""
import random
from rose.client.game import actions
from rose.common import actions

driver_name = "My Driver"
driver_name = "Driver"


def drive(world):
Expand Down
16 changes: 16 additions & 0 deletions rose/common/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Use Red Hat Universal Base Image (UBI) with Python
FROM registry.access.redhat.com/ubi8/python-38

# Set the working directory in the Docker container
WORKDIR /app/rose/common

# Make app.rose a python package
COPY __init__.py /app/rose/

# Copy the local package files to rose.common
COPY __init__.py /app/rose/common/
COPY actions.py /app/rose/common/
COPY obstacles.py /app/rose/common/

# Add the rose client package to the python path
ENV PYTHONPATH "${PYTHONPATH}:/app"
19 changes: 19 additions & 0 deletions rose/common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: lint lint-fix build-image

IMAGE_NAME ?= quay.io/rose/rose-common

# By default, run both linting and tests
all: lint

lint:
@echo "Running flake8 linting..."
flake8 --show-source --statistics .
black --check --diff .

lint-fix:
@echo "Running lint fixing..."
@black --verbose --color .

build-image:
@echo "Building container image ..."
podman build -t $(IMAGE_NAME) .
Empty file added rose/common/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions rose/common/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
""" Driving actions """

NONE = "none" # NOQA
RIGHT = "right" # NOQA
LEFT = "left" # NOQA
PICKUP = "pickup" # NOQA
JUMP = "jump" # NOQA
BRAKE = "brake" # NOQA

ALL = (NONE, RIGHT, LEFT, PICKUP, JUMP, BRAKE)
17 changes: 17 additions & 0 deletions rose/common/obstacles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
""" Game obstacles """

import random

NONE = "" # NOQA
CRACK = "crack" # NOQA
TRASH = "trash" # NOQA
PENGUIN = "penguin" # NOQA
BIKE = "bike" # NOQA
WATER = "water" # NOQA
BARRIER = "barrier" # NOQA

ALL = (NONE, CRACK, TRASH, PENGUIN, BIKE, WATER, BARRIER)


def get_random_obstacle():
return random.choice(ALL)
8 changes: 8 additions & 0 deletions rose/server/game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ def reset(self):
self.collisions = 0
self.jumps = 0

def __cmp__(self, other):
x = self.score
y = other.score
return (x > y) - (x < y)

def __lt__(self, other):
return self.score < other.score

def in_lane(self):
current_lane = self.x // config.cells_per_player
return current_lane == self.lane
Expand Down
15 changes: 15 additions & 0 deletions rose/server/game/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ def test_player_not_in_lane():
assert not player1.in_lane()


def test_player_comparison():
player1 = player.Player("John", 1, 1)
player2 = player.Player("Doe", 2, 2)

# Initial scores are the same
assert not player1 < player2
assert not player2 < player1

# Modify scores
player1.score = 10
player2.score = 20

assert player1 < player2


def test_player_state():
player1 = player.Player("John", 2, 1)

Expand Down

0 comments on commit 1b8a343

Please sign in to comment.