@ -5,12 +5,13 @@
//return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
//return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
//}
//}
var tz_offset = 240 ; //subtract this from server time to get local time; 4hrs is 240; 5hrs is 300
var tzOffsetMinutes = 240 ; //subtract this from server time to get local time; 4hrs is 240; 5hrs is 300
// TODO: handle tz_offset not divisible by 60
// TODO: handle tzOffsetMinutes not divisible by 60
// var selected_date_s = null;
// var selectedDateStr = null;
// selected_date_s = "2018-05-08";
// selectedDateStr = "2018-05-08";
var express = require ( 'express' ) ,
const express = require ( 'express' ) ,
multer = require ( 'multer' ) ,
// var exphbs = require("express-handlebars");
// var exphbs = require("express-handlebars");
// exphbs = require('../../'); // "express-handlebars"
// exphbs = require('../../'); // "express-handlebars"
cookieParser = require ( 'cookie-parser' ) ,
cookieParser = require ( 'cookie-parser' ) ,
@ -19,368 +20,432 @@ var express = require('express'),
fs = require ( 'fs' ) ,
fs = require ( 'fs' ) ,
readlines = require ( 'n-readlines' ) ;
readlines = require ( 'n-readlines' ) ;
const os = require ( 'os' ) ;
const os = require ( 'os' ) ;
var formidable = require ( 'formidable' )
//var formidable = require('formidable');
var querystring = require ( "querystring" ) ; // built-in
const querystring = require ( "querystring" ) ; // built-in
var mv = require ( 'mv' ) ;
// TODO: var config = require(storagePath + '/config.js') // config file contains all tokens and other private info
// TODO: var config = require(storage_path + '/config.js') // config file contains all tokens and other private info
// var fun = require('./functions.js'); // functions file contains our non-app-specific functions including those for our Passport and database work
// var fun = require('./functions.js'); // functions file contains our non-app-specific functions including those for our Passport and database work
var mt = require ( './minetestinfo.js' ) ; // functions file contains our non-app-specific functions including those for our Passport and database work
var mt = require ( './minetestinfo.js' ) ; // functions file contains our non-app-specific functions including those for our Passport and database work
// var util = require('util')
// var util = require('util')
var app = express ( ) ;
var app = express ( ) ;
app . set ( 'view engine' , 'ejs' ) ;
// see https://medium.com/@TheJesseLewis/how-to-make-a-basic-html-form-file-upload-using-multer-in-an-express-node-js-app-16dac2476610
const port = process . env . PORT || 64638 ;
app . use ( bodyParser . urlencoded ( { extended : false } ) ) ; // handle body requests
app . use ( bodyParser . json ( ) ) ; // make JSON work
app . use ( '/public' , express . static ( __ dirname + '/public' ) ) ;
//app.engine('handlebars', exphbs({defaultLayout: 'main'}));
//app.engine('handlebars', exphbs({defaultLayout: 'main'}));
//app.set('view engine', 'handlebars');
//app.set('view engine', 'handlebars');
var players = [ ] ;
var players = [ ] ;
var player_indices = { } ;
var playerI ndices = { } ;
var play_dates = [ ] ;
var activityD ates = [ ] ;
//see https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction
//see https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction
var prev_line = null ;
var previousL ine = null ;
//#region derived from mtsenliven.py
//#region derived from mtsenliven.py
var msgprefix_f lags = [ "WARNING[Server]: " , "ACTION[Server]: " ]
var msgPrefixF lags = [ "WARNING[Server]: " , "ACTION[Server]: " ]
var msgprefix_l ists = { } // where flag is key
var msgPrefixL ists = { } // where flag is key
var mf_len = msgprefix_f lags . length ;
var mfLen = msgPrefixF lags . length ;
for ( var mf_i = 0 ; mf_i < mf_len ; mf_i ++ ) {
for ( var mfIndex = 0 ; mfIndex < mfLen ; mfIndex ++ ) {
msgprefix_lists [ msgprefix_flags [ mf_i ] ] = [ ] ;
msgPrefixLists [ msgPrefixFlags [ mfIndex ] ] = [ ] ;
}
}
var non_unique_w raps = [ ] ;
var nonUniqueW raps = [ ] ;
non_unique_w raps . push ( {
nonUniqueW raps . push ( {
"opener" : "active block modifiers took " ,
"opener" : "active block modifiers took " ,
"closer" : "ms (longer than 200ms)"
"closer" : "ms (longer than 200ms)"
} ) ;
} ) ;
var unique_f lags = [
var uniqueF lags = [
"leaves game" ,
"leaves game" ,
"joins game"
"joins game"
] ;
] ;
//#endregion derived from mtsenliven.py
//#endregion derived from mtsenliven.py
const skinStorage = multer . diskStorage ( {
// see https://medium.com/@TheJesseLewis/how-to-make-a-basic-html-form-file-upload-using-multer-in-an-express-node-js-app-16dac2476610
destination : function ( req , file , next ) {
next ( null , mt . skinsPath ( ) ) ; // or something like './public/photo-storage'
} ,
limits : {
fileSize : 1 * 1024 * 1024 // in bytes
} ,
// Change filename
filename : function ( req , file , next ) {
const ext = "png" ;
// var errMsg = null;
console . log ( "* Checking name..." ) ;
if ( ! req . body . userName ) {
return next ( new Error ( "userName is missing" ) ) ;
}
else if ( req . body . userName . length < 1 ) {
return next ( new Error ( "userName is blank." ) ) ;
}
if ( file . size < 1 ) {
return next ( new Error ( "image not selected" ) ) ;
}
//if (errMsg === null) {
var directName = "player_" + req . body . userName + '.' + ext ;
console . log ( "* Renaming '" + file + "' to " + directName ) ;
next ( null , directName ) ;
//}
//else {
//console.log(errMsg);
//next(new Error(errMsg));
//}
// const ext = file.mimetype.split('/')[1];
// next(null, file.fieldname + '-' + Date.now() + '.'+ext);
}
} ) ;
// see https://medium.com/@bmshamsnahid/nodejs-file-upload-using-multer-3a904516f6d2
const skinUpload = multer ( {
storage : skinStorage ,
fileFilter : function ( req , file , next ) {
const ext = "png" ;
console . log ( "filtering..." ) ;
var errMsg = null ;
// NOTE: return with error aborts the upload.
if ( ! file ) {
errMsg = "You did not select a file." ;
req . fileValidationError = errMsg ;
return next ( new Error ( errMsg ) ) ; // next(null, false, new Error(errMsg))
}
else if ( file . size < 1 ) {
errMsg = "Empty file." ;
req . fileValidationError = errMsg ;
return next ( new Error ( errMsg ) ) ;
}
if ( ! file . mimetype . startsWith ( 'image/' ) ) {
errMsg = "* ERROR: file type " + file . mimetype
+ " is not supported" ;
req . fileValidationError = errMsg ;
return next ( new Error ( errMsg ) ) ;
}
if ( errMsg === null ) {
var directName = "player_" + req . body . userName + '.' + ext ;
console . log ( "* " + file . mimetype + " '" + file + "' uploaded..." ) ;
// set player skin name to new file name:
mt . setSkin ( req . body . userName , directName ) ;
// TODO: allow setSkin output to res
next ( null , true ) ;
} else {
console . log ( errMsg ) ;
return next ( new Error ( errMsg ) ) ;
}
}
} ) ;
function process_logline ( line , line_number ) {
function processLogL ine ( line , lineN umber ) {
//selected_date_s
//selectedDateStr
//TODO: use store_unique_log_data instead of this function
//TODO: use storeUniqueLogD ata instead of this function
var player_name = null ;
var playerN ame = null ;
var verb = "" ;
var verb = "" ;
var time_s = "" ;
var timeStr = "" ;
var date_s = "" ;
var dateStr = "" ;
var player_ip = null ;
var playerIP = null ;
const time_start_i = 11 ;
const timeStartInt = 11 ;
var uf_len = unique_flags . length ;
var ufLen = uniqueF lags . length ;
mf_len = msgprefix_flags . length ;
mfLen = msgPrefixF lags . length ;
var verb_i = - 1 ;
var verbIndex = - 1 ;
var verb_number = - 1 ;
var verbN umber = - 1 ;
var msgprefix_i = - 1 ;
var msgPrefixIndex = - 1 ;
var msgprefix_number = - 1 ;
var msgPrefixN umber = - 1 ;
var msgprefix = null ;
var msgprefix = null ;
var index_msg = "" ;
var indexM sg = "" ;
for ( var mf_i = 0 ; mf_i < mf_len ; mf_i ++ ) {
for ( var mfIndex = 0 ; mfIndex < mfLen ; mfIndex ++ ) {
msgprefix_i = line . indexOf ( msgprefix_flags [ mf_i ] ) ;
msgPrefixIndex = line . indexOf ( msgPrefixFlags [ mfIndex ] ) ;
if ( msgprefix_i > - 1 ) {
if ( msgPrefixIndex > - 1 ) {
msgprefix_number = mf_i ;
msgPrefixNumber = mfIndex ;
msgprefix = msgprefix_flags [ mf_i ] ;
msgprefix = msgPrefixFlags [ mfIndex ] ;
break ;
break ;
}
}
}
}
var skip_date_enable = false ;
var skipDateE nable = false ;
for ( var uf_i = 0 ; uf_i < uf_len ; uf_i ++ ) {
for ( var ufIndex = 0 ; ufIndex < ufLen ; ufIndex ++ ) {
verb_i = line . indexOf ( unique_flags [ uf_i ] ) ;
verbIndex = line . indexOf ( uniqueFlags [ ufIndex ] ) ;
if ( verb_i > - 1 ) {
if ( verbIndex > - 1 ) {
verb_number = uf_i ;
verbNumber = ufIndex ;
verb = unique_flags [ uf_i ] ;
verb = uniqueFlags [ ufIndex ] ;
date_s = line . substring ( 0 , 10 ) . trim ( ) ;
dateStr = line . substring ( 0 , 10 ) . trim ( ) ;
//if (selected_date_s==null || selected_date_s==date_s) {
//if (selectedDateStr==null || selectedDateStr==dateStr ) {
//console.log("(verbose message in process_logline) using '" + date_s + "' since selected '"+selected_date_s+"'");
//console.log("(verbose message in processLogLine) using '" + dateStr + "' since selected '"+selectedDateStr +"'");
time_s = line . substring ( time_start_i , time_start_i + 8 ) ;
timeStr = line . substring ( timeStartInt , timeStartInt + 8 ) ;
//console.log("using time "+time_s);
//console.log("using time "+timeStr );
if ( msgprefix != null ) {
if ( msgprefix != null ) {
player_name = line . substring ( msgprefix_i + msgprefix . length , verb_i ) . trim ( ) ;
playerN ame = line . substring ( msgPrefixIndex + msgprefix . length , verbIndex ) . trim ( ) ;
var ip_f lag = " [" ;
var ipF lag = " [" ;
var ip_i = player_n ame . indexOf ( ip_f lag ) ;
var ipIndex = playerN ame . indexOf ( ipF lag ) ;
if ( ip_i > - 1 ) {
if ( ipIndex > - 1 ) {
player_ip = player_n ame . substring ( ip_i + ip_f lag . length , player_n ame . length - 1 ) ;
playerIP = playerN ame . substring ( ipIndex + ipF lag . length , playerN ame . length - 1 ) ;
player_name = player_n ame . substring ( 0 , ip_i ) ;
playerName = playerN ame . substring ( 0 , ipIndex ) ;
}
}
}
}
else {
else {
player_n ame = "<missing msgprefix&rt;" ;
playerN ame = "<missing msgprefix&rt;" ;
}
}
//}
//}
//else {
//else {
// skip_date_e nable = true;
// skipDateE nable = true;
//console.log("WARNING in process_logline: skipping '" + date_s + "' since not '"+selected_date_s +"'");
//console.log("WARNING in processLogLine: skipping '" + dateStr + "' since not '"+selectedDateStr +"'");
//}
//}
break ;
break ;
}
}
}
}
var index = - 1 ; // player index
var index = - 1 ; // player index
if ( player_n ame != null ) {
if ( playerN ame != null ) {
if ( player_n ame . length > 0 ) {
if ( playerN ame . length > 0 ) {
if ( player_i ndices . hasOwnProperty ( player_n ame ) ) {
if ( playerI ndices . hasOwnProperty ( playerN ame ) ) {
index = player_indices [ player_n ame ] ;
index = playerIndices [ playerN ame ] ;
index_m sg = "cached " ;
indexM sg = "cached " ;
}
}
else {
else {
index = players . length ;
index = players . length ;
//players.push({});
//players.push({});
players [ index ] = { } ;
players [ index ] = { } ;
players [ index ] . display_name = player_n ame ;
players [ index ] . displayName = playerN ame ;
player_indices [ player_n ame ] = index ;
playerIndices [ playerN ame ] = index ;
//console.log("created new index "+index);
//console.log("created new index "+index);
}
}
}
}
else {
else {
console . log ( "WARNING in process_logl ine: zero-length player name" ) ;
console . log ( "WARNING in processLogL ine: zero-length player name" ) ;
}
}
}
}
if ( index < 0 && ( verb == "leaves game" || verb == "joins game" ) ) {
if ( index < 0 && ( verb == "leaves game" || verb == "joins game" ) ) {
console . log ( "(ERROR in process_logline) " + index_m sg +
console . log ( "(ERROR in processLogLine) " + indexM sg +
"index was '" + index + "' but date was present '" +
"index was '" + index + "' but date was present '" +
date_s + "' for '" + line + "' (no player found, but" +
dateStr + "' for '" + line + "' (no player found, but" +
"verb is a player verb)." ) ;
"verb is a player verb)." ) ;
}
}
var play_date_e nable = false ;
var playDateE nable = false ;
if ( verb == "leaves game" ) {
if ( verb == "leaves game" ) {
if ( index > - 1 ) {
if ( index > - 1 ) {
var play_i = - 1 ;
var playIndex = - 1 ;
if ( ! players [ index ] . hasOwnProperty ( "plays" ) ) {
if ( ! players [ index ] . hasOwnProperty ( "plays" ) ) {
players [ index ] . plays = { } ;
players [ index ] . plays = { } ;
}
}
if ( ! players [ index ] . plays . hasOwnProperty ( date_s ) ) {
if ( ! players [ index ] . plays . hasOwnProperty ( dateStr ) ) {
//leave login time blank--player must have logged in before the available part of the log began
//leave login time blank--player must have logged in before the available part of the log began
players [ index ] . plays [ date_s ] = [ ] ;
players [ index ] . plays [ dateStr ] = [ ] ;
players [ index ] . plays [ date_s ] . push ( { } ) ;
players [ index ] . plays [ dateStr ] . push ( { } ) ;
play_i = 0 ;
playIndex = 0 ;
}
}
else {
else {
if ( players [ index ] . plays [ date_s ] . length == 0 ) players [ index ] . plays [ date_s ] . push ( { } ) ;
if ( players [ index ] . plays [ dateStr ] . length == 0 ) players [ index ] . plays [ dateStr ] . push ( { } ) ;
play_i = players [ index ] . plays [ date_s ] . length - 1 ;
playIndex = players [ index ] . plays [ dateStr ] . length - 1 ;
if ( players [ index ] . plays [ date_s ] [ play_i ] . hasOwnProperty ( "logout_t ime" ) ) {
if ( players [ index ] . plays [ dateStr ] [ playIndex ] . hasOwnProperty ( "logoutT ime" ) ) {
//If last entry is incomplete, start a new one:
//If last entry is incomplete, start a new one:
players [ index ] . plays [ date_s ] . push ( { } ) ;
players [ index ] . plays [ dateStr ] . push ( { } ) ;
play_i ++ ;
playIndex ++ ;
}
}
}
}
players [ index ] . plays [ date_s ] [ play_i ] . logout_time = time_s ;
players [ index ] . plays [ dateStr ] [ playIndex ] . logoutTime = timeStr ;
play_date_e nable = true ;
playDateE nable = true ;
}
}
}
}
else if ( verb == "joins game" ) {
else if ( verb == "joins game" ) {
if ( index > - 1 ) {
if ( index > - 1 ) {
if ( player_ip != null ) {
if ( playerIP != null ) {
players [ index ] . player_ip = player_ip ;
players [ index ] . playerIP = playerIP ;
var play_i = - 1 ;
var playIndex = - 1 ;
if ( ! players [ index ] . hasOwnProperty ( "plays" ) ) {
if ( ! players [ index ] . hasOwnProperty ( "plays" ) ) {
players [ index ] . plays = { } ;
players [ index ] . plays = { } ;
}
}
if ( ! players [ index ] . plays . hasOwnProperty ( date_s ) ) {
if ( ! players [ index ] . plays . hasOwnProperty ( dateStr ) ) {
players [ index ] . plays [ date_s ] = [ ] ;
players [ index ] . plays [ dateStr ] = [ ] ;
play_i = 0 ;
playIndex = 0 ;
}
}
else play_i = players [ index ] . plays [ date_s ] . length ;
else playIndex = players [ index ] . plays [ dateStr ] . length ;
players [ index ] . plays [ date_s ] . push ( { } ) ;
players [ index ] . plays [ dateStr ] . push ( { } ) ;
//console.log(verb+" on "+date_s+" (length "+players[index].plays[date_s].length+") play "+play_i+"+1 for player ["+index+"] "+player_n ame+"...");
//console.log(verb+" on "+dateStr+" (length "+players[index].plays[dateStr].length+") play "+playIndex+"+1 for player ["+index+"] "+playerN ame+"...");
players [ index ] . plays [ date_s ] [ play_i ] . login_time = time_s ;
players [ index ] . plays [ dateStr ] [ playIndex ] . loginTime = timeStr ;
play_date_e nable = true ;
playDateE nable = true ;
}
}
// else redundant (server writes " joins game " again
// else redundant (server writes " joins game " again
// and shows list of players instead of ip).
// and shows list of players instead of ip).
//TODO: else analyze list of players to confirm in case player logged in all day
//TODO: else analyze list of players to confirm in case player logged in all day
}
}
}
}
if ( play_date_e nable ) {
if ( playDateE nable ) {
if ( date_s . length > 0 ) {
if ( dateStr . length > 0 ) {
if ( play_d ates . indexOf ( date_s ) < 0 ) {
if ( activityD ates . indexOf ( dateStr ) < 0 ) {
play_d ates . push ( date_s ) ;
activityD ates . push ( dateStr ) ;
}
}
}
}
}
}
}
}
function store_unique_log_d ata ( output , line_number , err_f lag = false ) {
function storeUniqueLogD ata ( output , lineNumber , errF lag = false ) {
var ret = "" ;
var ret = "" ;
var output_strip = output . trim ( ) ;
var outputTrim = output . trim ( ) ;
var u_p refix = "active block modifiers took " ;
var uP refix = "active block modifiers took " ;
var u_s uffix = "ms (longer than 200ms)" ;
var uS uffix = "ms (longer than 200ms)" ;
// (out_b ytes is bytes)
// (outB ytes is bytes)
var show_e nable = true ;
var showE nable = true ;
var found_f lag = null ;
var foundF lag = null ;
var f_i = null ;
var fIndex = null ;
var always_show_e nable = false ;
var alwaysShowE nable = false ;
var msg_m sg = "previous message" ;
var msgM sg = "previous message" ;
var uf_len = unique_f lags . length ;
var ufLen = uniqueF lags . length ;
for ( var uf_i = 0 ; uf_i < uf_len ; uf_i ++ ) {
for ( var ufIndex = 0 ; ufIndex < ufLen ; ufIndex ++ ) {
if ( output . includes ( unique_flags [ uf_i ] ) ) {
if ( output . includes ( uniqueFlags [ ufIndex ] ) ) {
always_show_e nable = true ;
alwaysShowE nable = true ;
}
}
}
}
if ( ! always_show_e nable ) {
if ( ! alwaysShowE nable ) {
var mf_len = msgprefix_f lags . length ;
var mfLen = msgPrefixF lags . length ;
for ( var mf_i = 0 ; mf_i < mf_len ; mf_i ++ ) {
for ( var mfIndex = 0 ; mfIndex < mfLen ; mfIndex ++ ) {
// such as '2018-02-06 21:08:06: WARNING[Server]: Deprecated call to get_look_yaw, use get_look_horizontal instead'
// such as '2018-02-06 21:08:06: WARNING[Server]: Deprecated call to get_look_yaw, use get_look_horizontal instead'
// or 2018-02-06 21:08:05: ACTION[Server]: [playereffects] Wrote playereffects data into /home/owner/.minetest/worlds/FCAGameAWorld/playereffects.mt.
// or 2018-02-06 21:08:05: ACTION[Server]: [playereffects] Wrote playereffects data into /home/owner/.minetest/worlds/FCAGameAWorld/playereffects.mt.
f_i = output . find ( msgprefix_flags [ mf_i ] ) ;
fIndex = output . find ( msgPrefixFlags [ mfIndex ] ) ;
if ( f_i >= 0 ) {
if ( fIndex >= 0 ) {
found_flag = msgprefix_flags [ mf_i ] ;
foundFlag = msgPrefixFlags [ mfIndex ] ;
break ;
break ;
}
}
}
}
if ( found_f lag != null ) {
if ( foundF lag != null ) {
var sub_m sg = output . substring ( f_i + flag . length ) . trim ( ) ;
var subM sg = output . substring ( fIndex + flag . length ) . trim ( ) ;
var nuw_len = non_unique_w raps . length ;
var nUWLen = nonUniqueW raps . length ;
for ( var nuw_i = 0 ; nuw_i < nuw_len ; nuw_i ++ ) {
for ( var nUWIndex = 0 ; nUWIndex < nUWLen ; nUWIndex ++ ) {
//for (wrap in non_unique_w raps) {
//for (wrap in nonUniqueW raps) {
var wrap = non_unique_wraps [ nuw_i ] ;
var wrap = nonUniqueWraps [ nUWIndex ] ;
if ( sub_m sg . includes ( wrap [ "opener" ] ) && sub_m sg . includes ( wrap [ "closer" ] ) ) {
if ( subM sg . includes ( wrap [ "opener" ] ) && subM sg . includes ( wrap [ "closer" ] ) ) {
sub_m sg = wrap [ "opener" ] + "..." + wrap [ "closer" ] ;
subM sg = wrap [ "opener" ] + "..." + wrap [ "closer" ] ;
msg_m sg = "similar messages" ;
msgM sg = "similar messages" ;
break ;
break ;
}
}
}
}
if ( msgprefix_lists [ found_f lag ] . indexOf ( sub_m sg ) > - 1 ) {
if ( msgPrefixLists [ foundF lag ] . indexOf ( subM sg ) > - 1 ) {
show_e nable = false ;
showE nable = false ;
}
}
else {
else {
msgprefix_lists [ found_f lag ] . push ( sub_m sg ) ;
msgPrefixLists [ foundF lag ] . push ( subM sg ) ;
}
}
}
}
}
}
if ( show_e nable ) {
if ( showE nable ) {
ret = output_strip ;
ret = outputTrim ;
if ( found_f lag != null ) {
if ( foundF lag != null ) {
ret += "\n [ EnlivenMinetest ] " + msg_m sg + " will be suppressed" ;
ret += "\n [ EnlivenMinetest ] " + msgM sg + " will be suppressed" ;
}
}
}
}
return ret ;
return ret ;
}
}
function read_l og ( ) {
function readL og ( ) {
if ( players == null ) players = [ ] ;
if ( players == null ) players = [ ] ;
if ( player_i ndices == null ) player_i ndices = { } ;
if ( playerI ndices == null ) playerI ndices = { } ;
// os.homedir() + "/.minetest/debug_archived/2018/05/08.txt",
// os.homedir() + "/.minetest/debug_archived/2018/05/08.txt",
// var log_p aths = [os.homedir() + "/.minetest/debug.txt"];
// var logP aths = [os.homedir() + "/.minetest/debug.txt"];
var log_p aths = [ os . homedir ( ) + "/minetest/bin/debug.txt" ] ;
var logP aths = [ os . homedir ( ) + "/minetest/bin/debug.txt" ] ;
var lp_len = log_p aths . length ;
var lpLen = logP aths . length ;
for ( var lp_i = 0 ; lp_i < lp_len ; lp_i ++ ) {
for ( var lpIndex = 0 ; lpIndex < lpLen ; lpIndex ++ ) {
var this_log_path = log_paths [ lp_i ] ;
var thisLogPath = logPaths [ lpIndex ] ;
console . log ( "EnlivenMinetest webapp reading '" + this_log_p ath + "'..." ) ;
console . log ( "EnlivenMinetest webapp reading '" + thisLogP ath + "'..." ) ;
var line_n umber = 1 ;
var lineN umber = 1 ;
if ( fs . existsSync ( this_log_p ath ) ) {
if ( fs . existsSync ( thisLogP ath ) ) {
//uses n-readlines package: see https://stackoverflow.com/questions/34223065/read-lines-synchronously-from-file-in-node-js
//uses n-readlines package: see https://stackoverflow.com/questions/34223065/read-lines-synchronously-from-file-in-node-js
var liner = new readlines ( this_log_p ath ) ;
var readLines = new readlines ( thisLogP ath ) ;
var next = true ;
var nextLine = true ;
while ( next ) {
while ( nextLine ) {
next = liner . next ( ) ;
nextLine = readLines . next ( ) ;
if ( next != false ) {
if ( nextLine != false ) {
process_logl ine ( next . toString ( 'ascii' ) , line_n umber ) ;
processLogL ine ( nextLine . toString ( 'ascii' ) , lineN umber ) ;
line_n umber ++ ;
lineN umber ++ ;
}
}
}
}
}
}
else {
else {
console . log ( "WARNING: file not found: '" + this_log_p ath + "' (listing actual archived log folders is not yet implemented, so this is a hard-coded demo folder only on poikilos' server)" ) ;
console . log ( "WARNING: file not found: '" + thisLogP ath + "' (listing actual archived log folders is not yet implemented, so this is a hard-coded demo folder only on poikilos' server)" ) ;
}
}
}
}
}
}
app . get ( '/skin-form' , function ( req , res ) {
app . get ( '/skin-upload-form' , function ( req , res , next ) {
var ret = "" ;
//var ending = "";
ret += '<html><body style="font-family:calibri,sans">' + "\n" ;
//ending += '<a href="/">Back to Main Site</a><br/>' + "\n";
ret += '<form action="/set-skin" method="post" enctype="multipart/form-data">' + "\n" ;
////ending += '<a href="/skin-upload-form">Back to Upload</a><br/>' + "\n";
ret += 'User Name (case-sensitive): <input type="text" name="userName" id="userName">' + "\n" ;
//ending += '</body></html>';
ret += 'Select a png image to upload:' + "\n" ;
//res.write('<html><body style="font-family:calibri,sans">'+"\n");
ret += '<input type="file" name="userFile" id="userFile">' + "\n" ;
//res.write('<form action="/upload-skin" method="post" enctype="multipart/form-data">'+"\n");
ret += '<input type="submit" value="Upload Image" name="submit">' + "\n" ;
//res.write('User Name (case-sensitive): <input type="text" name="userName" id="userName">'+"\n");
ret += '</form>' + "\n" ;
//res.write('Select a png image to upload:'+"\n");
ret += '</body></html>' ;
//res.write('<input type="file" name="userFile" id="userFile">'+"\n");
res . send ( ret ) ;
//res.write('<input type="submit" value="Upload Image" name="submit">'+"\n");
//res.render('home');
//res.write('</form>'+"\n");
//res.end(ending);
var msg = "" ;
res . render ( 'pages/skin-upload-form' , {
msg : msg
} ) ;
} ) ;
app . get ( '/skin-selection-form' , function ( req , res , next ) {
var msg = "" ;
res . render ( 'pages/skin-selection-form' , {
msg : msg ,
skinFileNames : mt . selectableSkinFileNames ( )
} ) ;
} ) ;
} ) ;
//using express & formidable:
// see "new way" of handling multer errors: https://github.com/expressjs/multer#error-handling
app . post ( '/set-skin' , function ( req , res ) {
var singleSkinUpload = skinUpload . single ( 'userFile' ) ;
var form = new formidable . IncomingForm ( ) ;
app . post ( '/upload-skin' , function ( req , res ) {
// from coderskins/readme.txt:
singleSkinUpload ( req , res , function ( err ) {
//To install a specific skin for a specific player, name the PNG file
if ( err instanceof multer . MulterError ) {
//to be used as follows:
// A Multer error occurred when uploading.
//player_NAME.png
res . render ( 'multer error' , { error : err } ) ;
//where NAME is the player's in-game nick. Then copy the PNG file into
//res.render('pages/result', {
//the mod's "textures" directory.
//msg: "An error occurred in processing the form: " + err,
//The PNG file should be a standard Minetest 64x32 or Minecraft 64x64
//});
//"skin" file.
} else if ( err ) {
//Or, if you prefer, create a text file, in the mod's "textures" direc-
// An unknown error occurred when uploading.
//tory with a similar filename:
res . render ( 'unknown error' , { error : err } ) ;
//player_NAME.skin
//res.render('pages/result', {
//(OldCoder, 2019)
//msg: "An error occurred in processing: " + err,
var directPath = "" ;
//});
var indirectPath = "" ;
var destNameNoExt = "" ;
var msg = "Uploading..." ;
form . parse ( req , function ( err , fields , files ) {
if ( err ) next ( err ) ;
destNameNoExt = destNameNoExt = "player_" + fields . userName ;
directPath = mt . skinDir ( ) + "/" + destNameNoExt + ".png" ;
indirectPath = mt . skinDir ( ) + "/" + destNameNoExt + ".skin" ;
// TODO: make sure my_file and userName values are present
if ( files . hasOwnProperty ( 'userFile' ) ) {
if ( fields . hasOwnProperty ( 'userName' ) ) {
var originalPath = files . userFile . path ;
console . log ( "trying to rename " + files . userFile . path
+ " to " + directPath ) ;
// NOTE: rename does not work if tmp is on different device (common)
mv ( files . userFile . path , directPath , function ( err ) {
// fs.rename(files.userFile.path, directPath, function(err) {
if ( err ) {
msg = "Failed to rename " + originalPath
+ " to " + directPath ;
console . log ( msg ) ;
console . log ( JSON . stringify ( err ) ) ;
msg += "<br/>\n" ;
//next(err);
// TODO: why does next above show:
//ReferenceError: next is not defined
//at /home/owner/git/EnlivenMinetest/webapp/server.js:355:6
//at FSReqWrap.oncomplete (fs.js:135:15)
}
else {
var thisData = destNameNoExt + ".png" ;
fs . writeFile ( indirectPath , thisData , function ( err , data ) {
if ( err ) console . log ( err ) ;
console . log ( "Successfully wrote " + thisData
+ " to " + indirectPath + "." ) ;
} ) ;
}
res . end ( ) ;
} ) ;
}
}
else {
else {
console . log ( "userName is undefined." ) ;
// var ending = "";
}
var msg = "" ;
// ending += '<a href="/">Back to Main Site</a><br/>' + "\n";
// ending += '<a href="/skin-upload-form">Back to Upload</a><br/>' + "\n";
// ending += '</body></html>';
// res.write('<html>');
// res.write('<body style="font-family:calibri,sans">');
if ( ! req . fileValidationError ) {
// res.write('<p>Complete!</p>');
msg = "Complete!" ;
}
}
else {
else {
console . log ( "userFile is undefined." ) ;
msg = req . fileValidationError ;
// res.write('<p>' + req.fileValidationError + '</p>');
}
}
//res.end(ending);
res . render ( 'pages/result' , {
msg : msg
} ) ;
} ) ;
//form.on('fileBegin', function (name, file){
}
////file.path = __dirname + '/uploads/' + file.name;
} )
//// file.path = skinDir + "/" + file.name;
} ) ;
//// manual_path = "player_" +
//});
app . get ( '/select-skin' , function ( req , res , next ) {
form . on ( 'file' , function ( name , file ) {
mt . setSkin ( req . query . userName , req . query . skinFileName ) ;
msg = 'Uploaded ' + file . name + "<br/>\n" ;
res . render ( 'pages/result' , {
console . log ( msg ) ;
msg : "Complete."
} ) ;
} ) ;
//res.sendFile(__dirname + '/index.html');
res . redirect ( "/?msg=" + querystring . stringify ( msg ) ) ;
} ) ;
} ) ;
app . get ( '/' , function ( req , res ) {
app . get ( '/' , function ( req , res , next ) {
var ret = "" ;
//var ret = "";
ret += '<html><body style="font-family:calibri,sans">' ;
//res.write('<html>');
//res.write('<body style="font-family:calibri,sans">');
// Whenever server starts the following is logged
// Whenever server starts the following is logged
// (see also etc/example-input.txt):
// (see also etc/example-input.txt):
//
//
@ -388,11 +453,12 @@ app.get('/', function (req, res) {
// Separator
// Separator
//-------------
//-------------
//
//
var selected_date_s = null ;
var selectedDateStr = null ;
if ( req . query . date ) selected_date_s = req . query . date
var msg = "" ;
if ( req . query . date ) selectedDateStr = req . query . date
if ( req . query . msg != undefined ) {
if ( req . query . msg != undefined ) {
ret += "<br/>\n" ;
//res.write("<br/>");
//ret += "<b>" + querystring.parse(req.query.msg) + "</b><br>\n";
//res.write( "<b>" + querystring.parse(req.query.msg) + "</b><br>\n") ;
// line above causes:
// line above causes:
//TypeError: Cannot convert object to primitive value
//TypeError: Cannot convert object to primitive value
//at /home/owner/git/EnlivenMinetest/webapp/server.js:390:16
//at /home/owner/git/EnlivenMinetest/webapp/server.js:390:16
@ -406,53 +472,59 @@ app.get('/', function (req, res) {
//at expressInit (/home/owner/git/EnlivenMinetest/webapp/node_modules/express/lib/middleware/init.js:40:5)
//at expressInit (/home/owner/git/EnlivenMinetest/webapp/node_modules/express/lib/middleware/init.js:40:5)
//at Layer.handle [as handle_request] (/home/owner/git/EnlivenMinetest/webapp/node_modules/express/lib/router/layer.js:95:5)
//at Layer.handle [as handle_request] (/home/owner/git/EnlivenMinetest/webapp/node_modules/express/lib/router/layer.js:95:5)
ret += "<br/>\n" ;
//res.write("<br/>");
}
}
ret += "Server must restart for uploaded skins to take effect.<br/>\n" ;
//res.write('<p><a href="/skin-upload-form">Upload Skin</a></p>');
ret += "assuming minetestserver ran as: " + os . homedir ( ) + "<br/>\n" ;
//res.write("<h3>Server info</h3>");
ret += "timezone (tz_offset/60*-1): " + ( Math . floor ( tz_offset / 60 ) * - 1 ) + '<span name="tzArea" id="tzArea"></span><br/>\n' ;
//res.write("<ul>");
ret += 'date: <span id="dateArea" name="dateArea">' + selected_date_s + '</span><br/>' + "\n" ;
//res.write("<li>assuming minetestserver ran as: " + os.homedir() + "</li>");
//ret += 'var play_dates = [];';
//res.write("<li>timezone (tzOffsetMinutes/60*-1): " + (Math.floor(tzOffsetMinutes/60)*-1) + '<span name="tzArea" id="tzArea"></span>' + "</li>");
//res.write('<li>date: <span id="dateArea" name="dateArea">' + ((selectedDateStr!=null)?selectedDateStr:"not selected") + '</span>' + "</li>");
//res.write('var activityDates = [];');
var pdLength = 0 ;
var pdLength = 0 ;
if ( play_dates != null ) pdLength = play_dates . length ;
if ( activityDates != null ) pdLength = activityDates . length ;
for ( var pd_i = 0 ; pd_i < pdLength ; pd_i ++ ) {
var logDates = [ ] ;
//ret += 'play_dates.push("' + play_dates[pd_i] + '");';
for ( var pdIndex = 0 ; pdIndex < pdLength ; pdIndex ++ ) {
if ( selected_date_s != play_dates [ pd_i ] ) {
//res.write('activityDates.push("' + activityDates[pdIndex] + '");');
ret += '<a href="?date=' + play_dates [ pd_i ] + '">' + play_dates [ pd_i ] + '</a> ' ;
if ( selectedDateStr != activityDates [ pdIndex ] ) {
//res.write('<a href="?date='+activityDates[pdIndex]+'">'+activityDates[pdIndex]+'</a> ');
logDates . push ( { date : activityDates [ pdIndex ] , active : true } ) ;
}
}
else {
else {
ret += play_dates [ pd_i ] + ' ' ;
logDates . push ( { date : activityDates [ pdIndex ] , active : false } ) ;
//res.write(activityDates[pdIndex]+' ');
}
}
}
}
//if (selected_date_s ==null) {
//if (selectedDateStr ==null) {
//ret += '<a href="?date=2018-05-08">2018-05-08</a>';
//res.write( '<a href="?date=2018-05-08">2018-05-08</a>') ;
//}
//}
//see ~/.minetest/debug.txt
//see ~/.minetest/debug.txt
//and logs archived by EnlivenMinetest:
//and logs archived by EnlivenMinetest:
//~/.minetest/debug_archived/2018/05/08.txt
//~/.minetest/debug_archived/2018/05/08.txt
ret += `
//res.write('</body></html>');
< div style = "color:gray" id = "statusArea" > < / d i v >
//res.end('</body></html>');
< canvas id = "myCanvas" width = "100" height = "1540" > <!-- style = "border:1px solid #c3c3c3;" > -- >
Your browser does not support the canvas element .
< / c a n v a s >
< div style = "color:gray" id = "outputArea" > loading ... < / d i v >
< script src = "js/main.js" > < / s c r i p t > ` ;
ret += '</body></html>' ;
res . send ( ret ) ;
//res.render('home');
//res.render('home');
res . render ( 'pages/index' , {
msg : msg ,
serverUser : os . homedir ( ) ,
tzOffset : ( Math . floor ( tzOffsetMinutes / 60 ) * - 1 ) ,
selectedDate : ( ( selectedDateStr != null ) ? selectedDateStr : "not selected" ) ,
logDates : logDates
} ) ;
} ) ;
} ) ;
var server = app . listen ( 64638 , function ( ) {
var server = app . listen ( port , function ( ) {
// 8123 is default for Minecraft DynMap
// 8123 is default for Minecraft DynMap
// 64638 spells 'minet' on a telephone keypad, but 6463, 6472 is already Discord RPC server
// 64638 spells 'minet' on a telephone keypad, but 6463, 6472 is already Discord RPC server
//console.log('express-handlebars example server listening on: 3000');
//console.log('express-handlebars example server listening on: ' + port );
var host = server . address ( ) . address ;
var host = server . address ( ) . address ;
var port = server . address ( ) . port ;
var port = server . address ( ) . port ;
console . log ( "server address:" ) ;
console . log ( "server address:" ) ;
console . log ( JSON . stringify ( server . address ( ) ) ) ;
console . log ( JSON . stringify ( server . address ( ) ) ) ;
console . log ( "reading log..." ) ;
console . log ( "reading log..." ) ;
read_l og ( ) ;
readL og ( ) ;
console . log ( "EnlivenMinetest webapp is listening at http://%s:%s" , host , port ) ;
console . log ( "EnlivenMinetest webapp is listening at http://%s:%s" , host , port ) ;
} ) ;
} ) ;