diff --git a/server/src/routes/api/copy_chest_to_all_bots.ts b/server/src/routes/api/copy_chest_to_all_bots.ts new file mode 100644 index 0000000..7ea39b5 --- /dev/null +++ b/server/src/routes/api/copy_chest_to_all_bots.ts @@ -0,0 +1,25 @@ +import createHttpError from 'http-errors' +import { socketVariables } from '@/libs/socketVariables' +import express from 'express' +import { io } from "@/server"; + +const router = express.Router() + +router.post('/copy_chest_to_all_bots', async (req, res, next) => { + const chests = req.body.chests + + socketVariables.botsConnected.forEach(bot => { + io.timeout(5000).to(bot.socketId).emitWithAck("getConfig", {}) + .then((data) => { + const botConfig = data[0] + botConfig.chests = chests + io.timeout(5000).to(bot.socketId).emitWithAck("saveConfig", { botConfig }) + }) + .catch((error) => next(createHttpError(500, error))) + }) + + + res.json({ success: true }) +}) + +export default router \ No newline at end of file diff --git a/server/src/routes/api/index.ts b/server/src/routes/api/index.ts index f2ea3c4..b3c4885 100644 --- a/server/src/routes/api/index.ts +++ b/server/src/routes/api/index.ts @@ -7,6 +7,7 @@ import get_bot_config from './get_bot_config' import get_master_position from './get_master_position' import start_copy_master_position from './start_copy_master_position' import save_bot_config from './save_bot_config' +import copy_chest_to_all_bots from './copy_chest_to_all_bots' const router = express.Router() router.use(isAuthenticated) @@ -20,6 +21,7 @@ router.use("/", get_bot_config) router.use("/", get_master_position) router.use("/", start_copy_master_position) router.use("/", save_bot_config) +router.use("/", copy_chest_to_all_bots) router.use('/', (req, res, next) => next(createHttpError(404, "Endpoint not found"))) router.use('/', (error: unknown, req: Request, res: Response, next: NextFunction) => { diff --git a/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx b/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx index 02915d4..e71d9dd 100644 --- a/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx +++ b/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx @@ -5,6 +5,7 @@ import { Chest as ChestType, Item } from "base-types"; import { Vec3 } from "vec3"; import { v4 as uuidv4 } from 'uuid'; import { Chest } from "./Chest"; +import axios from "axios"; export const ConfigurebotChests: React.FC = () => { @@ -84,7 +85,12 @@ export const ConfigurebotChests: React.FC = () => { newChests[index] = chest updateConfig("chests", newChests) + } + const handleCopyChestToAllBots = () => { + if (window.confirm("Are you sure you want to copy the chest configuration to all bots?")) { + axios.post(`/api/copy_chest_to_all_bots`, { chests: botConfig.chests }) + } } return ( @@ -104,6 +110,10 @@ export const ConfigurebotChests: React.FC = () => { onChange={() => updateConfig("canCraftItemWithdrawChest", !botConfig.canCraftItemWithdrawChest)} /> +
+ +
+