Skip to content

Commit

Permalink
Fixing show process list
Browse files Browse the repository at this point in the history
  • Loading branch information
sefirosweb committed Apr 6, 2024
1 parent 4d2b5d3 commit 6062963
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 150 deletions.
13 changes: 3 additions & 10 deletions core/src/BehaviorModules/BehaviorMoveTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -134,6 +126,7 @@ export class BehaviorMoveTo implements StateBehavior {
}

restart() {
console.log('restarting...')
if (!this.active) {
return
}
Expand Down
56 changes: 15 additions & 41 deletions core/src/NestedStateModules/MainStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -198,24 +167,29 @@ const startStateMachine = (bot: Bot) => {

if (debugMode) { // Only enable on debug mode

bot.on('newListener', () => {
const sentEvents = () => {
const events = bot.eventNames()
const eventsToSend: Array<string> = []
events.forEach(event => {
const eventName = (typeof event === 'string' ? event : event.toString()) as keyof BotEvents
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<string> = []
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)
Expand Down Expand Up @@ -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: ''
Expand Down
47 changes: 1 addition & 46 deletions server/src/socketEvents/sendAction.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,20 @@
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) {
case "action":
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;
Expand Down
7 changes: 4 additions & 3 deletions web/src/components/LayoutComponents/ModalAddBot.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,7 +15,7 @@ export const ModalAddBot: React.FC<Props> = (props) => {
const inputBotName = useRef<HTMLInputElement>(null)
const [botName, setBotName] = useState('')
const [botPassword, setBotPassword] = useState('')
const socket = useGetSocket()
const socket = useStore((state) => state.socket)

const changeBotName = (e: React.ChangeEvent<HTMLInputElement>) => {
setBotName(e.target.value.trim())
Expand Down
14 changes: 0 additions & 14 deletions web/src/hooks/useGetSocket.ts

This file was deleted.

18 changes: 0 additions & 18 deletions web/src/hooks/useSendActionSocket.ts

This file was deleted.

32 changes: 25 additions & 7 deletions web/src/pages/ConfigureBot/ProcessList.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
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<string[]>([])

useEffect(() => {
// pending to fix
socket?.on('receiveDirectMessage', ({ type, message }: { type: string, message: Array<string> }) => {
if (type !== 'current_events') return
setEvents(message)
// socket?.on('receiveDirectMessage', ({ type, message }: { type: string, message: Array<string> }) => {
// 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 (
<>
<h4>List of all events used by bot</h4>
<div className='border border-warning'>
<ul>
{events.map((e, i) => (
{bot.events.map((e, i) => (
<li key={i}>{e}</li>
))}

{/* {events.map((e, i) => (
<li key={i}>{e}</li>
))} */}
</ul>
</div>
</>
Expand Down
15 changes: 11 additions & 4 deletions web/src/pages/Dashboard/BotActionsButtons.tsx
Original file line number Diff line number Diff line change
@@ -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("");

Expand Down
11 changes: 4 additions & 7 deletions web/src/pages/Masterlist.tsx
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -28,7 +25,7 @@ export const Masterlist: React.FC = () => {
}

const handleSendMessageButton = () => {
socket.emit('sendAction', {
socket?.emit('sendAction', {
action: 'addMaster',
value: inputBox
})
Expand All @@ -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
})
Expand Down

0 comments on commit 6062963

Please sign in to comment.