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

172 lines
5.2 KiB
Python
Raw Normal View History

2021-10-13 21:51:21 +01:00
from datetime import datetime
2021-10-13 17:08:48 +01:00
from http import HTTPStatus
2021-10-13 21:51:21 +01:00
from lnbits.decorators import check_user_exists, WalletTypeInfo, get_key_type
from . import satsdice_ext, satsdice_renderer
2021-10-13 17:08:48 +01:00
from .crud import (
get_satsdice_pay,
update_satsdice_payment,
get_satsdice_payment,
create_satsdice_withdraw,
get_satsdice_withdraw,
)
2021-10-15 14:28:41 +01:00
from lnbits.core.crud import (
get_payments,
get_standalone_payment,
delete_expired_invoices,
get_balance_checks,
)
2021-10-17 18:33:29 +01:00
from lnbits.core.services import check_invoice_status
2021-10-13 21:51:21 +01:00
from fastapi import FastAPI, Request
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, Payment
2021-10-14 23:08:09 +01:00
from fastapi.params import Depends
from fastapi.param_functions import Query
import random
from .models import CreateSatsDiceWithdraw
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-19 20:54:02 +01:00
@satsdice_ext.get("/")
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()}
)
@satsdice_ext.get("/{link_id}")
async def display(request: Request, link_id: str = Query(None)):
2021-10-13 17:08:48 +01:00
link = await get_satsdice_pay(link_id) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist."
)
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-10-19 23:05:29 +01:00
@satsdice_ext.get("/win/{link_id}/{payment_hash}", name="satsdice.displaywin")
async def displaywin(
request: Request, link_id: str = Query(None), payment_hash: str = Query(None)
):
2021-10-13 17:08:48 +01:00
satsdicelink = await get_satsdice_pay(link_id) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist."
)
status = await check_invoice_status(
wallet_id=satsdicelink.wallet, payment_hash=payment_hash
)
withdrawLink = await get_satsdice_withdraw(payment_hash)
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
)
payment = await get_standalone_payment(payment_hash) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist."
)
if payment.pending == 1:
await check_invoice_status(payment.wallet_id, payment_hash)
payment = await get_standalone_payment(payment_hash) or abort(
HTTPStatus.NOT_FOUND, "satsdice link does not exist."
)
if payment.pending == 1:
print("cunt")
2021-10-13 21:51:21 +01:00
return satsdice_renderer().TemplateResponse(
"satsdice/error.html",
{
"request": request,
"link": satsdicelink.id,
"paid": False,
"lost": False,
},
2021-10-13 17:08:48 +01:00
)
await update_satsdice_payment(payment_hash, paid=1)
paylink = await get_satsdice_payment(payment_hash)
if not paylink:
2021-10-13 17:08:48 +01: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
rand = random.randint(0, 100)
chance = satsdicelink.chance
if rand > chance:
await update_satsdice_payment(payment_hash, lost=1)
2021-10-15 14:28:41 +01:00
return satsdice_renderer().TemplateResponse(
"satsdice/error.html",
{
"request": request,
"link": satsdicelink.id,
"paid": False,
"lost": True,
},
2021-10-15 14:28:41 +01:00
)
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-13 21:51:21 +01:00
@satsdice_ext.get("/img/{link_id}")
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
)