diff --git a/learn_sql_model/api/websocket.py b/learn_sql_model/api/websocket.py index 375c42c..f38c19b 100644 --- a/learn_sql_model/api/websocket.py +++ b/learn_sql_model/api/websocket.py @@ -1,11 +1,15 @@ +from contextlib import contextmanager + from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect from fastapi.responses import HTMLResponse +from sqlalchemy import create_engine +from sqlalchemy.orm import scoped_session, sessionmaker from sqlmodel import Session from websockets.exceptions import ConnectionClosed from learn_sql_model.api.websocket_connection_manager import manager -from learn_sql_model.config import get_session -from learn_sql_model.models.hero import HeroUpdate, Heros +from learn_sql_model.config import get_config, get_session +from learn_sql_model.models.hero import Hero, HeroUpdate, Heros web_socket_router = APIRouter() @@ -76,16 +80,32 @@ async def websocket_endpoint(websocket: WebSocket): await manager.broadcast(f"Client #{id} left the chat", id) +@contextmanager +def db_session(db_url): + """Creates a context with an open SQLAlchemy session.""" + engine = create_engine(db_url, convert_unicode=True) + connection = engine.connect() + db_session = scoped_session( + sessionmaker(autocommit=False, autoflush=True, bind=engine) + ) + yield db_session + db_session.close() + connection.close() + + @web_socket_router.websocket("/wsecho") -async def websocket_endpoint( +async def websocket_endpoint_hero_echo( websocket: WebSocket, session: Session = Depends(get_session), ): + config = get_config() await websocket.accept() try: while True: - heros = Heros.list(session=session) - await websocket.send_text(heros.json()) + with db_session(config.database_url) as db: + heros = Heros(heros=db.query(Hero).all()) + # heros = Heros.list(session=session) + await websocket.send_text(heros.json()) except WebSocketDisconnect: print("disconnected") except ConnectionClosed: @@ -93,7 +113,7 @@ async def websocket_endpoint( @web_socket_router.websocket("/ws-hero-update") -async def websocket_endpoint( +async def websocket_endpoint_hero_update( websocket: WebSocket, session: Session = Depends(get_session), ): diff --git a/learn_sql_model/cli/hero.py b/learn_sql_model/cli/hero.py index b3e938a..1815a2f 100644 --- a/learn_sql_model/cli/hero.py +++ b/learn_sql_model/cli/hero.py @@ -13,6 +13,7 @@ from learn_sql_model.models.hero import ( HeroDelete, HeroRead, HeroUpdate, + Heros, ) hero_app = typer.Typer() @@ -44,8 +45,18 @@ def list( limit: Optional[int] = None, ) -> Union[Hero, List[Hero]]: "list many heros" - hero = HeroRead.list(where=where, offset=offset, limit=limit) - Console().print(hero) + heros = Heros.list(where=where, offset=offset, limit=limit) + Console().print(heros) + return hero + + +@hero_app.command() +def clear() -> Union[Hero, List[Hero]]: + "list many heros" + heros = Heros.list() + for hero in heros.heros: + HeroDelete(id=hero.id).delete() + return hero diff --git a/learn_sql_model/game/game.py b/learn_sql_model/game/game.py index 394b1b9..b98f43d 100644 --- a/learn_sql_model/game/game.py +++ b/learn_sql_model/game/game.py @@ -107,7 +107,12 @@ class Client: # Console().print(self.hero) self.screen.fill((0, 0, 0)) - for other in self.others.heros: + raw_heros = self.ws.recv() + others = Heros.parse_raw(raw_heros) + Console().print(others) + + for other in others.heros: + Console().print(f"drawing {other.name}") pygame.draw.circle(self.screen, (255, 0, 0), (other.x, other.y), other.size) self.screen.blit( my_font.render(other.name, False, (255, 255, 255), 1),