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"; 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 => { module.exports = bot => {
bot._trigger.set("mcmaniac_add", new bot.trigger({ bot._trigger.set("mcmaniac_add", new bot.trigger({
call: /Mc.*iaC/, call: /Mc.*iaC/,
active: false, active: true,
clients: ["irc", "tg"], clients: ["irc", "tg"],
f: e => { f: e => {
const match = e.message.match(/.*(Mc\S+iaC?).*/)[0]; const match = e.message.match(/.*(Mc\S+iaC?).*/)[0];
console.log(match); sql.any(_query_add, [match])
sql.any("insert into `mcmaniacs` (`item`) values (?)", [match])
.then() .then()
.catch(err => {}); .catch(err => {});
} }
@ -16,13 +27,13 @@ module.exports = bot => {
bot._trigger.set("mcmaniac_get", new bot.trigger({ bot._trigger.set("mcmaniac_get", new bot.trigger({
call: /^(\.|\/)mcmaniac/i, call: /^(\.|\/)mcmaniac/i,
active: false, active: true,
clients: ["irc", "tg"], clients: ["irc", "tg"],
f: e => { f: e => {
const args = e.message.trim().substring(10); 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" let order = "id asc"
, offset; , offset
, query = _query_get;
if (args.length > 0) { if (args.length > 0) {
if (isNaN(parseInt(args))) if (isNaN(parseInt(args)))
@ -38,13 +49,13 @@ module.exports = bot => {
query += " offset {offset}"; query += " offset {offset}";
} }
else else
order = "rand()"; order = "random()";
query = query query = query
.split("{order}").join(order) .split("{order}").join(order)
.split("{offset}").join(offset); .split("{offset}").join(offset);
sql.any(query) sql.any(query)
.then(rows => { .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 => { .catch(err => {
console.log(err); console.log(err);

View File

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