Move gacha files to separate directory and refactor file serving

This commit is contained in:
Hotaru 2022-05-15 03:24:34 +02:00 committed by Melledy
parent 159feb4064
commit 3a216bf1bb
7 changed files with 63 additions and 54 deletions

View File

@ -84,7 +84,7 @@
var fiveStarItems = {{FIVE_STARS}}; var fiveStarItems = {{FIVE_STARS}};
var fourStarItems = {{FOUR_STARS}}; var fourStarItems = {{FOUR_STARS}};
var threeStarItems = {{THREE_STARS}}; var threeStarItems = {{THREE_STARS}};
var lang = "{{LANGUAGE}}"; var lang = "{{LANGUAGE}}".toLowerCase();
function getNameForId(itemId) { function getNameForId(itemId) {
if (mappings[lang] != null && mappings[lang][itemId] != null) { if (mappings[lang] != null && mappings[lang][itemId] != null) {

View File

@ -58,7 +58,7 @@
<!-- Otherwise you may onle see number IDs in the gacha record --> <!-- Otherwise you may onle see number IDs in the gacha record -->
<script type="text/javascript" src="/gacha/mappings"></script> <script type="text/javascript" src="/gacha/mappings"></script>
<script> <script>
record = {{REPLACE_RECORD}}; records = {{REPLACE_RECORDS}};
maxPage = {{REPLACE_MAXPAGE}}; maxPage = {{REPLACE_MAXPAGE}};
mappings['default'] = mappings['en-us']; // make en-us as default/fallback option mappings['default'] = mappings['en-us']; // make en-us as default/fallback option
@ -111,7 +111,8 @@
</footer> </footer>
<script> <script>
var lang = new window.URLSearchParams(window.location.search).get("lang"); var lang = "{{LANGUAGE}}".toLowerCase();
function itemMapper(itemID) { function itemMapper(itemID) {
if (mappings[lang] != null && mappings[lang][itemID] != null) { if (mappings[lang] != null && mappings[lang][itemID] != null) {
var entry = mappings[lang][itemID]; var entry = mappings[lang][itemID];
@ -128,17 +129,19 @@
} }
return "<span class='blue'>" + itemID + "</span>"; return "<span class='blue'>" + itemID + "</span>";
} }
(function (){ (function (){
var container = document.getElementById("container"); var container = document.getElementById("container");
record.forEach(element => { records.forEach(element => {
var e = document.createElement("tr"); var e = document.createElement("tr");
e.innerHTML= "<td>" + (new Date(element.time).toLocaleString(lang)) + "</td><td>" + itemMapper(element.item) + "</td>"; e.innerHTML= "<td>" + (new Date(element.time).toLocaleString(lang)) + "</td><td>" + itemMapper(element.item) + "</td>";
container.appendChild(e); container.appendChild(e);
}); });
// setup pagenation buttons // setup pagenation buttons
var page = parseInt(new window.URLSearchParams(window.location.search).get("p")); var page = parseInt(new window.URLSearchParams(window.location.search).get("p"));
if (!page){ if (!page) {
page = 0; page = 0;
} }
document.getElementById("curpage").innerText = page + 1; document.getElementById("curpage").innerText = page + 1;
@ -147,7 +150,6 @@
document.getElementById("prev").href = href.toString(); document.getElementById("prev").href = href.toString();
href.searchParams.set("p", page + 1); href.searchParams.set("p", page + 1);
document.getElementById("next").href = href.toString(); document.getElementById("next").href = href.toString();
if (page <= 0) { if (page <= 0) {
document.getElementById("prev").style.display = "none"; document.getElementById("prev").style.display = "none";
} }
@ -157,11 +159,10 @@
// setup gacha type info // setup gacha type info
var gachaType = new window.URLSearchParams(window.location.search).get("gachaType"); var gachaType = new window.URLSearchParams(window.location.search).get("gachaType");
var gachaString;
if (mappings[lang] != null && mappings[lang][gachaType] != null) { if (mappings[lang] != null && mappings[lang][gachaType] != null) {
gachaString = mappings[lang][gachaType]; var gachaString = mappings[lang][gachaType];
}else{ } else {
gachaString = mappings['default'][gachaType]; var gachaString = mappings['default'][gachaType];
if (gachaString == null) { if (gachaString == null) {
gachaString = gachaType; gachaString = gachaType;
} }

View File

@ -17,6 +17,7 @@ import io.javalin.Javalin;
import io.javalin.http.staticfiles.Location; import io.javalin.http.staticfiles.Location;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Set; import java.util.Set;
@ -30,11 +31,8 @@ import static emu.grasscutter.utils.Language.translate;
public final class GachaHandler implements Router { public final class GachaHandler implements Router {
private final String gachaMappings; private final String gachaMappings;
private static String recordsTemplate = "";
private static String detailsTemplate = "";
public GachaHandler() { public GachaHandler() {
this.gachaMappings = Utils.toFilePath(DATA("/gacha_mappings.js")); this.gachaMappings = Utils.toFilePath(DATA("/gacha/mappings.js"));
if(!(new File(this.gachaMappings).exists())) { if(!(new File(this.gachaMappings).exists())) {
try { try {
Tools.createGachaMapping(this.gachaMappings); Tools.createGachaMapping(this.gachaMappings);
@ -42,12 +40,6 @@ public final class GachaHandler implements Router {
Grasscutter.getLogger().warn("Failed to create gacha mappings.", exception); Grasscutter.getLogger().warn("Failed to create gacha mappings.", exception);
} }
} }
var templateFile = new File(DATA("/gacha_records.html"));
recordsTemplate = templateFile.exists() ? new String(FileUtils.read(templateFile)) : "{{REPLACE_RECORD}}";
templateFile = new File(Utils.toFilePath(DATA("/gacha_details.html")));
detailsTemplate = templateFile.exists() ? new String(FileUtils.read(templateFile)) : null;
} }
@Override public void applyRoutes(Express express, Javalin handle) { @Override public void applyRoutes(Express express, Javalin handle) {
@ -58,7 +50,24 @@ public final class GachaHandler implements Router {
} }
private static void gachaRecords(Request request, Response response) { private static void gachaRecords(Request request, Response response) {
var sessionKey = request.query("s"); File recordsTemplate = new File(Utils.toFilePath(DATA("gacha/records.html")));
if (!recordsTemplate.exists()) {
Grasscutter.getLogger().warn("File does not exist: " + recordsTemplate);
response.status(500);
return;
}
String sessionKey = request.query("s");
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
if(account == null) {
response.status(403).send("Requested account was not found");
return;
}
Player player = Grasscutter.getGameServer().getPlayerByUid(account.getPlayerUid());
if (player == null) {
response.status(403).send("No player associated with requested account");
return;
}
int page = 0, gachaType = 0; int page = 0, gachaType = 0;
if(request.query("p") != null) if(request.query("p") != null)
@ -66,35 +75,38 @@ public final class GachaHandler implements Router {
if(request.query("gachaType") != null) if(request.query("gachaType") != null)
gachaType = Integer.parseInt(request.query("gachaType")); gachaType = Integer.parseInt(request.query("gachaType"));
// Get account from session key. String records = DatabaseHelper.getGachaRecords(player.getUid(), page, gachaType).toString();
var account = DatabaseHelper.getAccountBySessionKey(sessionKey); long maxPage = DatabaseHelper.getGachaRecordsMaxPage(player.getUid(), page, gachaType);
if(account == null) // Send response. String template = new String(FileUtils.read(recordsTemplate), StandardCharsets.UTF_8)
response.status(404).send("Unable to find account."); .replace("{{REPLACE_RECORDS}}", records)
else { .replace("{{REPLACE_MAXPAGE}}", String.valueOf(maxPage))
String records = DatabaseHelper.getGachaRecords(account.getPlayerUid(), gachaType, page).toString(); .replace("{{LANGUAGE}}", Utils.getLanguageCode(account.getLocale()));
long maxPage = DatabaseHelper.getGachaRecordsMaxPage(account.getPlayerUid(), page, gachaType); response.send(template);
response.send(recordsTemplate
.replace("{{REPLACE_RECORD}}", records)
.replace("{{REPLACE_MAXPAGE}}", String.valueOf(maxPage)));
}
} }
private static void gachaDetails(Request request, Response response) { private static void gachaDetails(Request request, Response response) {
String template = detailsTemplate; File detailsTemplate = new File(Utils.toFilePath(DATA("gacha/details.html")));
if (!detailsTemplate.exists()) {
// Get player info (for langauge). Grasscutter.getLogger().warn("File does not exist: " + detailsTemplate);
String sessionKey = request.query("s"); response.status(500);
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
Player player = Grasscutter.getGameServer().getPlayerByUid(account.getPlayerUid());
// If the template was not loaded, return an error.
if (detailsTemplate == null) {
response.send(translate(player, "gacha.details.template_missing"));
return; return;
} }
String sessionKey = request.query("s");
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
if(account == null) {
response.status(403).send("Requested account was not found");
return;
}
Player player = Grasscutter.getGameServer().getPlayerByUid(account.getPlayerUid());
if (player == null) {
response.status(403).send("No player associated with requested account");
return;
}
String template = new String(FileUtils.read(detailsTemplate), StandardCharsets.UTF_8);
// Add translated title etc. to the page. // Add translated title etc. to the page.
template = template.replace("{{TITLE}}", translate(player, "gacha.details.title")) template = template.replace("{{TITLE}}", translate(player, "gacha.details.title"))
.replace("{{AVAILABLE_FIVE_STARS}}", translate(player, "gacha.details.available_five_stars")) .replace("{{AVAILABLE_FIVE_STARS}}", translate(player, "gacha.details.available_five_stars"))

View File

@ -380,8 +380,7 @@
"title": "Banner Details", "title": "Banner Details",
"available_five_stars": "Available 5-star Items", "available_five_stars": "Available 5-star Items",
"available_four_stars": "Available 4-star Items", "available_four_stars": "Available 4-star Items",
"available_three_stars": "Available 3-star Items", "available_three_stars": "Available 3-star Items"
"template_missing": "data/gacha_details.html is missing."
} }
} }
} }

View File

@ -308,8 +308,7 @@
"title": "Banner Details", "title": "Banner Details",
"available_five_stars": "Available 5-star Items", "available_five_stars": "Available 5-star Items",
"available_four_stars": "Available 4-star Items", "available_four_stars": "Available 4-star Items",
"available_three_stars": "Available 3-star Items", "available_three_stars": "Available 3-star Items"
"template_missing": "data/gacha_details.html is missing."
} }
} }
} }

View File

@ -379,8 +379,7 @@
"title": "祈愿详情", "title": "祈愿详情",
"available_five_stars": "可获得的5星物品", "available_five_stars": "可获得的5星物品",
"available_four_stars": "可获得的4星物品", "available_four_stars": "可获得的4星物品",
"available_three_stars": "可获得的3星物品", "available_three_stars": "可获得的3星物品"
"template_missing": "缺失文件data/gacha_details.html"
} }
} }
} }

View File

@ -371,8 +371,7 @@
"title": "祈願詳情", "title": "祈願詳情",
"available_five_stars": "可獲得的5星物品", "available_five_stars": "可獲得的5星物品",
"available_four_stars": "可獲得的4星物品", "available_four_stars": "可獲得的4星物品",
"available_three_stars": "可獲得的3星物品", "available_three_stars": "可獲得的3星物品"
"template_missing": "data/gacha_details.html 不存在。"
} }
} }
} }