Frontend for adding rivals, versionless backend function to support

This commit is contained in:
Wanich Keatkajonjumroen 2023-09-02 06:21:02 +09:00
parent 1f545aed41
commit 147d7adaaf
4 changed files with 127 additions and 14 deletions

View File

@ -77,6 +77,9 @@
margin-bottom: 10px;
width: 15%;
}
.modal-content {
background-color: #181a1b;
}
</style>
</head>
<body>

View File

@ -2,6 +2,7 @@ import yaml
import jinja2
from twisted.web.http import Request
from os import path
from twisted.web.util import redirectTo
from twisted.web.server import Session
from core.frontend import FE_Base, IUserSession
@ -10,6 +11,7 @@ from core.config import CoreConfig
from titles.ongeki.config import OngekiConfig
from titles.ongeki.const import OngekiConstants
from titles.ongeki.database import OngekiData
from titles.ongeki.base import OngekiBase
class OngekiFrontend(FE_Base):
@ -24,19 +26,51 @@ class OngekiFrontend(FE_Base):
yaml.safe_load(open(f"{cfg_dir}/{OngekiConstants.CONFIG_NAME}"))
)
self.nav_name = "O.N.G.E.K.I."
self.geki_version = OngekiConstants.VERSION_NAMES[-1]+"?"
print(OngekiConstants.VERSION_NAMES[-1])
self.version = 7
def render_GET(self, request: Request) -> bytes:
template = self.environment.get_template(
"titles/ongeki/frontend/ongeki_index.jinja"
)
sesh: Session = request.getSession()
usr_sesh = IUserSession(sesh)
if getattr(usr_sesh, "userId", 0) != 0:
profile_data =self.data.profile.get_profile_data_ignore_version(usr_sesh.userId)
rival_list = self.data.profile.get_rivals(usr_sesh.userId)
rival_data = {
"userRivalList": rival_list,
"userId": usr_sesh.userId
}
self.version = getattr(profile_data, "version" , 7)
if len(rival_list) > 0:
rival_info = OngekiBase.handle_get_user_rival_data_api_request(self, rival_data)
return template.render(
data=self.data.profile,
title=f"{self.core_config.server.name} | {self.nav_name}",
game_list=self.environment.globals["game_list"],
gachas=self.game_cfg.gachas.enabled_gachas,
version= self.geki_version,
profile_data=profile_data,
rival_info=rival_info["userRivalDataList"],
sesh=vars(usr_sesh)
).encode("utf-16")
else:
return redirectTo(b"/gate/", request)
def render_POST(self, request: Request):
uri = request.uri.decode()
sesh: Session = request.getSession()
usr_sesh = IUserSession(sesh)
if hasattr(usr_sesh, "userId"):
if uri == "/game/ongeki/rival.add":
rival_id = request.args[b"rivalUserId"][0].decode()
self.data.profile.put_rival(usr_sesh.userId, rival_id)
self.logger.info(f"{usr_sesh.userId} added a rival")
return redirectTo(b"/game/ongeki/", request)
elif uri == "/game/ongeki/rival.delete":
return b""
else:
return b""
else:
return b"User is not logged in"

View File

@ -1,8 +1,73 @@
{% extends "core/frontend/index.jinja" %}
{% block content %}
{% if sesh is defined and sesh["userId"] > 0 %}
<br>
<br>
<br>
<h1>{{ title }}</h1>
Version: {{version}}
<div class="container">
<div class="row">
<h2> Profile </h2>
</div>
<div class="row">
<div class="col">
<h2> {{ profile_data.userName }}</h2>
</div>
<div class="col">
<h4> ID: {{ profile_data.user}}</h4>
</div>
</div>
<div class="row">
<h2> Rivals <button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#rival_add">Add</button></h2>
</div>
<div class="row">
<table class="table table-dark table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{% for rival in rival_info%}
<tr>
<td>{{rival.rivalUserId}}</td>
<td>{{rival.rivalUserName}}</td>
<td><button class="btn-danger btn btn-sm">Delete</button></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="modal fade" id="rival_add" tabindex="-1" aria-labelledby="card_add_label" data-bs-theme="dark" aria-hidden="true">
<form id="rival" action="/game/ongeki/rival.add" method="post">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Note:<br>
Please use the ID show next to your name in the profile page.
<br>
<label for="rivalUserId">ID:&nbsp;</label><input form="rival" id="rivalUserId" name="rivalUserId" maxlength="5" type="number" required>
</div>
<div class="modal-footer">
<input type=submit class="btn btn-primary" type="button" form="rival" value="Add">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</form>
</div>
</div>
{# <script>
</script> #}
{% else %}
<h2>Not Currently Logged In</h2>
{% endif %}
{% endblock content %}

View File

@ -295,6 +295,17 @@ class OngekiProfileData(BaseData):
return None
return result.fetchone()
def get_profile_data_ignore_version(self, aime_id: int) -> Optional[Row]:
sql = select(profile).where(
and_(
profile.c.user == aime_id,
)
)
result = self.execute(sql)
if result is None:
return None
return result.fetchone()
def get_profile_options(self, aime_id: int) -> Optional[Row]:
sql = select(option).where(
and_(
@ -499,7 +510,7 @@ class OngekiProfileData(BaseData):
def put_rival(self, aime_id: int, rival_id: int) -> Optional[int]:
sql = insert(rival).values(user=aime_id, rivalUserId=rival_id)
conflict = sql.on_duplicate_key_update(rival=rival_id)
conflict = sql.on_duplicate_key_update(rivalUserId=rival_id)
result = self.execute(conflict)
if result is None: