Implement handbook SDK for commands (avatars)

This commit is contained in:
KingRainbow44 2023-04-10 03:23:07 -04:00
parent 2bd992592d
commit 8cc725e45c
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
5 changed files with 65 additions and 2 deletions

View 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());
}

View File

@ -118,6 +118,11 @@ export enum ItemCategory {
Miscellaneous
}
export type CommandResponse = {
status: number | 200 | 500;
message: string;
}
/**
* Checks if a string is a page.
*

View File

@ -2,11 +2,23 @@ import React from "react";
import Character from "@app/ui/widgets/Character";
import type { Avatar } from "@backend/types";
import { listAvatars } from "@backend/data";
import { grantAvatar } from "@backend/server";
import "@css/pages/AvatarsPage.scss";
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() {
return (
<div className={"AvatarsPage"}>
@ -14,7 +26,11 @@ class AvatarsPage extends React.PureComponent {
<div className={"AvatarsPage_List"}>
{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>

View File

@ -5,6 +5,7 @@ import SideBarButton from "@app/ui/widgets/SideBarButton";
import { navigate } from "@app/backend/events";
import "@css/views/SideBar.scss";
import { setTargetPlayer } from "@backend/server";
interface IState {
uid: string | null;
@ -31,6 +32,7 @@ class SideBar extends React.Component<{}, IState> {
if (uid && uid.length > 10) return;
this.setState({ uid });
setTargetPlayer(parseInt(uid ?? "0"));
}
render() {

View File

@ -18,6 +18,8 @@ const nameSwitch: { [key: number]: string } = {
interface IProps {
data: Avatar;
onClick?: () => void;
}
class Character extends React.PureComponent<IProps> {
@ -33,7 +35,11 @@ class Character extends React.PureComponent<IProps> {
if (ignored.includes(id)) return undefined;
return (
<div className={"Character"} style={{ backgroundColor: `var(${qualityColor})` }}>
<div
className={"Character"}
style={{ backgroundColor: `var(${qualityColor})` }}
onClick={this.props.onClick}
>
<img
className={"Character_Icon"}
alt={name}