mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-25 03:12:52 +08:00
Separate the dispatch and game servers (pt. 2)
this commit fixes the gacha page
This commit is contained in:
parent
e079eebc0f
commit
15b1718052
@ -1,7 +1,5 @@
|
|||||||
package emu.grasscutter.database;
|
package emu.grasscutter.database;
|
||||||
|
|
||||||
import static com.mongodb.client.model.Filters.eq;
|
|
||||||
|
|
||||||
import com.mongodb.client.result.DeleteResult;
|
import com.mongodb.client.result.DeleteResult;
|
||||||
import dev.morphia.query.FindOptions;
|
import dev.morphia.query.FindOptions;
|
||||||
import dev.morphia.query.Sort;
|
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.player.Player;
|
||||||
import emu.grasscutter.game.quest.GameMainQuest;
|
import emu.grasscutter.game.quest.GameMainQuest;
|
||||||
import emu.grasscutter.game.world.SceneGroupInstance;
|
import emu.grasscutter.game.world.SceneGroupInstance;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static com.mongodb.client.model.Filters.eq;
|
||||||
|
|
||||||
public final class DatabaseHelper {
|
public final class DatabaseHelper {
|
||||||
public static Account createAccount(String username) {
|
public static Account createAccount(String username) {
|
||||||
return createAccountWithUid(username, 0);
|
return createAccountWithUid(username, 0);
|
||||||
@ -217,6 +218,20 @@ public final class DatabaseHelper {
|
|||||||
.first();
|
.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) {
|
public static boolean checkIfPlayerExists(int uid) {
|
||||||
return DatabaseManager.getGameDatastore()
|
return DatabaseManager.getGameDatastore()
|
||||||
.find(Player.class)
|
.find(Player.class)
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
package emu.grasscutter.server.dispatch;
|
package emu.grasscutter.server.dispatch;
|
||||||
|
|
||||||
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
|
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
|
import emu.grasscutter.database.DatabaseHelper;
|
||||||
import emu.grasscutter.server.game.GameServer;
|
import emu.grasscutter.server.game.GameServer;
|
||||||
import emu.grasscutter.server.http.handlers.GachaHandler;
|
import emu.grasscutter.server.http.handlers.GachaHandler;
|
||||||
import emu.grasscutter.utils.Crypto;
|
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.ConnectException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
@ -17,11 +22,8 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import lombok.Getter;
|
|
||||||
import org.java_websocket.WebSocket;
|
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
|
||||||
import org.java_websocket.client.WebSocketClient;
|
|
||||||
import org.java_websocket.handshake.ServerHandshake;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
public final class DispatchClient extends WebSocketClient implements IDispatcher {
|
public final class DispatchClient extends WebSocketClient implements IDispatcher {
|
||||||
@Getter private final Logger logger = Grasscutter.getLogger();
|
@Getter private final Logger logger = Grasscutter.getLogger();
|
||||||
@ -54,7 +56,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
|
|||||||
var response = new JsonObject();
|
var response = new JsonObject();
|
||||||
|
|
||||||
// Find a player with the specified account ID.
|
// Find a player with the specified account ID.
|
||||||
var player = Grasscutter.getGameServer().getPlayerByAccountId(accountId);
|
var player = DatabaseHelper.getPlayerByAccount(accountId);
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
response.addProperty("retcode", 1);
|
response.addProperty("retcode", 1);
|
||||||
this.sendMessage(PacketIds.GachaHistoryRsp, response);
|
this.sendMessage(PacketIds.GachaHistoryRsp, response);
|
||||||
|
@ -129,7 +129,7 @@ public final class HttpServer {
|
|||||||
@SuppressWarnings("UnusedReturnValue")
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
public HttpServer addRouter(Class<? extends Router> router, Object... args) {
|
public HttpServer addRouter(Class<? extends Router> router, Object... args) {
|
||||||
// Get all constructor parameters.
|
// Get all constructor parameters.
|
||||||
Class<?>[] types = new Class<?>[args.length];
|
var types = new Class<?>[args.length];
|
||||||
for (var argument : args)
|
for (var argument : args)
|
||||||
types[args.length - 1] = argument.getClass();
|
types[args.length - 1] = argument.getClass();
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package emu.grasscutter.server.http.handlers;
|
package emu.grasscutter.server.http.handlers;
|
||||||
|
|
||||||
import static emu.grasscutter.utils.Language.translate;
|
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.database.DatabaseHelper;
|
import emu.grasscutter.database.DatabaseHelper;
|
||||||
@ -13,14 +11,16 @@ import emu.grasscutter.utils.Utils;
|
|||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
import io.javalin.http.ContentType;
|
import io.javalin.http.ContentType;
|
||||||
import io.javalin.http.Context;
|
import io.javalin.http.Context;
|
||||||
import io.javalin.http.staticfiles.Location;
|
import lombok.Getter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import lombok.Getter;
|
|
||||||
|
import static emu.grasscutter.utils.Language.translate;
|
||||||
|
|
||||||
/** Handles all gacha-related HTTP requests. */
|
/** Handles all gacha-related HTTP requests. */
|
||||||
public final class GachaHandler implements Router {
|
public final class GachaHandler implements Router {
|
||||||
@ -49,14 +49,14 @@ public final class GachaHandler implements Router {
|
|||||||
|
|
||||||
// Make request to dispatch server.
|
// Make request to dispatch server.
|
||||||
var data = DispatchUtils.fetchGachaRecords(account.getId(), page, gachaType);
|
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 maxPage = data.get("maxPage").getAsLong();
|
||||||
|
|
||||||
var locale = account.getLocale();
|
var locale = account.getLocale();
|
||||||
var template =
|
var template =
|
||||||
new String(
|
new String(
|
||||||
FileUtils.read(FileUtils.getDataPath("gacha/records.html")), StandardCharsets.UTF_8)
|
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("'{{REPLACE_MAXPAGE}}'", String.valueOf(maxPage))
|
||||||
.replace("{{TITLE}}", translate(locale, "gacha.records.title"))
|
.replace("{{TITLE}}", translate(locale, "gacha.records.title"))
|
||||||
.replace("{{DATE}}", translate(locale, "gacha.records.date"))
|
.replace("{{DATE}}", translate(locale, "gacha.records.date"))
|
||||||
@ -164,12 +164,7 @@ public final class GachaHandler implements Router {
|
|||||||
public void applyRoutes(Javalin javalin) {
|
public void applyRoutes(Javalin javalin) {
|
||||||
javalin.get("/gacha", GachaHandler::gachaRecords);
|
javalin.get("/gacha", GachaHandler::gachaRecords);
|
||||||
javalin.get("/gacha/details", GachaHandler::gachaDetails);
|
javalin.get("/gacha/details", GachaHandler::gachaDetails);
|
||||||
|
javalin.get("/gacha/mappings", ctx ->
|
||||||
javalin.cfg.staticFiles.add(
|
ctx.result(FileUtils.read(gachaMappingsPath.toString())));
|
||||||
cfg -> {
|
|
||||||
cfg.hostedPath = "/gacha/mappings";
|
|
||||||
cfg.directory = gachaMappingsPath.toString();
|
|
||||||
cfg.location = Location.EXTERNAL;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user