Skip to content

a3-sameer-desai #54

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

Open
wants to merge 55 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
7a664b5
copied over a2 code for a3. creating express server in express.server.js
SmeeBoi Sep 15, 2023
093ddc2
abstracted sorting players in server to rankPlayers()
SmeeBoi Sep 15, 2023
4a1b68b
add application/json header for each request client sends to server
SmeeBoi Sep 15, 2023
b5871f0
edit functionality works with express code
SmeeBoi Sep 15, 2023
d0c4511
more readable code with extra functions for delete and edit
SmeeBoi Sep 15, 2023
b1dfceb
express server code is more readable
SmeeBoi Sep 15, 2023
f196938
deleted old server code from a2 I was referencing
SmeeBoi Sep 15, 2023
ce9d4b1
express server code lookin good
SmeeBoi Sep 15, 2023
eb249e5
forgot to add gitignore file. now its there
SmeeBoi Sep 15, 2023
41f8a9e
deleted and reinstalled mongodb
SmeeBoi Sep 17, 2023
139f723
deleted and reinstalled node
SmeeBoi Sep 17, 2023
21f3d8d
update gitignore
SmeeBoi Sep 17, 2023
bd38725
changinge gitignore
SmeeBoi Sep 17, 2023
b568d69
removing unwanted files that got commited accidentally
SmeeBoi Sep 17, 2023
efc5aff
proper commit
SmeeBoi Sep 17, 2023
57dadf9
renamed file to server.jer
SmeeBoi Sep 17, 2023
f08cfd2
changed user to tester in .env file fixed uri
SmeeBoi Sep 17, 2023
7d91f5b
POST code works to add player to database and send full database json…
SmeeBoi Sep 17, 2023
c4f78f3
Able to add new player to database. And get back full database that c…
SmeeBoi Sep 18, 2023
bbf587b
sorting players by score works on mongodb and sends back to client pr…
SmeeBoi Sep 19, 2023
5a55312
Delete on mongodb works
SmeeBoi Sep 19, 2023
8663f97
now when adding, it actually adds rank field to mongodb
SmeeBoi Sep 19, 2023
bb94acf
delete works but rank doesnt update
SmeeBoi Sep 19, 2023
bcbb3bf
delete works to update rank in database. i have no idea why it starte…
SmeeBoi Sep 19, 2023
9b86e78
edit works to change mongodb data and still does same sort and add ra…
SmeeBoi Sep 19, 2023
5888d9c
got rid of excess commented out code. still ineffecient as i am repea…
SmeeBoi Sep 19, 2023
ace4c89
got rid of extra console logs
SmeeBoi Sep 19, 2023
a81960f
get rid of /docs functionality
SmeeBoi Sep 19, 2023
5edd5a8
post and delete with helper function seem to work
SmeeBoi Sep 19, 2023
7831dc0
put works with clean version too wow
SmeeBoi Sep 19, 2023
753b8a0
helper function works well
SmeeBoi Sep 19, 2023
7d08d9b
use newName instead
SmeeBoi Sep 19, 2023
01f12e0
create akim passport branch
SmeeBoi Sep 20, 2023
580f443
change readme in passport branch
SmeeBoi Sep 20, 2023
6a7a2cb
got rid of express default behavior to serve indexhtml. now it gives …
SmeeBoi Sep 20, 2023
a4ce117
session part works. had to change directory permissions with chmod to…
SmeeBoi Sep 20, 2023
33d4064
now once logged in. always takes you to dashboard page
SmeeBoi Sep 20, 2023
2ebd77f
fixed logout route. now redirects to login page
SmeeBoi Sep 20, 2023
ebb1e1b
well after clicking login it takes me to edit player page. but i thin…
SmeeBoi Sep 23, 2023
a13a105
ok github username within session works i think. Interesting whole th…
SmeeBoi Sep 23, 2023
d8c74ba
dont really understand why but I switched to index : false for expres…
SmeeBoi Sep 23, 2023
05cb1ad
got rid of a lot of console logs
SmeeBoi Sep 23, 2023
06f42fb
changed sign in text on welcome page (index.html)
SmeeBoi Sep 23, 2023
5d1fd61
tired so seeing how bootstrap works
SmeeBoi Sep 24, 2023
760d490
ejs works from views folder instead of sending the html files
SmeeBoi Sep 24, 2023
671ce06
got rid of html comments
SmeeBoi Sep 24, 2023
538a96d
messing around
SmeeBoi Sep 25, 2023
3d233c8
adding to readme
SmeeBoi Sep 25, 2023
294a78e
more readme stuff
SmeeBoi Sep 25, 2023
bac30d9
change name in package.json
SmeeBoi Sep 25, 2023
3c05e7b
add node 16 to package json. heroku not working
SmeeBoi Sep 25, 2023
70b1ef4
fixed results.js to use container class instead
SmeeBoi Sep 25, 2023
53c9095
change to glitch
SmeeBoi Sep 25, 2023
0deabe6
comment
SmeeBoi Sep 25, 2023
44c46be
Update README.md
SmeeBoi Sep 25, 2023
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules/
package-lock.json
.env
123 changes: 17 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,114 +1,25 @@
Assignment 3 - Persistence: Two-tier Web Application with Database, Express server, and CSS template
===
## a3-SmeeBoi

Due: September 22nd, by 11:59 AM.
Project Link: https://a3-sameer-desai.glitch.me/

This assignnment continues where we left off, extending it to use the most popular Node.js server framework (express),
a database (mongodb), and a CSS application framework / template of your choice (Boostrap, Material Design, Semantic UI, Pure etc.)
Modified a2 "game" to use Express, MongoDB, GitHub OAuth Authentication (with Passport.js), and CSS with Bootstrp. I also used EJS to render different views instead of sending different html files in server code.

Baseline Requirements
---
Bug: not enough time. to see results do
https://a3-sameer-desai.glitch.me/results

Your application is required to implement the following functionalities:
5 main views
1) Welcome - user can sign in with GitHub
2) Edit Player - form to create Player account in database
3) Menu - choose to play game or edit player
4) Game - play game
5) Results - see current player stats and top 10 player stats

- a `Server`, created using Express (no alternatives will be accepted for this assignment)
- a `Results` functionality which shows all data associated with a logged in user (except passwords)
- a `Form/Entry` functionality which allows users to add, modify, and delete data items (must be all three!) associated with their user name / account.
- Persistent data storage in between server sessions using [mongodb](https://www.mongodb.com/cloud/atlas) (you *must* use mongodb for this assignment).
- Use of a [CSS framework or template](https://github.com/troxler/awesome-css-frameworks).
This should do the bulk of your styling/CSS for you and be appropriate to your application.
For example, don't use [NES.css](https://nostalgic-css.github.io/NES.css/) (which is awesome!) unless you're creating a game or some type of retro 80s site.
- Goal: meet requirements
- Challenges: routes with OAuth
- Authentication: I chose GitHub OAuth because it seemed straightforward (or so I thought)
- CSS: I used Bootstrap for CSS because it seemed like most popular option
- Express middleware: I used 2 middlewares. One to check database connection and one to see if user is authenticated.

Your application is required to demonstrate the use of the following concepts:

HTML:
- HTML input tags and form fields of various flavors (`<textarea>`, `<input>`, checkboxes, radio buttons etc.)
- HTML that can display all data *for a particular authenticated user*. Note that this is different from the last assignnment, which required the display of all data in memory on the server.

Note that it might make sense to have two pages for this assignment, one that handles login / authentication, and one that contains the rest of your application.
For example, when visiting the home page for the assignment, users could be presented with a login form. After submitting the login form, if the login is
successful, they are taken to the main application. If they fail, they are sent back to the login to try again. For this assignment, it is acceptable to simply create
new user accounts upon login if none exist, however, you must alert your users to this fact.

CSS:
- CSS styling should primarily be provided by your chosen template/framework.
Oftentimes a great deal of care has been put into designing CSS templates;
don't override their stylesheets unless you are extremely confident in your graphic design capabilities.
The idea is to use CSS templates that give you a professional looking design aesthetic without requiring you to be a graphic designer yourself.

JavaScript:
- At minimum, a small amount of front-end JavaScript to get / fetch data from the server.
See the [previous assignment](https://github.com/cs-4241-23/shortstack) for reference.

Node.js:
- A server using Express and a persistent database (mongodb).

General:
- Your site should achieve at least 90% on the `Performance`, `Best Practices`, `Accessibility`, and `SEO` tests
using Google [Lighthouse](https://developers.google.com/web/tools/lighthouse) (don't worry about the PWA test, and don't worry about scores for mobile devices).
Test early and often so that fixing problems doesn't lead to suffering at the end of the assignment.

Deliverables
---

Do the following to complete this assignment:

1. Implement your project with the above requirements. I'd begin by converting your A2 assignment. First, change the server to use express. Then, modify the server to use mongodb instead of storing data locally. Last but not least, implement user accounts and login. User accounts and login is often the hardest part of this assignment, so budget your time accordingly.
2. If you developed your project locally, deploy your project to Glitch (unless completing the alternative server technical acheivement described below), and fill in the appropriate fields in your package.json file.
3. Test your project to make sure that when someone goes to your main page on Glitch (or an alternative server), it displays correctly.
4. Ensure that your project has the proper naming scheme `a3-yourfirstname-yourlastname` so we can find it.
5. Fork this repository and modify the README to the specifications below.
6. Create and submit a Pull Request to the original repo. Name the pull request using the following template: `a3-firstname-lastname`.

Acheivements
---

Below are suggested technical and design achievements. You can use these to help boost your grade up to an A and customize the
assignment to your personal interests, for a maximum twenty additional points and a maximum grade of a 100%.
These are recommended acheivements, but feel free to create/implement your own... just make sure you thoroughly describe what you did in your README,
why it was challenging, and how many points you think the achievement should be worth.
ALL ACHIEVEMENTS MUST BE DESCRIBED IN YOUR README IN ORDER TO GET CREDIT FOR THEM.

*Technical*
- (10 points) Implement OAuth authentication, perhaps with a library like [passport.js](http://www.passportjs.org/).
*You must either use Github authenticaion or provide a username/password to access a dummy account*.
Course staff cannot be expected, for example, to have a personal Facebook, Google, or Twitter account to use when grading this assignment.
Please contact the course staff if you have any questions about this. THIS IS THE HARDEST ACHEIVEMENT OFFERED IN WEBWARE. You have been warned!
- (5 points) Instead of Glitch, host your site on a different service like [Heroku](https://www.heroku.com) or [Digital Ocean](https://www.digitalocean.com). Make sure to describe this a bit in your README. What was better about using the service you chose as compared to Glitch? What (if anything) was worse?
- (5 points) Get 100% (not 98%, not 99%, but 100%) in all four lighthouse tests required for this assignment.

*Design/UX*
- (10 points) Make your site accessible using the [resources and hints available from the W3C](https://www.w3.org/WAI/), Implement/follow twelve tips from their [tips for writing](https://www.w3.org/WAI/tips/writing/), [tips for designing](https://www.w3.org/WAI/tips/designing/), and [tips for development](https://www.w3.org/WAI/tips/developing/). *Note that all twelve must require active work on your part*.
For example, even though your page will most likely not have a captcha, you don't get this as one of your twelve tips to follow because you're effectively
getting it "for free" without having to actively change anything about your site.
Contact the course staff if you have any questions about what qualifies and doesn't qualify in this regard.
List each tip that you followed and describe what you did to follow it in your site.
- (5 points) Describe how your site uses the CRAP principles in the Non-Designer's Design Book readings.
Which element received the most emphasis (contrast) on each page?
How did you use proximity to organize the visual information on your page?
What design elements (colors, fonts, layouts, etc.) did you use repeatedly throughout your site?
How did you use alignment to organize information and/or increase contrast for particular elements.
Write a paragraph of at least 125 words *for each of four principles* (four paragraphs, 500 words in total).

Sample Readme (delete the above when you're ready to submit, and modify the below so with your links and descriptions)
---

## Your Web Application Title

your glitch (or alternative server) link e.g. http://a3-charlie-roberts.glitch.me

Include a very brief summary of your project here. Images are encouraged, along with concise, high-level text. Be sure to include:

- the goal of the application
- challenges you faced in realizing the application
- what authentication strategy you chose to use and why (choosing one because it seemed the easiest to implement is perfectly acceptable)
- what CSS framework you used and why
- include any modifications to the CSS framework you made via custom CSS you authored
- the five Express middleware packages you used and a short (one sentence) summary of what each one does. If you use a custom function for *one* (and one alone) middleware please
add a little more detail about what it does.

## Technical Achievements
- **Tech Achievement 1**: I used OAuth authentication via the GitHub strategy

### Design/Evaluation Achievements
- **Design Achievement 1**: I followed the following tips from the W3C Web Accessibility Initiative...
- **Tech Achievement 1**: OAuth authentication with GitHub using Passport.js
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "a3-Sameer-Desai",
"version": "0.1",
"description": "Spacebar Game",
"author": "Sameer Desai",
"scripts": {
"start": "node server.js"
}, "engines" : {
"node" : "16.x"
},
"dependencies": {
"bootstrap": "^5.3.2",
"dotenv": "^16.3.1",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
"mime": "^2.4.4",
"mongodb": "^6.1.0",
"passport": "^0.6.0",
"passport-github2": "^0.1.12"
}
}
12 changes: 12 additions & 0 deletions public/css/bootstrap.min.css

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* *{
margin: 0px;
padding: 0px;
border: 0px;
font-family: 'Josefin Sans', sans-serif;
font-family: 'Jost', sans-serif;
} */

.container {
min-height: 100vh;
min-width: 100vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

#playerList {
font-size: 1rem;
margin-top: 0px;
margin-bottom: 0px;
}

li{
size: 1rem;
margin-top: 0px;
margin-bottom: 0px;
}


#input-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}


/* h1.vert-move {
-webkit-animation: mover 1.0s infinite alternate;
animation: mover 0.8s infinite alternate;
} */

@keyframes mover {
0% { transform: translateY(0); }
100% { transform: translateY(-8px); }
}

input{
width: 70%;
height: 80%;
font-size: 60%;
border: none;
outline: none;
border-bottom: 1px solid black;

}


button {
margin-top: 20px;
margin-left: 10px;
align-items: center;
background-color: white;
border-radius: 12px;
cursor: pointer;
font-size: 1.2rem;
outline: none;
padding: 0.5rem 0.5rem;
text-align: center;
text-decoration: none;
transition: box-shadow .2s,-webkit-box-shadow .2s;
white-space: nowrap;
border: 0;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
}

button:hover {
box-shadow: #121212 0 0 0 3px, transparent 0 0 0 0;
}
148 changes: 148 additions & 0 deletions public/js/editPlayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
'use strict'

// CLIENT CODE

let playerList = undefined;

// Random int between min and max
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

// When play button is clicked, submit form data to server
const submit = async function (event) {
event.preventDefault(); // Stop page defualt reload behavior

// hide div with input-container
// const inputContainer = document.querySelector('#input-container');
// inputContainer.style.display = 'none';

const name = document.querySelector('#yourname'); // get form input element with id=yourname
const color = document.querySelector('#color');

let formData = {} // json for client data
formData.name = name.value
formData.color = color.value
formData.score = randomNum(1, 100); // Add random score between 1-100

const response = await fetch('/submit', { // Send client data to server
method: 'POST',
headers: { 'Content-Type': 'application/json' }, // **Added this to get body data in server
body: JSON.stringify(formData) // Convert formData to JSON string
})

let serverData = await response.json() // Get server response
console.log('SERVER DATA (after add player):')
console.log(serverData) // Print current server data
// populateList(serverData); // Populate list of players in html with server data
window.location.href = '/game'; // Redirect to game
}

// Populate list of players in html with server data
function populateList(serverData) {
playerList = document.getElementById('playerList');
if (playerList) {
playerList.remove(); // Reset player list by removing old list
}

playerList = document.createElement('ol'); // Ordered list of players
playerList.id = 'playerList';

// document.body.appendChild(playerList);

// add playerList to body inside the div with id=container
let container = document.getElementById('container');
container.appendChild(playerList);

// For each player in server data, create a list item
serverData.forEach(d => {
const player = document.createElement('li') // Player list item

// Add edit button
const editButton = document.createElement('button')
editButton.innerHTML = 'Edit'
editButton.class = 'editButton'

// Add delete button
const deleteButton = document.createElement('button')
deleteButton.innerHTML = 'Delete'
deleteButton.class = 'deleteButton'

// Server Data to be displayed in list player
player.innerHTML = `Name: ${d.name}, Color: ${d.color}, Score: ${d.score}, Rank: ${d.rank}`
player.className = 'player'
player.id = `${d.name}`; // Set id of player list item to name of player

// Add buttons in li player
player.appendChild(editButton)
player.appendChild(deleteButton)

// Add player <li> to list <ol>
playerList.appendChild(player)
})
}

// When page loads, add event listeners to buttons
window.onload = function () {
const playbutton = document.querySelector('#playbutton');
playbutton.onclick = submit;

// Delete button clicked
document.addEventListener('click', function (e) {
if (e.target && e.target.class == 'deleteButton') {
let player = e.target.closest('.player');
if (confirm(`Are you sure you want to delete Player: ${player.id}?`) == true) {
deletePlayer(player.id); // player id is name of player
}
}
});

// Edit button clicked
document.addEventListener('click', function (e) {
if (e.target && e.target.class == 'editButton') {
let player = e.target.closest('.player');
editPlayer(player.id); // player id is name of player
}
});

}

// Edit player name on server
async function editPlayer(playerName) {
let newName = prompt("Enter new name:", "New Name"); // Get new name from user
if (newName == null || newName == "") {
alert("Player name not changed"); // If user cancels or enters empty string, do nothing
} else {
let editPlayer = {}
editPlayer.name = playerName
editPlayer.newName = newName // New name to be changed to

const response = await fetch('/edit', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' }, // **Added this to get body data in server
body: JSON.stringify(editPlayer)
})

let serverData = await response.json()
console.log('SERVER DATA (after edit):');
console.log(serverData);
populateList(serverData);
}
}

// Delete player from server
async function deletePlayer(playerName) {
let deletePlayer = {}
deletePlayer.name = playerName // Player name to be deleted

const response = await fetch('/delete', {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }, // **Added this to get body data in server
body: JSON.stringify(deletePlayer)
})

let serverData = await response.json()
console.log('SERVER DATA (after delete):');
console.log(serverData);
populateList(serverData);
}
Loading