1
0
mirror of synced 2024-12-24 02:24:49 +01:00
artemis/titles/pokken/frontend.py

102 lines
3.3 KiB
Python
Raw Normal View History

2023-04-11 17:40:05 +02:00
import yaml
import jinja2
from typing import List
2024-01-09 09:07:04 +01:00
from starlette.requests import Request
from starlette.responses import Response, RedirectResponse
from starlette.routing import Route
2023-04-11 17:40:05 +02:00
from os import path
from core.frontend import FE_Base, UserSession
2023-04-11 17:40:05 +02:00
from core.config import CoreConfig
from .database import PokkenData
from .config import PokkenConfig
from .const import PokkenConstants
class PokkenFrontend(FE_Base):
SN_PREFIX = PokkenConstants.SERIAL_IDENT
NETID_PREFIX = PokkenConstants.NETID_PREFIX
2023-04-11 17:40:05 +02:00
def __init__(
self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str
) -> None:
super().__init__(cfg, environment)
self.data = PokkenData(cfg)
self.game_cfg = PokkenConfig()
if path.exists(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"):
self.game_cfg.update(
yaml.safe_load(open(f"{cfg_dir}/{PokkenConstants.CONFIG_NAME}"))
)
self.nav_name = "Pokken"
def get_routes(self) -> List[Route]:
return [
Route("/", self.render_GET, methods=['GET']),
Route("/update.name", self.change_name, methods=['POST']),
]
2023-04-11 17:40:05 +02:00
async def render_GET(self, request: Request) -> Response:
2023-04-11 17:40:05 +02:00
template = self.environment.get_template(
"titles/pokken/templates/pokken_index.jinja"
2023-04-11 17:40:05 +02:00
)
pf = None
usr_sesh = self.validate_session(request)
if not usr_sesh:
usr_sesh = UserSession()
else:
profile = await self.data.profile.get_profile(usr_sesh.user_id)
if profile is not None and profile['trainer_name']:
pf = profile._asdict()
if "e" in request.query_params:
try:
err = int(request.query_params.get("e", 0))
except Exception:
err = 0
else:
err = 0
if "s" in request.query_params:
try:
succ = int(request.query_params.get("s", 0))
except Exception:
succ = 0
else:
succ = 0
return Response(template.render(
2023-04-11 17:40:05 +02:00
title=f"{self.core_config.server.name} | {self.nav_name}",
game_list=self.environment.globals["game_list"],
sesh=vars(usr_sesh),
profile=pf,
error=err,
success=succ
2024-01-13 23:15:02 +01:00
), media_type="text/html; charset=utf-8")
async def change_name(self, request: Request) -> RedirectResponse:
usr_sesh = self.validate_session(request)
if not usr_sesh:
return RedirectResponse("/game/pokken/?e=9", 303)
frm = await request.form()
new_name = frm.get("new_name")
gender = frm.get("new_gender", 1)
if len(new_name) > 14:
return RedirectResponse("/game/pokken/?e=8", 303)
if not gender.isdigit():
return RedirectResponse("/game/pokken/?e=4", 303)
gender = int(gender)
if gender != 1 and gender != 2:
return RedirectResponse("/game/pokken/?e=4", 303) # no code for this yet, whatever
await self.data.profile.set_profile_name(usr_sesh.user_id, new_name, gender)
return RedirectResponse("/game/pokken/?s=1", 303)