@ -1,4 +1,4 @@
const express = require ( 'express' )
const express = require ( 'express' )
const axios = require ( 'axios' )
const axios = require ( 'axios' )
const twitchStreams = require ( 'twitch-get-stream' )
const twitchStreams = require ( 'twitch-get-stream' )
const app = express ( )
const app = express ( )
@ -45,6 +45,29 @@ new Pool({
port : 5432 ,
port : 5432 ,
} )
} )
/ * c o n s t d b 2 =
new Pool ( {
user : 'read_only_user' ,
password : 'divar1234' ,
host : 'postgres' ,
database : 'divar' ,
port : 5432 ,
} )
app . get ( '/event/query' , ( req , res ) => {
if ( req . query . query && req . query . query . length > 0 ) {
db2 . query ( req . query . query )
. then ( ( data ) => {
res . status ( 200 ) . json ( data . rows )
} )
. catch ( ( err ) => {
res . status ( 500 ) . json ( err . message )
} )
} else {
res . status ( 400 ) . send ( "Empty query!" )
}
} ) * /
app . get ( '/song/:songname' , ( req , res ) => {
app . get ( '/song/:songname' , ( req , res ) => {
db . query ( 'select * from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1' , [ req . params . songname ] , ( error , results ) => {
db . query ( 'select * from songs where name=$1 or romanized_name=$1 or english_name=$1 limit 1' , [ req . params . songname ] , ( error , results ) => {
if ( error ) {
if ( error ) {
@ -1166,15 +1189,28 @@ app.post('/streaminfo/:id',function (req,res){
}
}
} )
} )
const lastscores = { }
var lastscores = { }
const EVENTID = 23 ;
var EVENTSTART = moment ( '2021-06-28 03:00:00+00' ) ;
var EVENTEND = moment ( '2021-07-06 11:59:59+00' ) ;
var lastscores _EN = { }
const EVENTID _EN = 23 ;
var EVENTSTART _EN = moment ( '2021-06-28 03:00:00+00' ) ;
var EVENTEND _EN = moment ( '2021-07-06 11:59:59+00' ) ;
app . get ( "/helpmetestwithoutbreakingshit" , ( req , res ) => {
res . status ( 200 ) . send ( "hi" )
db . query ( "select eventid, startdate, enddate from event order by id desc limit 1" ) . then ( res . status ( 200 ) . send )
} )
app . post ( '/eventsubmit' , function ( req , res ) {
app . post ( '/eventsubmit' , function ( req , res ) {
const EVENTID = 10 ;
lastscores = { }
function submit ( ) {
function submit ( ) {
lastscores [ req . body . rank ] = Number ( req . body . points )
lastscores [ req . body . rank ] = Number ( req . body . points )
db . query ( "insert into eventdata(eventid,rank,date,name,description,points) values($1,$2,$3,$4,$5,$6) returning *;" ,
db . query ( "insert into " + ( req . query . en ? "en_" : "" ) + " eventdata(eventid,rank,date,name,description,points) values($1,$2,$3,$4,$5,$6) returning *;" ,
[ req . body . eventid , req . body . rank , req . body . date ? req . body . date : new Date ( ) , req . body . name , req . body . description , req . body . points ] )
[ req . body . eventid , req . body . rank , req . body . date ? req . body . date : req . body . fin ? moment ( EVENTEND ) . add ( 5 , 'minutes' ) . format ( "YYYY-MM-DD HH:mm:ssZ" ) : new Date ( ) , req . body . name , req . body . description , req . body . points ] )
. then ( ( data ) => {
. then ( ( data ) => {
if ( data . rows . length > 0 ) {
if ( data . rows . length > 0 ) {
res . status ( 200 ) . send ( "Submitted." )
res . status ( 200 ) . send ( "Submitted." )
@ -1189,8 +1225,59 @@ app.post('/eventsubmit',function(req,res) {
//add to table.
//add to table.
function FurtherTierIsOkay ( tier , scores , points ) {
var tiers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 50 , 100 , 500 , 1000 , 2000 , 5000 , 10000 , 20000 , 30000 , 50000 ]
if ( tier <= 1 ) {
return true ;
} else
{
//Find the previous tier.
var previousTier = - 1 ;
for ( var i = 0 ; i < tiers . length ; i ++ ) {
if ( tiers [ i ] == tier ) {
previousTier = tiers [ i - 1 ]
}
}
if ( previousTier == - 1 ) {
console . log ( "Something weird happened...." )
return false ; //Something terrible happened.
} else
if ( ! scores [ tier ] ) {
return true ; //It's okay since no score is submitted yet.
} else
{
return points < scores [ previousTier ] //If it's greater something's wrong...
}
}
}
function EndsWithZeroes ( str ) {
var zeroCount = 0 ;
var string = String ( str )
for ( var i = 0 ; i < string . length ; i ++ ) {
if ( string [ i ] == '0' ) {
zeroCount ++ ;
} else {
zeroCount = 0 ;
}
}
return zeroCount >= 2 ;
}
function ScoreIsSanitary ( rank , name , description , points ) {
if ( Number ( rank ) <= 20 ) {
return true ;
} else {
if ( EndsWithZeroes ( name ) || EndsWithZeroes ( description ) || EndsWithZeroes ( points ) ) {
return false ;
} else {
return true ;
}
}
}
//Try to update last scores.
//Try to update last scores.
db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points from eventdata where eventid=' + EVENTID + ' order by rank,date desc;' )
db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points,difference from (select lead(points) over (partition by rank order by rank,date desc)-points difference,* from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where eventid=' + EVENTID + ' order by rank,date desc)t order by rank,date desc ' )
. then ( ( data ) =>
. then ( ( data ) =>
{
{
if ( data . rows . length > 0 ) {
if ( data . rows . length > 0 ) {
@ -1198,20 +1285,14 @@ app.post('/eventsubmit',function(req,res) {
}
}
if ( ! lastscores [ req . body . rank ] || ( lastscores [ req . body . rank ] < req . body . points &&
if ( ! lastscores [ req . body . rank ]
( req . body . rank > 20 || req . body . points < lastscores [ req . body . rank ] + 30000 )
|| ( /*FurtherTierIsOkay(req.body.rank,lastscores,req.body.points)&&*/ lastscores [ req . body . rank ] < req . body . points
) ) {
&& ( req . body . fin || ScoreIsSanitary ( req . body . rank , req . body . name , req . body . description , req . body . points ) ) / * || ( lastscores [ req . body . rank ] < req . body . points
&& ( FurtherTierIsOkay ( req . body . rank , lastscores , req . body . points ) ) * / ) )
{
submit ( )
submit ( )
} else {
} else {
if ( lastscores [ req . body . rank ] !== undefined && req . body . rank <= 20 && ( lastscores [ req . body . rank ] < req . body . points && req . body . points < lastscores [ req . body . rank ] + 30000 ) ) {
res . status ( 200 ) . send ( "An invalid score attempted to be uploaded. Expected " + ( lastscores [ req . body . rank ] + 30000 ) + " or less but got " + req . body . points + "." )
} else {
if ( req . body . rank <= 20 ) {
res . status ( 200 ) . send ( "No update required." )
res . status ( 200 ) . send ( "No update required." )
} else {
submit ( )
}
}
}
}
}
}
)
)
@ -1219,15 +1300,21 @@ app.post('/eventsubmit',function(req,res) {
var chartData = { }
var chartData = { }
var predictionChartData = { }
var predictionChartData = { }
var EVENTSTART = moment ( '2021-02-14 12:00:00+09:00' ) ;
var EVENTEND = moment ( '2021-02-22 20:59:59+09:00' ) ;
var diffData = [ ]
var diffData = [ ]
var en _chartData = { }
var en _predictionChartData = { }
var en _diffData = [ ]
const PREDICTIONS = true
const PREDICTIONS = true
const en _PREDICTIONS = true
var lastCachedDate = EVENTSTART
var lastCachedDate = EVENTSTART
var en _lastCachedDate = EVENTSTART
const nyoomfactor = { //Percentage of original speed to use when nyoom'ing
const nyoomfactor = { //Percentage of original speed to use when nyoom'ing
1 : 1.0 ,
1 : 1.0 ,
@ -1250,16 +1337,17 @@ const nyoomfactor={//Percentage of original speed to use when nyoom'ing
18 : 0.3 ,
18 : 0.3 ,
19 : 0.3 ,
19 : 0.3 ,
20 : 0.3 ,
20 : 0.3 ,
50 : 0.79 ,
50 : 0.81 ,
100 : 0.72 ,
100 : 0.76 ,
500 : 0.25 ,
500 : 0.28 ,
1000 : 0.2 ,
1000 : 0.24 ,
2000 : 0.06 ,
2000 : 0.09 ,
5000 : 0.055 ,
5000 : 0.07 ,
10000 : 0.015 ,
10000 : 0.04 ,
20000 : 0.01
20000 : 0.02
}
}
const slowdownFactor = { //Percentage of slowdown per hour.
const slowdownFactor = { //Percentage of slowdown per hour.
1 : 0.00001 ,
1 : 0.00001 ,
2 : 0.00003 ,
2 : 0.00003 ,
@ -1285,27 +1373,66 @@ const slowdownFactor={//Percentage of slowdown per hour.
100 : 0.0003 ,
100 : 0.0003 ,
500 : 0.0004 ,
500 : 0.0004 ,
1000 : 0.0005 ,
1000 : 0.0005 ,
2000 : 0.0007 ,
2000 : 0.0005 ,
5000 : 0.001 ,
5000 : 0.0005 ,
10000 : 0.002 ,
10000 : 0.0005 ,
20000 : 0.003
20000 : 0.0005
}
}
var MAXSPEED = 0
var MAXSPEED = 0
function SetupPredictionModel ( ) {
function SetupPredictionModel ( ) {
if ( chartData [ '1' ] && chartData [ '1' ] . length > 1 00) {
if ( chartData [ '1' ] && chartData [ '1' ] . length > 4 00) {
MAXSPEED = Math . floor ( chartData [ '1' ] [ 1 00] . points / ( moment ( chartData [ '1' ] [ 1 00] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
MAXSPEED = Math . floor ( chartData [ '1' ] [ 4 00] . points / ( moment ( chartData [ '1' ] [ 4 00] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
} else
} else
if ( chartData [ '1' ] && chartData [ '1' ] . length > 0 ) {
if ( chartData [ '1' ] && chartData [ '1' ] . length > 0 ) {
MAXSPEED = Math . floor ( chartData [ '1' ] [ chartData [ '1' ] . length - 1 ] . points / ( moment ( chartData [ '1' ] [ chartData [ '1' ] . length - 1 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
MAXSPEED = Math . floor ( chartData [ '1' ] [ chartData [ '1' ] . length - 1 ] . points / ( moment ( chartData [ '1' ] [ chartData [ '1' ] . length - 1 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
} else {
} else {
MAXSPEED = 0
MAXSPEED = 0
}
}
if ( en _chartData [ '1' ] && en _chartData [ '1' ] . length > 400 ) {
MAXSPEED = Math . floor ( en _chartData [ '1' ] [ 400 ] . points / ( moment ( en _chartData [ '1' ] [ 400 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
} else
if ( en _chartData [ '1' ] && en _chartData [ '1' ] . length > 0 ) {
MAXSPEED = Math . floor ( en _chartData [ '1' ] [ en _chartData [ '1' ] . length - 1 ] . points / ( moment ( en _chartData [ '1' ] [ en _chartData [ '1' ] . length - 1 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
} else {
MAXSPEED = 0
}
}
}
const RATEDURATION = 2 //In hours. How much EP/hr is shown.
const RATEDURATION = 2 //In hours. How much EP/hr is shown.
function GetRate ( rank ) {
function GetRate ( rank , en ) {
if ( en ) {
if ( en _chartData [ rank ] . length > 2 ) {
var lastpoint = en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ]
for ( var i = en _chartData [ rank ] . length - 1 ; i >= 0 ; i -- ) {
var diff = moment ( ) . diff ( en _chartData [ rank ] [ i ] . date , 'hours' )
if ( diff >= RATEDURATION ) {
break ;
} else {
lastpoint = en _chartData [ rank ] [ i ]
}
}
var timediff = moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) . diff ( moment ( lastpoint . date ) , 'minutes' )
if ( timediff < 120 ) {
if ( lastpoint === en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] ) {
return "???"
} else
return ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points - lastpoint . points ) / RATEDURATION
} else {
return Math . ceil ( ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points - lastpoint . points ) /
( moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) . diff ( moment ( lastpoint . date ) , 'minutes' ) / 60 )
)
}
} else {
if ( en _chartData [ rank ] . length > 0 ) {
var startPoint = en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ]
return Math . ceil ( GetRank ( rank ) / ( moment ( startPoint . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
} else {
return 0
}
}
} else {
if ( chartData [ rank ] . length > 2 ) {
if ( chartData [ rank ] . length > 2 ) {
var lastpoint = chartData [ rank ] [ chartData [ rank ] . length - 1 ]
var lastpoint = chartData [ rank ] [ chartData [ rank ] . length - 1 ]
for ( var i = chartData [ rank ] . length - 1 ; i >= 0 ; i -- ) {
for ( var i = chartData [ rank ] . length - 1 ; i >= 0 ; i -- ) {
@ -1335,10 +1462,36 @@ function GetRate(rank) {
return 0
return 0
}
}
}
}
}
}
}
function GetPointCount ( rank ) {
function GetPointCount ( rank , en ) {
var pointCount = 1 ;
var pointCount = 1 ;
if ( en ) {
if ( ! en _chartData [ rank ] ) {
return pointCount ;
}
if ( en _chartData [ rank ] . length > 2 ) {
var lastpoint = en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ]
for ( var i = en _chartData [ rank ] . length - 1 ; i >= 0 ; i -- ) {
var diff = moment ( ) . diff ( en _chartData [ rank ] [ i ] . date , 'hours' )
if ( diff >= RATEDURATION ) {
break ;
} else {
lastpoint = en _chartData [ rank ] [ i ]
pointCount ++ ;
}
}
return pointCount ;
} else {
if ( en _chartData [ rank ] . length > 0 ) {
return en _chartData [ rank ] . length ;
} else {
return pointCount ;
}
}
} else
{
if ( ! chartData [ rank ] ) {
if ( ! chartData [ rank ] ) {
return pointCount ;
return pointCount ;
}
}
@ -1361,9 +1514,72 @@ function GetPointCount(rank) {
return pointCount ;
return pointCount ;
}
}
}
}
}
}
}
function CreatePrediction ( precision , rank ) {
function CreatePrediction ( precision , rank , en ) {
if ( en ) {
if ( ! en _chartData [ rank ] ) {
return [ ]
}
var startPoint = en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ]
if ( rank <= 20 ) {
startPoint = { points : startPoint . points , date : moment ( ) }
}
var startTime = moment ( startPoint . date )
if ( en _PREDICTIONS && startTime . diff ( EVENTSTART _en , 'hours' ) > 36 && moment ( startPoint . date ) . diff ( EVENTSTART _en , 'hours' ) >= 36 ) {
//console.log(MAXSPEED)
//Precision is in hours. 1 is default
var finalChart = [ { y : en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points , x : en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date } ]
//Start from the time of the last reported rank.
var myPoints = startPoint . points
var pointSpeed = Math . ceil ( GetRank ( rank , en ) / ( moment ( startPoint . date ) . diff ( EVENTSTART _en , 'minutes' ) / 60 ) )
var speedGoal = MAXSPEED * nyoomfactor [ rank ]
while ( startTime < EVENTEND _en ) {
startTime . add ( precision , 'hours' )
myPoints += Math . floor ( pointSpeed )
if ( EVENTEND _en . diff ( startTime , 'hours' ) > 11 ) {
pointSpeed -= pointSpeed * ( slowdownFactor [ rank ] * 10 /*CONSTANT for adjustment*/ )
} else {
pointSpeed = Math . max (
GetRank ( rank , en ) / ( moment ( startPoint . date ) . diff ( EVENTSTART _en , 'minutes' ) / 60 ) ,
Math . min ( ( 12 - EVENTEND _en . diff ( startTime , 'hours' ) ) * ( speedGoal / 5 ) , speedGoal ) )
//pointSpeed+=(speedGoal-pointSpeed) //Increase towards final goal.
//console.log(pointSpeed)
}
finalChart = [ ... finalChart , { y : Number . isInteger ( myPoints ) ? myPoints : "???" , x : moment ( startTime ) } ]
}
predictionen _chartData [ rank ] = finalChart
return finalChart
} else
if ( PREDICTIONS _en && startTime . diff ( EVENTSTART _en , 'hours' ) > 24 && moment ( startPoint . date ) . diff ( EVENTSTART _en , 'hours' ) >= 24 ) {
//console.log(MAXSPEED)
//Precision is in hours. 1 is default
var finalChart = [ { y : en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points , x : en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date } ]
//Start from the time of the last reported rank.
var myPoints = startPoint . points
var pointSpeed = GetRate ( rank , en )
var speedGoal = MAXSPEED * nyoomfactor [ rank ]
while ( startTime < EVENTEND _en ) {
startTime . add ( precision , 'hours' )
myPoints += Math . floor ( pointSpeed )
if ( EVENTEND _en . diff ( startTime , 'hours' ) > 11 ) {
pointSpeed -= pointSpeed * ( slowdownFactor [ rank ] * 10 /*CONSTANT for adjustment*/ )
} else {
pointSpeed = Math . max (
GetRank ( rank , en ) / ( moment ( startPoint . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) ,
Math . min ( ( 12 - EVENTEND _en . diff ( startTime , 'hours' ) ) * ( speedGoal / 5 ) , speedGoal ) )
//pointSpeed+=(speedGoal-pointSpeed) //Increase towards final goal.
//console.log(pointSpeed)
}
finalChart = [ ... finalChart , { y : Number . isInteger ( myPoints ) ? myPoints : "???" , x : moment ( startTime ) } ]
}
en _predictionChartData [ rank ] = finalChart
return finalChart
} else {
return [ ]
}
} else {
if ( ! chartData [ rank ] ) {
if ( ! chartData [ rank ] ) {
return [ ]
return [ ]
}
}
@ -1424,6 +1640,7 @@ function CreatePrediction(precision,rank) {
} else {
} else {
return [ ]
return [ ]
}
}
}
}
}
function numberWithCommas ( x ) {
function numberWithCommas ( x ) {
@ -1436,7 +1653,17 @@ function numberWithCommas(x) {
}
}
}
}
function ChartData ( rank ) {
function ChartData ( rank , en ) {
if ( en ) {
if ( ! en _chartData [ rank ] ) {
return [ { x : 0 , y : 0 } ]
}
if ( rank <= 20 ) {
return [ ... en _chartData [ rank ] . map ( ( data ) => { return { x : data . date , y : data . points } } ) , { x : moment ( ) . isBefore ( EVENTEND ) ? moment ( ) : EVENTEND , y : en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points } ]
} else {
return [ { x : EVENTSTART _en , y : 0 } , ... en _chartData [ rank ] . map ( ( data ) => { return { x : data . date , y : data . points } } ) ]
}
} else {
if ( ! chartData [ rank ] ) {
if ( ! chartData [ rank ] ) {
return [ { x : 0 , y : 0 } ]
return [ { x : 0 , y : 0 } ]
}
}
@ -1445,17 +1672,51 @@ function ChartData(rank) {
} else {
} else {
return [ { x : EVENTSTART , y : 0 } , ... chartData [ rank ] . map ( ( data ) => { return { x : data . date , y : data . points } } ) ]
return [ { x : EVENTSTART , y : 0 } , ... chartData [ rank ] . map ( ( data ) => { return { x : data . date , y : data . points } } ) ]
}
}
}
}
}
function GetRank ( rank ) {
function GetRank ( rank , en ) {
if ( en ) {
if ( en _chartData [ rank ] ) {
return en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . points
} else {
return "???"
}
} else {
if ( chartData [ rank ] ) {
if ( chartData [ rank ] ) {
return chartData [ rank ] [ chartData [ rank ] . length - 1 ] . points
return chartData [ rank ] [ chartData [ rank ] . length - 1 ] . points
} else {
} else {
return "???"
return "???"
}
}
}
}
}
function GetEstimate ( rank ) {
function GetEstimate ( rank , en ) {
if ( en ) {
if ( en _predictionChartData [ rank ] ) {
var currentEstimate = 0
if ( rank > 20 && moment ( ) . diff ( moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) , 'hours' ) > 0.5 ) {
for ( var i = en _predictionChartData [ rank ] . length - 1 ; i >= 0 ; i -- ) {
if ( moment ( en _predictionChartData [ rank ] [ i ] . x ) . isAfter ( moment ( ) ) ) {
currentEstimate = en _predictionChartData [ rank ] [ i ] . y
} else {
break ;
}
}
return currentEstimate
} else {
if ( rank > 20 ) {
return GetRank ( rank , en )
} else {
return "---"
}
}
} else {
return "???"
}
} else {
if ( predictionChartData [ rank ] ) {
if ( predictionChartData [ rank ] ) {
var currentEstimate = 0
var currentEstimate = 0
if ( rank > 20 && moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) > 0.5 ) {
if ( rank > 20 && moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) > 0.5 ) {
@ -1478,32 +1739,50 @@ function GetEstimate(rank) {
} else {
} else {
return "???"
return "???"
}
}
}
}
}
function GetTime ( rank ) {
function GetTime ( rank , en ) {
if ( en ) {
if ( en _chartData [ rank ] ) {
return moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) . fromNow ( )
} else {
return ""
}
} else {
if ( chartData [ rank ] ) {
if ( chartData [ rank ] ) {
return moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) . fromNow ( )
return moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) . fromNow ( )
} else {
} else {
return ""
return ""
}
}
}
}
}
function GetUpdateColor ( rank ) {
function GetUpdateColor ( rank , en ) {
if ( en ) {
if ( en _chartData [ rank ] ) {
return "rgba(255," + Math . max ( 255 - moment ( ) . diff ( moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + "," + Math . max ( 255 - moment ( ) . diff ( moment ( en _chartData [ rank ] [ en _chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + ",1)"
} else {
return ""
}
} else {
if ( chartData [ rank ] ) {
if ( chartData [ rank ] ) {
return "rgba(255," + Math . max ( 255 - moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + "," + Math . max ( 255 - moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + ",1)"
return "rgba(255," + Math . max ( 255 - moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + "," + Math . max ( 255 - moment ( ) . diff ( moment ( chartData [ rank ] [ chartData [ rank ] . length - 1 ] . date ) , 'hours' ) * 3 , 0 ) + ",1)"
} else {
} else {
return ""
return ""
}
}
}
}
}
var tableValues = { }
var tableValues = { }
var en _tableValues = { }
app . get ( '/eventdata' , function ( req , res ) {
app . get ( '/eventdata' , function ( req , res ) {
var eventinfo = [ ]
var eventinfo = [ ]
db . query ( 'select * from event order by id desc limit 1' )
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + ' event order by id desc limit 1' )
. then ( ( data ) => {
. then ( ( data ) => {
eventinfo = data . rows ;
eventinfo = data . rows ;
if ( ! req . query . event ) {
if ( ! req . query . event ) {
return db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points from eventdata where eventid=$1 order by rank,date desc' , [ moment ( eventinfo [ 0 ] . startdate ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
return db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points,difference from (select lead(points) over (partition by rank order by rank,date desc)-points difference,* from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where eventid=$1 order by rank,date desc)t order by rank,date desc; ' , [ moment ( eventinfo [ 0 ] . startdate ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
} else {
} else {
}
}
@ -1533,10 +1812,87 @@ app.get('/eventdata',function(req,res){
} )
} )
} )
} )
var rankings = {
1 : "Maho" ,
2 : "Shinobu" ,
3 : "Muni" ,
4 : "Rei" ,
5 : "Yuka" ,
6 : "Kyoko" ,
7 : "Esoran" ,
8 : "Rinku" ,
9 : "Ibuki" ,
10 : "Esora" ,
11 : "Noa" ,
12 : "Saki" ,
13 : "Towa" ,
14 : "Rika" ,
15 : "Aoi" ,
16 : "Kurumi" ,
17 : "Saori" ,
18 : "Hiiro" ,
19 : "Michiru" ,
20 : "Tsubaki"
}
var lastRankingUpdate = moment ( )
var notPlaying = [
"Dalia" , "Marika" , "Nyochio" , "Nagisa" , "Miyu" , "Haruna" , "Miiko" , "Airi" , "Mana" , "Shano" , "Touka"
]
function RunRankingUpdate ( ) {
if ( moment ( ) . diff ( lastRankingUpdate , 'minutes' ) >= 1 ) {
for ( var i = 0 ; i < 20 ; i ++ ) {
//Possibility to switch two positions.
var swap = false
if ( i == 0 ) {
if ( Math . random ( ) <= 0.01 ) {
swap = true ;
}
} else
if ( Math . random ( ) <= 0.05 ) {
swap = true ;
}
if ( swap ) {
if ( i < 19 ) {
var previousName = rankings [ i + 1 ]
var newName = rankings [ i + 2 ]
rankings [ i + 1 ] = newName
rankings [ i + 2 ] = previousName
} else {
//Bring in a new name, swap out the old.
var previousName = rankings [ i + 1 ]
var newRandomSlot = Math . floor ( Math . random ( ) * notPlaying . length )
var newName = notPlaying [ newRandomSlot ]
rankings [ i + 1 ] = newName
notPlaying [ newRandomSlot ] = previousName
}
}
}
lastRankingUpdate = moment ( )
}
}
app . get ( '/eventdata/t50' , function ( req , res ) {
var eventinfo = [ ]
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + 'event order by id desc limit 1' )
. then ( ( data ) => {
eventinfo = data . rows ;
return db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points,difference from (select lead(points) over (partition by rank order by rank,date desc)-points difference,* from ' + ( req . query . en ? "en_" : "" ) + 'eventdata where rank>20 and eventid=$1 order by rank,date desc)t order by rank,date desc;' , [ moment ( eventinfo [ 0 ] . rank _end ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
} )
. then ( ( data ) => {
return res . status ( 200 ) . json ( data . rows )
} )
. catch ( ( err ) => {
res . status ( 500 ) . send ( err . message )
} )
} )
app . get ( '/eventdata/t20' , function ( req , res ) {
app . get ( '/eventdata/t20' , function ( req , res ) {
var eventinfo = [ ]
var eventinfo = [ ]
if ( req . query . date && req . query . rank ) {
if ( req . query . date && req . query . rank ) {
db . query ( 'select * from eventdata where date<=$1 and rank=$2 and eventid=$3 order by date desc limit 1;' , [ req . query . date , req . query . rank , 10 ] )
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where date<=$1 and rank=$2 and eventid=$3 order by date desc limit 1;' , [ req . query . date , req . query . rank , req . query . eventid ] )
. then ( ( data ) => {
. then ( ( data ) => {
res . status ( 200 ) . json ( data . rows )
res . status ( 200 ) . json ( data . rows )
} )
} )
@ -1545,7 +1901,7 @@ app.get('/eventdata/t20',function(req,res){
} )
} )
} else
} else
if ( req . query . luminous ) {
if ( req . query . luminous ) {
db . query ( "select * from eventdata where (date>='2021-02-17 23:36:16.383+00' and date<'2021-02-19 15:35:16.716+00' and rank=12) or (date>='2021-02-19 15:35:16.716+00' and rank=11) and eventid=10 order by id asc;" )
db . query ( "select * from " + ( req . query . en ? "en_" : "" ) + " eventdata where (date>='2021-02-17 23:36:16.383+00' and date<'2021-02-19 15:35:16.716+00' and rank=12) or (date>='2021-02-19 15:35:16.716+00' and rank=11) and eventid=10 order by id asc;" )
. then ( ( data ) => {
. then ( ( data ) => {
res . status ( 200 ) . json ( data . rows )
res . status ( 200 ) . json ( data . rows )
} )
} )
@ -1554,7 +1910,7 @@ app.get('/eventdata/t20',function(req,res){
} )
} )
} else
} else
if ( req . query . all && req . query . event ) {
if ( req . query . all && req . query . event ) {
db . query ( 'select * from eventdata where eventid=$1 order by date asc; ' , [ req . query . event ] )
db . query ( 'select * from (select lag(points) over (partition by rank order by date asc)-points difference,rank,eventid,date,name,points from ' + ( req . query . en ? "en_" : "" ) + 'eventdata where eventid=$1 order by date asc)t ' , [ req . query . event ] )
. then ( ( data ) => {
. then ( ( data ) => {
res . status ( 200 ) . json ( data . rows )
res . status ( 200 ) . json ( data . rows )
} )
} )
@ -1563,7 +1919,7 @@ app.get('/eventdata/t20',function(req,res){
} )
} )
} else
} else
if ( req . query . tier && req . query . event ) {
if ( req . query . tier && req . query . event ) {
db . query ( 'select * from eventdata where eventid=$1 and rank=$2 order by date desc' , [ req . query . event , req . query . tier ] )
db . query ( 'select * from (select lag(points) over (partition by rank order by date asc)-points difference,rank,eventid,date,name,points from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where eventid=$1 and rank=$2 order by date desc)t ' , [ req . query . event , req . query . tier ] )
. then ( ( data ) => {
. then ( ( data ) => {
res . status ( 200 ) . json ( data . rows )
res . status ( 200 ) . json ( data . rows )
} )
} )
@ -1572,58 +1928,131 @@ app.get('/eventdata/t20',function(req,res){
} )
} )
} else
} else
if ( req . query . chart ) {
if ( req . query . chart ) {
function RandomQuestion ( ) {
var value = Math . random ( )
if ( value <= 0.7 ) {
return "???"
} else
if ( value <= 0.9 ) {
return "Miyu"
} else
if ( value <= 0.95 ) {
return "Muni"
} else {
return "MuniMuni"
}
}
if ( req . query . event || moment ( ) . diff ( lastCachedDate , 'minutes' ) >= 1 || ( req . query . force && moment ( ) . diff ( lastCachedDate , 'seconds' ) >= 10 ) ) {
if ( req . query . event || moment ( ) . diff ( lastCachedDate , 'minutes' ) >= 1 || ( req . query . force && moment ( ) . diff ( lastCachedDate , 'seconds' ) >= 10 ) ) {
chartData = { }
chartData = { }
predictionChartData = { }
predictionChartData = { }
diffData = [ ]
diffData = [ ]
db . query ( 'select * from event order by id desc limit 1' )
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + ' event order by id desc limit 1' )
. then ( ( data ) => {
. then ( ( data ) => {
eventinfo = data . rows ;
eventinfo = data . rows ;
return db . query ( 'select * from eventdata where eventid=$1 order by date asc' , [ ( req . query . event ) ? req . query . event : eventinfo [ 0 ] . eventid ] )
return db . query ( 'select * from (select lag(points) over (partition by rank order by date asc)-points difference,rank,eventid,date,name,points from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where eventid=$1 order by date asc)t ' , [ ( req . query . event ) ? req . query . event : moment ( eventinfo [ 0 ] . rank _end ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
} )
} )
. then ( ( data ) => {
. then ( ( data ) => {
if ( data && data . rows && data . rows . length > 0 ) {
if ( data && data . rows && data . rows . length > 0 ) {
data . rows . map ( ( obj ) => { if ( chartData [ obj . rank ] ) { chartData [ obj . rank ] = [ ... chartData [ obj . rank ] , obj ] } else { chartData [ obj . rank ] = [ obj ] } } )
data . rows . map ( ( obj ) => {
if ( req . query . en ) {
if ( en _chartData [ obj . rank ] ) { en _chartData [ obj . rank ] = [ ... en _chartData [ obj . rank ] , obj ] } else { en _chartData [ obj . rank ] = [ obj ] }
} else {
if ( chartData [ obj . rank ] ) { chartData [ obj . rank ] = [ ... chartData [ obj . rank ] , obj ] } else { chartData [ obj . rank ] = [ obj ] }
}
} )
SetupPredictionModel ( )
SetupPredictionModel ( )
var tiers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 50 , 100 , 500 , 1000 , 2000 , 5000 , 10000 , 20000 , 30000 , 50000 ]
var tiers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 50 , 100 , 500 , 1000 , 2000 , 5000 , 10000 , 20000 , 30000 , 50000 ]
if ( req . query . en ) {
for ( t of tiers ) {
CreatePrediction ( 1 , t , req . query . en )
var est = GetEstimate ( t , req . query . en )
var temprate = 0
if ( en _chartData [ t ] ) {
temprate = ( t <= 20 ) ? ( en _chartData [ t ] ) ? Math . ceil ( GetRate ( t , req . query . en ) ) : undefined : Math . ceil ( GetRank ( t , req . query . en ) / ( moment ( en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] . date ) . diff ( EVENTSTART _en , 'minutes' ) / 60 ) )
}
var tempname = "" ;
if ( en _chartData [ t ] && en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] && en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] . name ) {
tempname = en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] . name
}
var tempdesc = "" ;
if ( en _chartData [ t ] && en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] && en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] . description ) {
tempdesc = en _chartData [ t ] [ en _chartData [ t ] . length - 1 ] . description
}
en _tableValues [ t ] = {
points : GetRank ( t , req . query . en ) ,
lastUpdate : GetTime ( t , req . query . en ) ,
lastUpdateColor : GetUpdateColor ( t , req . query . en ) ,
rate : temprate ? temprate : 0 ,
count : GetPointCount ( t , req . query . en ) ,
name : tempname ,
description : tempdesc ,
estimate : Number . isInteger ( est ) ? Math . ceil ( est ) : est ,
prediction : ( en _predictionChartData [ t ] && moment ( ) . isBefore ( EVENTEND _en ) ) ? en _predictionChartData [ t ] [ en _predictionChartData [ t ] . length - 1 ] . y : RandomQuestion ( )
}
}
lastCachedDate = moment ( )
res . status ( 200 ) . send ( { predictionData : en _predictionChartData , statistics : tableValues } )
} else {
for ( t of tiers ) {
for ( t of tiers ) {
CreatePrediction ( 1 , t )
CreatePrediction ( 1 , t , req . query . en )
var est = GetEstimate ( t )
var est = GetEstimate ( t , req . query . en )
var temprate = 0
var temprate = 0
if ( chartData [ t ] ) {
if ( chartData [ t ] ) {
temprate = ( t <= 20 ) ? ( chartData [ t ] ) ? Math . ceil ( GetRate ( t ) ) : undefined : Math . ceil ( GetRank ( t ) / ( moment ( chartData [ t ] [ chartData [ t ] . length - 1 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
temprate = ( t <= 20 ) ? ( chartData [ t ] ) ? Math . ceil ( GetRate ( t ) ) : undefined : Math . ceil ( GetRank ( t ) / ( moment ( chartData [ t ] [ chartData [ t ] . length - 1 ] . date ) . diff ( EVENTSTART , 'minutes' ) / 60 ) )
}
}
var tempname = "" ;
if ( chartData [ t ] && chartData [ t ] [ chartData [ t ] . length - 1 ] && chartData [ t ] [ chartData [ t ] . length - 1 ] . name ) {
tempname = chartData [ t ] [ chartData [ t ] . length - 1 ] . name
}
var tempdesc = "" ;
if ( chartData [ t ] && chartData [ t ] [ chartData [ t ] . length - 1 ] && chartData [ t ] [ chartData [ t ] . length - 1 ] . description ) {
tempdesc = chartData [ t ] [ chartData [ t ] . length - 1 ] . description
}
tableValues [ t ] = {
tableValues [ t ] = {
points : GetRank ( t ) ,
points : GetRank ( t ) ,
lastUpdate : GetTime ( t ) ,
lastUpdate : GetTime ( t ) ,
lastUpdateColor : GetUpdateColor ( t ) ,
lastUpdateColor : GetUpdateColor ( t ) ,
rate : temprate ? temprate : 0 ,
rate : temprate ? temprate : 0 ,
count : GetPointCount ( t ) ,
count : GetPointCount ( t ) ,
name : tempname ,
description : tempdesc ,
estimate : Number . isInteger ( est ) ? Math . ceil ( est ) : est ,
estimate : Number . isInteger ( est ) ? Math . ceil ( est ) : est ,
prediction : ( predictionChartData [ t ] ) ? predictionChartData [ t ] [ predictionChartData [ t ] . length - 1 ] . y : "???"
prediction : ( predictionChartData [ t ] && moment ( ) . isBefore ( EVENTEND ) ) ? predictionChartData [ t ] [ predictionChartData [ t ] . length - 1 ] . y : RandomQuestion ( )
}
}
}
}
lastCachedDate = moment ( )
lastCachedDate = moment ( )
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
}
} else {
if ( req . query . en ) {
res . status ( 200 ) . send ( { predictionData : en _predictionChartData , statistics : en _tableValues } )
} else {
} else {
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
}
}
}
} )
} )
. catch ( ( err ) => {
. catch ( ( err ) => {
res . status ( 500 ) . send ( err . message )
res . status ( 500 ) . send ( err . message )
} )
} )
} else {
if ( req . query . en ) {
res . status ( 200 ) . send ( { predictionData : en _predictionChartData , statistics : en _tableValues } )
} else {
} else {
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
res . status ( 200 ) . send ( { predictionData : predictionChartData , statistics : tableValues } )
}
}
}
} else {
} else {
db . query ( 'select * from event order by id desc limit 1' )
db . query ( 'select * from event order by id desc limit 1' )
. then ( ( data ) => {
. then ( ( data ) => {
eventinfo = data . rows ;
eventinfo = data . rows ;
return db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points from eventdata where rank<=20 and eventid=$1 order by rank,date desc' , [ moment ( eventinfo [ 0 ] . startdate ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
return db . query ( 'select distinct on (rank) rank,eventid,date,name,description,points,difference from (select lead(points) over (partition by rank order by rank,date desc)-points difference,* from ' + ( req . query . en ? "en_" : "" ) + ' eventdata where rank<=20 and eventid=$1 order by rank,date desc)t order by rank,date desc; ' , [ moment ( eventinfo [ 0 ] . rank _end ) . isBefore ( moment ( ) ) ? eventinfo [ 0 ] . eventid : eventinfo [ 0 ] . eventid - 1 ] )
} )
} )
. then ( ( data ) => {
. then ( ( data ) => {
var finaldata = data . rows
var finaldata = data . rows
var tiers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ]
var tiers = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 ]
//RunRankingUpdate()
for ( t of tiers ) {
for ( t of tiers ) {
//console.log(t)
//console.log(t)
//if (finaldata[String(t)]===undefined) {finaldata[String(t)]={"rank":t,"eventid":eventinfo[0].eventid,"name":"","description":"","date":eventinfo[0].startdate,"points":0}}
//if (finaldata[String(t)]===undefined) {finaldata[String(t)]={"rank":t,"eventid":eventinfo[0].eventid,"name":"","description":"","date":eventinfo[0].startdate,"points":0}}
@ -1638,6 +2067,10 @@ app.get('/eventdata/t20',function(req,res){
} else {
} else {
finaldata [ i ] . rate = temprate
finaldata [ i ] . rate = temprate
}
}
//finaldata[i].name="Muni";
/ * i f ( r a n k i n g s [ t ] ) {
finaldata [ i ] . name = rankings [ t ]
} * /
break ;
break ;
}
}
}
}
@ -1645,7 +2078,11 @@ app.get('/eventdata/t20',function(req,res){
finaldata = [ ... finaldata , { "rate" : 0 , "rank" : t , "eventid" : eventinfo [ 0 ] . eventid , "name" : "" , "description" : "" , "date" : eventinfo [ 0 ] . startdate , "points" : 0 } ]
finaldata = [ ... finaldata , { "rate" : 0 , "rank" : t , "eventid" : eventinfo [ 0 ] . eventid , "name" : "" , "description" : "" , "date" : eventinfo [ 0 ] . startdate , "points" : 0 } ]
}
}
}
}
if ( req . query . maxspeed ) {
res . status ( 200 ) . json ( [ ... finaldata , { maxspeed : MAXSPEED } ] )
} else {
res . status ( 200 ) . json ( finaldata )
res . status ( 200 ) . json ( finaldata )
}
} )
} )
. catch ( ( err ) => {
. catch ( ( err ) => {
res . status ( 500 ) . send ( err . message )
res . status ( 500 ) . send ( err . message )
@ -1653,6 +2090,33 @@ app.get('/eventdata/t20',function(req,res){
}
}
} )
} )
app . get ( '/cans' , function ( req , res ) {
db . query ( 'select (select count(*) from cans where can=true) as cans,(select count(*) from cans where can=false) as notcan;' )
. then ( ( data ) => { res . status ( 200 ) . send ( data . rows [ 0 ] ) } )
. catch ( ( err ) => { res . status ( 500 ) . send ( err . message ) } )
} )
app . get ( '/ev' , function ( req , res ) {
if ( req . query . all ) {
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + 'event order by id desc;' )
. then ( ( data ) => { res . status ( 200 ) . send ( data . rows ) } )
. catch ( ( err ) => { res . status ( 500 ) . send ( err . message ) } )
} else {
db . query ( 'select * from ' + ( req . query . en ? "en_" : "" ) + 'event order by id desc limit 1;' )
. then ( ( data ) => { res . status ( 200 ) . send ( data . rows [ 0 ] ) } )
. catch ( ( err ) => { res . status ( 500 ) . send ( err . message ) } )
}
} )
app . post ( '/cans' , function ( req , res ) {
if ( req . body && ( req . body . cans !== undefined && req . body . cans !== null ) ) {
db . query ( 'insert into cans(date,can) values($1,$2)' , [ moment ( ) , req . body . cans ] )
. then ( ( data ) => { res . status ( 200 ) . send ( "Done!" ) } )
} else {
res . status ( 400 ) . send ( "Invalid credentials!" )
}
} )
app . post ( '/streamkill/:id' , function ( req , res ) {
app . post ( '/streamkill/:id' , function ( req , res ) {
if ( req . body && req . body . username && req . body . authentication _token ) {
if ( req . body && req . body . username && req . body . authentication _token ) {
GetUserLoginAllowed ( req . body . username . trim ( ) , req . body . authentication _token . trim ( ) )
GetUserLoginAllowed ( req . body . username . trim ( ) , req . body . authentication _token . trim ( ) )