From d1388d9ca9a2c28ea50d252548204daa99693ffd Mon Sep 17 00:00:00 2001 From: wuya666 <30381159+wuya666@users.noreply.github.com> Date: Thu, 13 Jun 2019 05:47:54 +0800 Subject: [PATCH] fizzbot nodejs solver --- fizznodejs/.gitignore | 2 + fizznodejs/README.md | 5 ++ fizznodejs/fizzsolver.js | 153 +++++++++++++++++++++++++++++++++++ fizznodejs/package-lock.json | 13 +++ fizznodejs/package.json | 15 ++++ 5 files changed, 188 insertions(+) create mode 100644 fizznodejs/.gitignore create mode 100644 fizznodejs/README.md create mode 100644 fizznodejs/fizzsolver.js create mode 100644 fizznodejs/package-lock.json create mode 100644 fizznodejs/package.json diff --git a/fizznodejs/.gitignore b/fizznodejs/.gitignore new file mode 100644 index 0000000..e212594 --- /dev/null +++ b/fizznodejs/.gitignore @@ -0,0 +1,2 @@ + +node_modules diff --git a/fizznodejs/README.md b/fizznodejs/README.md new file mode 100644 index 0000000..7865a48 --- /dev/null +++ b/fizznodejs/README.md @@ -0,0 +1,5 @@ +## Usage: + +`npm install` + +`npm start` diff --git a/fizznodejs/fizzsolver.js b/fizznodejs/fizzsolver.js new file mode 100644 index 0000000..35b181d --- /dev/null +++ b/fizznodejs/fizzsolver.js @@ -0,0 +1,153 @@ +"use strict"; + +const fetch = require("node-fetch"); +const basePath = "https://api.noopschallenge.com"; +const startPath = "/fizzbot"; + +// GET method with node-fetch +async function tryGet(url) { + let result = {}; + + try { + let response = await fetch(url); + result = await response.json(); + } catch (error) { + console.log("Something Bad Happened!!") + console.log(error); + } + + return result; +} + +// POST method with node-fetch +async function tryPost(url, data) { + let result = {} + + try { + let response = await fetch(url, { + method: "post", + body: JSON.stringify(data), + headers: {"Content-Type": "application/json"} + }); + result = await response.json(); + } catch (error) { + console.log("Something Bad Happened!!") + console.log(error); + } + + return result; +} + +// Calculate the answer with the rules and the numbers +function calcAnswer(rules, numbers) { + let answer = []; + try { + for (let n of numbers) { + let tempo = []; + for (let r of rules) { + if (n % r["number"] === 0) { + tempo.push(r["response"]); + } + } + let result = tempo.join(""); + if (result === "") { + result = String(n); + } + answer.push(result); + } + } catch (error) { + console.log("Something Bad Happened!!") + console.log(error); + } + return {"answer": answer.join(" ")}; +} + +// Main solver function +const startSolver = async (url) => { + let cont = true; + let answer = {}; + + // main loop + while (cont) { + // send GET request to url + console.log("GET the question...") + let res = await tryGet(url); + + if (JSON.stringify(res) === "{}") { + console.log("Oops, something is wrong!!"); + cont = false; + break; + } + + if (typeof res["message"] !== "undefined") { + // GET response contains message, show it + console.log("====== GET message begins ======"); + console.log(res["message"]); + console.log("====== GET message ends ======"); + } + + if (typeof res["nextQuestion"] !== "undefined") { + // GET response contains the nextQuestion link, go there + url = basePath + res["nextQuestion"] + continue; + } + + if (typeof res["rules"] !== "undefined" && typeof res["numbers"] !== "undefined") { + // it's a real question + let rules = res["rules"]; + let numbers = res["numbers"]; + + // show the rules + console.log("====== Rules begins ======"); + console.log(JSON.stringify(rules)) + console.log("====== Rules ends ======"); + + // show the numbers + console.log("====== Numbers begins ======"); + console.log(JSON.stringify(numbers)) + console.log("====== Numbers ends ======"); + + // calculate the answer + answer = calcAnswer(rules, numbers); + } else { + // the first question or something, answer "JavaScript!!" anyway + answer = {"answer": "JavaScript!!"}; + } + + // show our decided answer + console.log("====== Our answer ======"); + console.log(JSON.stringify(answer)); + console.log("====== Answer ends ======"); + + // POST the answer back to the url + console.log("POST the answer...") + res = await tryPost(url, answer); + + if (JSON.stringify(res) === "{}") { + console.log("Oops, something is wrong!!"); + cont = false; + break; + } + + if (typeof res["message"] !== "undefined") { + // POST response contains message, show it + console.log("====== POST message begins ======"); + console.log(res["message"]); + console.log("====== POST message ends ======"); + } + + if (typeof res["nextQuestion"] !== "undefined") { + // POST response contains the nextQuestion link, go there + url = basePath + res["nextQuestion"] + continue; + } + + // No next question? + console.log("No more questions, test finished!!"); + cont = false; + + } +} + +// Entry point +startSolver(basePath + startPath); diff --git a/fizznodejs/package-lock.json b/fizznodejs/package-lock.json new file mode 100644 index 0000000..2f14493 --- /dev/null +++ b/fizznodejs/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "fizznodejs", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + } + } +} diff --git a/fizznodejs/package.json b/fizznodejs/package.json new file mode 100644 index 0000000..d7ef34c --- /dev/null +++ b/fizznodejs/package.json @@ -0,0 +1,15 @@ +{ + "name": "fizznodejs", + "version": "1.0.0", + "description": "fizzbot nodejs solver", + "main": "fizzsolver.js", + "scripts": { + "test": "node fizzsolver.js", + "start": "node fizzsolver.js" + }, + "author": "wuya666", + "license": "ISC", + "dependencies": { + "node-fetch": "^2.6.0" + } +}