Separate the dispatch and game servers (pt. 2)

this commit fixes the gacha page
This commit is contained in:
KingRainbow44 2023-05-15 02:37:35 -04:00
parent e079eebc0f
commit 15b1718052
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
4 changed files with 36 additions and 24 deletions

View File

@ -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)

View File

@ -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);

View File

@ -129,7 +129,7 @@ public final class HttpServer {
@SuppressWarnings("UnusedReturnValue")
public HttpServer addRouter(Class<? extends Router> 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();

View File

@ -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())));
}
}