Skip to content

Commit

Permalink
feat(core): get demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jayceslesar committed Apr 7, 2024
1 parent 282af8e commit 7ca1957
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions masterbase/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
check_steam_id_has_api_key,
check_steam_id_is_beta_tester,
close_session_helper,
demodata_helper,
generate_uuid4_int,
is_limited_account,
late_bytes_helper,
Expand Down Expand Up @@ -161,6 +162,14 @@ def late_bytes(request: Request, api_key: str, data: dict[str, str]) -> dict[str
return {"late_bytes": True}


@get("/demodata", guards=[valid_key_guard], sync_to_thread=False)
def demodata(request: Request, api_key: str, session_id: str) -> bytes:
"""Return the demo."""
engine = request.app.state.engine
data = demodata_helper(engine, api_key, session_id)
return data


class DemoHandler(WebsocketListener):
"""Custom Websocket Class."""

Expand Down
20 changes: 20 additions & 0 deletions masterbase/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,26 @@ def late_bytes_helper(engine: Engine, api_key: str, late_bytes: bytes, current_t
conn.commit()


def demodata_helper(engine: Engine, api_key: str, session_id: str) -> bytes:
"""Return demo data as bytes."""
sql = """
SELECT loid, STRING_AGG(data, '' ORDER BY pageno) AS all_data
FROM pg_largeobject
JOIN demo_sessions demo ON demo.demo_oid = pg_largeobject.loid
WHERE demo.session_id = :session_id
GROUP BY loid;
"""
with engine.connect() as conn:
result = conn.execute(sa.text(sql), dict(session_id=session_id))

row = result.fetchone()

if row is not None:
return row[0].encode("utf-8") if row[0] else b""

return b""


def check_steam_id_has_api_key(engine: Engine, steam_id: str) -> str | None:
"""Check that a given steam id has an API key or not."""
with engine.connect() as conn:
Expand Down

0 comments on commit 7ca1957

Please sign in to comment.