Skip to content

Commit

Permalink
Added method to copy chest configuration to all bots
Browse files Browse the repository at this point in the history
  • Loading branch information
sefirosweb committed Apr 1, 2024
1 parent f947376 commit 96f01b2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions server/src/routes/api/copy_chest_to_all_bots.ts
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions server/src/routes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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 (
Expand All @@ -104,6 +110,10 @@ export const ConfigurebotChests: React.FC = () => {
onChange={() => updateConfig("canCraftItemWithdrawChest", !botConfig.canCraftItemWithdrawChest)}
/>

<div className="mb-3">
<Button onClick={handleCopyChestToAllBots}>Copy chest configuration to all bots</Button>
</div>

<Row>
<Col>
<ListGroup className='mb-3'>
Expand Down

0 comments on commit 96f01b2

Please sign in to comment.