added queue start time calcuation, so you play at a specified time
This commit is contained in:
9
src/.gitignore → .gitignore
vendored
9
src/.gitignore → .gitignore
vendored
@ -61,4 +61,11 @@ typings/
|
|||||||
.next
|
.next
|
||||||
|
|
||||||
#secrets
|
#secrets
|
||||||
../secrets.json
|
secrets.json
|
||||||
|
#discord user id
|
||||||
|
saveid
|
||||||
|
#hiden files
|
||||||
|
.idea
|
||||||
|
.project
|
||||||
|
#package-lock
|
||||||
|
package-lock.json
|
||||||
164
src/main.js
164
src/main.js
@ -20,6 +20,9 @@ var c = 0;
|
|||||||
var notisend = false;
|
var notisend = false;
|
||||||
var loginpacket;
|
var loginpacket;
|
||||||
var id;
|
var id;
|
||||||
|
var totalWaitTime;
|
||||||
|
var starttimestring;
|
||||||
|
var playTime;
|
||||||
webserver.restartQueue = config.reconnect.notConnectedQueueEnd;
|
webserver.restartQueue = config.reconnect.notConnectedQueueEnd;
|
||||||
if (config.webserver) {
|
if (config.webserver) {
|
||||||
webserver.createServer(config.ports.web); // create the webserver
|
webserver.createServer(config.ports.web); // create the webserver
|
||||||
@ -31,7 +34,6 @@ webserver.onstart(() => { // set up actions for the webserver
|
|||||||
webserver.onstop(() => {
|
webserver.onstop(() => {
|
||||||
stop();
|
stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (config.openBrowserOnStart && config.webserver) {
|
if (config.openBrowserOnStart && config.webserver) {
|
||||||
opn('http://localhost:' + config.ports.web); //open a browser window
|
opn('http://localhost:' + config.ports.web); //open a browser window
|
||||||
}
|
}
|
||||||
@ -85,7 +87,7 @@ function startQueuing() {
|
|||||||
chunkData.push(data);
|
chunkData.push(data);
|
||||||
break;
|
break;
|
||||||
case "playerlist_header":
|
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 headermessage = JSON.parse(data.header);
|
||||||
let positioninqueue = headermessage.text.split("\n")[5].substring(25);
|
let positioninqueue = headermessage.text.split("\n")[5].substring(25);
|
||||||
let ETA = headermessage.text.split("\n")[6].substring(27);
|
let ETA = headermessage.text.split("\n")[6].substring(27);
|
||||||
@ -97,7 +99,7 @@ function startQueuing() {
|
|||||||
log("Position in Queue: " + webserver.queuePlace)
|
log("Position in Queue: " + webserver.queuePlace)
|
||||||
if (config.notification.enabled && webserver.queuePlace <= config.notification.queuePlace && !notisend && config.discordBot && id != null) {
|
if (config.notification.enabled && webserver.queuePlace <= config.notification.queuePlace && !notisend && config.discordBot && id != null) {
|
||||||
dc.fetchUser(id, false).then(user => {
|
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;
|
notisend = true;
|
||||||
}
|
}
|
||||||
@ -174,7 +176,7 @@ function startQueuing() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server.on('login', (newProxyClient) => { // handle login
|
server.on('login', (newProxyClient) => { // handle login
|
||||||
setTimeout(send, 50)
|
setTimeout(sendChunks, 50)
|
||||||
newProxyClient.write('login', loginpacket);
|
newProxyClient.write('login', loginpacket);
|
||||||
newProxyClient.write('position', {
|
newProxyClient.write('position', {
|
||||||
x: 0,
|
x: 0,
|
||||||
@ -209,7 +211,7 @@ function startQueuing() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function send() {
|
function sendChunks() {
|
||||||
for (let i = 0; i < chunkData.length; i++) {
|
for (let i = 0; i < chunkData.length; i++) {
|
||||||
proxyClient.write("map_chunk", chunkData[i]);
|
proxyClient.write("map_chunk", chunkData[i]);
|
||||||
}
|
}
|
||||||
@ -286,25 +288,7 @@ function userInput(cmd, DiscordOrigin, discordMsg) {
|
|||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case "start":
|
case "start":
|
||||||
startQueuing();
|
startQueuing();
|
||||||
if (DiscordOrigin) discordMsg.channel.send({
|
if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Queue", "Queue is starting up");
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
else console.log("Queue is starting up.")
|
else console.log("Queue is starting up.")
|
||||||
break;
|
break;
|
||||||
case "update":
|
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;
|
break;
|
||||||
case "stop":
|
case "stop":
|
||||||
if (webserver.isInQueue) {
|
if (webserver.isInQueue) {
|
||||||
stop();
|
stop();
|
||||||
if (DiscordOrigin) discordMsg.channel.send({
|
if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Queue", "Queue is **stopped**");
|
||||||
embed: {
|
else console.log("Queue is stopped");
|
||||||
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")
|
|
||||||
} else {
|
} else {
|
||||||
clearTimeout(timedStart);
|
clearTimeout(timedStart);
|
||||||
if (DiscordOrigin) discordMsg.channel.send({
|
if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "Timer", "Timer is **stopped**");
|
||||||
embed: {
|
else console.log("Queue timer is stopped");
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
let startregex = /^start (\d|[0-1]\d|2[0-3]):[0-5]\d$/
|
if (/start (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) {
|
||||||
if (startregex.test(cmd)) {
|
timedStart = setTimeout(startQueuing, timeStringtoDateTime(cmd).toMillis()-DateTime.local().toMillis());
|
||||||
let starttimestring = msg.content.split(" ");
|
activity("Starting at " + starttimestring);
|
||||||
let starttime = starttimestring[1].split(":");
|
if (DiscordOrigin) {
|
||||||
let currentTime = DateTime.local();
|
sendDiscordMsg(discordMsg.channel, "Timer", "Queue is starting at " + starttimestring);
|
||||||
let startdt = currentTime.set({hour: starttime[0], minute: starttime[1]});
|
} else console.log("Queue is starting at " + starttimestring);
|
||||||
if (startdt.toMillis() < currentTime.toMillis()) sartdt = startdt.plus({days: 1});
|
} else if (/^play (\d|[0-1]\d|2[0-3]):[0-5]\d$/.test(cmd)) {
|
||||||
timedStart = setTimeout(startQueuing, startdt.toMillis() - currentTime.toMillis());
|
calcTime(cmd);
|
||||||
dc.user.setActivity("Starting at " + starttimestring[1]);
|
let output = "The perfect time to start the will be calculated, so you play at " + starttimestring;
|
||||||
discordMsg.channel.send({
|
if (DiscordOrigin) sendDiscordMsg(discordMsg.channel, "time calculator", output);
|
||||||
embed: {
|
else console.log(output);
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else if (DiscordOrigin) discordMsg.channel.send("Error: Unknown command");
|
} else if (DiscordOrigin) discordMsg.channel.send("Error: Unknown command");
|
||||||
else console.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 = {
|
module.exports = {
|
||||||
startQueue: function () {
|
startQueue: function () {
|
||||||
startQueuing();
|
startQueuing();
|
||||||
|
|||||||
Reference in New Issue
Block a user