From ed9fcbe57b303850fe037badd658709118650ed2 Mon Sep 17 00:00:00 2001 From: sorrelbri Date: Mon, 25 May 2020 23:40:51 -0700 Subject: [PATCH] add submit handler to seed field with calendar data --- dist/app.bundle.js | 1 - dist/index.html | 1 - src/components/Controls.js | 17 ++++++++++++++++- src/components/GameFieldTable.js | 16 ++++++++-------- src/components/controls.html | 2 +- src/utils/index.js | 13 ++++++++++--- webpack.common.js | 6 ++++++ 7 files changed, 41 insertions(+), 15 deletions(-) delete mode 100644 dist/app.bundle.js delete mode 100644 dist/index.html diff --git a/dist/app.bundle.js b/dist/app.bundle.js deleted file mode 100644 index d10b5a0..0000000 --- a/dist/app.bundle.js +++ /dev/null @@ -1 +0,0 @@ -!function(n){var e={};function t(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return n[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}t.m=n,t.c=e,t.d=function(n,e,r){t.o(n,e)||Object.defineProperty(n,e,{enumerable:!0,get:r})},t.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},t.t=function(n,e){if(1&e&&(n=t(n)),8&e)return n;if(4&e&&"object"==typeof n&&n&&n.__esModule)return n;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:n}),2&e&&"string"!=typeof n)for(var i in n)t.d(r,i,function(e){return n[e]}.bind(null,i));return r},t.n=function(n){var e=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(e,"a",e),e},t.o=function(n,e){return Object.prototype.hasOwnProperty.call(n,e)},t.p="",t(t.s=3)}([function(n,e,t){"use strict";var r,i=function(){return void 0===r&&(r=Boolean(window&&document&&document.all&&!window.atob)),r},o=function(){var n={};return function(e){if(void 0===n[e]){var t=document.querySelector(e);if(window.HTMLIFrameElement&&t instanceof window.HTMLIFrameElement)try{t=t.contentDocument.head}catch(n){t=null}n[e]=t}return n[e]}}(),a=[];function c(n){for(var e=-1,t=0;tn.length)&&(e=n.length);for(var t=0,r=new Array(e);tn.length)&&(e=n.length);for(var t=0,r=new Array(e);tn.length)&&(e=n.length);for(var t=0,r=new Array(e);t0&&(t.map["".concat(e,",").concat(r)]=d(!0,0))}))})),Object.entries(a).forEach((function(n){var e=u(n,2),r=e[0],i=u(e[1],2),o=i[0],a=i[1];return t.map[r]=d(o,a)}))},m=function(n){!function(n,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");n.prototype=Object.create(e&&e.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),e&&o(n,e)}(l,n);var e,t,r,c=a(l);function l(n,e){return f(this,l),c.call(this,n,e)}return e=l,(t=[{key:"addLiveNeighbor",value:function(n){void 0===this.map[n]&&(this.map[n]=d(!1)),this.map[n].addLiveNeighbor()}},{key:"map",get:function(){return this.head.map}}])&&i(e.prototype,t),r&&i(e,r),l}(h),y=function(n,e){var t=u(e,2),r=t[0],i=t[1];return n[r]=i,n},g=function(n){var e=u(n,2);e[0];return!0===e[1].living},w=function(n){var e=u(n,2),t=e[0],r=e[1];return[t,[r.living,r.liveNeighbors]]},x=function(n){var e=u(n,2),t=e[0],r=e[1];return r.setLiving(),[t,[r.living,0]]};n.exports={GameField:v,fieldStream:function n(e){var t=e.fieldArray,r=e.fieldMap;return new m(new v({fieldArray:t,fieldMap:r}),(function(){var e;Object.entries(this.map).filter(g).forEach((e=this,function(n){var t=u(n,1)[0];return b(t).forEach((function(n){return e.addLiveNeighbor(n)}))}));var t=Object.entries(this.map).map(w).reduce(y,{});return new m(new v({fieldMap:t}),(function(){var e=Object.entries(this.map).map(x).reduce(y,{});return n({fieldMap:e})}))}))}}},function(n,e,t){function r(n){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n})(n)}function i(n,e){return(i=Object.setPrototypeOf||function(n,e){return n.__proto__=e,n})(n,e)}function o(n){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(n){return!1}}();return function(){var t,r=c(n);if(e){var i=c(this).constructor;t=Reflect.construct(r,arguments,i)}else t=r.apply(this,arguments);return a(this,t)}}function a(n,e){return!e||"object"!==r(e)&&"function"!=typeof e?function(n){if(void 0===n)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return n}(n):e}function c(n){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(n){return n.__proto__||Object.getPrototypeOf(n)})(n)}function l(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}function u(n,e){for(var t=0;t0&&void 0!==arguments[0]&&arguments[0],t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;l(this,n),this.living=e,this.liveNeighbors=t}return s(n,[{key:"toggleLiving",value:function(){this.living=!this.living}},{key:"addLiveNeighbor",value:function(){this.liveNeighbors++}},{key:"setLiving",value:function(){return this.living&&2!==this.liveNeighbors&&3!==this.liveNeighbors?(this.liveNeighbors=0,this.living=!1):3===this.liveNeighbors?(this.liveNeighbors=0,this.living=!0):void(this.liveNeighbors=0)}}]),n}(),p=function(n){!function(n,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");n.prototype=Object.create(e&&e.prototype,{constructor:{value:n,writable:!0,configurable:!0}}),e&&i(n,e)}(t,n);var e=o(t);function t(n,r){return l(this,t),e.call(this,n,r)}return s(t,[{key:"addLiveNeighbor",value:function(){this.head.addLiveNeighbor()}},{key:"setLiving",value:function(){this.head.setLiving()}},{key:"toggleLiving",value:function(){this.head.toggleLiving()}},{key:"living",get:function(){return this.head.living}},{key:"liveNeighbors",get:function(){return this.head.liveNeighbors},set:function(n){this.head.liveNeighbors=n}}]),t}(f);n.exports={Cell:d,cellStream:function(){var n=arguments.length>0&&void 0!==arguments[0]&&arguments[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return new p(new d(n,e),(function(){return this.head.setLiving(),this}))}}},function(n,e,t){var r=t(14),i=(t(15),t(2).getCalendar);n.exports={init:function(n){document.getElementById("controls").innerHTML+=r;var e=document.getElementById("rate"),t=document.getElementById("forward"),o=document.getElementById("play"),a=document.getElementById("reset"),c=document.getElementById("clear"),l=document.getElementById("game-field"),u=document.getElementById("calendar-form"),s={interval:null,play:function(){this.interval=setInterval((function(){return n.advance()}),1e3/this.rate)},pause:function(){this.interval&&(clearInterval(this.interval),this.interval=null)},clear:function(){n=n.clear()},reset:function(){this.pause(),n=n.reset()},forward:function(){n.advance()},rate:10,updateField:function(e,t){n=n.toggleCell(e,t)},updateRate:function(n){console.log("updating rate"),console.log(n),console.log(this.interval),s.rate=n,this.interval&&(clearInterval(this.interval),this.play())}};return e.addEventListener("change",(function(n){n.preventDefault(),s.updateRate(n.target.value)})),t.addEventListener("click",(function(n){n.preventDefault(),s.forward()})),o.addEventListener("click",(function(n){if(n.preventDefault(),s.interval)return s.pause(),t.disabled=!1;s.play(),t.disabled=!0})),a.addEventListener("click",(function(n){n.preventDefault(),s.reset()})),c.addEventListener("click",(function(n){n.preventDefault(),s.clear()})),l.addEventListener("click",(function(n){if(n.preventDefault(),!s.interval){var e=n.offsetX,t=n.offsetY;s.updateField(e,t)}})),u.addEventListener("submit",(function(n){n.preventDefault();var e=n.target[0].value;console.log(i(e))})),s}}},function(n,e){n.exports="
"},function(n,e,t){var r=t(0),i=t(16);"string"==typeof(i=i.__esModule?i.default:i)&&(i=[[n.i,i,""]]);var o={insert:"head",singleton:!1};r(i,o);n.exports=i.locals||{}},function(n,e,t){(e=t(1)(!1)).push([n.i,'aside {\n background: red;\n justify-content: space-evenly;\n display: flex;\n flex-flow: column nowrap;\n position: float;\n top: 0;\n}\n\naside * {\n background: red;\n color: white;\n}\n\nh1 {\n font-size: 130%;\n padding: 1em;\n\n}\n\ndiv.playControls {\n min-width: 30vw;\n display: grid;\n border-radius: 1vh;\n border: solid white .5vh;\n grid-template-rows: 2fr 1fr;\n grid-template-columns: 1fr 1fr 1fr 1fr;\n gap: 1vh;\n grid-template-areas: "buttons buttons buttons buttons" "slider slider slider slider";\n padding: 1vh;\n}\n\nbutton {\n width: 100%;\n border-radius: .5vh;\n border: solid white .5vh;\n}\n\ninput[type="range"] {\n grid-area: slider;\n}\n',""]),n.exports=e}]); \ No newline at end of file diff --git a/dist/index.html b/dist/index.html deleted file mode 100644 index d75651b..0000000 --- a/dist/index.html +++ /dev/null @@ -1 +0,0 @@ -Game of Life
\ No newline at end of file diff --git a/src/components/Controls.js b/src/components/Controls.js index 0e79cb8..6d19460 100644 --- a/src/components/Controls.js +++ b/src/components/Controls.js @@ -31,6 +31,12 @@ const init = (gameField) => { this.pause(); gameField = gameField.reset(); }, + seed(weeks) { + if (weeks.length) { + gameField = gameField.seed(weeks); + console.log(gameField); + } + }, forward() { gameField.advance(); }, @@ -83,7 +89,16 @@ const init = (gameField) => { calendarFormEl.addEventListener("submit", (e) => { e.preventDefault(); const user = e.target[0].value; - console.log(getCalendar(user)); + getCalendar(user) + .then((data) => { + return data.map((week) => { + return week.contributionDays.map((day) => day.contributionCount); + }); + }) + .then((weeks) => controls.seed(weeks)) + .catch((e) => { + calendarFormEl.elements[0].value = "enter valid handle"; + }); }); return controls; }; diff --git a/src/components/GameFieldTable.js b/src/components/GameFieldTable.js index d57b81e..9a72159 100644 --- a/src/components/GameFieldTable.js +++ b/src/components/GameFieldTable.js @@ -8,21 +8,17 @@ canvas2D.fillStyle = "white"; const parseSeed = (seed) => { if (seed && Array.isArray(seed)) { return { - fieldArray: [ - [0, 1, 0], - [0, 0, 1], - [1, 1, 1], - ], + fieldArray: seed, }; } return seed; }; const fieldView = (seed) => { - console.log(seed); + // console.log(seed); seed = parseSeed(seed); - // generateTable(true); - console.log(seed); + // // generateTable(true); + // console.log(seed); const field = fieldStream(seed); const view = { draw(x, y) { @@ -46,6 +42,10 @@ const fieldView = (seed) => { newField.updateView(); return newField; }, + seed(seed) { + console.log(seed); + return fieldView(seed); + }, advance() { this.field = this.field.next.next; this.updateView(); diff --git a/src/components/controls.html b/src/components/controls.html index 12fb9e4..b02bb43 100644 --- a/src/components/controls.html +++ b/src/components/controls.html @@ -12,6 +12,6 @@ />
- +
diff --git a/src/utils/index.js b/src/utils/index.js index ccca9a6..7feaeb2 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,3 +1,5 @@ +const token = process.env.GITHUB_API_TOKEN; + const query = (user) => `query { user (login: "${user}") { contributionsCollection { @@ -17,6 +19,7 @@ const options = (user) => ({ method: "post", headers: { "Content-Type": "application/json", + Authorization: `bearer ${token}`, }, body: JSON.stringify({ query: query(user), @@ -24,9 +27,13 @@ const options = (user) => ({ }); const getCalendar = (user) => { - // return fetch(`https://api.github.com/graphql`, options(user)).then((res) => - // res.json() - // ); + return fetch(`https://api.github.com/graphql`, options(user)) + .then((res) => res.json()) + .then( + (result) => + result.data.user.contributionsCollection.contributionCalendar.weeks + ) + .catch((e) => e); }; class Stream { diff --git a/webpack.common.js b/webpack.common.js index 5f39f71..fe29932 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -1,6 +1,7 @@ const path = require("path"); const { CleanWebpackPlugin } = require("clean-webpack-plugin"); const HtmlWebpackPlugin = require("html-webpack-plugin"); +const { DefinePlugin } = require("webpack"); module.exports = { entry: { @@ -12,6 +13,11 @@ module.exports = { new HtmlWebpackPlugin({ template: "index.html", }), + new DefinePlugin({ + "process.env.GITHUB_API_TOKEN": JSON.stringify( + process.env.GITHUB_API_TOKEN + ), + }), ], output: { filename: "[name].bundle.js",