wip
This commit is contained in:
parent
7cce3940fc
commit
6676011ec6
2 changed files with 29 additions and 16 deletions
|
|
@ -2,6 +2,7 @@ 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 rich.console import Console
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
|
|
@ -9,7 +10,7 @@ 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_config, get_session
|
from learn_sql_model.config import get_config, get_session
|
||||||
from learn_sql_model.models.hero import Hero, HeroUpdate, Heros
|
from learn_sql_model.models.hero import HeroUpdate, Heros
|
||||||
|
|
||||||
web_socket_router = APIRouter()
|
web_socket_router = APIRouter()
|
||||||
|
|
||||||
|
|
@ -100,12 +101,18 @@ async def websocket_endpoint_hero_echo(
|
||||||
):
|
):
|
||||||
config = get_config()
|
config = get_config()
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
with config.database.engine.connect() as con:
|
||||||
with db_session(config.database_url) as db:
|
while True:
|
||||||
heros = Heros(heros=db.query(Hero).all())
|
data = await websocket.receive_text()
|
||||||
# heros = Heros.list(session=session)
|
hero = HeroUpdate.parse_raw(data)
|
||||||
|
heros = con.execute("SELECT * FROM hero").fetchall()
|
||||||
|
heros = Heros.parse_obj({"heros": heros})
|
||||||
|
hero.update(session=session)
|
||||||
|
Console().print(heros)
|
||||||
await websocket.send_text(heros.json())
|
await websocket.send_text(heros.json())
|
||||||
|
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
print("disconnected")
|
print("disconnected")
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@ my_font = pygame.font.SysFont("Comic Sans MS", 30)
|
||||||
|
|
||||||
config = get_config()
|
config = get_config()
|
||||||
|
|
||||||
|
console = Console()
|
||||||
|
console.quiet = True
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self, name, secret_name):
|
def __init__(self, name, secret_name):
|
||||||
|
|
@ -71,19 +74,19 @@ class Client:
|
||||||
connect()
|
connect()
|
||||||
return self._ws_update
|
return self._ws_update
|
||||||
|
|
||||||
@property
|
|
||||||
def others(self):
|
|
||||||
raw_heros = self.ws.recv()
|
|
||||||
return Heros.parse_raw(raw_heros)
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
|
console.print("running")
|
||||||
|
console.print("handle_events")
|
||||||
self.handle_events()
|
self.handle_events()
|
||||||
|
console.print("update")
|
||||||
self.update()
|
self.update()
|
||||||
|
console.print("render")
|
||||||
self.render()
|
self.render()
|
||||||
self.clock.tick(60)
|
time = self.clock.tick(60)
|
||||||
self.ticks += 1
|
self.ticks += 1
|
||||||
Console().print("not running, quitting")
|
console.print(f"time: {time}")
|
||||||
|
console.print(f"ticks: {self.ticks}")
|
||||||
self.quit()
|
self.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
|
|
@ -100,19 +103,22 @@ class Client:
|
||||||
self.hero.x += speed
|
self.hero.x += speed
|
||||||
|
|
||||||
# if self.ticks % 1 == 0:
|
# if self.ticks % 1 == 0:
|
||||||
|
console.print("updating")
|
||||||
update = HeroUpdate(**self.hero.dict(exclude_unset=True))
|
update = HeroUpdate(**self.hero.dict(exclude_unset=True))
|
||||||
self.ws_update.send(update.json())
|
console.print(update)
|
||||||
|
|
||||||
|
self.ws.send(update.json())
|
||||||
|
console.print("sent")
|
||||||
|
|
||||||
def render(self):
|
def render(self):
|
||||||
# Console().print(self.hero)
|
|
||||||
self.screen.fill((0, 0, 0))
|
self.screen.fill((0, 0, 0))
|
||||||
|
|
||||||
raw_heros = self.ws.recv()
|
raw_heros = self.ws.recv()
|
||||||
|
console.print(raw_heros)
|
||||||
|
|
||||||
others = Heros.parse_raw(raw_heros)
|
others = Heros.parse_raw(raw_heros)
|
||||||
Console().print(others)
|
|
||||||
|
|
||||||
for other in others.heros:
|
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