mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2026-05-25 21:24:03 +08:00
Separate the dispatch and game servers (pt. 3)
implement handbook across servers!
This commit is contained in:
@@ -7,6 +7,9 @@ import emu.grasscutter.database.DatabaseHelper;
|
||||
import emu.grasscutter.server.game.GameServer;
|
||||
import emu.grasscutter.server.http.handlers.GachaHandler;
|
||||
import emu.grasscutter.utils.Crypto;
|
||||
import emu.grasscutter.utils.DispatchUtils;
|
||||
import emu.grasscutter.utils.JsonUtils;
|
||||
import emu.grasscutter.utils.objects.HandbookBody;
|
||||
import lombok.Getter;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
@@ -38,6 +41,7 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
|
||||
this.setAttachment(true);
|
||||
|
||||
this.registerHandler(PacketIds.GachaHistoryReq, this::fetchGachaHistory);
|
||||
this.registerHandler(PacketIds.GmTalkReq, this::handleHandbookAction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,6 +74,35 @@ public final class DispatchClient extends WebSocketClient implements IDispatcher
|
||||
this.sendMessage(PacketIds.GachaHistoryRsp, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the handbook action packet sent by the client.
|
||||
*
|
||||
* @param socket The socket the packet was received from.
|
||||
* @param object The packet data.
|
||||
*/
|
||||
private void handleHandbookAction(WebSocket socket, JsonElement object) {
|
||||
var message = IDispatcher.decode(object);
|
||||
var actionStr = message.get("action").getAsString();
|
||||
var data = message.getAsJsonObject("data");
|
||||
|
||||
// Parse the action into an enum.
|
||||
var action = HandbookBody.Action.valueOf(actionStr);
|
||||
|
||||
// Produce a handbook response.
|
||||
var response = DispatchUtils.performHandbookAction(action, switch (action) {
|
||||
case GRANT_AVATAR -> JsonUtils.decode(data, HandbookBody.GrantAvatar.class);
|
||||
case GIVE_ITEM -> JsonUtils.decode(data, HandbookBody.GiveItem.class);
|
||||
case TELEPORT_TO -> JsonUtils.decode(data, HandbookBody.TeleportTo.class);
|
||||
case SPAWN_ENTITY -> JsonUtils.decode(data, HandbookBody.SpawnEntity.class);
|
||||
});
|
||||
|
||||
// Check if the response's status is '1'.
|
||||
if (response.getStatus() == 1) return;
|
||||
|
||||
// Send the response to the server.
|
||||
this.sendMessage(PacketIds.GmTalkRsp, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a serialized encrypted message to the server.
|
||||
*
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package emu.grasscutter.server.dispatch;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import emu.grasscutter.utils.Crypto;
|
||||
import emu.grasscutter.utils.JsonAdapters.ByteArrayAdapter;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.DISPATCH_INFO;
|
||||
|
||||
public interface IDispatcher {
|
||||
Gson JSON =
|
||||
@@ -53,7 +54,7 @@ public interface IDispatcher {
|
||||
}
|
||||
|
||||
// Un-escape the data.
|
||||
data = data.replaceAll("\"", "");
|
||||
data = data.replaceAll("\\\\\"", "\"");
|
||||
data = data.replaceAll("\\\\", "");
|
||||
|
||||
// De-serialize the data.
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package emu.grasscutter.server.dispatch;
|
||||
|
||||
import emu.grasscutter.net.packet.PacketOpcodes;
|
||||
|
||||
/* Packet IDs for the dispatch server. */
|
||||
public interface PacketIds {
|
||||
int LoginNotify = 1;
|
||||
@@ -7,4 +9,6 @@ public interface PacketIds {
|
||||
int TokenValidateRsp = 3;
|
||||
int GachaHistoryReq = 4;
|
||||
int GachaHistoryRsp = 5;
|
||||
int GmTalkReq = PacketOpcodes.GmTalkReq;
|
||||
int GmTalkRsp = PacketOpcodes.GmTalkRsp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user