From 22b915ee56511a552aa3180079173f489e7f7042 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Sun, 10 Dec 2023 16:50:59 -0500 Subject: [PATCH] player list --- nodejs/conf/base.js | 3 +- nodejs/controller/mc-bot.js | 6 ++- nodejs/controller/player_list.js | 86 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 nodejs/controller/player_list.js diff --git a/nodejs/conf/base.js b/nodejs/conf/base.js index 59dd4d5..94c602b 100644 --- a/nodejs/conf/base.js +++ b/nodejs/conf/base.js @@ -8,5 +8,6 @@ module.exports = { // 'auth': "microsoft" // } // } - } + }, + "playerListDir": "/home/william/test_list/" } \ No newline at end of file diff --git a/nodejs/controller/mc-bot.js b/nodejs/controller/mc-bot.js index 7251c13..b4af70f 100644 --- a/nodejs/controller/mc-bot.js +++ b/nodejs/controller/mc-bot.js @@ -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}; diff --git a/nodejs/controller/player_list.js b/nodejs/controller/player_list.js new file mode 100644 index 0000000..5bb3e53 --- /dev/null +++ b/nodejs/controller/player_list.js @@ -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}; +