artemis/titles/wacca/frontend.py

48 lines
1.5 KiB
Python
Raw Normal View History

from os import path
2024-03-22 16:22:07 +00:00
from typing import List
2023-02-19 20:40:25 +00:00
2024-03-22 16:22:07 +00:00
import jinja2
import yaml
2023-02-19 20:40:25 +00:00
from core.config import CoreConfig
2024-03-22 16:22:07 +00:00
from core.frontend import FE_Base, UserSession
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
2023-02-19 20:40:25 +00:00
from titles.wacca.config import WaccaConfig
from titles.wacca.const import WaccaConstants
2024-03-22 16:22:07 +00:00
from titles.wacca.database import WaccaData
2023-02-19 20:40:25 +00:00
2023-03-09 16:38:58 +00:00
2023-02-19 20:40:25 +00:00
class WaccaFrontend(FE_Base):
2023-03-09 16:38:58 +00:00
def __init__(
self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str
) -> None:
2023-02-19 20:40:25 +00:00
super().__init__(cfg, environment)
self.data = WaccaData(cfg)
self.game_cfg = WaccaConfig()
if path.exists(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"):
self.game_cfg.update(
yaml.safe_load(open(f"{cfg_dir}/{WaccaConstants.CONFIG_NAME}"))
)
self.nav_name = "Wacca"
2023-03-09 16:38:58 +00:00
2024-03-22 16:22:07 +00:00
def get_routes(self) -> List[Route]:
return [Route("/", self.render_GET, methods=["GET"])]
async def render_GET(self, request: Request) -> bytes:
2023-03-09 16:38:58 +00:00
template = self.environment.get_template(
2024-03-22 16:22:07 +00:00
"titles/wacca/templates/wacca_index.jinja"
)
usr_sesh = self.validate_session(request)
if not usr_sesh:
usr_sesh = UserSession()
return Response(
template.render(
title=f"{self.core_config.server.name} | {self.nav_name}",
game_list=self.environment.globals["game_list"],
sesh=vars(usr_sesh),
),
media_type="text/html; charset=utf-8",
2023-03-09 16:38:58 +00:00
)