bonktools 2.0.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +191 -480
- package/bonk_fullchain.pem +36 -0
- package/examples/host-bot.js +63 -0
- package/examples/simple-bot.js +127 -0
- package/old/bonkbot.js +1266 -0
- package/old/examplebot.js +64 -0
- package/package.json +43 -64
- package/scripts/download-cert.js +121 -0
- package/src/bot.js +1498 -0
- package/src/index.js +23 -0
- package/src/packet.js +374 -0
- package/src/utils/constants.js +153 -0
- package/src/utils/errors.js +137 -0
- package/src/utils/logger.js +109 -0
- package/src/utils/validation.js +130 -0
- package/dist/connection/BonkConnection.d.ts +0 -173
- package/dist/connection/BonkConnection.js +0 -482
- package/dist/connection/PacketBuilder.d.ts +0 -13
- package/dist/connection/PacketBuilder.js +0 -25
- package/dist/connection/PacketParser.d.ts +0 -14
- package/dist/connection/PacketParser.js +0 -26
- package/dist/connection/types.d.ts +0 -0
- package/dist/connection/types.js +0 -1
- package/dist/game/GameLoop.d.ts +0 -0
- package/dist/game/GameLoop.js +0 -1
- package/dist/index.d.ts +0 -48
- package/dist/index.js +0 -73
- package/dist/simulator/GameState.d.ts +0 -0
- package/dist/simulator/GameState.js +0 -1
- package/dist/simulator/PhysicsWorld.d.ts +0 -0
- package/dist/simulator/PhysicsWorld.js +0 -1
- package/dist/types/events.d.ts +0 -0
- package/dist/types/events.js +0 -1
- package/dist/types/index.d.ts +0 -0
- package/dist/types/index.js +0 -1
- package/dist/utils/constants.d.ts +0 -175
- package/dist/utils/constants.js +0 -187
- package/dist/utils/logger.d.ts +0 -31
- package/dist/utils/logger.js +0 -133
- package/dist/utils/validation.d.ts +0 -5
- package/dist/utils/validation.js +0 -28
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger utility for BonkBot
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// Log levels
|
|
6
|
+
const LOG_LEVELS = {
|
|
7
|
+
DEBUG: 0,
|
|
8
|
+
INFO: 1,
|
|
9
|
+
WARN: 2,
|
|
10
|
+
ERROR: 3,
|
|
11
|
+
NONE: 4,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Logger class for consistent logging throughout the library
|
|
16
|
+
*/
|
|
17
|
+
class Logger {
|
|
18
|
+
/**
|
|
19
|
+
* Create a new logger
|
|
20
|
+
* @param {string} name - Name of the logger (typically the module name)
|
|
21
|
+
* @param {number} level - Minimum log level to display (defaults to WARN)
|
|
22
|
+
*/
|
|
23
|
+
constructor(name, level = LOG_LEVELS.WARN) {
|
|
24
|
+
this.name = name;
|
|
25
|
+
this.level = level;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Set the log level
|
|
30
|
+
* @param {number} level - New log level
|
|
31
|
+
*/
|
|
32
|
+
setLevel(level) {
|
|
33
|
+
this.level = level;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Format a log message
|
|
38
|
+
* @param {string} level - Log level name
|
|
39
|
+
* @param {string} message - Log message
|
|
40
|
+
* @returns {string} Formatted log message
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
43
|
+
_format(level, message) {
|
|
44
|
+
return `[${level}] [${this.name}] ${message}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Log a debug message
|
|
49
|
+
* @param {string} message - Message to log
|
|
50
|
+
* @param {any} [data] - Optional data to include
|
|
51
|
+
*/
|
|
52
|
+
debug(message, data) {
|
|
53
|
+
if (this.level <= LOG_LEVELS.DEBUG) {
|
|
54
|
+
const formattedMessage = this._format("DEBUG", message);
|
|
55
|
+
console.debug(formattedMessage, data !== undefined ? data : "");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Log an info message
|
|
61
|
+
* @param {string} message - Message to log
|
|
62
|
+
* @param {any} [data] - Optional data to include
|
|
63
|
+
*/
|
|
64
|
+
info(message, data) {
|
|
65
|
+
if (this.level <= LOG_LEVELS.INFO) {
|
|
66
|
+
const formattedMessage = this._format("INFO", message);
|
|
67
|
+
console.info(formattedMessage, data !== undefined ? data : "");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Log a warning message
|
|
73
|
+
* @param {string} message - Message to log
|
|
74
|
+
* @param {any} [data] - Optional data to include
|
|
75
|
+
*/
|
|
76
|
+
warn(message, data) {
|
|
77
|
+
if (this.level <= LOG_LEVELS.WARN) {
|
|
78
|
+
const formattedMessage = this._format("WARN", message);
|
|
79
|
+
console.warn(formattedMessage, data !== undefined ? data : "");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Log an error message
|
|
85
|
+
* @param {string} message - Message to log
|
|
86
|
+
* @param {Error|any} [error] - Optional error or data to include
|
|
87
|
+
*/
|
|
88
|
+
error(message, error) {
|
|
89
|
+
if (this.level <= LOG_LEVELS.ERROR) {
|
|
90
|
+
const formattedMessage = this._format("ERROR", message);
|
|
91
|
+
console.error(formattedMessage, error !== undefined ? error : "");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Create a new logger instance
|
|
98
|
+
* @param {string} name - Name of the logger
|
|
99
|
+
* @param {number} level - Minimum log level
|
|
100
|
+
* @returns {Logger} New logger instance
|
|
101
|
+
*/
|
|
102
|
+
function createLogger(name, level = LOG_LEVELS.INFO) {
|
|
103
|
+
return new Logger(name, level);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = {
|
|
107
|
+
LOG_LEVELS,
|
|
108
|
+
createLogger,
|
|
109
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation utilities for BonkBot
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates account configuration
|
|
7
|
+
* @param {Object} account - Account configuration object
|
|
8
|
+
* @returns {Object} Validated account object with defaults applied
|
|
9
|
+
* @throws {Error} If required fields are missing
|
|
10
|
+
*/
|
|
11
|
+
function validateAccount(account = {}) {
|
|
12
|
+
// Clone to avoid modifying the original
|
|
13
|
+
const validatedAccount = { ...account };
|
|
14
|
+
|
|
15
|
+
// Set default guest status if not provided
|
|
16
|
+
if (validatedAccount.guest === undefined) {
|
|
17
|
+
validatedAccount.guest = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// If not a guest, username and password are required
|
|
21
|
+
if (!validatedAccount.guest) {
|
|
22
|
+
if (!validatedAccount.username) {
|
|
23
|
+
throw new Error("Username is required for non-guest accounts");
|
|
24
|
+
}
|
|
25
|
+
if (!validatedAccount.password) {
|
|
26
|
+
throw new Error("Password is required for non-guest accounts");
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
// For guests, generate a random username if not provided
|
|
30
|
+
if (!validatedAccount.username) {
|
|
31
|
+
validatedAccount.username = `BonkBot-${Math.random()
|
|
32
|
+
.toString()
|
|
33
|
+
.substr(2, 5)}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return validatedAccount;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validates room configuration
|
|
42
|
+
* @param {Object} options - Room configuration options
|
|
43
|
+
* @returns {Object} Validated room options with defaults applied
|
|
44
|
+
*/
|
|
45
|
+
function validateRoomOptions(options = {}) {
|
|
46
|
+
return {
|
|
47
|
+
roomname:
|
|
48
|
+
options.roomname ||
|
|
49
|
+
`BonkBot Room ${Math.random().toString().substr(2, 5)}`,
|
|
50
|
+
maxplayers: options.maxplayers || 8,
|
|
51
|
+
roompassword: options.roompassword || "",
|
|
52
|
+
basecolor: options.basecolor !== undefined ? options.basecolor : 0,
|
|
53
|
+
skin: options.skin,
|
|
54
|
+
peerid: options.peerid || generatePeerID(),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Validates join room options
|
|
60
|
+
* @param {Object} options - Join room options
|
|
61
|
+
* @throws {Error} If required fields are missing
|
|
62
|
+
* @returns {Object} Validated join options
|
|
63
|
+
*/
|
|
64
|
+
function validateJoinOptions(options = {}) {
|
|
65
|
+
if (!options.address) {
|
|
66
|
+
throw new Error("Room address is required to join a room");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
address: options.address,
|
|
71
|
+
roompassword: options.roompassword || "",
|
|
72
|
+
basecolor: options.basecolor !== undefined ? options.basecolor : 16448250,
|
|
73
|
+
skin: options.skin,
|
|
74
|
+
peerid: options.peerid || generatePeerID(),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Generates a random peer ID
|
|
80
|
+
* @returns {string} Random peer ID
|
|
81
|
+
*/
|
|
82
|
+
function generatePeerID() {
|
|
83
|
+
return Math.random().toString(36).substr(2, 10) + "v00000";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Validates that a value is a non-empty string
|
|
88
|
+
* @param {any} value - Value to validate
|
|
89
|
+
* @param {string} name - Name of the parameter for error messages
|
|
90
|
+
* @throws {Error} If validation fails
|
|
91
|
+
*/
|
|
92
|
+
function validateString(value, name) {
|
|
93
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
94
|
+
throw new Error(`${name} must be a non-empty string`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Validates that a value is a number
|
|
100
|
+
* @param {any} value - Value to validate
|
|
101
|
+
* @param {string} name - Name of the parameter for error messages
|
|
102
|
+
* @throws {Error} If validation fails
|
|
103
|
+
*/
|
|
104
|
+
function validateNumber(value, name) {
|
|
105
|
+
if (typeof value !== "number" || isNaN(value)) {
|
|
106
|
+
throw new Error(`${name} must be a number`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Validates that a value is a boolean
|
|
112
|
+
* @param {any} value - Value to validate
|
|
113
|
+
* @param {string} name - Name of the parameter for error messages
|
|
114
|
+
* @throws {Error} If validation fails
|
|
115
|
+
*/
|
|
116
|
+
function validateBoolean(value, name) {
|
|
117
|
+
if (typeof value !== "boolean") {
|
|
118
|
+
throw new Error(`${name} must be a boolean`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = {
|
|
123
|
+
validateAccount,
|
|
124
|
+
validateRoomOptions,
|
|
125
|
+
validateJoinOptions,
|
|
126
|
+
validateString,
|
|
127
|
+
validateNumber,
|
|
128
|
+
validateBoolean,
|
|
129
|
+
generatePeerID,
|
|
130
|
+
};
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
|
-
export type Account = {
|
|
3
|
-
username: string;
|
|
4
|
-
password?: string;
|
|
5
|
-
guest: boolean;
|
|
6
|
-
};
|
|
7
|
-
export type RoomInfo = {
|
|
8
|
-
address: string | null;
|
|
9
|
-
name: string | null;
|
|
10
|
-
server: string;
|
|
11
|
-
bypass: string;
|
|
12
|
-
id: number | null;
|
|
13
|
-
dbid: number | null;
|
|
14
|
-
teamsLocked: boolean;
|
|
15
|
-
map: any;
|
|
16
|
-
inGame: boolean;
|
|
17
|
-
};
|
|
18
|
-
export type GameInfo = {
|
|
19
|
-
id: number | null;
|
|
20
|
-
host: number | null;
|
|
21
|
-
banned: boolean;
|
|
22
|
-
};
|
|
23
|
-
export type Player = {
|
|
24
|
-
id: number;
|
|
25
|
-
username: string;
|
|
26
|
-
guest: boolean;
|
|
27
|
-
peerID: string;
|
|
28
|
-
};
|
|
29
|
-
export type JoinOptions = {
|
|
30
|
-
password?: string;
|
|
31
|
-
peerID?: string;
|
|
32
|
-
};
|
|
33
|
-
export type CreateRoomOptions = {
|
|
34
|
-
roomname?: string;
|
|
35
|
-
maxplayers?: number;
|
|
36
|
-
password?: string;
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Gerencia a conexão WebSocket com o servidor Bonk.io e o estado do jogo.
|
|
40
|
-
*/
|
|
41
|
-
export declare class BonkConnection extends EventEmitter {
|
|
42
|
-
private socket;
|
|
43
|
-
private connected;
|
|
44
|
-
private keepAliveTimer;
|
|
45
|
-
private PROTOCOL_VERSION;
|
|
46
|
-
account: Account;
|
|
47
|
-
server: string;
|
|
48
|
-
room: RoomInfo;
|
|
49
|
-
game: GameInfo;
|
|
50
|
-
players: Map<number, Player>;
|
|
51
|
-
token: string | null;
|
|
52
|
-
peerID: string;
|
|
53
|
-
avatar: any;
|
|
54
|
-
location: any;
|
|
55
|
-
constructor(account: Account, server?: string);
|
|
56
|
-
/**
|
|
57
|
-
* Gera um ID de par (peerID) aleatório.
|
|
58
|
-
* @returns {string} O peerID gerado.
|
|
59
|
-
*/
|
|
60
|
-
private generatePeerID;
|
|
61
|
-
/**
|
|
62
|
-
* Obtém o token de autenticação do usuário.
|
|
63
|
-
* (Implementação baseada no bonkbot original, usando axios)
|
|
64
|
-
* @param {string} username - Nome de usuário.
|
|
65
|
-
* @param {string} password - Senha.
|
|
66
|
-
* @returns {Promise<string>} O token de autenticação.
|
|
67
|
-
*/
|
|
68
|
-
private getToken;
|
|
69
|
-
/**
|
|
70
|
-
* Obtém informações do servidor (localização, etc.).
|
|
71
|
-
* (Implementação baseada no bonkbot original)
|
|
72
|
-
* @returns {Promise<any>} Informações do servidor.
|
|
73
|
-
*/
|
|
74
|
-
private getServerInfo;
|
|
75
|
-
/**
|
|
76
|
-
* Inicializa a conexão (autenticação, obtenção de info do servidor).
|
|
77
|
-
* @returns {Promise<BonkConnection>} Esta instância.
|
|
78
|
-
*/
|
|
79
|
-
init(): Promise<BonkConnection>;
|
|
80
|
-
/**
|
|
81
|
-
* Inicia o temporizador de keep-alive.
|
|
82
|
-
*/
|
|
83
|
-
private startKeepAlive;
|
|
84
|
-
/**
|
|
85
|
-
* Para o bot (limpa timers, etc.).
|
|
86
|
-
*/
|
|
87
|
-
private stopBot;
|
|
88
|
-
/**
|
|
89
|
-
* Configura os ouvintes de eventos do Socket.IO.
|
|
90
|
-
*/
|
|
91
|
-
private setupSocketEvents;
|
|
92
|
-
/**
|
|
93
|
-
* Conecta-se ao servidor Bonk.io.
|
|
94
|
-
* @returns {Promise<BonkConnection>} Esta instância.
|
|
95
|
-
*/
|
|
96
|
-
connect(): Promise<BonkConnection>;
|
|
97
|
-
/**
|
|
98
|
-
* Desconecta do servidor.
|
|
99
|
-
*/
|
|
100
|
-
disconnect(): void;
|
|
101
|
-
/**
|
|
102
|
-
* Envia uma mensagem (pacote) para o servidor.
|
|
103
|
-
* @param {number} type - Tipo da mensagem (CLIENT_MESSAGE_TYPES).
|
|
104
|
-
* @param {any} data - Dados da mensagem.
|
|
105
|
-
*/
|
|
106
|
-
sendMessage(type: number, data: any): void;
|
|
107
|
-
/**
|
|
108
|
-
* Envia o pacote TIMESYNC (keep-alive).
|
|
109
|
-
*/
|
|
110
|
-
private sendTimesync;
|
|
111
|
-
/**
|
|
112
|
-
* Envia uma mensagem de chat para a sala.
|
|
113
|
-
* @param {string} message - A mensagem a ser enviada.
|
|
114
|
-
*/
|
|
115
|
-
sendChat(message: string): void;
|
|
116
|
-
/**
|
|
117
|
-
* Envia um comando de input do jogador (movimento).
|
|
118
|
-
* @param {number} input - O valor do input (ex: 1 para esquerda, 2 para direita, etc.).
|
|
119
|
-
* @param {number} frame - O frame atual do jogo.
|
|
120
|
-
* @param {number} sequence - O número de sequência do input.
|
|
121
|
-
*/
|
|
122
|
-
sendInput(input: number, frame: number, sequence: number): void;
|
|
123
|
-
/**
|
|
124
|
-
* Envia o comando para iniciar o jogo.
|
|
125
|
-
*/
|
|
126
|
-
startGame(): void;
|
|
127
|
-
/**
|
|
128
|
-
* Envia o comando para mudar de time.
|
|
129
|
-
* @param {number} team - O ID do time (1 a 5, ou 0 para espectador).
|
|
130
|
-
*/
|
|
131
|
-
changeTeam(team: number): void;
|
|
132
|
-
/**
|
|
133
|
-
* Envia o comando para definir o status de pronto/não pronto.
|
|
134
|
-
* @param {boolean} ready - True para pronto, False para não pronto.
|
|
135
|
-
*/
|
|
136
|
-
setReady(ready: boolean): void;
|
|
137
|
-
/**
|
|
138
|
-
* Envia o comando para chutar/banir um jogador.
|
|
139
|
-
* @param {number} id - O ID do jogador.
|
|
140
|
-
* @param {boolean} ban - Se deve banir (true) ou apenas chutar (false).
|
|
141
|
-
*/
|
|
142
|
-
kickBanPlayer(id: number, ban?: boolean): void;
|
|
143
|
-
/**
|
|
144
|
-
* Cria uma nova sala.
|
|
145
|
-
* @param {CreateRoomOptions} options - Opções de criação de sala.
|
|
146
|
-
* @returns {Promise<RoomInfo>} Informações da sala criada.
|
|
147
|
-
*/
|
|
148
|
-
createRoom(options?: CreateRoomOptions): Promise<RoomInfo>;
|
|
149
|
-
/**
|
|
150
|
-
* Entra em uma sala existente.
|
|
151
|
-
* @param {string} roomAddress - Endereço da sala (ex: '123456' ou '123456abcde').
|
|
152
|
-
* @param {JoinOptions} options - Opções de entrada.
|
|
153
|
-
* @returns {Promise<void>}
|
|
154
|
-
*/
|
|
155
|
-
joinRoom(roomAddress: string, options?: JoinOptions): Promise<void>;
|
|
156
|
-
/**
|
|
157
|
-
* Define o endereço da sala.
|
|
158
|
-
* @param {any} addressInfo - Informações de endereço da sala.
|
|
159
|
-
*/
|
|
160
|
-
private setAddress;
|
|
161
|
-
/**
|
|
162
|
-
* Obtém informações de endereço da sala a partir de uma URL/ID.
|
|
163
|
-
* (Implementação baseada no bonkbot original)
|
|
164
|
-
* @param {string} url - URL ou ID da sala.
|
|
165
|
-
* @returns {Promise<any>} Informações de endereço.
|
|
166
|
-
*/
|
|
167
|
-
private getAddressFromUrl;
|
|
168
|
-
/**
|
|
169
|
-
* Manipula os pacotes recebidos do servidor.
|
|
170
|
-
* @param {any} packet - Pacote analisado.
|
|
171
|
-
*/
|
|
172
|
-
private handlePacket;
|
|
173
|
-
}
|