diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index 94c391a9e..73789aa22 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -1,7 +1,5 @@ package emu.grasscutter.database; -import static com.mongodb.client.model.Filters.eq; - import com.mongodb.client.result.DeleteResult; import dev.morphia.query.FindOptions; import dev.morphia.query.Sort; @@ -22,9 +20,12 @@ import emu.grasscutter.game.mail.Mail; import emu.grasscutter.game.player.Player; import emu.grasscutter.game.quest.GameMainQuest; import emu.grasscutter.game.world.SceneGroupInstance; + import java.util.List; import java.util.stream.Stream; +import static com.mongodb.client.model.Filters.eq; + public final class DatabaseHelper { public static Account createAccount(String username) { return createAccountWithUid(username, 0); @@ -217,6 +218,20 @@ public final class DatabaseHelper { .first(); } + /** + * Use {@link DatabaseHelper#getPlayerByAccount(Account, Class)} for creating a real player. + * This method is used for fetching the player's data. + * + * @param accountId The account's ID. + * @return The player. + */ + public static Player getPlayerByAccount(String accountId) { + return DatabaseManager.getGameDatastore() + .find(Player.class) + .filter(Filters.eq("accountId", accountId)) + .first(); + } + public static boolean checkIfPlayerExists(int uid) { return DatabaseManager.getGameDatastore() .find(Player.class) diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java index 38ae5e411..71d353cbc 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchClient.java @@ -1,13 +1,18 @@ package emu.grasscutter.server.dispatch; -import static emu.grasscutter.config.Configuration.DISPATCH_INFO; - import com.google.gson.JsonElement; import com.google.gson.JsonObject; import emu.grasscutter.Grasscutter; +import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.http.handlers.GachaHandler; import emu.grasscutter.utils.Crypto; +import lombok.Getter; +import org.java_websocket.WebSocket; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; +import org.slf4j.Logger; + import java.net.ConnectException; import java.net.URI; import java.nio.ByteBuffer; @@ -17,11 +22,8 @@ import java.util.List; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; -import lombok.Getter; -import org.java_websocket.WebSocket; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; -import org.slf4j.Logger; + +import static emu.grasscutter.config.Configuration.DISPATCH_INFO; public final class DispatchClient extends WebSocketClient implements IDispatcher { @Getter private final Logger logger = Grasscutter.getLogger(); @@ -54,7 +56,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher var response = new JsonObject(); // Find a player with the specified account ID. - var player = Grasscutter.getGameServer().getPlayerByAccountId(accountId); + var player = DatabaseHelper.getPlayerByAccount(accountId); if (player == null) { response.addProperty("retcode", 1); this.sendMessage(PacketIds.GachaHistoryRsp, response); diff --git a/src/main/java/emu/grasscutter/server/http/HttpServer.java b/src/main/java/emu/grasscutter/server/http/HttpServer.java index 51480ea09..9bfac1122 100644 --- a/src/main/java/emu/grasscutter/server/http/HttpServer.java +++ b/src/main/java/emu/grasscutter/server/http/HttpServer.java @@ -129,7 +129,7 @@ public final class HttpServer { @SuppressWarnings("UnusedReturnValue") public HttpServer addRouter(Class router, Object... args) { // Get all constructor parameters. - Class[] types = new Class[args.length]; + var types = new Class[args.length]; for (var argument : args) types[args.length - 1] = argument.getClass(); diff --git a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java index 6253e416c..1f5b4f74d 100644 --- a/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java +++ b/src/main/java/emu/grasscutter/server/http/handlers/GachaHandler.java @@ -1,7 +1,5 @@ package emu.grasscutter.server.http.handlers; -import static emu.grasscutter.utils.Language.translate; - import com.google.gson.JsonObject; import emu.grasscutter.Grasscutter; import emu.grasscutter.database.DatabaseHelper; @@ -13,14 +11,16 @@ import emu.grasscutter.utils.Utils; import io.javalin.Javalin; import io.javalin.http.ContentType; import io.javalin.http.Context; -import io.javalin.http.staticfiles.Location; +import lombok.Getter; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.LinkedHashSet; -import lombok.Getter; + +import static emu.grasscutter.utils.Language.translate; /** Handles all gacha-related HTTP requests. */ public final class GachaHandler implements Router { @@ -49,14 +49,14 @@ public final class GachaHandler implements Router { // Make request to dispatch server. var data = DispatchUtils.fetchGachaRecords(account.getId(), page, gachaType); - var records = data.get("records").getAsString(); + var records = data.get("records").getAsJsonArray(); var maxPage = data.get("maxPage").getAsLong(); var locale = account.getLocale(); var template = new String( FileUtils.read(FileUtils.getDataPath("gacha/records.html")), StandardCharsets.UTF_8) - .replace("'{{REPLACE_RECORDS}}'", records) + .replace("'{{REPLACE_RECORDS}}'", records.toString()) .replace("'{{REPLACE_MAXPAGE}}'", String.valueOf(maxPage)) .replace("{{TITLE}}", translate(locale, "gacha.records.title")) .replace("{{DATE}}", translate(locale, "gacha.records.date")) @@ -164,12 +164,7 @@ public final class GachaHandler implements Router { public void applyRoutes(Javalin javalin) { javalin.get("/gacha", GachaHandler::gachaRecords); javalin.get("/gacha/details", GachaHandler::gachaDetails); - - javalin.cfg.staticFiles.add( - cfg -> { - cfg.hostedPath = "/gacha/mappings"; - cfg.directory = gachaMappingsPath.toString(); - cfg.location = Location.EXTERNAL; - }); + javalin.get("/gacha/mappings", ctx -> + ctx.result(FileUtils.read(gachaMappingsPath.toString()))); } }