LOG-SURROUNDINGS cooldown
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s
This commit is contained in:
parent
9ae853529d
commit
37ee4a0ac1
1 changed files with 28 additions and 30 deletions
|
|
@ -2,7 +2,6 @@ const { getBot } = require('../core/context');
|
||||||
const { saveMemory, getMemory } = require('../memory');
|
const { saveMemory, getMemory } = require('../memory');
|
||||||
const db = require('../db');
|
const db = require('../db');
|
||||||
|
|
||||||
// Define what kinds of blocks to remember. This should probably be a variable later so that I can add to this list.
|
|
||||||
const INTERESTING_BLOCKS = [
|
const INTERESTING_BLOCKS = [
|
||||||
'coal_ore', 'iron_ore', 'diamond_ore', 'gold_ore', 'emerald_ore',
|
'coal_ore', 'iron_ore', 'diamond_ore', 'gold_ore', 'emerald_ore',
|
||||||
'redstone_ore', 'lapis_ore', 'copper_ore',
|
'redstone_ore', 'lapis_ore', 'copper_ore',
|
||||||
|
|
@ -14,6 +13,16 @@ const INTERESTING_BLOCKS = [
|
||||||
|
|
||||||
const COOLDOWN_MINUTES = 5;
|
const COOLDOWN_MINUTES = 5;
|
||||||
|
|
||||||
|
// Wrap getMemory in a Promise for async/await
|
||||||
|
function getMemoryAsync(db, label) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
getMemory(db, label, (err, data) => {
|
||||||
|
if (err) return reject(err);
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = async function logSurroundings() {
|
module.exports = async function logSurroundings() {
|
||||||
const bot = getBot();
|
const bot = getBot();
|
||||||
if (!bot) return;
|
if (!bot) return;
|
||||||
|
|
@ -26,6 +35,7 @@ module.exports = async function logSurroundings() {
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const observedTypes = new Set();
|
const observedTypes = new Set();
|
||||||
|
const chatSummary = {};
|
||||||
|
|
||||||
for (const pos of found) {
|
for (const pos of found) {
|
||||||
const block = bot.blockAt(pos);
|
const block = bot.blockAt(pos);
|
||||||
|
|
@ -38,47 +48,35 @@ module.exports = async function logSurroundings() {
|
||||||
timestamp: now
|
timestamp: now
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Save exact block location if it's not already recorded
|
||||||
db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => {
|
db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => {
|
||||||
if (err || row) return;
|
if (!err && !row) saveMemory(db, label, memoryData);
|
||||||
saveMemory(db, label, memoryData);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!observedTypes.has(block.name)) {
|
if (!observedTypes.has(block.name)) {
|
||||||
observedTypes.add(block.name);
|
observedTypes.add(block.name);
|
||||||
|
|
||||||
const typeLabel = `recent:${block.name}`;
|
const typeLabel = `recent:${block.name}`;
|
||||||
await new Promise(resolve => {
|
try {
|
||||||
getMemory(db, typeLabel, (err, memory) => {
|
const memory = await getMemoryAsync(db, typeLabel);
|
||||||
if (err || !memory) {
|
const age = memory ? (now - memory.lastSeen) / 60000 : Infinity;
|
||||||
saveMemory(db, typeLabel, { lastSeen: now });
|
|
||||||
bot.chat(`I noticed ${block.name.replace(/_/g, ' ')} nearby.`);
|
|
||||||
} else {
|
|
||||||
const age = (now - memory.lastSeen) / (1000 * 60); // minutes
|
|
||||||
if (age > COOLDOWN_MINUTES) {
|
|
||||||
saveMemory(db, typeLabel, { lastSeen: now });
|
|
||||||
bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const newTypes = Array.from(observedTypes);
|
if (age >= COOLDOWN_MINUTES) {
|
||||||
if (newTypes.length > 0) {
|
await saveMemory(db, typeLabel, { lastSeen: now });
|
||||||
const counts = {};
|
chatSummary[block.name] = (chatSummary[block.name] || 0) + 1;
|
||||||
for (const pos of found) {
|
}
|
||||||
const name = bot.blockAt(pos)?.name;
|
} catch (err) {
|
||||||
if (newTypes.includes(name)) {
|
await saveMemory(db, typeLabel, { lastSeen: now });
|
||||||
counts[name] = (counts[name] || 0) + 1;
|
chatSummary[block.name] = (chatSummary[block.name] || 0) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const summaryMsg = Object.entries(counts)
|
const entries = Object.entries(chatSummary);
|
||||||
|
if (entries.length > 0) {
|
||||||
|
const summary = entries
|
||||||
.map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`)
|
.map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`)
|
||||||
.join(', ');
|
.join(', ');
|
||||||
|
bot.chat(`I noticed: ${summary}`);
|
||||||
if (summaryMsg) bot.chat(`I noticed: ${summaryMsg}`);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue