local sync

This commit is contained in:
Flummi
2019-04-24 15:26:39 +02:00
parent 67b0fc291a
commit 175b994be3
12 changed files with 160 additions and 35 deletions

View File

@@ -61,7 +61,7 @@ const sandbox = (lang, code) => {
method: "POST",
body: langs[lang]
};
fetch("http://rextester.com/rundotnet/api", options)
fetch("https://rextester.com/rundotnet/api", options)
.then(res => res.json())
.then(body => {
if (body.Errors) {
@@ -76,7 +76,10 @@ const sandbox = (lang, code) => {
if (body.Result.length === 0)
return reject("lol keine Ausgabe");
resolve(body.Result);
}).catch(err => reject("Error!"));
}).catch(err => {
console.log(err);
reject("Error!");
});
});
};
@@ -92,4 +95,4 @@ const bfgen = text => {
return out;
};
export { maxoutput, sandbox, bfgen, hsimports };
export { maxoutput, sandbox, bfgen, hsimports };

View File

@@ -19,25 +19,23 @@ export default new class timer {
}
add(time, fn, begin = ~~(Date.now()/1000)) {
return new Promise((resolve, reject) => {
if(this.regex.test(time)) {
let seconds = 0;
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
seconds += parseInt(this.calc[mod](val));
});
if(seconds > 0) {
const rest = seconds - (~~(Date.now() / 1000) - begin);
lt.setTimeout(() => {
fn();
}, rest * 1000);
this._timers.add(rest);
resolve(rest);
}
else
reject();
}
else
reject();
if(!this.regex.test(time))
return reject();
let seconds = 0;
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
seconds += parseInt(this.calc[mod](val));
});
if(seconds < 1)
return reject();
const rest = seconds - (~~(Date.now() / 1000) - begin);
if(begin < ~~(Date.now() / 1000))
return reject();
lt.setTimeout(() => {
fn();
}, rest * 1000);
this._timers.add(rest);
resolve(rest);
});
}
getTimers() {
@@ -53,6 +51,12 @@ export default new class timer {
{
this.add(r.delay, () => {
client.client.sendmsg("normal", r.target.channel, client.client.format(`[b]${r.target.nick}[/b]: ${r.message} [i](${r.delay})[/i]`));
sql.any("delete from nxy_timers where id = $1", [ r.id ])
.then(() => {
client.client.sendmsg("normal", r.target.channel, client.client.format(`timer ${r.id} gelöscht.`));
console.log(`deleted timer ${r.id}`);
})
.catch(err => console.log(err));
}, r.created).catch(err => {});
}
});

View File

@@ -0,0 +1,64 @@
//import sql from "../../../inc/sql";
import { clients } from "../../wrapper";
export default new class timer {
constructor() {
this.regex = /(\d+)(mon|[smhdwy])/;
this.calc = {
y: 365 * 24 * 60 * 60, // years
mon: 30 * 24 * 60 * 60, // months
w: 7 * 24 * 60 * 60, // weeks
d: 24 * 60 * 60, // days
h: 60 * 60, // hours
m: 60, // minutes
s: 1 // seconds
};
this._timers = new Map();
setInterval(() => { this.loop(); }, 1000);
//setTimeout(() => { this.getTimers(); }, 1000);
}
add(time, fn, now = ~~(Date.now() / 1000)) {
return new Promise((resolve, reject) => {
try {
if(!this.regex.test(time))
return reject("ungültig lol");
let seconds = 0;
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
seconds += val * this.calc[mod];
});
if(seconds < 1)
return reject("lel ne");
const until = now + seconds;
const rest = until - now;
if(!this._timers.has(until))
this._timers.set(until, []);
this._timers.get(until).push(fn);
resolve({
now: new Date(now * 1000),
rest: rest,
until: new Date((now + rest) * 1000)
});
} catch(err) {
reject(err);
};
});
}
loop() {
this._timers.forEach((val, key) => {
if(key === ~~(Date.now() / 1000)) {
val.forEach(k => {
k.target = JSON.parse(k.target);
clients.forEach(client => {
if(client.type.toLowerCase() === k.target.type.toLowerCase() && client.client.network.toLowerCase() === k.target.network.toLowerCase()) {
client.client.sendmsg("normal", k.target.channel, client.client.format(`[b]${k.target.nick}[/b]: ${k.message} [i](${k.delay})[/i]`));
}
});
this._timers.delete(key);
});
}
});
}
};