From 60629633a25c3a6595c65cdf89a94ab8e115105d Mon Sep 17 00:00:00 2001 From: Sefirosweb Date: Sun, 7 Apr 2024 00:16:57 +0200 Subject: [PATCH] Fixing show process list --- core/src/BehaviorModules/BehaviorMoveTo.ts | 13 +---- .../NestedStateModules/MainStateMachine.ts | 56 +++++-------------- server/src/socketEvents/sendAction.ts | 47 +--------------- .../LayoutComponents/ModalAddBot.tsx | 7 ++- web/src/hooks/useGetSocket.ts | 14 ----- web/src/hooks/useSendActionSocket.ts | 18 ------ web/src/pages/ConfigureBot/ProcessList.tsx | 32 ++++++++--- web/src/pages/Dashboard/BotActionsButtons.tsx | 15 +++-- web/src/pages/Masterlist.tsx | 11 ++-- 9 files changed, 63 insertions(+), 150 deletions(-) delete mode 100644 web/src/hooks/useGetSocket.ts delete mode 100644 web/src/hooks/useSendActionSocket.ts diff --git a/core/src/BehaviorModules/BehaviorMoveTo.ts b/core/src/BehaviorModules/BehaviorMoveTo.ts index 302f7b4..7753482 100644 --- a/core/src/BehaviorModules/BehaviorMoveTo.ts +++ b/core/src/BehaviorModules/BehaviorMoveTo.ts @@ -70,10 +70,10 @@ export class BehaviorMoveTo implements StateBehavior { onStateExited() { this.bot.removeAllListeners('customEventPhysicTick') - this.isEndFinished = false - this.success = false this.bot.removeAllListeners('path_update') this.bot.removeAllListeners('goal_reached') + this.isEndFinished = false + this.success = false this.stopMoving() clearTimeout(this.timeLimit) } @@ -84,14 +84,6 @@ export class BehaviorMoveTo implements StateBehavior { this.isEndFinished = true } - setMoveTarget(position: Vec3WithDimension) { - if (this.targets.position === position) { - return - } - this.targets.position = position - this.restart() - } - stopMoving() { this.bot.removeAllListeners('customEventPhysicTick') this.bot.pathfinder.setGoal(null) @@ -134,6 +126,7 @@ export class BehaviorMoveTo implements StateBehavior { } restart() { + console.log('restarting...') if (!this.active) { return } diff --git a/core/src/NestedStateModules/MainStateMachine.ts b/core/src/NestedStateModules/MainStateMachine.ts index 6f6c10e..3b84ce8 100644 --- a/core/src/NestedStateModules/MainStateMachine.ts +++ b/core/src/NestedStateModules/MainStateMachine.ts @@ -13,37 +13,6 @@ import inventoryViewer from 'mineflayer-web-inventory' const startStateMachine = (bot: Bot) => { const { debugMode } = config - // const targets: LegionStateMachineTargets = { - // aListener: function (object, val) { - // if (!debugMode) return - // console.log(`Detected change ${object} value:`, val) - // }, - - // set position (val) { - // this.positionVal = val - // this.aListener('position', val) - // }, - // get position () { - // return this.positionVal - // }, - - // set entity (val) { - // this.entityVal = val - // this.aListener('entity', val) - // }, - // get entity () { - // return this.entityVal - // }, - - // set item (val) { - // this.itemVal = val - // this.aListener('item', val) - // }, - // get item () { - // return this.itemVal - // } - // } - const movements = new mineflayerPathfinder.Movements(bot) const targets: LegionStateMachineTargets = { @@ -198,7 +167,7 @@ const startStateMachine = (bot: Bot) => { if (debugMode) { // Only enable on debug mode - bot.on('newListener', () => { + const sentEvents = () => { const events = bot.eventNames() const eventsToSend: Array = [] events.forEach(event => { @@ -206,16 +175,21 @@ const startStateMachine = (bot: Bot) => { eventsToSend.push(`${eventName}: ${bot.listenerCount(eventName)}`) }) botWebsocket.emitEvents(eventsToSend) + } + + botWebsocket.on('action', (action) => { + if (action.type !== 'getProcessList') return + console.log('actionactionaction', action.type) + // bot.on('newListener', () => { + // }) + }) + + bot.on('newListener', () => { + sentEvents() }) bot.on('removeListener', () => { - const events = bot.eventNames() - const eventsToSend: Array = [] - events.forEach(event => { - const eventName = (typeof event === 'string' ? event : event.toString()) as keyof BotEvents - eventsToSend.push(`${eventName}: ${bot.listenerCount(eventName)}`) - }) - botWebsocket.emitEvents(eventsToSend) + sentEvents() }) webserver = new StateMachineWebserver(bot, stateMachine, 4550) @@ -277,14 +251,14 @@ const startStateMachine = (bot: Bot) => { } }) - function getChests() { + const getChests = () => { botWebsocket.emit('sendAction', { action: 'getChests', value: '' }) } - function getPortals() { + const getPortals = () => { botWebsocket.emit('sendAction', { action: 'getPortals', value: '' diff --git a/server/src/socketEvents/sendAction.ts b/server/src/socketEvents/sendAction.ts index e107b9e..e380b71 100644 --- a/server/src/socketEvents/sendAction.ts +++ b/server/src/socketEvents/sendAction.ts @@ -1,17 +1,13 @@ import { log } from "@/config"; -import { findBotBySocketId } from "@/libs/botStore"; import { socketVariables } from "@/libs/socketVariables"; import { io } from "@/server"; -import { sendBotsOnline, sendChests, sendMastersOnline, sendPortals } from "@/socketEmit"; -import { BotsConnected } from "base-types" +import { sendChests, sendMastersOnline, sendPortals } from "@/socketEmit"; import { Socket } from "socket.io"; export default (socket: Socket) => { const { masters, chests, setChests, portals, setPortals } = socketVariables socket.on("sendAction", (data) => { - let bot: BotsConnected | undefined; - log(data); switch (data.action) { @@ -19,47 +15,6 @@ export default (socket: Socket) => { io.to(data.socketId).emit("action", data.value); break; - case "startStateMachine": - bot = findBotBySocketId(data.socketId) - if (!bot) return - - io.to(data.socketId).emit("action", { - type: "startStateMachine", - value: data.value, - }); - - bot.stateMachinePort = data.value.port; - sendBotsOnline() - - break; - - case "startInventory": - bot = findBotBySocketId(data.socketId) - if (!bot) return - - io.to(data.socketId).emit("action", { - type: "startInventory", - value: data.value, - }); - - bot.inventoryPort = data.value.port; - sendBotsOnline() - break; - - case "startViewer": - bot = findBotBySocketId(data.socketId) - if (!bot) return - - io.to(data.socketId).emit("action", { - type: "startViewer", - value: data.value, - }); - - bot.viewerPort = data.value.port; - sendBotsOnline() - - break; - case "sendDisconnect": io.to(data.socketId).emit("sendDisconnect", data.value); break; diff --git a/web/src/components/LayoutComponents/ModalAddBot.tsx b/web/src/components/LayoutComponents/ModalAddBot.tsx index cdb9a4d..3d7250e 100644 --- a/web/src/components/LayoutComponents/ModalAddBot.tsx +++ b/web/src/components/LayoutComponents/ModalAddBot.tsx @@ -1,8 +1,9 @@ import Modal from 'react-bootstrap/Modal' import Button from 'react-bootstrap/Button' -import { Col, Form, Row } from 'react-bootstrap' +import { Form } from 'react-bootstrap' import { FormEvent, useRef, useState } from 'react' -import { useGetSocket } from '@/hooks/useGetSocket' +import { useStore } from "@/hooks/useStore"; + type Props = { show: boolean, @@ -14,7 +15,7 @@ export const ModalAddBot: React.FC = (props) => { const inputBotName = useRef(null) const [botName, setBotName] = useState('') const [botPassword, setBotPassword] = useState('') - const socket = useGetSocket() + const socket = useStore((state) => state.socket) const changeBotName = (e: React.ChangeEvent) => { setBotName(e.target.value.trim()) diff --git a/web/src/hooks/useGetSocket.ts b/web/src/hooks/useGetSocket.ts deleted file mode 100644 index cfbbe31..0000000 --- a/web/src/hooks/useGetSocket.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { useEffect, useState } from "react" -import { Socket } from "socket.io-client"; -import { useStore } from "./useStore"; - -export const useGetSocket = () => { - const [currentSocket, setCurrentSocket] = useState(null) - const socket = useStore((state) => state.socket) - - useEffect(() => { - setCurrentSocket(socket) - }, [socket]) - - return currentSocket -} diff --git a/web/src/hooks/useSendActionSocket.ts b/web/src/hooks/useSendActionSocket.ts deleted file mode 100644 index 08e0383..0000000 --- a/web/src/hooks/useSendActionSocket.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { useGetSocket } from "./useGetSocket" -import { useParams } from "react-router"; - -export const useSendActionSocket = () => { - const socket = useGetSocket() - const { selectedSocketId } = useParams() - - return (action: string, value?: any) => { - if (!socket || !selectedSocketId) return - - socket.emit("sendAction", { - action, - socketId: selectedSocketId, - value, - }); - } - -} \ No newline at end of file diff --git a/web/src/pages/ConfigureBot/ProcessList.tsx b/web/src/pages/ConfigureBot/ProcessList.tsx index d9dcf76..59c4c02 100644 --- a/web/src/pages/ConfigureBot/ProcessList.tsx +++ b/web/src/pages/ConfigureBot/ProcessList.tsx @@ -1,20 +1,34 @@ import React, { useContext, useEffect, useState } from "react"; import { useStore } from "@/hooks/useStore"; +import { useParams } from "react-router"; +import { BotSelectedContext } from "./ConfigurationContext"; + export const ProcessList: React.FC = () => { + const { bot } = useContext(BotSelectedContext); + + const { selectedSocketId } = useParams() const [socket] = useStore(state => [state.socket]) const [events, setEvents] = useState([]) useEffect(() => { // pending to fix - socket?.on('receiveDirectMessage', ({ type, message }: { type: string, message: Array }) => { - if (type !== 'current_events') return - setEvents(message) + // socket?.on('receiveDirectMessage', ({ type, message }: { type: string, message: Array }) => { + // if (type !== 'current_events') return + // setEvents(message) + // }); + + socket?.emit('sendAction', { + action: 'action', + socketId: selectedSocketId, + value: { + type: 'getProcessList' + } }); - return () => { - socket?.off("receiveDirectMessage"); - } + // return () => { + // socket?.off("receiveDirectMessage"); + // } }, [setEvents]) return ( @@ -22,9 +36,13 @@ export const ProcessList: React.FC = () => {

List of all events used by bot

    - {events.map((e, i) => ( + {bot.events.map((e, i) => (
  • {e}
  • ))} + + {/* {events.map((e, i) => ( +
  • {e}
  • + ))} */}
diff --git a/web/src/pages/Dashboard/BotActionsButtons.tsx b/web/src/pages/Dashboard/BotActionsButtons.tsx index d9ea31c..bba5c97 100644 --- a/web/src/pages/Dashboard/BotActionsButtons.tsx +++ b/web/src/pages/Dashboard/BotActionsButtons.tsx @@ -1,17 +1,24 @@ import { useGetSelectedBot } from "@/hooks/useGetSelectedBot"; -import { useSendActionSocket } from "@/hooks/useSendActionSocket"; import { useStore } from "@/hooks/useStore"; import { Bot } from "@/types"; import React, { ChangeEvent, KeyboardEvent, useEffect, useState } from "react"; import { Button, Col, Row } from "react-bootstrap"; import { useNavigate, useParams } from "react-router"; + export const BotActionButtons: React.FC = () => { + const selectedSocketId = useParams().selectedSocketId const bot = useGetSelectedBot() as Bot const navigate = useNavigate(); - const sendAction = useSendActionSocket() - const selectedSocketId = useParams().selectedSocketId - const master = useStore(state => state.master) + const [master, socket] = useStore(state => [state.master, state.socket]) + + const sendAction = (action: string, value?: any) => { + socket?.emit("sendAction", { + action, + socketId: selectedSocketId, + value, + }); + } const [chat, setChat] = useState(""); diff --git a/web/src/pages/Masterlist.tsx b/web/src/pages/Masterlist.tsx index 6686d05..3661455 100644 --- a/web/src/pages/Masterlist.tsx +++ b/web/src/pages/Masterlist.tsx @@ -1,12 +1,9 @@ -import { useGetSocket } from '@/hooks/useGetSocket'; -import { useStore } from '@/hooks/useStore'; import React, { useState } from 'react' import { Form } from 'react-bootstrap'; -import { Socket } from 'socket.io-client'; +import { useStore } from "@/hooks/useStore"; export const Masterlist: React.FC = () => { - const socket = useGetSocket() as Socket - const master = useStore(state => state.master) + const [socket, master] = useStore((state) => [state.socket, state.master]) const setMaster = useStore(state => state.setMaster) const masters = useStore(state => state.masters) @@ -28,7 +25,7 @@ export const Masterlist: React.FC = () => { } const handleSendMessageButton = () => { - socket.emit('sendAction', { + socket?.emit('sendAction', { action: 'addMaster', value: inputBox }) @@ -37,7 +34,7 @@ export const Masterlist: React.FC = () => { const handleRemoveMaster = (masterToDelete: string) => { if (masterToDelete === master) return - socket.emit('sendAction', { + socket?.emit('sendAction', { action: 'removeMaster', value: masterToDelete })