lnbits-legend/lnbits/extensions/satsdice/views.py

140 lines
4.4 KiB
Python
Raw Normal View History

2021-10-25 11:54:58 +01:00
import random
2021-10-13 17:08:48 +01:00
from http import HTTPStatus
2021-10-25 11:54:58 +01:00
from fastapi import Request
2021-10-25 11:54:58 +01:00
from fastapi.param_functions import Query
from fastapi.params import Depends
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse
from lnbits.core.models import User
2021-10-25 11:54:58 +01:00
from lnbits.core.views.api import api_payment
from lnbits.decorators import check_user_exists
2021-10-25 11:54:58 +01:00
2021-10-13 21:51:21 +01:00
from . import satsdice_ext, satsdice_renderer
2021-10-13 17:08:48 +01:00
from .crud import (
2021-10-25 11:54:58 +01:00
create_satsdice_withdraw,
2021-10-13 17:08:48 +01:00
get_satsdice_pay,
get_satsdice_payment,
get_satsdice_withdraw,
2021-10-25 11:54:58 +01:00
update_satsdice_payment,
2021-10-13 17:08:48 +01:00
)
2021-10-25 11:54:58 +01:00
from .models import CreateSatsDiceWithdraw, satsdiceLink
2021-10-13 17:08:48 +01:00
2021-10-13 21:51:21 +01:00
templates = Jinja2Templates(directory="templates")
2021-10-13 17:08:48 +01:00
2021-10-25 11:54:58 +01:00
@satsdice_ext.get("/", response_class=HTMLResponse)
2021-10-13 21:51:21 +01:00
async def index(request: Request, user: User = Depends(check_user_exists)):
return satsdice_renderer().TemplateResponse(
"satsdice/index.html", {"request": request, "user": user.dict()}
)
2021-10-25 11:54:58 +01:00
@satsdice_ext.get("/{link_id}", response_class=HTMLResponse)
async def display(request: Request, link_id: str = Query(None)):
2021-10-25 11:54:58 +01:00
link = await get_satsdice_pay(link_id)
if not link:
raise HTTPException(
2021-11-04 11:11:56 +00:00
status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist."
)
2021-10-25 11:54:58 +01:00
2021-10-13 21:51:21 +01:00
return satsdice_renderer().TemplateResponse(
2021-10-13 17:08:48 +01:00
"satsdice/display.html",
{
"request": request,
"chance": link.chance,
"multiplier": link.multiplier,
"lnurl": link.lnurl(request),
"unique": True,
},
2021-10-13 17:08:48 +01:00
)
2021-11-04 11:11:56 +00:00
@satsdice_ext.get(
"/win/{link_id}/{payment_hash}",
name="satsdice.displaywin",
response_class=HTMLResponse,
)
async def displaywin(
request: Request, link_id: str = Query(None), payment_hash: str = Query(None)
):
2021-10-25 11:54:58 +01:00
satsdicelink = await get_satsdice_pay(link_id)
2021-11-16 03:11:01 +00:00
if not satsdicelink:
2021-10-25 11:54:58 +01:00
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="satsdice link does not exist."
)
2021-11-16 03:30:37 +00:00
withdrawLink = await get_satsdice_withdraw(payment_hash)
2021-11-16 03:25:13 +00:00
payment = await get_satsdice_payment(payment_hash)
if payment.lost:
2021-11-16 02:53:16 +00:00
return satsdice_renderer().TemplateResponse(
"satsdice/error.html",
{"request": request, "link": satsdicelink.id, "paid": False, "lost": True},
)
2021-10-13 17:08:48 +01:00
if withdrawLink:
2021-10-13 21:51:21 +01:00
return satsdice_renderer().TemplateResponse(
2021-10-13 17:08:48 +01:00
"satsdice/displaywin.html",
{
"request": request,
"value": withdrawLink.value,
"chance": satsdicelink.chance,
"multiplier": satsdicelink.multiplier,
"lnurl": withdrawLink.lnurl(request),
"paid": False,
"lost": False,
},
2021-10-13 17:08:48 +01:00
)
2021-10-22 03:05:48 +01:00
rand = random.randint(0, 100)
2021-10-13 17:08:48 +01:00
chance = satsdicelink.chance
2021-10-22 03:05:48 +01:00
status = await api_payment(payment_hash)
if not rand < chance or not status["paid"]:
2021-11-16 02:53:16 +00:00
await update_satsdice_payment(payment_hash, lost=1)
2021-10-15 14:28:41 +01:00
return satsdice_renderer().TemplateResponse(
"satsdice/error.html",
2021-11-12 04:14:55 +00:00
{"request": request, "link": satsdicelink.id, "paid": False, "lost": True},
2021-10-15 14:28:41 +01:00
)
2021-10-22 03:05:48 +01:00
await update_satsdice_payment(payment_hash, paid=1)
paylink = await get_satsdice_payment(payment_hash)
data: CreateSatsDiceWithdraw = {
"satsdice_pay": satsdicelink.id,
"value": paylink.value * satsdicelink.multiplier,
"payment_hash": payment_hash,
"used": 0,
}
2021-10-15 14:28:41 +01:00
withdrawLink = await create_satsdice_withdraw(data)
2021-10-13 21:51:21 +01:00
return satsdice_renderer().TemplateResponse(
2021-10-13 17:08:48 +01:00
"satsdice/displaywin.html",
{
"request": request,
"value": withdrawLink.value,
"chance": satsdicelink.chance,
"multiplier": satsdicelink.multiplier,
"lnurl": withdrawLink.lnurl(request),
"paid": False,
"lost": False,
},
2021-10-13 17:08:48 +01:00
)
2021-10-25 11:54:58 +01:00
@satsdice_ext.get("/img/{link_id}", response_class=HTMLResponse)
2021-10-13 17:08:48 +01:00
async def img(link_id):
link = await get_satsdice_pay(link_id) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist."
)
qr = pyqrcode.create(link.lnurl)
stream = BytesIO()
qr.svg(stream, scale=3)
return (
stream.getvalue(),
200,
{
"Content-Type": "image/svg+xml",
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
2021-10-14 23:08:09 +01:00
},
2021-10-13 17:08:48 +01:00
)