Implement handbook teleporting

also a few formatting changes and sort data by logical sense
This commit is contained in:
KingRainbow44 2023-05-12 20:18:53 -04:00
parent f8054a82a9
commit 78ee3e8db1
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
9 changed files with 149 additions and 33 deletions

View File

@ -17,6 +17,21 @@ function basicGive(item: number, amount = 1): string {
return `/give ${item} x${amount}`;
}
/**
* Generates a basic teleport command.
* This creates a relative teleport command.
*/
function teleport(scene: number): string {
// Validate the number.
if (invalid(scene)) return "Invalid arguments.";
return `/teleport ~ ~ ~ ${scene}`;
}
export const give = {
basic: basicGive
};
export const action = {
teleport: teleport
};

View File

@ -70,7 +70,9 @@ export function getCommands(): CommandDump {
* Fetches and lists all the commands in the file.
*/
export function listCommands(): Command[] {
return Object.values(getCommands());
return Object.values(getCommands())
.sort((a, b) =>
a.name[0].localeCompare(b.name[0]));
}
/**
@ -110,22 +112,26 @@ export function getAvatars(): AvatarDump {
* Fetches and lists all the avatars in the file.
*/
export function listAvatars(): Avatar[] {
return Object.values(getAvatars());
return Object.values(getAvatars())
.sort((a, b) =>
a.name.localeCompare(b.name));
}
/**
* Fetches and casts all scenes in the file.
*/
export function getScenes(): Scene[] {
return scenes.map((entry) => {
const values = Object.values(entry) as string[];
const id = parseInt(values[0]);
return {
id,
identifier: values[1],
type: values[2] as SceneType
};
});
return scenes
.map((entry) => {
const values = Object.values(entry) as string[];
const id = parseInt(values[0]);
return {
id,
identifier: values[1],
type: values[2] as SceneType
};
})
.sort((a, b) => a.id - b.id);
}
/**

View File

@ -77,3 +77,21 @@ export async function giveItem(item: number, amount = 1): Promise<CommandRespons
})
}).then((res) => res.json());
}
/**
* Teleports the player to a new scene.
*
* @param scene The scene's ID.
*/
export async function teleportTo(scene: number): Promise<CommandResponse> {
// Validate the number.
if (isNaN(scene) || scene < 1) return { status: -1, message: "Invalid scene." };
return await fetch(`https://localhost:443/handbook/teleport`, {
method: "POST",
body: JSON.stringify({
player: targetPlayer.toString(),
scene: scene.toString()
})
}).then((res) => res.json());
}

View File

@ -10,11 +10,11 @@
width: 100%;
overflow: hidden;
box-sizing: border-box;
}
.Character :hover {
cursor: pointer;
box-shadow: 1px 1px black;
}
.Character_Icon {

View File

@ -4,6 +4,9 @@ import Card from "@widgets/Card";
import { SceneType } from "@backend/types";
import { getScenes } from "@backend/data";
import { connected, teleportTo } from "@backend/server";
import { action } from "@backend/commands";
import { copyToClipboard } from "@app/utils";
import "@css/pages/ScenesPage.scss";
@ -38,8 +41,12 @@ class ScenesPage extends React.PureComponent {
* Teleports the player to the specified scene.
* @private
*/
private async teleport(): Promise<void> {
// TODO: Implement teleporting.
private async teleport(scene: number): Promise<void> {
if (connected) {
await teleportTo(scene);
} else {
await copyToClipboard(action.teleport(scene))
}
}
render() {
@ -48,13 +55,15 @@ class ScenesPage extends React.PureComponent {
<h1 className={"ScenesPage_Title"}>Scenes</h1>
<div className={"ScenesPage_List"}>
{getScenes().map((command) => (
{getScenes().map((scene) => (
<Card
key={command.identifier}
title={command.identifier}
alternate={`ID: ${command.id} | ${sceneTypeToString(command.type)}`}
key={scene.id}
title={scene.identifier}
alternate={`ID: ${scene.id} | ${sceneTypeToString(scene.type)}`}
button={
<button className={"ScenesPage_Button"} onClick={this.teleport.bind(this)}>
<button className={"ScenesPage_Button"}
onClick={() => this.teleport(scene.id)}
>
Teleport
</button>
}

View File

@ -9,11 +9,10 @@ import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.utils.JsonUtils;
import lombok.NoArgsConstructor;
import java.net.URI;
import java.util.Set;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import static emu.grasscutter.Grasscutter.config;
@ -204,7 +203,7 @@ public class ConfigContainer {
public Level serverLoggerLevel = Level.DEBUG;
/* Log level of the third-party services (works only with -debug arg):
javalin, quartz, reflections, jetty, mongodb.driver*/
javalin, quartz, reflections, jetty, mongodb.driver */
public Level servicesLoggersLevel = Level.INFO;
/* Controls whether packets should be logged in console or not */

View File

@ -5,7 +5,8 @@ import emu.grasscutter.data.GameData;
import emu.grasscutter.data.GameDepot;
import emu.grasscutter.data.binout.SceneNpcBornEntry;
import emu.grasscutter.data.binout.routes.Route;
import emu.grasscutter.data.excels.*;
import emu.grasscutter.data.excels.ItemData;
import emu.grasscutter.data.excels.SceneData;
import emu.grasscutter.data.excels.codex.CodexAnimalData;
import emu.grasscutter.data.excels.monster.MonsterData;
import emu.grasscutter.data.excels.world.WorldLevelData;
@ -40,14 +41,15 @@ import emu.grasscutter.server.packet.send.*;
import emu.grasscutter.utils.KahnsSort;
import emu.grasscutter.utils.Position;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.Getter;
import lombok.Setter;
import lombok.val;
import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
import lombok.val;
public final class Scene {
@Getter private final World world;
@ -555,7 +557,7 @@ public final class Scene {
/**
* @return The script's default rotation, or the player's rotation.
*/
private Position getDefaultRot(Player player) {
public Position getDefaultRotation(Player player) {
var defaultRotation = this.getScriptManager().getConfig().born_rot;
return defaultRotation != null ? defaultRotation : player.getRotation();
}
@ -581,7 +583,7 @@ public final class Scene {
private Position getRespawnRotation(Player player) {
var lastCheckpointRot =
this.dungeonManager != null ? this.dungeonManager.getRespawnRotation() : null;
return lastCheckpointRot != null ? lastCheckpointRot : this.getDefaultRot(player);
return lastCheckpointRot != null ? lastCheckpointRot : this.getDefaultRotation(player);
}
/**

View File

@ -1,7 +1,5 @@
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;
@ -14,6 +12,10 @@ import emu.grasscutter.utils.objects.HandbookBody;
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 {
private final byte[] handbook;
@ -38,6 +40,7 @@ public final class HandbookHandler implements Router {
// Handbook control routes.
javalin.post("/handbook/avatar", this::grantAvatar);
javalin.post("/handbook/item", this::giveItem);
javalin.post("/handbook/teleport", this::teleportTo);
}
/**
@ -100,8 +103,8 @@ public final class HandbookHandler implements Router {
var avatar = new Avatar(avatarData);
avatar.setLevel(request.getLevel());
avatar.setPromoteLevel(Avatar.getMinPromoteLevel(avatar.getLevel()));
avatar
.getSkillDepot()
Objects.requireNonNull(avatar
.getSkillDepot())
.getSkillsAndEnergySkill()
.forEach(id -> avatar.setSkillLevel(id, request.getTalentLevels()));
avatar.forceConstellationLevel(request.getConstellations());
@ -166,4 +169,62 @@ public final class HandbookHandler implements Router {
Grasscutter.getLogger().debug("A handbook command error occurred.", exception);
}
}
/**
* Teleports the user to a location.
*
* @route POST /handbook/teleport
* @param ctx The Javalin request context.
*/
private void teleportTo(Context ctx) {
if (!this.controlSupported()) {
ctx.status(500).result("Handbook control not supported.");
return;
}
// 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 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);
}
}
}

View File

@ -29,4 +29,10 @@ public interface HandbookBody {
private int amount = 1; // Range between 1 - Long.MAX_VALUE.
}
@Getter
class TeleportTo {
private String player; // Parse into online player ID.
private String scene; // Parse into a scene ID.
}
}