added queue start time calcuation, so you play at a specified time

This commit is contained in:
MrGeorgen
2020-04-17 00:18:36 +02:00
parent 9fd69974b1
commit 18b329a12d
2 changed files with 79 additions and 94 deletions

View File

@ -61,4 +61,11 @@ typings/
.next
#secrets
../secrets.json
secrets.json
#discord user id
saveid
#hiden files
.idea
.project
#package-lock
package-lock.json

View File

@ -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
}
@ -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,66 +317,38 @@ 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({
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: {
@ -400,8 +356,8 @@ function userInput(cmd, DiscordOrigin, discordMsg) {
icon_url: dc.user.avatarURL
},
fields: [{
name: "Queue",
value: `Queue is starting at ` + starttimestring[1]
name: titel,
value: content
}
],
timestamp: new Date(),
@ -411,9 +367,31 @@ function userInput(cmd, DiscordOrigin, discordMsg) {
}
}
});
} else if (DiscordOrigin) discordMsg.channel.send("Error: Unknown command");
else console.error("Unknown command")
}
}
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 = {