mcmaniac & quotes

This commit is contained in:
Flummi 2017-11-28 10:45:16 +01:00
parent d91d886fc6
commit 5c4745fea6
2 changed files with 39 additions and 31 deletions

View File

@ -1,14 +1,25 @@
import sql from "../sql.js";
let _query_get = `
select item,
rank() over (order by id),
count(*) over (rows between unbounded preceding and unbounded following) as total
from mcmaniacs
order by {order}
limit 1
`;
let _query_add = `
insert into mcmaniacs (item) values ($1) on conflict do nothing
`;
module.exports = bot => {
bot._trigger.set("mcmaniac_add", new bot.trigger({
call: /Mc.*iaC/,
active: false,
active: true,
clients: ["irc", "tg"],
f: e => {
const match = e.message.match(/.*(Mc\S+iaC?).*/)[0];
console.log(match);
sql.any("insert into `mcmaniacs` (`item`) values (?)", [match])
sql.any(_query_add, [match])
.then()
.catch(err => {});
}
@ -16,13 +27,13 @@ module.exports = bot => {
bot._trigger.set("mcmaniac_get", new bot.trigger({
call: /^(\.|\/)mcmaniac/i,
active: false,
active: true,
clients: ["irc", "tg"],
f: e => {
const args = e.message.trim().substring(10);
let query = "select id, item, (select count(*) from mcmaniacs) as sum from mcmaniacs order by {order} limit 1";
let order = "id asc"
, offset;
, offset
, query = _query_get;
if (args.length > 0) {
if (isNaN(parseInt(args)))
@ -38,13 +49,13 @@ module.exports = bot => {
query += " offset {offset}";
}
else
order = "rand()";
order = "random()";
query = query
.split("{order}").join(order)
.split("{offset}").join(offset);
sql.any(query)
.then(rows => {
e.reply(`[${rows[0].id}/${rows[0].sum}] ${rows[0].item}`);
e.reply(`[${rows[0].rank}/${rows[0].total}] ${rows[0].item}`);
})
.catch(err => {
console.log(err);

View File

@ -1,20 +1,19 @@
import sql from "../sql.js";
let _query_get = `
set @s := (select count(id) as sum from nxy_quotes where nick like $1);
select id, nick, item, rank, @s as sum
from (
select @r:= if(@u = nick, @r {rankorder}) as rank,
id, nick, item, @u:= nick
from nxy_quotes,
(select @r:= 1) as r,
(select @u:= '') as u
order by nick, id {order}
) as blah
where nick like $1
{rand}
limit 1
offset {offset};
with ranked_quotes as (
select nick,
item,
rank() over (partition by nick order by id),
count(*) over (partition by nick) as total
from nxy_quotes
)
select *
from ranked_quotes
where lower(nick) like lower($1)
order by {order}
limit 1
{offset}
`;
let _query_add = `
insert into nxy_quotes
@ -26,7 +25,7 @@ insert into nxy_quotes
module.exports = bot => {
bot._trigger.set("quotes", new bot.trigger({
call: /^(\.|\/)q .*/i,
active: false,
active: true,
f: e => {
let args = e.message.trim().substring(3).split(" ");
const cmd = args[0].toLowerCase();
@ -51,7 +50,7 @@ module.exports = bot => {
let nick = cmd;
let index = 0
, offset = 0
, order = "asc"
, order = "rank asc"
, rand = false
, query = _query_get;
@ -60,22 +59,20 @@ module.exports = bot => {
return e.reply("Mayonnaise ist keine Zahl du Pfosten.");
index = parseInt(args[1]);
offset = index;
order = (index < 0 ? "desc" : "asc");
order = (index < 0 ? "rank desc" : "rank asc");
offset = Math.abs(offset) - 1;
}
else
rand = true;
query = query
.replace("{rand}", rand ? "order by rand()" : "")
.replace("{rankorder}", rand ? "+ 1,1" : (order === "asc" ? "+ 1,1" : "- 1,@s"))
.replace("{order}", index ? order : "asc")
.replace("{offset}", rand ? 0 : offset);
.replace("{order}", rand ? "random()" : order)
.replace("{offset}", rand ? "" : `offset ${offset}`);
sql.any(query, [nick])
.then(rows => {
if(!rows[1][0])
if(!rows[0])
return e.reply("index nicht vorhanden");
e.reply(`[${rows[1][0].rank}/${rows[1][0].sum}] <${rows[1][0].nick}> ${rows[1][0].item}`)
e.reply(`[${rows[0].rank}/${rows[0].total}] <${rows[0].nick}> ${rows[0].item}`)
})
.catch(err => console.log(err));