-
Notifications
You must be signed in to change notification settings - Fork 896
Expand file tree
/
Copy pathaction_logger.js
More file actions
197 lines (172 loc) · 6.41 KB
/
Copy pathaction_logger.js
File metadata and controls
197 lines (172 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { appendFileSync, mkdirSync, writeFileSync } from 'fs';
import path from 'path';
import settings from '../../settings.js';
const ACTION_LOG_STATE = Symbol.for('mindcraft.actionLogState');
export function isActionLoggingEnabled() {
return Boolean(settings.action_logging ?? settings.action_logging_enabled ?? false);
}
function timestampForFilename(date = new Date()) {
return date.toISOString().replace(/[:.]/g, '-');
}
function safeName(name, fallback = 'andy') {
return String(name || fallback).replace(/[^a-zA-Z0-9_-]/g, '_');
}
function getBotLogDirectory(bot) {
const botName = safeName(bot?.username || bot?.name);
return path.join('.', 'bots', botName, 'logs');
}
function getActionLogPath(bot) {
if (!bot[ACTION_LOG_STATE]) {
const logDir = getBotLogDirectory(bot);
mkdirSync(logDir, { recursive: true });
const prefix = safeName(settings.action_log_file_prefix || 'andy_log', 'andy_log');
const logPath = path.resolve(logDir, `${prefix}_${timestampForFilename()}.txt`);
writeFileSync(logPath, '', 'utf8');
bot[ACTION_LOG_STATE] = { logPath };
}
return bot[ACTION_LOG_STATE].logPath;
}
function blockCoord(coord) {
if (!coord) return null;
return {
x: Math.floor(coord.x),
y: Math.floor(coord.y),
z: Math.floor(coord.z),
};
}
function entityPosition(bot) {
const pos = bot?.entity?.position;
if (!pos) return null;
return { x: pos.x, y: pos.y, z: pos.z };
}
function standingState(bot) {
const pos = bot?.entity?.position;
if (!pos) {
return { position: null, blockName: 'unknown' };
}
const standing = {
x: Math.floor(pos.x),
y: Math.floor(pos.y - 0.01),
z: Math.floor(pos.z),
};
const blockName = blockNameAt(bot, standing);
return { position: standing, blockName };
}
function blockNameAt(bot, coord) {
if (!bot || !coord) return 'unknown';
try {
const pos = {
x: Math.floor(coord.x),
y: Math.floor(coord.y),
z: Math.floor(coord.z),
};
pos.floor = () => pos;
pos.floored = () => pos;
const block = bot.blockAt(pos);
return block?.name || 'unknown';
} catch (_) {
return 'unknown';
}
}
function heldItemName(bot) {
return bot?.heldItem?.name || 'none';
}
function getSneakState(bot) {
if (typeof bot?.getControlState === 'function') {
return Boolean(bot.getControlState('sneak'));
}
return Boolean(bot?.controlState?.sneak);
}
function getBotTick(bot) {
const candidates = [bot?.time?.age, bot?.time?.time, bot?.time?.timeOfDay];
return candidates.find(value => Number.isFinite(value)) ?? 'unknown';
}
function radToDeg(radians) {
return radians * 180 / Math.PI;
}
function actualYawDegrees(bot) {
return Number.isFinite(bot?.entity?.yaw) ? radToDeg(bot.entity.yaw) : null;
}
function actualPitchDegrees(bot) {
return Number.isFinite(bot?.entity?.pitch) ? radToDeg(bot.entity.pitch) : null;
}
function actionValue(value) {
if (value === null || value === undefined || value === '') return 'unknown';
return String(value);
}
function intCoordText(coord) {
if (!coord) return 'unknown';
return `[${Math.floor(coord.x)},${Math.floor(coord.y)},${Math.floor(coord.z)}]`;
}
function floatText(value, precision = 6) {
return Number.isFinite(value) ? Number(value).toFixed(precision) : 'unknown';
}
function floatCoordText(coord, precision = 6) {
if (!coord) return 'unknown';
return `[${floatText(coord.x, precision)},${floatText(coord.y, precision)},${floatText(coord.z, precision)}]`;
}
function actionNumber(value, precision = 3) {
return Number.isFinite(value) ? Number(value).toFixed(precision) : 'unknown';
}
export function faceNameFromVector(vec) {
if (!vec) return 'unknown';
if (vec.x === 1 && vec.y === 0 && vec.z === 0) return 'east';
if (vec.x === -1 && vec.y === 0 && vec.z === 0) return 'west';
if (vec.x === 0 && vec.y === 1 && vec.z === 0) return 'up';
if (vec.x === 0 && vec.y === -1 && vec.z === 0) return 'down';
if (vec.x === 0 && vec.y === 0 && vec.z === 1) return 'south';
if (vec.x === 0 && vec.y === 0 && vec.z === -1) return 'north';
return 'unknown';
}
export function formatActionLogLine(record) {
return [
`ACTION ${record.type}`,
`item=${actionValue(record.item)}`,
`previous_block=${actionValue(record.previousBlock)}`,
`result_block=${actionValue(record.resultBlock)}`,
`result_coord=${intCoordText(record.resultCoord)}`,
`clicked_block=${intCoordText(record.clickedBlock)}`,
`clicked_face=${actionValue(record.clickedFace)}`,
`player_pos=${floatCoordText(record.playerPos, 6)}`,
`yaw=${actionNumber(record.yaw, 3)}`,
`pitch=${actionNumber(record.pitch, 3)}`,
`sneaking=${record.sneaking === null || record.sneaking === undefined ? 'unknown' : String(Boolean(record.sneaking))}`,
`standing_on=${intCoordText(record.standingOn)}`,
`standing_on_block=${actionValue(record.standingOnBlock)}`,
`hand=${actionValue(record.hand)}`,
`tick=${actionValue(record.tick)}`,
`timestamp=${actionValue(record.timestamp)}`,
].join(' ');
}
export function appendActionLog(bot, action) {
if (!bot || !isActionLoggingEnabled()) {
return false;
}
try {
const standing = standingState(bot);
const resultCoord = blockCoord(action.resultCoord);
const record = {
type: action.type,
item: action.item || heldItemName(bot),
previousBlock: action.previousBlock,
resultBlock: action.resultBlock ?? blockNameAt(bot, resultCoord),
resultCoord,
clickedBlock: blockCoord(action.clickedBlock),
clickedFace: action.clickedFace,
playerPos: entityPosition(bot),
yaw: actualYawDegrees(bot),
pitch: actualPitchDegrees(bot),
sneaking: getSneakState(bot),
standingOn: standing.position,
standingOnBlock: standing.blockName,
hand: action.hand || 'main_hand',
tick: getBotTick(bot),
timestamp: new Date().toISOString(),
};
appendFileSync(getActionLogPath(bot), `${formatActionLogLine(record)}\n`, 'utf8');
return true;
} catch (error) {
console.warn(`Failed to append action log: ${error.message}`);
return false;
}
}