wip
This commit is contained in:
parent
f6a3e69536
commit
7cce3940fc
3 changed files with 45 additions and 9 deletions
|
|
@ -1,11 +1,15 @@
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
from websockets.exceptions import ConnectionClosed
|
from websockets.exceptions import ConnectionClosed
|
||||||
|
|
||||||
from learn_sql_model.api.websocket_connection_manager import manager
|
from learn_sql_model.api.websocket_connection_manager import manager
|
||||||
from learn_sql_model.config import get_session
|
from learn_sql_model.config import get_config, get_session
|
||||||
from learn_sql_model.models.hero import HeroUpdate, Heros
|
from learn_sql_model.models.hero import Hero, HeroUpdate, Heros
|
||||||
|
|
||||||
web_socket_router = APIRouter()
|
web_socket_router = APIRouter()
|
||||||
|
|
||||||
|
|
@ -76,15 +80,31 @@ async def websocket_endpoint(websocket: WebSocket):
|
||||||
await manager.broadcast(f"Client #{id} left the chat", id)
|
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")
|
@web_socket_router.websocket("/wsecho")
|
||||||
async def websocket_endpoint(
|
async def websocket_endpoint_hero_echo(
|
||||||
websocket: WebSocket,
|
websocket: WebSocket,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
):
|
):
|
||||||
|
config = get_config()
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
heros = Heros.list(session=session)
|
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())
|
await websocket.send_text(heros.json())
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
print("disconnected")
|
print("disconnected")
|
||||||
|
|
@ -93,7 +113,7 @@ async def websocket_endpoint(
|
||||||
|
|
||||||
|
|
||||||
@web_socket_router.websocket("/ws-hero-update")
|
@web_socket_router.websocket("/ws-hero-update")
|
||||||
async def websocket_endpoint(
|
async def websocket_endpoint_hero_update(
|
||||||
websocket: WebSocket,
|
websocket: WebSocket,
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
):
|
):
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ from learn_sql_model.models.hero import (
|
||||||
HeroDelete,
|
HeroDelete,
|
||||||
HeroRead,
|
HeroRead,
|
||||||
HeroUpdate,
|
HeroUpdate,
|
||||||
|
Heros,
|
||||||
)
|
)
|
||||||
|
|
||||||
hero_app = typer.Typer()
|
hero_app = typer.Typer()
|
||||||
|
|
@ -44,8 +45,18 @@ def list(
|
||||||
limit: Optional[int] = None,
|
limit: Optional[int] = None,
|
||||||
) -> Union[Hero, List[Hero]]:
|
) -> Union[Hero, List[Hero]]:
|
||||||
"list many heros"
|
"list many heros"
|
||||||
hero = HeroRead.list(where=where, offset=offset, limit=limit)
|
heros = Heros.list(where=where, offset=offset, limit=limit)
|
||||||
Console().print(hero)
|
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
|
return hero
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,12 @@ class Client:
|
||||||
# Console().print(self.hero)
|
# Console().print(self.hero)
|
||||||
self.screen.fill((0, 0, 0))
|
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)
|
pygame.draw.circle(self.screen, (255, 0, 0), (other.x, other.y), other.size)
|
||||||
self.screen.blit(
|
self.screen.blit(
|
||||||
my_font.render(other.name, False, (255, 255, 255), 1),
|
my_font.render(other.name, False, (255, 255, 255), 1),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue