mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-21 11:24:47 +01: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;
|
||||
}
|
||||
|
||||
@@ -45,9 +45,11 @@ public final class HttpServer {
|
||||
if (HTTP_POLICIES.cors.enabled) {
|
||||
var allowedOrigins = HTTP_POLICIES.cors.allowedOrigins;
|
||||
config.plugins.enableCors(cors -> cors.add(corsConfig -> {
|
||||
if (allowedOrigins.length > 0)
|
||||
corsConfig.allowHost(Arrays.toString(allowedOrigins));
|
||||
else corsConfig.anyHost();
|
||||
if (allowedOrigins.length > 0) {
|
||||
if (Arrays.asList(allowedOrigins).contains("*"))
|
||||
corsConfig.anyHost();
|
||||
else corsConfig.allowHost(Arrays.toString(allowedOrigins));
|
||||
} else corsConfig.anyHost();
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
package emu.grasscutter.server.http.documentation;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.Grasscutter.ServerRunMode;
|
||||
import emu.grasscutter.data.GameData;
|
||||
import emu.grasscutter.game.avatar.Avatar;
|
||||
import emu.grasscutter.game.entity.EntityMonster;
|
||||
import emu.grasscutter.game.inventory.GameItem;
|
||||
import emu.grasscutter.game.props.ActionReason;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
import emu.grasscutter.utils.DispatchUtils;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import emu.grasscutter.utils.objects.HandbookBody;
|
||||
import emu.grasscutter.utils.objects.HandbookBody.Action;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.http.Context;
|
||||
import java.util.Objects;
|
||||
|
||||
import static emu.grasscutter.config.Configuration.HANDBOOK;
|
||||
|
||||
/** Handles requests for the new GM Handbook. */
|
||||
public final class HandbookHandler implements Router {
|
||||
@@ -41,13 +35,14 @@ public final class HandbookHandler implements Router {
|
||||
javalin.post("/handbook/avatar", this::grantAvatar);
|
||||
javalin.post("/handbook/item", this::giveItem);
|
||||
javalin.post("/handbook/teleport", this::teleportTo);
|
||||
javalin.post("/handbook/spawn", this::spawnEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if the server can execute handbook commands.
|
||||
*/
|
||||
private boolean controlSupported() {
|
||||
return HANDBOOK.enable && Grasscutter.getRunMode() == ServerRunMode.HYBRID;
|
||||
return HANDBOOK.enable;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,46 +73,12 @@ public final class HandbookHandler implements Router {
|
||||
|
||||
// Parse the request body into a class.
|
||||
var request = ctx.bodyAsClass(HandbookBody.GrantAvatar.class);
|
||||
// Validate the request.
|
||||
if (request.getPlayer() == null || request.getAvatar() == null) {
|
||||
ctx.status(400).result("Invalid request.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the requested player.
|
||||
var playerId = Integer.parseInt(request.getPlayer());
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
|
||||
// Parse the requested avatar.
|
||||
var avatarId = Integer.parseInt(request.getAvatar());
|
||||
var avatarData = GameData.getAvatarDataMap().get(avatarId);
|
||||
|
||||
// Validate the request.
|
||||
if (player == null || avatarData == null) {
|
||||
ctx.status(400).result("Invalid player UID or avatar ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the new avatar.
|
||||
var avatar = new Avatar(avatarData);
|
||||
avatar.setLevel(request.getLevel());
|
||||
avatar.setPromoteLevel(Avatar.getMinPromoteLevel(avatar.getLevel()));
|
||||
Objects.requireNonNull(avatar.getSkillDepot())
|
||||
.getSkillsAndEnergySkill()
|
||||
.forEach(id -> avatar.setSkillLevel(id, request.getTalentLevels()));
|
||||
avatar.forceConstellationLevel(request.getConstellations());
|
||||
avatar.recalcStats(true);
|
||||
avatar.save();
|
||||
|
||||
player.addAvatar(avatar); // Add the avatar.
|
||||
ctx.json(HandbookBody.Response.builder().status(200).message("Avatar granted.").build());
|
||||
} catch (NumberFormatException ignored) {
|
||||
ctx.status(500).result("Invalid player UID or avatar ID.");
|
||||
} catch (Exception exception) {
|
||||
ctx.status(500).result("An error occurred while granting the avatar.");
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
// Get the response.
|
||||
var response = DispatchUtils.performHandbookAction(
|
||||
Action.GRANT_AVATAR, request);
|
||||
// Send the response.
|
||||
ctx.status(response.getStatus() > 100 ?
|
||||
response.getStatus() : 500).json(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,39 +95,12 @@ public final class HandbookHandler implements Router {
|
||||
|
||||
// Parse the request body into a class.
|
||||
var request = ctx.bodyAsClass(HandbookBody.GiveItem.class);
|
||||
// Validate the request.
|
||||
if (request.getPlayer() == null || request.getItem() == null) {
|
||||
ctx.status(400).result("Invalid request.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the requested player.
|
||||
var playerId = Integer.parseInt(request.getPlayer());
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
|
||||
// Parse the requested item.
|
||||
var itemId = Integer.parseInt(request.getItem());
|
||||
var itemData = GameData.getItemDataMap().get(itemId);
|
||||
|
||||
// Validate the request.
|
||||
if (player == null || itemData == null) {
|
||||
ctx.status(400).result("Invalid player UID or item ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the new item stack.
|
||||
var itemStack = new GameItem(itemData, request.getAmount());
|
||||
// Add the item to the inventory.
|
||||
player.getInventory().addItem(itemStack, ActionReason.Gm);
|
||||
|
||||
ctx.json(HandbookBody.Response.builder().status(200).message("Item granted.").build());
|
||||
} catch (NumberFormatException ignored) {
|
||||
ctx.status(500).result("Invalid player UID or item ID.");
|
||||
} catch (Exception exception) {
|
||||
ctx.status(500).result("An error occurred while granting the item.");
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
// Get the response.
|
||||
var response = DispatchUtils.performHandbookAction(
|
||||
Action.GIVE_ITEM, request);
|
||||
// Send the response.
|
||||
ctx.status(response.getStatus() > 100 ?
|
||||
response.getStatus() : 500).json(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,47 +117,12 @@ public final class HandbookHandler implements Router {
|
||||
|
||||
// Parse the request body into a class.
|
||||
var request = ctx.bodyAsClass(HandbookBody.TeleportTo.class);
|
||||
// Validate the request.
|
||||
if (request.getPlayer() == null || request.getScene() == null) {
|
||||
ctx.status(400).result("Invalid request.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the requested player.
|
||||
var playerId = Integer.parseInt(request.getPlayer());
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
|
||||
// Parse the requested scene.
|
||||
var sceneId = Integer.parseInt(request.getScene());
|
||||
|
||||
// Validate the request.
|
||||
if (player == null) {
|
||||
ctx.status(400).result("Invalid player UID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the scene in the player's world.
|
||||
var scene = player.getWorld().getSceneById(sceneId);
|
||||
if (scene == null) {
|
||||
ctx.status(400).result("Invalid scene ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve the correct teleport position.
|
||||
var position = scene.getDefaultLocation(player);
|
||||
var rotation = scene.getDefaultRotation(player);
|
||||
// Teleport the player.
|
||||
scene.getWorld().transferPlayerToScene(player, scene.getId(), position);
|
||||
player.getRotation().set(rotation);
|
||||
|
||||
ctx.json(HandbookBody.Response.builder().status(200).message("Player teleported.").build());
|
||||
} catch (NumberFormatException ignored) {
|
||||
ctx.status(400).result("Invalid player UID or scene ID.");
|
||||
} catch (Exception exception) {
|
||||
ctx.status(500).result("An error occurred while teleporting to the scene.");
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
// Get the response.
|
||||
var response = DispatchUtils.performHandbookAction(
|
||||
Action.TELEPORT_TO, request);
|
||||
// Send the response.
|
||||
ctx.status(response.getStatus() > 100 ?
|
||||
response.getStatus() : 500).json(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,48 +138,13 @@ public final class HandbookHandler implements Router {
|
||||
}
|
||||
|
||||
// Parse the request body into a class.
|
||||
var request = ctx.bodyAsClass(HandbookBody.SpawnEntity.class);
|
||||
// Validate the request.
|
||||
if (request.getPlayer() == null || request.getEntity() == null) {
|
||||
ctx.status(400).result("Invalid request.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse the requested player.
|
||||
var playerId = Integer.parseInt(request.getPlayer());
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
|
||||
// Parse the requested entity.
|
||||
var entityId = Integer.parseInt(request.getEntity());
|
||||
var entityData = GameData.getMonsterDataMap().get(entityId);
|
||||
|
||||
// Validate the request.
|
||||
if (player == null || entityData == null) {
|
||||
ctx.status(400).result("Invalid player UID or entity ID.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate request properties.
|
||||
var scene = player.getScene();
|
||||
var level = request.getLevel();
|
||||
if (scene == null || level > 200 || level < 1) {
|
||||
ctx.status(400).result("Invalid scene or level.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the entity.
|
||||
for (var i = 1; i <= request.getAmount(); i++) {
|
||||
var entity = new EntityMonster(scene, entityData, player.getPosition(), level);
|
||||
scene.addEntity(entity);
|
||||
}
|
||||
|
||||
ctx.json(HandbookBody.Response.builder().status(200).message("Entity(s) spawned.").build());
|
||||
} catch (NumberFormatException ignored) {
|
||||
ctx.status(400).result("Invalid player UID or entity ID.");
|
||||
} catch (Exception exception) {
|
||||
ctx.status(500).result("An error occurred while teleporting to the scene.");
|
||||
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
|
||||
}
|
||||
var request = ctx.bodyAsClass(
|
||||
HandbookBody.SpawnEntity.class);
|
||||
// Get the response.
|
||||
var response = DispatchUtils.performHandbookAction(
|
||||
Action.SPAWN_ENTITY, request);
|
||||
// Send the response.
|
||||
ctx.status(response.getStatus() > 100 ?
|
||||
response.getStatus() : 500).json(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@ import emu.grasscutter.net.proto.AddNoGachaAvatarCardNotifyOuterClass.AddNoGacha
|
||||
|
||||
public class PacketAddNoGachaAvatarCardNotify extends BasePacket {
|
||||
|
||||
public PacketAddNoGachaAvatarCardNotify(Avatar avatar, ActionReason reason, GameItem item) {
|
||||
public PacketAddNoGachaAvatarCardNotify(Avatar avatar, ActionReason reason) {
|
||||
super(PacketOpcodes.AddNoGachaAvatarCardNotify, true);
|
||||
|
||||
AddNoGachaAvatarCardNotify proto =
|
||||
AddNoGachaAvatarCardNotify.newBuilder()
|
||||
.setAvatarId(avatar.getAvatarId())
|
||||
.setReason(reason.getValue())
|
||||
.setInitialLevel(1)
|
||||
.setItemId(item.getItemId())
|
||||
.setInitialPromoteLevel(0)
|
||||
.setInitialLevel(avatar.getLevel())
|
||||
.setItemId(1000 + (avatar.getAvatarId() % 10000000))
|
||||
.setInitialPromoteLevel(avatar.getPromoteLevel())
|
||||
.build();
|
||||
|
||||
this.setData(proto);
|
||||
|
||||
Reference in New Issue
Block a user