mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-02-10 02:32:57 +08:00
Implement handbook SDK for commands (avatars)
This commit is contained in:
parent
2bd992592d
commit
8cc725e45c
34
src/handbook/src/backend/server.ts
Normal file
34
src/handbook/src/backend/server.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import type { CommandResponse } from "@backend/types";
|
||||||
|
|
||||||
|
let targetPlayer = 0; // The UID of the target player.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the target player.
|
||||||
|
*
|
||||||
|
* @param player The UID of the target player.
|
||||||
|
*/
|
||||||
|
export function setTargetPlayer(player: number): void {
|
||||||
|
targetPlayer = player;
|
||||||
|
console.log(`Target Player is now: ${targetPlayer}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Grants an avatar to a player.
|
||||||
|
*
|
||||||
|
* @param avatar The avatar's ID.
|
||||||
|
* @param level The avatar's level.
|
||||||
|
* @param constellations The avatar's unlocked constellations.
|
||||||
|
* @param talents The level for the avatar's talents.
|
||||||
|
*/
|
||||||
|
export async function grantAvatar(
|
||||||
|
avatar: number, level = 90,
|
||||||
|
constellations = 6, talents = 6
|
||||||
|
): Promise<CommandResponse> {
|
||||||
|
return await fetch(`https://localhost:443/handbook/avatar`, {
|
||||||
|
method: "POST", body: JSON.stringify({
|
||||||
|
player: targetPlayer.toString(),
|
||||||
|
avatar: avatar.toString(),
|
||||||
|
level, constellations, talentLevels: talents
|
||||||
|
})
|
||||||
|
}).then(res => res.json());
|
||||||
|
}
|
@ -118,6 +118,11 @@ export enum ItemCategory {
|
|||||||
Miscellaneous
|
Miscellaneous
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CommandResponse = {
|
||||||
|
status: number | 200 | 500;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if a string is a page.
|
* Checks if a string is a page.
|
||||||
*
|
*
|
||||||
|
@ -2,11 +2,23 @@ import React from "react";
|
|||||||
|
|
||||||
import Character from "@app/ui/widgets/Character";
|
import Character from "@app/ui/widgets/Character";
|
||||||
|
|
||||||
|
import type { Avatar } from "@backend/types";
|
||||||
import { listAvatars } from "@backend/data";
|
import { listAvatars } from "@backend/data";
|
||||||
|
import { grantAvatar } from "@backend/server";
|
||||||
|
|
||||||
import "@css/pages/AvatarsPage.scss";
|
import "@css/pages/AvatarsPage.scss";
|
||||||
|
|
||||||
class AvatarsPage extends React.PureComponent {
|
class AvatarsPage extends React.PureComponent {
|
||||||
|
/**
|
||||||
|
* Grants the avatar to the user.
|
||||||
|
*
|
||||||
|
* @param avatar The avatar to grant.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private async grantAvatar(avatar: Avatar): Promise<void> {
|
||||||
|
console.log(await grantAvatar(avatar.id));
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className={"AvatarsPage"}>
|
<div className={"AvatarsPage"}>
|
||||||
@ -14,7 +26,11 @@ class AvatarsPage extends React.PureComponent {
|
|||||||
|
|
||||||
<div className={"AvatarsPage_List"}>
|
<div className={"AvatarsPage_List"}>
|
||||||
{listAvatars().map((avatar) =>
|
{listAvatars().map((avatar) =>
|
||||||
avatar.id > 11000000 ? undefined : <Character key={avatar.id} data={avatar} />
|
avatar.id > 11000000 ? undefined :
|
||||||
|
<Character
|
||||||
|
key={avatar.id} data={avatar}
|
||||||
|
onClick={this.grantAvatar.bind(this, avatar)}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -5,6 +5,7 @@ import SideBarButton from "@app/ui/widgets/SideBarButton";
|
|||||||
import { navigate } from "@app/backend/events";
|
import { navigate } from "@app/backend/events";
|
||||||
|
|
||||||
import "@css/views/SideBar.scss";
|
import "@css/views/SideBar.scss";
|
||||||
|
import { setTargetPlayer } from "@backend/server";
|
||||||
|
|
||||||
interface IState {
|
interface IState {
|
||||||
uid: string | null;
|
uid: string | null;
|
||||||
@ -31,6 +32,7 @@ class SideBar extends React.Component<{}, IState> {
|
|||||||
if (uid && uid.length > 10) return;
|
if (uid && uid.length > 10) return;
|
||||||
|
|
||||||
this.setState({ uid });
|
this.setState({ uid });
|
||||||
|
setTargetPlayer(parseInt(uid ?? "0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
@ -18,6 +18,8 @@ const nameSwitch: { [key: number]: string } = {
|
|||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
data: Avatar;
|
data: Avatar;
|
||||||
|
|
||||||
|
onClick?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Character extends React.PureComponent<IProps> {
|
class Character extends React.PureComponent<IProps> {
|
||||||
@ -33,7 +35,11 @@ class Character extends React.PureComponent<IProps> {
|
|||||||
if (ignored.includes(id)) return undefined;
|
if (ignored.includes(id)) return undefined;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={"Character"} style={{ backgroundColor: `var(${qualityColor})` }}>
|
<div
|
||||||
|
className={"Character"}
|
||||||
|
style={{ backgroundColor: `var(${qualityColor})` }}
|
||||||
|
onClick={this.props.onClick}
|
||||||
|
>
|
||||||
<img
|
<img
|
||||||
className={"Character_Icon"}
|
className={"Character_Icon"}
|
||||||
alt={name}
|
alt={name}
|
||||||
|
Loading…
Reference in New Issue
Block a user