Skip to content

Instantly share code, notes, and snippets.

@ismaelc
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismaelc/53c314f6c63399a0755a to your computer and use it in GitHub Desktop.
Save ismaelc/53c314f6c63399a0755a to your computer and use it in GitHub Desktop.
Rough quick demo for Thack Tnooz - Overhead Compartment Bin Map
var express = require('express');
var app = express();
var seatMap = [
["O","X","Y","N"],
["Y","X","O","O"],
["N","X","Y","Y"],
["N","N","X","O"],
["O","X","Y","N"],
["Y","X","O","O"],
["N","X","O","Y"],
["N","N","X","O"],
["O","X","Y","N"]
];
var copySeatMap = null;
var selectedValue = "Y";
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
//response.send('Hello World!');
response.send(returnHTMList(seatMap, null));
});
app.get('/find', function(request, response) {
findBestSeat(seatMap);
response.send(returnHTMList(copySeatMap, true));
});
app.get('/norm', function(request, response) {
findNormSeat(seatMap);
response.send(returnHTMList(copySeatMap, true));
});
app.get('/select', function(request, response) {
var isBest = request.query.isBest;
if(isBest == "false") findNormSeat(seatMap);
else findBestSeat(seatMap);
for(var i = 0; i < copySeatMap.length; i++)
for(var j = 0; j < copySeatMap[i].length; j++)
if(copySeatMap[i][j] == "G") copySeatMap[i][j] = "Y";
seatMap = copySeatMap;
response.send(returnHTMList(seatMap, false));
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
//--
function findBestSeat(sMap) {
copySeatMap = null;
selectedValue = "Y";
//copySeatMap = sMap.slice();
copySeatMap = JSON.parse(JSON.stringify(sMap))
var breakIt = false;
for(var i = 0; i < copySeatMap.length; i++) {
for(var j = 0; j < copySeatMap[i].length; j++) {
var nCtr = 0;
if(copySeatMap[i][j] == "O")
for(var k = 0; k < copySeatMap[i].length; k++) if(copySeatMap[i][k] == "N") nCtr++;
if(nCtr > 1) {
copySeatMap[i][j] = "G";
breakIt = true;
break;
}
}
if(breakIt) break;
}
return copySeatMap;
}
function findNormSeat(sMap) {
copySeatMap = null;
selectedValue = "N";
//copySeatMap = sMap.slice();
copySeatMap = JSON.parse(JSON.stringify(sMap))
var breakIt = false;
for(var i = 0; i < copySeatMap.length; i++) {
for(var j = 0; j < copySeatMap[i].length; j++) {
if(copySeatMap[i][j] == "O") {
copySeatMap[i][j] = "G";
breakIt = true;
break;
}
}
if(breakIt) break;
}
return copySeatMap;
}
function returnHTMList(sMap, find) {
var selected = "";
var html = "<!DOCTYPE html>";
html += "<html><head>";
html += "<script>";
html += "function findBestSeat() {";
html += " var e = document.getElementById('selectBest');"
html += " var strUser = e.options[e.selectedIndex].value;";
html += " if(strUser == 'Y') window.location.href = '/find';";
html += " else window.location.href = '/norm';";
html += "};";
html += "function selectSeat() {";
html += " var e = document.getElementById('selectBest');"
html += " var strUser = e.options[e.selectedIndex].value;";
html += " if(strUser == 'N') window.location.href = '/select?isBest=false';";
html += " else window.location.href = '/select?isBest=true';";
html += "};";
html += "</script>";
html += "</head>";
html += "<style>";
html += "";
html += "</style>";
html += "<body>";
if(selectedValue == "N") selected = "selected";
html += "<span style='font-size:150%'>Are you using the Overhead Compartment Bin?</span><br/><br/>";
html += "<select id='selectBest' style='font-size:150%'><option value='Y'>Yes</option><option value='N' " + selected + ">No</option></select>";
html += "<button onclick='findBestSeat()' style='font-size:150%; margin-left:20px'>Find best seat</button>";
if (find == true) html += "<button onclick='selectSeat()' style='font-size:150%; margin-left:20px'>Select seat [G]</button>";
html += "<ul>";
var color = "black";
for(var i = 0; i < sMap.length; i++) {
for(var j = 0; j < sMap[i].length; j++) {
switch(sMap[i][j]) {
case "O": color = "gray"; break;
case "X": color = "blue"; break;
case "Y": color = "red"; break;
case "N": color = "orange"; break;
case "G": color = "green"; break;
}
var style = "display:inline; font-size: 300%; background:" + color;
if(j == 1) style = "display:inline; margin-right:15px; font-size: 300%; background:" + color;
html += "<li style='" + style + "'>" + sMap[i][j] + "</li>";
}
html += "<br>";
}
html += "</ul>";
html += "</body>";
html += "</html>";
return html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment