hash-command-arbitrary
Avril 4 years ago
parent e3ec533df0
commit 446510d021
Signed by: flanchan
GPG Key ID: 284488987C31F630

File diff suppressed because it is too large Load Diff

@ -11,13 +11,13 @@ var working = false;
function _cds() function _cds()
{ {
diskspace.check(config.MOUNTPOINT, function(e, result) { diskspace.check(config.MOUNTPOINT, function(e, result) {
if(result) if(result)
ds = Math.round((result.used / result.total) * 100); ds = Math.round((result.used / result.total) * 100);
else else
ds = -1; ds = -1;
working=false; working=false;
}); });
} }
const loli = require("hashloli"); const loli = require("hashloli");
@ -32,169 +32,181 @@ const maxLolis = 10;
const maxLolisLong = 100; const maxLolisLong = 100;
function getLoli() { function getLoli() {
if(lolis[0]) if(lolis[0])
var ret = lolis.pop(); var ret = lolis.pop();
if(!ret || lolis.length<maxLolis) if(!ret || lolis.length<maxLolis)
loli.randomise(function(datas) { loli.randomise(function(datas) {
if(!datas) return; if(!datas) return;
datas.map(function(vv) { datas.map(function(vv) {
let v = vv.file_url; let v = vv.file_url;
if(v && !reqd.includes(v)) { if(v && !reqd.includes(v)) {
lolis.push({file: v, preview: vv.preview_url}); lolis.push({file: v, preview: vv.preview_url});
reqd.push(v); reqd.push(v);
} else if(reqd[0]) reqd.pop(); } else if(reqd[0]) reqd.pop();
while(reqd.length >= maxLolisLong) while(reqd.length >= maxLolisLong)
reqd.pop(); reqd.pop();
}); });
// console.log("AAAA "+JSON.stringify(lolis)); // console.log("AAAA "+JSON.stringify(lolis));
}); });
return ret; return ret;
} }
getLoli(); getLoli();
//_cds(); //_cds();
exports.roll_dice = function (frag, post, extra) { exports.roll_dice = function (frag, post, extra) {
var ms = frag.split(common.dice_re); var ms = frag.split(common.dice_re);
var dice = []; var dice = [];
for (var i = 1; i < ms.length && dice.length < rollLimit; i += 2) { for (var i = 1; i < ms.length && dice.length < rollLimit; i += 2) {
if (ms[i] == '#loli') { if (ms[i] == '#loli') {
let cute = getLoli(); let cute = getLoli();
if(cute) if(cute)
dice.push([cute.file, cute.preview]); dice.push([cute.file, cute.preview]);
else else
dice.push(["/404", "/s/error.jpg"]); dice.push(["/404", "/s/error.jpg"]);
} }
else if(ms[i] == '#?') else if(ms[i] == '#?')
{
if(!post.body) dice.push([-1]);
else {
var options=[];
var sbody = post.body.split("\n");
for(var j=sbody.length-2;j>=0;j--)
{ {
if(!post.body) dice.push([-1]); var cur = sbody[j].trim();
else { if(cur.length<1 || /#\?$/.test(cur)) continue;
var options=[]; if(/\?$/.test(cur)) options.push(cur.slice(0,-1));
var sbody = post.body.split("\n"); else break;
for(var j=sbody.length-2;j>=0;j--)
{
var cur = sbody[j].trim();
if(cur.length<1 || /#\?$/.test(cur)) continue;
if(/\?$/.test(cur)) options.push(cur.slice(0,-1));
else break;
}
var f = options.length;
if(options.length<1) dice.push([0]);
else dice.push([options[Math.floor(Math.random() * f)]]);
}
} }
else if(ms[i] == '#du') var f = options.length;
{ if(options.length<1) dice.push([0]);
working = true; else dice.push([options[Math.floor(Math.random() * f)]]);
if( dice.length + (post.dice ? post.dice.length : 0) < rollLimit)
_cds();
else working = false;
// while(working) ; //SPIN SPIN SPINNN~~~~
dice.push([ds]); }
} }
else if(ms[i] == '#pyu') { else if(ms[i] == '#du')
if( dice.length + (post.dice ? post.dice.length : 0) < rollLimit) {
pyu += 1; working = true;
dice.push([pyu]); if( dice.length + (post.dice ? post.dice.length : 0) < rollLimit)
} _cds();
else if(ms[i] == '#pcount') { else working = false;
dice.push([pyu]); // while(working) ; //SPIN SPIN SPINNN~~~~
}
else { dice.push([ds]);
var info = common.parse_dice(ms[i]); }
if (!info) else if (ms[i] == '#tea')
continue; {
var f = info.faces; const hours = new Date().getUTCHours();
var rolls = [f]; if ( (hours >= 14 && hours< 16) || (hours >= 2 && hours <4))
for (var j = 0; j < info.n; j++) dice.push([true]);
rolls.push(Math.floor(Math.random() * f) + 1); else {
if (info.bias) const fxdate = (h) => h<0?fxdate(24+h):h;
rolls.push({bias: info.bias}) const morning_session = fxdate(14 - hours);
dice.push(rolls); const evening_session = fxdate(2 - hours);
} dice.push([Math.min(morning_session, evening_session)]);
}
}
else if(ms[i] == '#pyu') {
if( dice.length + (post.dice ? post.dice.length : 0) < rollLimit)
pyu += 1;
dice.push([pyu]);
}
else if(ms[i] == '#pcount') {
dice.push([pyu]);
} }
else {
var info = common.parse_dice(ms[i]);
if (!info)
continue;
var f = info.faces;
var rolls = [f];
for (var j = 0; j < info.n; j++)
rolls.push(Math.floor(Math.random() * f) + 1);
if (info.bias)
rolls.push({bias: info.bias})
dice.push(rolls);
}
}
if (dice.length) {
// Would prefer an appending scheme for adding new rolls but
// there's no hash value append redis command...
// I don't want to spill into a separate redis list.
// Overwriting the whole log every time is quadratic though.
// Enforcing a roll limit to deter that and for sanity
var exist = post.dice ? post.dice.length : 0;
if (dice.length + exist > rollLimit)
dice = dice.slice(0, Math.max(0, rollLimit - exist));
if (dice.length) { if (dice.length) {
// Would prefer an appending scheme for adding new rolls but extra.new_dice = dice;
// there's no hash value append redis command... dice = post.dice ? post.dice.concat(dice) : dice;
// I don't want to spill into a separate redis list. post.dice = dice;
// Overwriting the whole log every time is quadratic though.
// Enforcing a roll limit to deter that and for sanity
var exist = post.dice ? post.dice.length : 0;
if (dice.length + exist > rollLimit)
dice = dice.slice(0, Math.max(0, rollLimit - exist));
if (dice.length) {
extra.new_dice = dice;
dice = post.dice ? post.dice.concat(dice) : dice;
post.dice = dice;
}
} }
}
}; };
function inline_dice(post, dice) { function inline_dice(post, dice) {
if (dice && dice.length) { if (dice && dice.length) {
dice = JSON.stringify(dice); dice = JSON.stringify(dice);
post.dice = dice.substring(1, dice.length - 1); post.dice = dice.substring(1, dice.length - 1);
} }
} }
hooks.hook('attachToPost', function (attached, cb) { hooks.hook('attachToPost', function (attached, cb) {
var new_dice = attached.extra.new_dice; var new_dice = attached.extra.new_dice;
if (new_dice) { if (new_dice) {
attached.attach.dice = new_dice; attached.attach.dice = new_dice;
inline_dice(attached.writeKeys, attached.post.dice); inline_dice(attached.writeKeys, attached.post.dice);
} }
cb(null); cb(null);
}); });
hooks.hook_sync('inlinePost', function (info) { hooks.hook_sync('inlinePost', function (info) {
inline_dice(info.dest, info.src.dice); inline_dice(info.dest, info.src.dice);
}); });
hooks.hook_sync('extractPost', function (post) { hooks.hook_sync('extractPost', function (post) {
if (!post.dice) if (!post.dice)
return; return;
try { try {
post.dice = JSON.parse('[' + post.dice + ']'); post.dice = JSON.parse('[' + post.dice + ']');
} }
catch (e) { catch (e) {
delete post.dice; delete post.dice;
} }
}); });
// This is looking rather boilerplatey // This is looking rather boilerplatey
hooks.hook('clientSynced', function (info, cb) { hooks.hook('clientSynced', function (info, cb) {
var op = info.op, client = info.client; var op = info.op, client = info.client;
if (op) { if (op) {
client.db.get_fun(op, function (err, js) { client.db.get_fun(op, function (err, js) {
if (err) if (err)
return cb(err); return cb(err);
if (js) if (js)
client.send([op, common.EXECUTE_JS, js]); client.send([op, common.EXECUTE_JS, js]);
cb(null); cb(null);
}); });
} }
else else
cb(null); cb(null);
}); });
hooks.hook('clientSynced', function (info, cb) { hooks.hook('clientSynced', function (info, cb) {
var client = info.client; var client = info.client;
client.db.get_banner(function (err, banner) { client.db.get_banner(function (err, banner) {
if (err) if (err)
return cb(err); return cb(err);
if (!banner) if (!banner)
return cb(null); return cb(null);
var msg = banner.message; var msg = banner.message;
if (msg) if (msg)
client.send([banner.op, common.UPDATE_BANNER, msg]); client.send([banner.op, common.UPDATE_BANNER, msg]);
cb(null); cb(null);
}); });
}); });

Loading…
Cancel
Save