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

View File

@ -17,6 +17,7 @@ import io.javalin.Javalin;
import io.javalin.http.staticfiles.Location;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
@ -30,11 +31,8 @@ import static emu.grasscutter.utils.Language.translate;
public final class GachaHandler implements Router {
private final String gachaMappings;
private static String recordsTemplate = "";
private static String detailsTemplate = "";
public GachaHandler() {
this.gachaMappings = Utils.toFilePath(DATA("/gacha_mappings.js"));
this.gachaMappings = Utils.toFilePath(DATA("/gacha/mappings.js"));
if(!(new File(this.gachaMappings).exists())) {
try {
Tools.createGachaMapping(this.gachaMappings);
@ -42,12 +40,6 @@ public final class GachaHandler implements Router {
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) {
@ -58,7 +50,24 @@ public final class GachaHandler implements Router {
}
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;
if(request.query("p") != null)
@ -66,35 +75,38 @@ public final class GachaHandler implements Router {
if(request.query("gachaType") != null)
gachaType = Integer.parseInt(request.query("gachaType"));
// Get account from session key.
var account = DatabaseHelper.getAccountBySessionKey(sessionKey);
String records = DatabaseHelper.getGachaRecords(player.getUid(), page, gachaType).toString();
long maxPage = DatabaseHelper.getGachaRecordsMaxPage(player.getUid(), page, gachaType);
if(account == null) // Send response.
response.status(404).send("Unable to find account.");
else {
String records = DatabaseHelper.getGachaRecords(account.getPlayerUid(), gachaType, page).toString();
long maxPage = DatabaseHelper.getGachaRecordsMaxPage(account.getPlayerUid(), page, gachaType);
response.send(recordsTemplate
.replace("{{REPLACE_RECORD}}", records)
.replace("{{REPLACE_MAXPAGE}}", String.valueOf(maxPage)));
}
String template = new String(FileUtils.read(recordsTemplate), StandardCharsets.UTF_8)
.replace("{{REPLACE_RECORDS}}", records)
.replace("{{REPLACE_MAXPAGE}}", String.valueOf(maxPage))
.replace("{{LANGUAGE}}", Utils.getLanguageCode(account.getLocale()));
response.send(template);
}
private static void gachaDetails(Request request, Response response) {
String template = detailsTemplate;
// Get player info (for langauge).
String sessionKey = request.query("s");
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"));
File detailsTemplate = new File(Utils.toFilePath(DATA("gacha/details.html")));
if (!detailsTemplate.exists()) {
Grasscutter.getLogger().warn("File does not exist: " + detailsTemplate);
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;
}
String template = new String(FileUtils.read(detailsTemplate), StandardCharsets.UTF_8);
// Add translated title etc. to the page.
template = template.replace("{{TITLE}}", translate(player, "gacha.details.title"))
.replace("{{AVAILABLE_FIVE_STARS}}", translate(player, "gacha.details.available_five_stars"))

View File

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

View File

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

View File

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

View File

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