Skip to content

Commit

Permalink
Refactor game_params.py and games_params_schema.py
Browse files Browse the repository at this point in the history
  • Loading branch information
fvergaracl committed Mar 11, 2024
1 parent d9d7d22 commit 480220a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
16 changes: 6 additions & 10 deletions app/model/game_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class GamesParams(BaseModel, table=True):
Represents the parameters for a game.
Attributes:
paramKey (str): The key of the parameter.
key (str): The key of the parameter.
value (str): The value of the parameter.
gameId (str): The ID of the game associated with the parameter.
"""

paramKey: str = Field(sa_column=Column(String))
key: str = Field(sa_column=Column(String))
value: str = Field(sa_column=Column(String))
gameId: str = Field(
sa_column=Column(UUID(as_uuid=True), ForeignKey("games.id")))
Expand All @@ -23,22 +23,18 @@ class Config:
orm_mode = True

def __str__(self):
return f"GameParams: (id={self.id}, "
"created_at={self.created_at}, updated_at={self.updated_at}, "
"paramKey={self.paramKey}, value={self.value}, gameId={self.gameId})"
return f"GameParams: (id={self.id}, created_at={self.created_at}, updated_at={self.updated_at}, key={self.key}, value={self.value}, gameId={self.gameId})"

def __repr__(self):
return f"GameParams: (id={self.id}, created_at={self.created_at}, "
"updated_at={self.updated_at}, paramKey={self.paramKey}, "
"value={self.value}, gameId={self.gameId})"
return f"GameParams: (id={self.id}, created_at={self.created_at}, updated_at={self.updated_at}, key={self.key}, value={self.value}, gameId={self.gameId})"

def __eq__(self, other):
return (
isinstance(other, GamesParams)
and self.paramKey == other.paramKey
and self.key == other.key
and self.value == other.value
and self.gameId == other.gameId
)

def __hash__(self):
return hash((self.paramKey, self.value, self.gameId))
return hash((self.key, self.value, self.gameId))
2 changes: 1 addition & 1 deletion app/schema/games_params_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ class BaseFindGameParams(BaseGameParams):

class UpdateGameParams(CreateGameParams):
id: UUID
paramKey: str
key: str
value: str | int | float | bool
35 changes: 32 additions & 3 deletions app/services/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app.schema.games_schema import (GameCreated, PatchGame, PostCreateGame,
ResponsePatchGame)
from app.services.base_service import BaseService

from app.engine.all_engine_strategies import all_engine_strategies

class GameService(BaseService):
def __init__(
Expand Down Expand Up @@ -47,8 +47,22 @@ def create(self, schema: PostCreateGame):
f"{externalGameId}")
)
created_params = []
default_strategyId = schema.strategyId

if (default_strategyId is None):
default_strategyId = "default"

strategies = all_engine_strategies()
strategy = next(
(strategy for strategy in strategies if strategy.id == default_strategyId),
None
)
if not strategy:
raise NotFoundError(
detail=f"Strategy with id: {default_strategyId} not found"
)

game = self.game_repository.create(schema)
print('**********', game)
if params:
del schema.params

Expand All @@ -62,7 +76,22 @@ def create(self, schema: PostCreateGame):
params_to_insert)

created_params.append(created_param)


print(' - ')
print(' - ')
print(' - ')
print(' - ')
print(' - ')
print(game)
print(created_params)
print(f"Game with externalGameId: {externalGameId} created")
print(' * ')
print(' * ')
print(' * ')
print(' * ')
json = game.dict()
print(json)

response = GameCreated(
**game.dict(), params=created_params,
message=f"Game with externalGameId: {externalGameId} created"
Expand Down
30 changes: 30 additions & 0 deletions migrations/versions/335e4bfcb868_paramkey_to_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""paramkey to key
Revision ID: 335e4bfcb868
Revises: ad1a314ac577
Create Date: 2024-03-11 22:33:36.416217
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '335e4bfcb868'
down_revision = 'ad1a314ac577'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('gamesparams', sa.Column('key', sa.String(), nullable=True))
op.drop_column('gamesparams', 'paramKey')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('gamesparams', sa.Column('paramKey', sa.VARCHAR(), autoincrement=False, nullable=True))
op.drop_column('gamesparams', 'key')
# ### end Alembic commands ###

0 comments on commit 480220a

Please sign in to comment.