From d16b96e85c3532cd92861389b658b787e62bff42 Mon Sep 17 00:00:00 2001 From: GameMas Date: Thu, 9 Sep 2021 11:32:23 -0400 Subject: [PATCH 1/2] finish! --- README.md | 103 ++-------- node_modules/.bin/mime | 15 ++ node_modules/.bin/mime.cmd | 17 ++ node_modules/.bin/mime.ps1 | 18 ++ node_modules/mime/CHANGELOG.md | 289 ++++++++++++++++++++++++++++ node_modules/mime/LICENSE | 21 ++ node_modules/mime/Mime.js | 97 ++++++++++ node_modules/mime/README.md | 187 ++++++++++++++++++ node_modules/mime/cli.js | 46 +++++ node_modules/mime/index.js | 4 + node_modules/mime/lite.js | 4 + node_modules/mime/package.json | 81 ++++++++ node_modules/mime/types/other.js | 1 + node_modules/mime/types/standard.js | 1 + package-lock.json | 11 ++ public/css/style.css | 123 +++++++++++- public/image/icon.png | Bin 0 -> 70272 bytes public/index.html | 220 ++++++++++++++++++++- server.improved.js | 185 ++++++++++++++++-- 19 files changed, 1313 insertions(+), 110 deletions(-) create mode 100644 node_modules/.bin/mime create mode 100644 node_modules/.bin/mime.cmd create mode 100644 node_modules/.bin/mime.ps1 create mode 100644 node_modules/mime/CHANGELOG.md create mode 100644 node_modules/mime/LICENSE create mode 100644 node_modules/mime/Mime.js create mode 100644 node_modules/mime/README.md create mode 100644 node_modules/mime/cli.js create mode 100644 node_modules/mime/index.js create mode 100644 node_modules/mime/lite.js create mode 100644 node_modules/mime/package.json create mode 100644 node_modules/mime/types/other.js create mode 100644 node_modules/mime/types/standard.js create mode 100644 package-lock.json create mode 100644 public/image/icon.png diff --git a/README.md b/README.md index 996cf12d..d6b7c11b 100644 --- a/README.md +++ b/README.md @@ -1,97 +1,22 @@ Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js -=== -Due: September 9th, by 11:59 AM. - -This assignment aims to introduce you to creating a prototype two-tiered web application. -Your application will include the use of HTML, CSS, JavaScript, and Node.js functionality, with active communication between the client and the server over the life of a user session. - -Baseline Requirements ---- - -There is a large range of application areas and possibilities that meet these baseline requirements. -Try to make your application do something useful! A todo list, storing / retrieving high scores for a very simple game... have a little fun with it. - -Your application is required to implement the following functionalities: - -- a `Server` which not only serves files, but also maintains a tabular dataset with 3 or more fields related to your application -- a `Results` functionality which shows the entire dataset residing in the server's memory -- a `Form/Entry` functionality which allows a user to add, modify, or delete data items residing in the server's memory -- a `Server Logic` which, upon receiving new or modified "incoming" data, includes and uses a function that adds at least one additional derived field to this incoming data before integrating it with the existing dataset -- the `Derived field` for a new row of data must be computed based on fields already existing in the row. -For example, a `todo` dataset with `task`, `priority`, and `creation_date` may generate a new field `deadline` by looking at `creation_date` and `priority` - -Your application is required to demonstrate the use of the following concepts: - -HTML: -- One or more [HTML Forms](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms), with any combination of form tags appropriate for the user input portion of the application -- A results page displaying all data currently available on the server. You will most likely use a `` tag for this, but `
+ + +
+ diff --git a/server.improved.js b/server.improved.js index 26673fc0..e64e4ca9 100644 --- a/server.improved.js +++ b/server.improved.js @@ -7,14 +7,16 @@ const http = require( 'http' ), port = 3000 const appdata = [ - { 'model': 'toyota', 'year': 1999, 'mpg': 23 }, - { 'model': 'honda', 'year': 2004, 'mpg': 30 }, - { 'model': 'ford', 'year': 1987, 'mpg': 14} + {'name': "Artoria Pendragon", 'occupation': "Saber", 'strength': "B", 'endurance': "B", "agility": "B", "magic": "A", "luck": "A+", "noble": "EX"}, + {'name': "Brynhild", 'occupation': "Lancer", 'strength': "B+", 'endurance': "A", "agility": "A", "magic": "C", "luck": "E", "noble": "A"}, + {'name': "Gilgamesh", 'occupation': "Archer", 'strength': "B", 'endurance': "C", "agility": "C", "magic": "B", "luck": "A", "noble": "EX"}, + {'name': "Tamamo-no-Mae", 'occupation': "Caster", 'strength': "E", 'endurance': "E", "agility": "B", "magic": "A", "luck": "D", "noble": "B"}, + {'name': "Morgan", 'occupation': "Berserker", 'strength': "C", 'endurance': "E", "agility": "B", "magic": "A+", "luck": "B", "noble": "EX"}, ] const server = http.createServer( function( request,response ) { if( request.method === 'GET' ) { - handleGet( request, response ) + handleGet( request, response ) }else if( request.method === 'POST' ){ handlePost( request, response ) } @@ -25,7 +27,8 @@ const handleGet = function( request, response ) { if( request.url === '/' ) { sendFile( response, 'public/index.html' ) - }else{ + } + else{ sendFile( response, filename ) } } @@ -38,12 +41,44 @@ const handlePost = function( request, response ) { }) request.on( 'end', function() { - console.log( JSON.parse( dataString ) ) - - // ... do something with the data here!!! - - response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) - response.end() + const json = JSON.parse( dataString ) + + if( request.url === '/result' ) { + json.tabledata = appdata; + json.noc = appdata.length; + } + else if ( request.url === '/submit' ) { + let acc = 0; + + for (let i = 0; i < appdata.length; i++){ + let character_data = appdata[i]; + if ( character_data.name === json.yourname && character_data.occupation === json.occup){ + acc += 1; + } + else{ + acc += 0; + } + } + + if (acc === 0){ + json.alert = 0; + } + else{ + json.alert = 1; + } + + + if(checkInfoComplete(json) && json.alert === 0){ + var generated = generateServant(json); + appdata.push(generated); + } + + json.tabledata = appdata; + json.noc = appdata.length; // number of characters + } + + response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) + response.end( JSON.stringify( json ) ) }) } @@ -69,4 +104,132 @@ const sendFile = function( response, filename ) { }) } +function checkInfoComplete( json ){ + if (json.yourname === "character name" || json.yourname === "" || json.occup === "None"){ + json.info_alert = 1; + return false; + } + else{ + json.info_alert = 0; + return true; + } +} + +function generateServant( json ){ + let returndata; + + if (json.occup === "Saber"){ + let returnST = getRandomAttribute(1, 3); + let returnEN = getRandomAttribute(3, 7); + let returnAG = getRandomAttribute(1, 4); + let returnMG = getRandomAttribute(2, 5); + let returnGL = getRandomAttribute(1, 5); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + } + else if (json.occup === "Lancer"){ + let returnST = getRandomAttribute(1, 5); + let returnEN = getRandomAttribute(3, 6); + let returnAG = getRandomAttribute(1, 3); + let returnMG = getRandomAttribute(3, 6); + let returnGL = getRandomAttribute(4, 5); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + } + else if (json.occup === "Archer"){ + let returnST = getRandomAttribute(3, 5); + let returnEN = getRandomAttribute(3, 6); + let returnAG = getRandomAttribute(1, 3); + let returnMG = getRandomAttribute(3, 6); + let returnGL = getRandomAttribute(1, 3); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + } + else if (json.occup === "Caster"){ + let returnST = getRandomAttribute(3, 6); + let returnEN = getRandomAttribute(3, 6); + let returnAG = getRandomAttribute(3, 6); + let returnMG = getRandomAttribute(1, 3); + let returnGL = getRandomAttribute(1, 5); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + } + else if (json.occup === "Rider"){ + let returnST = getRandomAttribute(1, 5); + let returnEN = getRandomAttribute(1, 3); + let returnAG = getRandomAttribute(2, 5); + let returnMG = getRandomAttribute(3, 6); + let returnGL = getRandomAttribute(1, 5); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + + } + else if (json.occup === "Assassin"){ + let returnST = getRandomAttribute(1, 7); + let returnEN = getRandomAttribute(3, 7); + let returnAG = getRandomAttribute(1, 2); + let returnMG = getRandomAttribute(2, 6); + let returnGL = getRandomAttribute(4, 5); + let returnNP = getRandomAttribute(1, 4); + returndata = {'name': json.yourname, 'occupation': json.occup, 'strength': returnST, 'endurance': returnEN, "agility": returnAG, "magic": returnMG, "luck": returnGL, "noble": returnNP}; + } + + return returndata; +} + +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min)) + min; //不含最大值,含最小值 +} + +function getRandomAttribute(min, max){ + //range: high (EX, A), mid (A, B, C), low (C, D, E) + let range = getRandomInt(min, max); + + if (range === 1){ + let num = getRandomInt(1, 20); + if (num === 1 ){ + return "EX"; + } + else if (num === 2 || num === 3){ + return "A++" + } + else if (num >= 4 && num <= 7){ + return "A+" + } + else if (num >= 8 && num <= 14){ + return "A" + } + else{ + return "A-" + } + } + else if (range === 2 || range === 3){ + let num = getRandomInt(1, 10); + if (num === 1 ){ + return "A--"; + } + else if (num >= 2 && num <= 5){ + return "B"; + } + else{ + return "C"; + } + } + else { + let num = getRandomInt(1, 5); + if (num === 1){ + return "C"; + } + else if (num >=2 && num <= 4){ + return "D"; + } + else{ + return "E"; + } + } + +} + server.listen( process.env.PORT || port ) From ce06a83b9f18ef74927f708e05f2873d1c41a25b Mon Sep 17 00:00:00 2001 From: GameMas Date: Thu, 9 Sep 2021 11:42:24 -0400 Subject: [PATCH 2/2] update1 --- README.md | 3 ++- public/css/style.css | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d6b7c11b..8e1324f2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js +Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js +website: http://a2-jingyu-xie.glitch.me/ ## Fate Servant Generator Include a very brief summary of your project here. Be sure to include the CSS positioning technique you used, and any required instructions to use your application. diff --git a/public/css/style.css b/public/css/style.css index a5120687..1df52f52 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -87,7 +87,7 @@ th, td { } tr:hover { - background-color: #4CAF50; + background-color: #f44336; } tr:nth-child(even) { @@ -95,11 +95,11 @@ tr:nth-child(even) { } tr:nth-child(even):hover { - background-color: #4CAF50; + background-color: #f44336; } th { - background-color: #4CAF50; + background-color: #f44336; color: black; text-align: center; }