diff --git a/config.json b/config.json index 2dce1bc..e99fd98 100644 --- a/config.json +++ b/config.json @@ -11,14 +11,14 @@ "logging": true, // log errors and queue place "reconnect": { "onError": true, // reconnect on error or if 2b2t kicks you - "notConnectedQueueEnd": true // restart the queue if you are not connect at the end of it + "notConnectedQueueEnd": false // restart the queue if you are not connect at the end of it }, "minecraftserver": { // the server you want to connect. Make not much sense to change it, was just added for development purpose - "hostname": "localhost", + "hostname": "2b2t.org", "port": 25565, "renderDistance": 8, "version": "1.12.2", - "onlinemode": false, // chunk caching does not work correctly if set to false + "onlinemode": true, // chunk caching does not work correctly if set to false "username": "lol" // the username to use if onlinemode is false }, "notification": { // sends a message via discord if the place in the queue reaches the specified number diff --git a/src/main.js b/src/main.js index 110c81f..5bb864f 100644 --- a/src/main.js +++ b/src/main.js @@ -208,7 +208,7 @@ function join() { }); server.on('login', (newProxyClient) => { // handle login - setTimeout(sendChunks, 50) + setTimeout(sendChunks, 1000) if (config.antiAntiAFK) clearInterval(antiAntiAFk); newProxyClient.write('login', loginpacket); newProxyClient.write('position', { @@ -237,7 +237,6 @@ function join() { chunkPos.lz = chunkPos.z; } filterPacketAndSend(data, meta, client); - //console.log("meta: " + JSON.stringify(meta) + "\ndata: " + JSON.stringify(data)) }); newProxyClient.on("end", () => { antiAntiAfkmsg(); @@ -254,26 +253,31 @@ function sendChunks() { } function log(logmsg) { - if (config.logging) fs.appendFile('../2smart2wait.log', DateTime.local().toLocaleString({ - hour: '2-digit', - minute: '2-digit', - hour12: false - }) + " " + logmsg + "\n", err => { - if (err) console.error(err) - }) + if (config.logging) { + fs.appendFile('../2smart2wait.log', DateTime.local().toLocaleString({ + hour: '2-digit', + minute: '2-digit', + hour12: false + }) + " " + logmsg + "\n", err => { + if (err) console.error(err) + }) + console.log(logmsg); + } } function reconnect() { doing = "reconnect" - reconnectinterval = setInterval(function () { - ping(config.minecraftserver.hostname, config.minecraftserver.port) - .then((response) => { - if (doing === "reconnect") startQueuing(); - clearInterval(reconnectinterval); - }) - .catch((error) => { - }); - }, 500) + reconnectLoop(); +} + +function reconnectLoop() { + ping(config.minecraftserver.hostname, config.minecraftserver.port) + .then((response) => { + startQueuing(); + }) + .catch((error) => { + setTimeout(reconnectLoop, 3000); + }); } //function to filter out some packets that would make us disconnect otherwise. @@ -331,54 +335,83 @@ function userInput(cmd, DiscordOrigin, discordMsg) { else console.log("Queue is starting up.") break; case "update": - if (DiscordOrigin) discordMsg.channel.send({ - embed: { - color: 3447003, - author: { - name: dc.user.username, - icon_url: dc.user.avatarURL - }, - title: "2bored2wait discord bridge", - description: "Start and stop the queue from discord!", - fields: [{ - name: "Position", - value: `You are in position **${webserver.queuePlace}**.` - }, - { - name: "ETA", - value: `Estimated time until login: **${webserver.ETA}**` + switch (doing) { + case "queue": + if (DiscordOrigin) discordMsg.channel.send({ + embed: { + color: 3447003, + author: { + name: dc.user.username, + icon_url: dc.user.avatarURL + }, + title: "2bored2wait discord bridge", + description: "Start and stop the queue from discord!", + fields: [{ + name: "Position", + value: `You are in position **${webserver.queuePlace}**.` + }, + { + name: "ETA", + value: `Estimated time until login: **${webserver.ETA}**` + } + ], + timestamp: new Date(), + footer: { + icon_url: dc.user.avatarURL, + text: "Author: Surprisejedi" + } } - ], - timestamp: new Date(), - footer: { - icon_url: dc.user.avatarURL, - text: "Author: Surprisejedi" - } - } - }); - else console.log("Position: " + webserver.queuePlace + " Estimated time until login: " + webserver.ETA); + }); + else console.log("Position: " + webserver.queuePlace + " Estimated time until login: " + webserver.ETA); + break; + case "timedStart": + let timerMsg = "Timer is set to " + starttimestring; + if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Timer", timerMsg); + else console.log(timerMsg); + break; + case "reconnect": + let reconnectMsg = "2bt is currently offline. Trying to reconnect"; + if (DiscordOrigin) sendDiscordMsg("Reconnecting", reconnectMsg); + else console.log(reconnectMsg); + break; + case "auth": + let authMsg = "Authentication"; + if (DiscordOrigin) sendDiscordMsg(authMsg, authMsg); + else console.log(authMsg); + break; + case "calcTime": + let calcMsg = "Calculating the time, so you can paly at " + starttimestring + if (DiscordOrigin) sendDiscordMsg("calculating time", calcMsg); + console.log(calcMsg); + break; + } break; case "stop": switch (doing) { case "queue": stop(); - stopMsg(DiscordOrigin, discordMsg.channel, "Queue"); + if (DiscordOrigin) stopMsg(DiscordOrigin, discordMsg.channel, "Queue"); + else console.log("The queue is stopped"); break; case "timedStart": clearTimeout(timedStart); - stopMsg(DiscordOrigin, discordMsg.channel, "Timer"); + if (DiscordOrigin) stopMsg(DiscordOrigin, discordMsg.channel, "Timer"); + else console.log("The timer is stopped"); break; case "reconnect": clearInterval(reconnectinterval); - stopMsg(DiscordOrigin, discordMsg.channel, "Reconnecting"); + if (DiscordOrigin) stopMsg(DiscordOrigin, discordMsg.channel, "Reconnecting"); + else console.log("Reconnecting is stoppd"); break; case "auth": clearInterval(authInterval); - stopMsg(DiscordOrigin, discordMsg.channel, "Authentication"); + if (DiscordOrigin) stopMsg(DiscordOrigin, discordMsg.channel, "Authentication"); + else console.log("Authentication is stopped"); break; case "calcTime": clearInterval(calcInterval); - stopMsg(DiscordOrigin, discordMsg.channel, "Time calculation"); + if (DiscordOrigin) stopMsg(DiscordOrigin, discordMsg.channel, "Time calculation"); + else console.log("Time calculation is stopped"); break; } break; @@ -396,6 +429,7 @@ function userInput(cmd, DiscordOrigin, discordMsg) { let output = "The perfect time to start the will be calculated, so you play at " + starttimestring; if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "time calculator", output); else console.log(output); + activity("You can play at " + starttimestring); } else if (DiscordOrigin) discordMsg.channel.send("Error: Unknown command"); else console.error("Unknown command") }