cuffeo ausgelagert..

This commit is contained in:
Flummi
2019-08-17 11:54:50 +00:00
parent 05d42650ec
commit c0ef557f72
31 changed files with 22 additions and 1021 deletions

View File

@@ -1,66 +0,0 @@
import lt from "long-timeout";
import sql from "../../../inc/sql";
import { clients } from "../../wrapper";
export default new class timer {
constructor() {
this.regex = /(\d+)(mon|[smhdwy])/;
this.calc = {
y: val => val * 365 * 24 * 60 * 60, // years
mon: val => val * 30 * 24 * 60 * 60, // months
w: val => val * 7 * 24 * 60 * 60, // weeks
d: val => val * 24 * 60 * 60, // days
h: val => val * 60 * 60, // hours
m: val => val * 60, // minutes
s: val => val // seconds
};
this._timers = new Set();
setTimeout(() => { this.getTimers(); }, 1000);
}
add(time, fn, begin = ~~(Date.now()/1000)) {
return new Promise((resolve, 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() {
sql.any("select * from nxy_timers")
.then(rows => {
if(rows.length === 0)
return;
rows.forEach(r => {
r.target = JSON.parse(r.target);
clients.forEach(client => {
if(client.type.toLowerCase() === r.target.type.toLowerCase()
&& client.client.network.toLowerCase() === r.target.network.toLowerCase())
{
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 => {});
}
});
});
}).catch(err => console.log(err));
}
}

View File

@@ -1,64 +0,0 @@
//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);
});
}
});
}
};