forked from wmantly/mc-bot-town
player list
This commit is contained in:
parent
956942bc90
commit
22b915ee56
@ -8,5 +8,6 @@ module.exports = {
|
||||
// 'auth': "microsoft"
|
||||
// }
|
||||
// }
|
||||
}
|
||||
},
|
||||
"playerListDir": "/home/william/test_list/"
|
||||
}
|
@ -7,7 +7,7 @@ const {CJbot} = require('../model/minecraft');
|
||||
const inventoryViewer = require('mineflayer-web-inventory');
|
||||
|
||||
const commands = require('./commands');
|
||||
|
||||
const {onJoin} = require('./player_list')
|
||||
|
||||
for(let name in conf.mc.bots){
|
||||
if(CJbot.bots[name]) continue;
|
||||
@ -22,9 +22,12 @@ for(let name in conf.mc.bots){
|
||||
|
||||
bot.on('onReady', async function(argument) {
|
||||
// inventoryViewer(bot.bot);
|
||||
|
||||
onJoin(bot);
|
||||
await sleep(1000);
|
||||
bot.bot.setControlState('jump', true);
|
||||
setTimeout(()=> bot.bot.setControlState('jump', false), 5000)
|
||||
|
||||
})
|
||||
|
||||
// bot.on('message', function(...args){
|
||||
@ -47,4 +50,5 @@ try{
|
||||
console.log('!!!!!!!! error:', e)
|
||||
}})()
|
||||
|
||||
|
||||
// module.exports = {bot: ez, henry, owen, linda, jimin, nova, ez};
|
||||
|
86
nodejs/controller/player_list.js
Normal file
86
nodejs/controller/player_list.js
Normal file
@ -0,0 +1,86 @@
|
||||
const fs = require('fs').promises;
|
||||
const conf = require('../conf');
|
||||
|
||||
const playerListDir = conf.playerListDir;
|
||||
|
||||
function getFormattedDate() {
|
||||
const months = [
|
||||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
||||
];
|
||||
|
||||
const currentDate = new Date();
|
||||
const year = currentDate.getUTCFullYear();
|
||||
const month = months[currentDate.getUTCMonth()];
|
||||
const day = currentDate.getUTCDate().toString().padStart(2, '0');
|
||||
|
||||
return `${month}-${day}-${year}`;
|
||||
}
|
||||
|
||||
function millisecondsUntilAMUTC() {
|
||||
const now = new Date();
|
||||
const utcMidnight = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1, 0, 0, 0); // Next UTC midnight
|
||||
|
||||
const millisecondsUntilMidnightUTC = utcMidnight - now.getTime();
|
||||
return millisecondsUntilMidnightUTC;
|
||||
}
|
||||
|
||||
async function currentListedPlayers(file){
|
||||
try{
|
||||
let res = await fs.readFile(`${playerListDir}/${file}`);
|
||||
return res.toString().split('\n');
|
||||
}catch(error){
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function updateListFile(file, players){
|
||||
try{
|
||||
fs.writeFile(`${playerListDir}/${file}`, players)
|
||||
}catch(error){
|
||||
console.error('Unkown error writting player file')
|
||||
}
|
||||
}
|
||||
|
||||
let addLock = false;
|
||||
let toAddList = [];
|
||||
|
||||
async function addPlayer(playerName){
|
||||
if(addLock){
|
||||
toAddList.push(playerName);
|
||||
return ;
|
||||
}
|
||||
|
||||
addLock = true
|
||||
|
||||
let now = getFormattedDate();
|
||||
let current = await currentListedPlayers(now)
|
||||
if(!current.includes(playerName)){
|
||||
current.push(playerName)
|
||||
await updateListFile(now, current.join('\n'))
|
||||
}
|
||||
|
||||
addLock = false;
|
||||
|
||||
if(toAddList.length){
|
||||
await addPlayer(toAddList.shift());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function onJoin(bot){
|
||||
bot.bot.on('playerJoined', async function(player){
|
||||
await addPlayer(player.username);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function doMidnight(bots){
|
||||
let later = millisecondsUntilAMUTC();
|
||||
setTimeout(function(){
|
||||
|
||||
}, later)
|
||||
}
|
||||
|
||||
module.exports = {onJoin, doMidnight};
|
||||
|
Loading…
x
Reference in New Issue
Block a user