Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aaron Saloff's Project #22

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# assignment_css_garden_of_good_and_evil
Create a CSS garden that adapts to your whims.

Aaron Saloff's Solution
54 changes: 54 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const express = require('express');
const path = require('path');
const logger = require('morgan');
const morganToolkit = require('morgan-toolkit')(logger);
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helpers = require('./helpers');
const exphbs = require('express-handlebars');

const index = require('./routes/index');
const visitorCount = require('./routes/visitors');

const app = express();

// view engine setup
const hbs = exphbs.create({
helpers: helpers,
partialsDir: 'views/partials/',
defaultLayout: 'main.hbs',
extname: '.hbs'
});

app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(logger('short'));
app.use(morganToolkit());

app.use('/', index);
app.use('/visitors', visitorCount);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('assignment-css-garden-of-good-and-evil:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
6 changes: 6 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const HelperLoader = require('load-helpers');
const helperLoader = new HelperLoader();

const helpers = helperLoader.load('helpers/*_helper.js').cache;

module.exports = helpers;
166 changes: 166 additions & 0 deletions helpers/view_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
const bodyColor = (color) => {
if (color) {
color = color.toLowerCase();
return `background-${ color }`;
}
};

const checkColorSelected = (favColor, colorValue) => {
if (!favColor && colorValue == '') {
return 'selected';
} else if (favColor == colorValue) {
return 'selected';
}
};

const checkGoalSelected = (goal, selectedGoal) => {
if (goal == selectedGoal) return 'checked';
};

const currentName = (name) => {
if (name) return formatName(name);
};

const currentFood = (food) => {
if (food) return formatName(food);
};

const currentInsanityLevel = (insanityLevel) => {
if (insanityLevel) {
return insanityLevel;
} else {
return '0';
}
};

const formatName = (name) => {
if (!name) return;
name = name.toLowerCase();

return name.replace(/(^[\w])|(\b[\w])/g, (m) => {
return m.toUpperCase();
});
};

const goalImg = (goal) => {
if (!goal) return;

return goal == 'good' ? './images/angel.png' : './images/devil.png';
};

const birthCity = (goal) => {
return goal == 'good' ? 'in the bright and beautiful city of Pleasantville.' : 'in the deep dark alleys of Chernobyl.';
};

const childHoodHabits = (goal, insanityLevel) => {
insanityLevel = parseInt(insanityLevel);

if (insanityLevel < 4) {
return goal == 'good' ? 'well dressed, clean, and very polite.' : 'wearing torn clothes, hated showers, and would walk around town with a menacing look.';
} else if (insanityLevel < 7) {
return goal == 'good' ? 'slightly messy, quirky, but ultimatley very pleasant.' : 'shirtless, shoeless, and muttering bad language to everyone.';
} else {
return goal == 'good' ? 'hyperactive, energetic, and would jump off a building if it meant saving a cat!' : 'walking around totally naked, peeing on anyone that would get close enough!';
}
};

const peopleReactions = (name, goal, insanityLevel) => {
insanityLevel = parseInt(insanityLevel);

if (insanityLevel < 4) {
return goal == 'good' ? `make sure to come over and say hi, showering ${ name } with love and affection. ${ name } was the perfect child!` : `feel bad for, and try to help ${ name } to be a positive member of society.`;
} else if (insanityLevel < 7) {
return goal == 'good' ? `smile, wave and be generally very hopeful about ${ name }'s future.` : `get angry, and have a sudden urge to give ${ name } a smack upside the head.`;
} else {
return goal == 'good' ? `politley steer clear. ${ name } tended to be a bit overwhelming.` : `run for the hills! That kid was completly bonkers!`;
}
};

const setFormattedProps = (cookies) => {
cookies.name = formatName(cookies.personaName);
cookies.food = formatName(cookies.favFood).toLowerCase();
cookies.color = formatName(cookies.favColor).toLowerCase();
return cookies;
};

const intro = (personaName, goal, insanityLevel) => {
let name = formatName(personaName);

return `${ name } grew up ${ birthCity(goal) } As a child, ${ name } was always ${ childHoodHabits(goal, insanityLevel) } Everyone that met ${ name } would ${ peopleReactions(name, goal, insanityLevel) }`;
};

const adultHoodOverview = (name, food, goal, insanityLevel) => {
let endOfGood =`${ name }'s absolute favorite food in the whole world is ${ food }!`;
let endOfBad = `${ name } would do anything for some ${ food }.`;

if (insanityLevel < 4) {
return goal == 'good' ?
`is a saint! ${ name } works at the homeless shelter and loves to distribute ${ food } to all in need. ` + endOfGood :
`is not very friendly and always yells at people to 'keep off the grass'. ${ name } never misses a chance to shoplift some ${ food } when at the market. ` + endOfBad;
} else if (insanityLevel < 7) {
return goal == 'good' ?
`is very giving, but a little crazy. ${ name } invites homeless people over to eat ${ food }. ` + endOfGood :
`is not a good person. ${ name } breaks into houses to steal ${ food } from desperate kids! ` + endOfBad;
} else {
return goal == 'good' ?
`is just downright crazy! ${ name } skydives into war torn countries delivering food to the innocent and needy bystanders. ` + endOfGood :
`is just pure evil! ${ name } runs a criminal orginazation with the sole purpose of gathering as much ${ food } as possible. Whomever stands in ${ name }'s way sleeps with the fishez! ` + endOfBad;
}
};

const colorLoveSentence = (name, color) => {
return `${ name } lives in a ${ color } house, drives a ${ color } car, and always wears as much ${ color } as possible.`;
};

const likesDislikes = (personaName, favFood, favColor, goal, insanityLevel) => {
let name = formatName(personaName);
let food = formatName(favFood).toLowerCase();
let color = formatName(favColor).toLowerCase();

return `As an adult, ${ name } ${ adultHoodOverview(name, food, goal, insanityLevel) } ${ colorLoveSentence(name, color) }`;
};

const colorComplement = (name, color, sentanceStart) => {
sentanceStart ? sentanceStart = 'C' : sentanceStart = 'c';
return sentanceStart + `omplement ${ name } for wearing so much ${ color } and pulling it off in such a stylish way!`;
};

const meetingReaction = (name, food, color, goal, insanityLevel) => {
insanityLevel = parseInt(insanityLevel);

if (insanityLevel < 4) {
return goal == 'good' ?
`make sure to go over and say hello. ${ colorComplement(name, color, true) } Don't be surprised if ${ name } offers you some fresh ${ food }. You will always walk away with a smile after talking to ${ name }.` :
`keep your distance. Try not to notice ${ name }'s all ${ color } look. Watch your bags if your carrying any ${ food }. ${ name } is always looking to steal some fresh ${ food }.`;
} else if (insanityLevel < 7) {
return goal == 'good' ?
`don't go over unless you want a long conversation about the benefits of eating ${ food } and the wonders of the color ${ color }. ${ name } will likely invite you over for some ${ food }, even though you are a complete starnger! What a nice but strange person. Make sure to ${ colorComplement(name, color, false) }` :
`turn the other way and hope ${ name } didnt see you. ${ name } will target your house if you enjoy bringing ${ food } home. If ${ name } does spot you, ${ colorComplement(name, color, false) } Then leave as quickly as possible.`;
} else {
return goal == 'good' ?
`think twice before approching. At the drop of a hat ${ name } will offer you a ${ color } Tesla if you join in on a dangoures mission to the mexican waters to save hostages surrounded by pirates. The plan will likely be to offer them ${ food } in exchange for the prisoners. If your not too shocked with ${ name }'s intensity, ${ colorComplement(name, color, false) }. The world would be a better place if there were more ${ name }s around.` :
`run for your life. If you have any ${ food } with you, throw whatever you have on you in the other direction. ${ name } is armed and extremely dangerous and will stop at nothing to obtain more delicious ${ food }. If ${ name } does get a hold of you ${ colorComplement(name, color, false) } Then pray that ${ name } lets you live!`;
}
};

const conclusion = (personaName, favFood, favColor, goal, insanityLevel) => {
let name = formatName(personaName);
let food = formatName(favFood).toLowerCase();
let color = formatName(favColor).toLowerCase();

return `Bottom line, if you ever see ${ name }, ${ meetingReaction(name, food, color, goal, insanityLevel) }`;
};

module.exports = {
currentName,
currentFood,
checkColorSelected,
currentInsanityLevel,
checkGoalSelected,
bodyColor,
formatName,
goalImg,
intro,
likesDislikes,
conclusion
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "assignment-css-garden-of-good-and-evil",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev_start": "nodemon ./bin/www",
"debug": "node debug ./bin/www"
},
"dependencies": {
"body-parser": "~1.17.1",
"cookie-parser": "~1.4.3",
"debug": "~2.6.3",
"express": "~4.15.2",
"express-handlebars": "^3.0.0",
"hbs": "~4.0.1",
"load-helpers": "^1.0.1",
"morgan": "~1.8.1",
"morgan-toolkit": "^1.0.2",
"redis": "^2.8.0",
"serve-favicon": "~2.4.2"
}
}
Binary file added public/images/angel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/devil.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/javascripts/level_updater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function outputUpdate(vol) {
document.querySelector('#insanity-level').value = vol;
document.getElementById('level').innerHTML = vol;
}
Loading