var common = require ( '../common' ) ,
hooks = require ( '../hooks' ) ,
config = require ( '../config' ) ,
diskspace = require ( 'diskspace' ) ;
var pyu = config . PYU _START ;
var rollLimit = 5 ;
var ds = - 1 ;
var working = false ;
function _cds ( )
{
diskspace . check ( config . MOUNTPOINT , function ( e , result ) {
if ( result )
ds = Math . round ( ( result . used / result . total ) * 100 ) ;
else
ds = - 1 ;
working = false ;
} ) ;
}
const loli = require ( "hashloli" ) ;
var lolis = [ ] ;
var reqd = [ ] ;
loli . defaultConfig . number = 3 ;
loli . defaultConfig . tags = [ "-rape" ] ;
loli . defaultConfig . page = 4000 ;
loli . defaultConfig . range = 20 ;
const maxLolis = 10 ;
const maxLolisLong = 100 ;
function getLoli ( ) {
if ( lolis [ 0 ] )
var ret = lolis . pop ( ) ;
if ( ! ret || lolis . length < maxLolis )
loli . randomise ( function ( datas ) {
if ( ! datas ) return ;
datas . map ( function ( vv ) {
let v = vv . file _url ;
if ( v && ! reqd . includes ( v ) ) {
lolis . push ( { file : v , preview : vv . preview _url } ) ;
reqd . push ( v ) ;
} else if ( reqd [ 0 ] ) reqd . pop ( ) ;
while ( reqd . length >= maxLolisLong )
reqd . pop ( ) ;
} ) ;
// console.log("AAAA "+JSON.stringify(lolis));
} ) ;
return ret ;
}
//getLoli();
//_cds();
exports . roll _dice = function ( frag , post , extra ) {
var ms = frag . split ( common . dice _re ) ;
var dice = [ ] ;
for ( var i = 1 ; i < ms . length && dice . length < rollLimit ; i += 2 ) {
if ( ms [ i ] == '#loli' ) {
let cute = getLoli ( ) ;
if ( cute )
dice . push ( [ cute . file , cute . preview ] ) ;
else
dice . push ( [ "/404" , "/s/error.jpg" ] ) ;
}
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 -- )
{
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' )
{
working = true ;
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 ] == '#tea' )
{
const hours = new Date ( ) . getUTCHours ( ) ;
if ( ( hours >= 14 && hours < 16 ) || ( hours >= 2 && hours < 4 ) )
dice . push ( [ true ] ) ;
else {
const fxdate = ( h ) => h < 0 ? fxdate ( 24 + h ) : h ;
const morning _session = fxdate ( 14 - hours ) ;
const evening _session = fxdate ( 2 - hours ) ;
dice . push ( [ Math . min ( morning _session , evening _session ) ] ) ;
}
}
else if ( ms [ i ] == '#fun' ) {
if ( dice . length + ( post . dice ? post . dice . length : 0 ) < rollLimit )
pyu += 1 ;
dice . push ( [ pyu ] ) ;
}
else if ( ms [ i ] == '#fcount' ) {
dice . push ( [ pyu ] ) ;
}
else if ( ms [ i ] . startsWith ( '#<' ) )
{
//TODO: Stupid hack on how to make this work with `async () =>` results... (pseudo-code, maybe come back to this when I can be bothered to write the needed utils, figure out how node capture groups works, and all the other bs needed...)
/ * X X X : [ [ p s u e d o - c o d e ] ]
const frag = ms [ i ] . slice ( 2 ) ;
const lookup _result = match _regexes _in _object _keys ( config . ARBITRARY , frag ) ; // { lookup: <value>, key: <regex>, matches: <regex match groups> };
const lookup = lookup _result . lookup ;
// XXX: Sets the extra.new_dice, and appends post.dice one value: `result`.
const set _dice = ( result ) => {
const dice = result ? [ result ] : [ ] ;
extra . new _dice = dice ;
dice = post . dice ? post . dice . concat ( dice ) : dice ;
post . dice = dice ;
return dice ;
} ;
if ( ! lookup ) ; // XXX: no match
else if ( is _function ( lookup ) )
{ let string = lookup ( frag , lookup _result . matches || { } , lookup _result . key ) ;
dice . push ( [ string ] ) ; }
else if ( is _string ( lookup ) ) dice . push ( [ string ] ) ;
else if ( is _promise ( lookup ) {
// We need to insert this via `attachToPost`. So we'll set `extra.new_dice` to be sure it's hit.
//set_dice("..."); //XXX: TODO: Won't this just mean the next result is *appeneded*, and treated as a result for the next command? So, we won't set an intermidiate, we'll just wait on `lookup().then(...)`
// When async lambda completes, we'll set `extra.new_dice()` and append `post.dice()` again.
lookup ( frag , lookup _result . matches || { } , lookup _result . key ) . then ( set _dice ) . catch ( ( err ) => {
set _dice ( "Error: " + ( err || "(unbound)" ) ) ;
} ) ;
return ; // XXX: Important to return here so we don't hit the below `if(dice.length)`
} else ; //XXX no match
* /
}
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 ) {
// TODO: for `#<` to work: The key to hitting `attachToPost` at all is in `extra.new_dice` vs `post.dice`
extra . new _dice = dice ;
dice = post . dice ? post . dice . concat ( dice ) : dice ;
post . dice = dice ;
}
}
} ;
function inline _dice ( post , dice ) {
if ( dice && dice . length ) {
dice = JSON . stringify ( dice ) ;
post . dice = dice . substring ( 1 , dice . length - 1 ) ;
}
}
hooks . hook ( 'attachToPost' , function ( attached , cb ) {
var new _dice = attached . extra . new _dice ;
if ( new _dice ) {
attached . attach . dice = new _dice ;
inline _dice ( attached . writeKeys , attached . post . dice ) ;
}
cb ( null ) ;
} ) ;
hooks . hook _sync ( 'inlinePost' , function ( info ) {
inline _dice ( info . dest , info . src . dice ) ;
} ) ;
hooks . hook _sync ( 'extractPost' , function ( post ) {
if ( ! post . dice )
return ;
try {
post . dice = JSON . parse ( '[' + post . dice + ']' ) ;
}
catch ( e ) {
delete post . dice ;
}
} ) ;
// This is looking rather boilerplatey
hooks . hook ( 'clientSynced' , function ( info , cb ) {
var op = info . op , client = info . client ;
if ( op ) {
client . db . get _fun ( op , function ( err , js ) {
if ( err )
return cb ( err ) ;
if ( js )
client . send ( [ op , common . EXECUTE _JS , js ] ) ;
cb ( null ) ;
} ) ;
}
else
cb ( null ) ;
} ) ;
hooks . hook ( 'clientSynced' , function ( info , cb ) {
var client = info . client ;
client . db . get _banner ( function ( err , banner ) {
if ( err )
return cb ( err ) ;
if ( ! banner )
return cb ( null ) ;
var msg = banner . message ;
if ( msg )
client . send ( [ banner . op , common . UPDATE _BANNER , msg ] ) ;
cb ( null ) ;
} ) ;
} ) ;