-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
54 lines (45 loc) · 1.86 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import HTMLResponse
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from lnbits.helpers import template_renderer
from .crud import get_coinflip, get_coinflip_settings_from_id
coinflip_generic_router: APIRouter = APIRouter()
def coinflip_renderer():
return template_renderer(["coinflip/templates"])
@coinflip_generic_router.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)):
return coinflip_renderer().TemplateResponse(
"coinflip/index.html", {"request": request, "user": user.json()}
)
@coinflip_generic_router.get(
"/coinflip/{coinflip_settings_id}/{game}", response_class=HTMLResponse
)
async def display_coinflip(request: Request, coinflip_settings_id: str, game: str):
coinflip_settings = await get_coinflip_settings_from_id(coinflip_settings_id)
if not coinflip_settings:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Coinflip game does not exist."
)
winner = None
if game:
coinflip = await get_coinflip(game)
if not coinflip:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Coinflip game does not exist."
)
if coinflip.completed:
winner = coinflip.players
return coinflip_renderer().TemplateResponse(
"coinflip/coinflip.html",
{
"request": request,
"coinflipHaircut": coinflip_settings.haircut,
"coinflipMaxPlayers": coinflip_settings.max_players,
"coinflipMaxBet": coinflip_settings.max_bet,
"coinflipPageId": coinflip_settings.id,
"coinflipGameId": game,
"coinflipWinner": winner,
},
)