mirror of
https://github.com/guilhermewerner/tcc
synced 2025-06-16 23:15:08 +00:00
Add server and multi bots
This commit is contained in:
53
src/bot.js
53
src/bot.js
@ -1,24 +1,43 @@
|
||||
// Copyright (c) 2024 Guilherme Werner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import mineflayer from "mineflayer";
|
||||
import { mineflayer as mineflayerViewer } from "prismarine-viewer";
|
||||
import mineflayer from 'mineflayer';
|
||||
import { parentPort, workerData } from 'worker_threads';
|
||||
|
||||
const bot = mineflayer.createBot({
|
||||
host: 'localhost',
|
||||
port: 25565,
|
||||
username: 'Bot',
|
||||
auth: 'offline'
|
||||
})
|
||||
const { startBotIndex, endBotIndex } = workerData;
|
||||
const movements = ['left', 'right'];
|
||||
|
||||
bot.once('spawn', () => {
|
||||
mineflayerViewer(bot, { port: 3000, firstPerson: true })
|
||||
})
|
||||
function getRandomMovement() {
|
||||
return movements[Math.floor(Math.random() * movements.length)];
|
||||
}
|
||||
|
||||
bot.on('chat', (username, message) => {
|
||||
if (username === bot.username) return
|
||||
bot.chat(message)
|
||||
})
|
||||
for (let i = startBotIndex; i < endBotIndex; i++) {
|
||||
const bot = mineflayer.createBot({
|
||||
host: 'localhost',
|
||||
port: 25565,
|
||||
username: `Bot${i}`,
|
||||
auth: 'offline'
|
||||
});
|
||||
|
||||
bot.on('kicked', console.log)
|
||||
bot.on('error', console.log)
|
||||
bot.on('time2', () => {
|
||||
const movement = getRandomMovement();
|
||||
const action = Math.random() > 0.5;
|
||||
bot.setControlState(movement, action);
|
||||
bot.setControlState('forward', true);
|
||||
bot.setControlState('sprint', true);
|
||||
bot.setControlState('jump', true);
|
||||
});
|
||||
|
||||
bot.on('chat', (username, message) => {
|
||||
if (username === bot.username) return;
|
||||
bot.chat(message);
|
||||
});
|
||||
|
||||
bot.on('kicked', (reason, loggedIn) => {
|
||||
parentPort.postMessage(`Bot${i} foi expulso por: ${reason} | LoggedIn: ${loggedIn}`);
|
||||
});
|
||||
|
||||
bot.on('error', (err) => {
|
||||
parentPort.postMessage(`Bot${i} encontrou um erro: ${err}`);
|
||||
});
|
||||
}
|
||||
|
32
src/multi.js
Normal file
32
src/multi.js
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2024 Guilherme Werner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { Worker } from 'worker_threads';
|
||||
import path from 'path';
|
||||
|
||||
const numBots = 100;
|
||||
const botsPerThread = 10;
|
||||
const numThreads = Math.ceil(numBots / botsPerThread);
|
||||
|
||||
for (let i = 0; i < numThreads; i++) {
|
||||
const startBotIndex = i * botsPerThread;
|
||||
const endBotIndex = Math.min(startBotIndex + botsPerThread, numBots);
|
||||
|
||||
const worker = new Worker(path.resolve('src/bot.js'), {
|
||||
workerData: { startBotIndex, endBotIndex }
|
||||
});
|
||||
|
||||
worker.on('message', (message) => {
|
||||
console.log(`Worker message: ${message}`);
|
||||
});
|
||||
|
||||
worker.on('error', (error) => {
|
||||
console.error(`Worker error: ${error}`);
|
||||
});
|
||||
|
||||
worker.on('exit', (code) => {
|
||||
if (code !== 0) {
|
||||
console.error(`Worker stopped with exit code ${code}`);
|
||||
}
|
||||
});
|
||||
}
|
25
src/single.js
Normal file
25
src/single.js
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright (c) 2024 Guilherme Werner
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import mineflayer from "mineflayer";
|
||||
import { mineflayer as mineflayerViewer } from "prismarine-viewer";
|
||||
|
||||
const bot = mineflayer.createBot({
|
||||
host: 'localhost',
|
||||
port: 25565,
|
||||
username: 'Bot',
|
||||
auth: 'offline'
|
||||
})
|
||||
|
||||
bot.once('spawn', () => {
|
||||
bot.setControlState('forward', true)
|
||||
mineflayerViewer(bot, { port: 3000, firstPerson: true })
|
||||
})
|
||||
|
||||
bot.on('chat', (username, message) => {
|
||||
if (username === bot.username) return
|
||||
bot.chat(message)
|
||||
})
|
||||
|
||||
bot.on('kicked', console.log)
|
||||
bot.on('error', console.log)
|
Reference in New Issue
Block a user