YOUR NAME: .edu



YOUR NAME:MIS3502 – Assignment 06 – The Rock Paper Scissors API endpoint (with data)PART 1In this assignment you will construct the API endpoint for a Rock-Paper-Scissors game. Your API endpoint will allow clients to write a record of wins, ties, and losses into a data store, and retrieve a history from that data store.Put your answers in the boxes provided. Deliver the word document to your instructor as directed. Get your database set upUse MySQL Workbench to connect to the “blue” database you previously created. Use the following statement to create the table that your application will need:CREATE TABLE history ( history_id int(11) PRIMARY KEY AUTO_INCREMENT, userwon int(11), computerwon int(11), tiegame int(11), usertoken int(11), createtime datetime NOT NULL DEFAULT now())Take a moment and consider the structure of this table and how it will be used. Every event in the rock paper scissors game will be recorded with a unique history_id. If the human player wins, a 1 will be placed in the userwon column. The computerwon and tiegame columns will get zeros. If a usertoken is provided then the usertoken should be stored. Some games might be anonymous in which case usertoken can be null or a zero length string.Every event recorded will also have a default date / time stamp associated with it. Getting StartedTake a look at the original “Rock Paper Scissors” API endpoint that you used previously. Your goal is to reproduce it’s functionality exactly.Open VS Code and connect to your server.Use wget to retrieve the mis3502template_endpoint.zip file. Unzip it into your nodeprojects folder on the server. Rename the unzipped folder as assignment06_endpoint.IMPORTANT NOTE: You can use mis3502template_endpoint.zip as a starting point for all future Node.js work in this course!Add in your database connection information into the constant named options.Try running app.js in node. Make corrections. This includes making sure you are working in the right folder. It also means using npm to install any missing packages and specifying a port of 8202.Once you have the confirmation message “The server is listening on port: 8202” press control-c to quit the application. Immediately relaunch the app.js code with the Node monitor command:nodemon app.jsBring in your work from the last assignmentGet the supporting functions you wrote for assignment 5: compareHands and pickHand. Copy and paste them into the supporting functions section of the template.Bring in the app event hander for “/shoot” that you created in the last assignment.Bring in the app event hander for “/history” that you created in the last assignment.Update the root event (“/”)In the root event, update the text in message[0] and message[1] so that it matches what is found at Test you work so far. New functionsNext we will write a new supporting function called addHistory. This function will take four arguments … the usertoken, a variable named userwon (which equals 1 if the human being won the game, and zero otherwise), a variable named computerwon, and a variable named tiegame.Set up our new function with the following code:var addHistory = function(usertoken,userwon,computerwon,tiegame){ //database magic happens here console.log(usertoken); };Before we can really complete the addHistory function, we need to make sure that we are passing it a value in the usertoken. The means stopping to think about where usertoken comes from (it is provided as a token in the HTTP query string) and then passing it along from function to function. You also need to consider what should happen if usertoken is not provided in the querystring! (This will be discussed further in class!) Make an appropriate call to the addHistory function for each one of the conditions found in compareHands.Before you even attempt the database call… can you test your work so far? How?CONTINUED…PART TWONow complete the addHistory function. Comment out the console.log() command and replace it with a call to connection.query(). A code sample follows:var addHistory = function(usertoken,userwon,computerwon,tiegame){ //database magic happens here //console.log(usertoken); let txtSQL = 'insert into history(usertoken,userwon,computerwon,tiegame) ' + ' values (?,?,?,?);' let results = connection.query(txtSQL, [usertoken,userwon,computerwon,tiegame]) console.log("inserted record: " + results.insertId);};In the above, pay close attention to:The use of question marks in the variable txtSQL. They are being used to parameterize the SQL statement. The use of the array used in the second argument of the connection.query callWhat is the variable named results? Notice how we are getting the value of history_id as it is being generated in the database. This is often a very handy thing in practical applications.We need another supporting function. The usefulness of this function may not be immediately apparent, however you will find that is can be used in many situations. // fixNull will check to see if x is null. // If x is null the function will return y.// Otherwise it will return x.var fixNull =function(x,y){ if (x == null){ return y; } else { return x; }}; CONTINUED…Now we can create a getHistory function. Notice the use of the SQL aggregate function here. Also notice how the fixNull() function simplifies this code.var getHistory = function(usertoken){ //if there is a problem with usertoken, then report //the problem and end the function if (usertoken == undefined || isNaN(usertoken) || usertoken == ""){ let result = "Error. No usertoken. No history."; return result; } //if there is a usertoken, then select the data from the database let result = "History:<br>"; let txtSQL = 'select sum(userwon) as sumuserwon, ' + ' sum(computerwon) as sumcomputerwon, ' + ' sum(tiegame) as sumtiegame ' + ' from history where usertoken = ?;' let results = connection.query(txtSQL, [usertoken]); let wins = fixNull(results[0]['sumuserwon'],0); let losses = fixNull(results[0]['sumcomputerwon'],0); let ties = fixNull(results[0]['sumtiegame'],0); result += "Wins: " + wins + '<br>'; result += "Losses: " + losses + '<br>'; result += "Tie Games: " + ties + '<br>'; return result; };If you are not familiar with the += operator, it is a shorthand way of concatenating text to a variable. X += 'fred'; is the same as X = X + 'fred';Now that you have all your supporting functions, you can use the app.get() method to manage a GET request and response against ‘/history’. What do you need to do to retrieve usertoken from the URL querystring and send it (and the response object) to the getHistory() function?Test your work. Consider the following sample URLs. Some are “good”, and some are “bad” but under no circumstances should a missing argument or a bad data value cause your API to crash!//SAMPLE TESTING//good the Node package pm2 to set up your solution as a service running on your EC2 server. The service should continue to run – even after you are logged out / disconnected from the server. Test this! The URLs you provide in the next question need to be working as expected. Revisit the Rock Paper Scissors HTML client you composed earlier in the semester. Replace the original game API endpoint with your own. Upload this to the MISDEMO class server. Upload this to your wwwroot on your EC2 server.Test your work. You have now written both the front and back end of the Rock Paper Scissors game. Fill in the following boxes with URLs that point to your work on your own personal AWS server:Instructions (/)Shoot Event (/shoot) with a choice of “rock”History Request (/history) URL to the updated HTML client (hosted on MISDEMO) On Your Own - Get ready for next weekNext class we will review the material we have covered so far in preparation for the upcoming exam. The exam will be closely modeled after the games we have created in class. A thorough review of the HTML clients and server-side API code will be beneficial to you. There’s no extra preparation beyond that. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download