From 18b329a12d54580281c4a72a320ba86612fb3264 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Fri, 17 Apr 2020 00:18:36 +0200 Subject: [PATCH] added queue start time calcuation, so you play at a specified time --- src/.gitignore => .gitignore | 9 +- src/main.js | 164 +++++++++++++++-------------------- 2 files changed, 79 insertions(+), 94 deletions(-) rename src/.gitignore => .gitignore (90%) diff --git a/src/.gitignore b/.gitignore similarity index 90% rename from src/.gitignore rename to .gitignore index f899e88..62d8a5a 100644 --- a/src/.gitignore +++ b/.gitignore @@ -61,4 +61,11 @@ typings/ .next #secrets -../secrets.json +secrets.json +#discord user id +saveid +#hiden files +.idea +.project +#package-lock +package-lock.json diff --git a/src/main.js b/src/main.js index 12a878f..558bc9a 100644 --- a/src/main.js +++ b/src/main.js @@ -20,6 +20,9 @@ var c = 0; var notisend = false; var loginpacket; var id; +var totalWaitTime; +var starttimestring; +var playTime; webserver.restartQueue = config.reconnect.notConnectedQueueEnd; if (config.webserver) { webserver.createServer(config.ports.web); // create the webserver @@ -31,7 +34,6 @@ webserver.onstart(() => { // set up actions for the webserver webserver.onstop(() => { stop(); }); - if (config.openBrowserOnStart && config.webserver) { opn('http://localhost:' + config.ports.web); //open a browser window } @@ -85,7 +87,7 @@ function startQueuing() { chunkData.push(data); break; case "playerlist_header": - if (!finishedQueue && config.minecraftserver.hostname === "2b2t.org") { // if the packet contains the player list, we can use it to see our place in the queue + if (!finishedQueue && config.minecraftserver.hostname === "2b2t.org") { // if the packet contains the player list, we can use it to see our place in the queue let headermessage = JSON.parse(data.header); let positioninqueue = headermessage.text.split("\n")[5].substring(25); let ETA = headermessage.text.split("\n")[6].substring(27); @@ -97,7 +99,7 @@ function startQueuing() { log("Position in Queue: " + webserver.queuePlace) if (config.notification.enabled && webserver.queuePlace <= config.notification.queuePlace && !notisend && config.discordBot && id != null) { dc.fetchUser(id, false).then(user => { - user.send("The queue is almost finished. You are in Position: " + webserver.queuePlace); + sendDiscordMsg(user.dmChannel, "Queue", "The queue is almost finished. You are in Position: " + webserver.queuePlace); }) notisend = true; } @@ -174,7 +176,7 @@ function startQueuing() { }); server.on('login', (newProxyClient) => { // handle login - setTimeout(send, 50) + setTimeout(sendChunks, 50) newProxyClient.write('login', loginpacket); newProxyClient.write('position', { x: 0, @@ -209,7 +211,7 @@ function startQueuing() { }); } -function send() { +function sendChunks() { for (let i = 0; i < chunkData.length; i++) { proxyClient.write("map_chunk", chunkData[i]); } @@ -286,25 +288,7 @@ function userInput(cmd, DiscordOrigin, discordMsg) { switch (cmd) { case "start": startQueuing(); - if (DiscordOrigin) discordMsg.channel.send({ - embed: { - color: 3447003, - author: { - name: dc.user.username, - icon_url: dc.user.avatarURL - }, - fields: [{ - name: "Queue", - value: `Queue is starting up.` - } - ], - timestamp: new Date(), - footer: { - icon_url: dc.user.avatarURL, - text: "Author: Surprisejedi" - } - } - }); + if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Queue", "Queue is starting up"); else console.log("Queue is starting up.") break; case "update": @@ -333,89 +317,83 @@ function userInput(cmd, DiscordOrigin, discordMsg) { } } }); - 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 "stop": if (webserver.isInQueue) { stop(); - if (DiscordOrigin) discordMsg.channel.send({ - embed: { - color: 3447003, - author: { - name: dc.user.username, - icon_url: dc.user.avatarURL - }, - fields: [{ - name: "Queue", - value: `Queue is **stopped**.` - } - ], - timestamp: new Date(), - footer: { - icon_url: dc.user.avatarURL, - text: "Author: Surprisejedi" - } - } - }); - else console.log("Queue is stopped") + if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Queue", "Queue is **stopped**"); + else console.log("Queue is stopped"); } else { clearTimeout(timedStart); - if (DiscordOrigin) discordMsg.channel.send({ - embed: { - color: 3447003, - author: { - name: dc.user.username, - icon_url: dc.user.avatarURL - }, - fields: [{ - name: "Queue", - value: `Queue timer is **stopped**.` - } - ], - timestamp: new Date(), - footer: { - icon_url: dc.user.avatarURL, - text: "Author: MrGeorgen" - } - } - }); - else console.log("Queue timer is stopped") + if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Timer", "Timer is **stopped**"); + else console.log("Queue timer is stopped"); } break; default: - let startregex = /^start (\d|[0-1]\d|2[0-3]):[0-5]\d$/ - if (startregex.test(cmd)) { - let starttimestring = msg.content.split(" "); - let starttime = starttimestring[1].split(":"); - let currentTime = DateTime.local(); - let startdt = currentTime.set({hour: starttime[0], minute: starttime[1]}); - if (startdt.toMillis() < currentTime.toMillis()) sartdt = startdt.plus({days: 1}); - timedStart = setTimeout(startQueuing, startdt.toMillis() - currentTime.toMillis()); - dc.user.setActivity("Starting at " + starttimestring[1]); - discordMsg.channel.send({ - embed: { - color: 3447003, - author: { - name: dc.user.username, - icon_url: dc.user.avatarURL - }, - fields: [{ - name: "Queue", - value: `Queue is starting at ` + starttimestring[1] - } - ], - timestamp: new Date(), - footer: { - icon_url: dc.user.avatarURL, - text: "Author: MrGeorgen" - } - } - }); + if (/start (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) { + timedStart = setTimeout(startQueuing, timeStringtoDateTime(cmd).toMillis()-DateTime.local().toMillis()); + activity("Starting at " + starttimestring); + if (DiscordOrigin) { + sendDiscordMsg(discordMsg.channel, "Timer", "Queue is starting at " + starttimestring); + } else console.log("Queue is starting at " + starttimestring); + } else if (/^play (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) { + calcTime(cmd); + 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); } else if (DiscordOrigin) discordMsg.channel.send("Error: Unknown command"); else console.error("Unknown command") } } +function sendDiscordMsg(channel, titel, content) { + channel.send({ + embed: { + color: 3447003, + author: { + name: dc.user.username, + icon_url: dc.user.avatarURL + }, + fields: [{ + name: titel, + value: content + } + ], + timestamp: new Date(), + footer: { + icon_url: dc.user.avatarURL, + text: "Author: MrGeorgen" + } + } + }); +} + +function timeStringtoDateTime(time) { + starttimestring = time.split(" "); + starttimestring = starttimestring[1]; + let starttime = starttimestring.split(":"); + let startdt = DateTime.local().set({hour: starttime[0], minute: starttime[1], second: 0, millisecond: 0}); + if (startdt.toMillis() < DateTime.local().toMillis()) startdt = startdt.plus({days: 1}); + return startdt; +} + +function calcTime(msg) { + https.get("https://2b2t.io/api/queue", (resp) => { + let data = ''; + resp.on('data', (chunk) => { + data += chunk; + }); + resp.on("end", () => { + data = JSON.parse(data); + totalWaitTime = Math.pow(data[0][1] / 35.4, 2 / 3); // data[0][1] is the current queue length + playTime = timeStringtoDateTime(msg); + if (playTime.toSeconds() - DateTime.local().toSeconds() < totalWaitTime * 3600) startQueuing(); + else setTimeout(calcTime, 60000); + }); + }); +} + module.exports = { startQueue: function () { startQueuing();