-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
3 lines (2 loc) · 17 KB
/
js.js
1
2
3
function e(e,t,n,r,o,a,l){try{var i=e[a](l),s=i.value}catch(e){n(e);return}i.done?t(s):Promise.resolve(s).then(r,o)}function t(t){return function(){var n=this,r=arguments;return new Promise(function(o,a){var l=t.apply(n,r);function i(t){e(l,o,a,i,s,"next",t)}function s(t){e(l,o,a,i,s,"throw",t)}i(void 0)})}}t(function*(){let e=e=>e,n=(performance.now(),document.currentScript.outerHTML),r=document.body.innerHTML.replace(n,""),o=0;function a(e){return e>=0?2*e:-2*e-1}function l(e){return e%2==0?e/2:-(e+1)/2}function i(){return(i=t(function*(e){let t=yield fetch(e);if(!t.ok)throw Error(`HTTP error! status: ${t.status}`);return yield t.text()})).apply(this,arguments)}function s(e){return c.apply(this,arguments)}function c(){return(c=t(function*(e){return(yield function(e){return i.apply(this,arguments)}(e)).split("\n").filter(e=>!e.startsWith("#")).map(e=>e.trim())})).apply(this,arguments)}e(function(e){let t=function(e){let t=Math.floor((Math.sqrt(8*e+1)-1)/2),n=e-(t*t+t)/2;return[t-n,n]}(a(42));return[l(t[0]),l(t[1])]}(0));let d=yield s("/data/wordsets.txt"),u=[];function m(){return(m=t(function*(e){let t=yield s(`/data/wordsets/${e}.txt`);return u.push(t),t})).apply(this,arguments)}function p(e){return h.apply(this,arguments)}function h(){return(h=t(function*(e){var t;let n=e%d.length;return null!==(t=u[n])&&void 0!==t?t:yield function(e){return m.apply(this,arguments)}(d[n])})).apply(this,arguments)}let f=[],g={s1:0,s2:0,setSeed(e){this.s1=e,this.s2=e},random(){return this.s1=1103515245*this.s1+12345&2147483647,this.s2^=this.s2<<13,this.s2^=this.s2>>17,this.s2^=this.s2<<5,e("random.random"),e(((this.s1^this.s2)+2147483648)/4294967295)}};function y(){requestIdleCallback(()=>{v()})}function v(){S("gameState")&&O("gameState",serializeGameState())}let b=["keyup","pointerup","pointercancel","scrollend"];function E(){return w.apply(this,arguments)}function w(){return(w=t(function*(){let t;if(window.hwgInitialized)throw Error("Game was already initialized.");window.hwgInitialized=!0;let n=document.getElementById("stage"),r=[[3,2,1],[4,-1,0],[5,6,7]],[i,s,c]=[..."각".normalize("NFD")],d=12623-s.charCodeAt(0),u=Object.fromEntries(Object.entries({ㄱㄱ:"ㄲ",ㄱㅅ:"ㄳ",ㄴㅈ:"ㄵ",ㄴㅎ:"ㄶ",ㄷㄷ:"ㄸ",ㄹㄱ:"ㄺ",ㄹㅁ:"ㄻ",ㄹㅂ:"ㄼ",ㄹㅅ:"ㄽ",ㄹㅌ:"ㄾ",ㄹㅍ:"ㄿ",ㄹㅎ:"ㅀ",ㅂㅂ:"ㅃ",ㅂㅅ:"ㅄ",ㅅㅅ:"ㅆ",ㅈㅈ:"ㅉ",ㅏㅣ:"ㅐ",ㅑㅣ:"ㅒ",ㅓㅣ:"ㅔ",ㅕㅣ:"ㅖ",ㅗㅏ:"ㅘ",ㅗㅏㅣ:"ㅙ",ㅗㅣ:"ㅚ",ㅜㅓ:"ㅝ",ㅜㅓㅣ:"ㅞ",ㅜㅣ:"ㅟ",ㅡㅣ:"ㅢ"}).map(([e,t])=>[t,e])),m=-1,h=1;try{(t=T(S("gameState")))&&(h=t.stageNumber,n.textContent=h)}catch(e){console.error(e),L("gameState")}n.textContent=h,f.length=0,f.push(...yield p(h));let E=function(e,t){let n=a(e),r=a(t);return l(.5*(n+r)*(n+r+1)+r)}(h,o);g.setSeed(E);let w=[...f];function C(e){return[...e.normalize("NFC")].flatMap(D)}let P=[];for(let e=0;e<16;e++)P.push(...w.splice(et(0,w.length-1),1));w.length=0;let j=document.getElementById("word-list"),I=document.getElementById("word-template");P.forEach(e=>{let t=I.content.cloneNode(!0).querySelector("li");t.textContent=e,t.dataset.word=e,j.appendChild(t)});let B=document.getElementById("jamo-board"),A=document.getElementById("jamo-template");B.addEventListener("selectstart",e=>{e.preventDefault()});let $=!1;B.addEventListener("contextmenu",e=>{$||e.preventDefault(),$=!1});let k="ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎㅏㅑㅓㅕㅗㅛㅜㅠㅡㅣ";function N(e){return[...e].map(e=>e.charCodeAt()).reduce((e,t)=>(e>>>1|(1&e)<<15)^t,0)}function R(){return{GAME_VERSION:4,width:12,height:12,completions:Array.from(B.querySelectorAll(".completion-bar")).filter(e=>e!==ep).map(e=>`${e.dataset.start},${e.dataset.end}`).join(),stageNumber:h}}function q(e=R()){let t=JSON.stringify(e);return`${t}|${N(t)}`}function T(e){if(null===e)return null;let[t,n]=e.split("|");if(N(t)!==1*n)throw Error("saved game state is corrupted");let r=JSON.parse(t);if(4!==r.GAME_VERSION)throw Error("The saved game state is from a different version of the game.");return r.completions?r.completions=r.completions.split(",").map(Number).reduce((e,t)=>(e.length&&4!==e.at(-1).length||e.push([]),e.at(-1).push(t),e),[]):r.completions=[],r}function D(e){let[t,n,r]=[...e.normalize("NFD")].concat(["","",""]).slice(0,3);return["ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ"[t.charCodeAt(0)-i.charCodeAt(0)],String.fromCharCode(n.charCodeAt(0)+d),r.length?"ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ"[r.charCodeAt(0)-c.charCodeAt(0)]:""].flatMap(e=>{var t;return[...null!==(t=u[e])&&void 0!==t?t:e]})}let z=()=>(e(z.name),e(k[et(0,k.length-1)])),F=P.flatMap(e=>[...e.normalize("NFC")].flatMap(D)),V=()=>(e(V.name),e(F[et(0,F.length-1)]));function U(e,t){t.classList.add("no-transition"),e(),requestAnimationFrame(()=>{requestAnimationFrame(()=>{t.classList.remove("no-transition")})})}B.style.setProperty("--gap","0.75rem"),B.style.setProperty("--width",12),B.style.setProperty("--height",12),U(()=>{for(let e=0;e<144;e++){let e=A.content.cloneNode(!0).querySelector("i"),t=et(0,1)?z():V();e.dataset.jamo=t,B.appendChild(e)}},B);let G=Array.from({length:144},()=>null),H=(e,t,n,o)=>{if(n<0||n>7)throw RangeError("direction must be 0 to 7");for(let a=-1;a<=1;a++)for(let l=-1;l<=1;l++)if(r[a+1][l+1]===n)return[e+l*o,t+a*o]},J=B.querySelectorAll("#jamo-board>i");J.forEach((e,t)=>{e.dataset.index=t});let X=(e,t,n,r)=>{let o=C(e);if(o.length>12&&o.length>12)throw RangeError("word too long for board");for(let e=0;e<o.length;e++){let[a,l]=H(t,n,r,e);if(a<0||a>=12||l<0||l>=12)return!1;let i=G[12*l+a];if(i&&i!==o[e])return!1}for(let e=0;e<o.length;e++){let[a,l]=H(t,n,r,e),i=o[e],s=12*l+a;G[s]=i,J[s].dataset.jamo=i}return!0};function Y(e,t){var n;let r=null!==(n=Y.cache)&&void 0!==n?n:Y.cache=new Map;if(r.has(e))return r.get(e);let o=t();return r.set(e,o),o}function W(e,t,n,r,o=null){var a;let l=Y(W,()=>document.getElementById("completion-bar-template")),i=null!=o?o:l.content.cloneNode(!0).querySelector(".completion-bar");i.dataset.start=`${e},${t}`,i.dataset.end=`${n},${r}`;let s=Math.min(e,n),c=Math.max(e,n),d=Math.min(t,r),u=Math.max(t,r),p=(c-s+1)*2+.75*(c-s),h=(u-d+1)*2+.75*(u-d),f=180*Math.atan2(Math.sign(r-t)*h,Math.sign(n-e)*p)/Math.PI,g=(null===(a=i.style.getPropertyValue("--angle").match(/(\d+)deg/))||void 0===a?void 0:a[1])*1;return isNaN(g)||g===f?i.classList.remove("no-transition"):i.classList.add("no-transition"),i.style.setProperty("--top",`${2*d+.75*d}rem`),i.style.setProperty("--left",`${2*s+.75*s}rem`),i.style.setProperty("--width",`${p}rem`),i.style.setProperty("--height",`${h}rem`),i.style.setProperty("--thick","2.25rem"),i.style.setProperty("--hypot",`${Math.hypot(p,h)+.25}rem`),i.style.setProperty("--angle",`${f}deg`),i.style.setProperty("--hue",m+Math.floor(43758.5453*Math.sin(12.9898*e+78.233*t)%1*en)*Math.floor(360/en)),i}B.style.setProperty("--size","2rem");let _=0,K=document.getElementById("stage-clear-dialog"),Q=K.querySelector("#next-stage"),Z=K.querySelector("#cancel-next-stage");function ee(e,t){_++,e.style.setProperty("--hue",t.style.getPropertyValue("--hue")),e.classList.add("found"),16===_&&(K.showModal(),p(h+1))}function et(t,n){return e(et.name),e([t,n]),e(Math.floor(g.random()*(n-t+1))+t)}Q.addEventListener("click",()=>{var e,t;o=0;let n=R();O("gameState",q((e=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){var r;r=n[t],t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r})}return e}({},n),t=t={completions:"",stageNumber:n.stageNumber+1},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):(function(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n.push.apply(n,r)}return n})(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}),e))),M()},{passive:!0}),Z.addEventListener("click",()=>{K.close()},{passive:!0});let en=16;m=et(0,359);let er=Array.from({length:4},()=>16),eo=64,ea=()=>{let e=et(0,eo-1),t=0;for(let n=0;n<4;n++)if(e<(t+=er[n]))return n};try{P.toSorted((e,t)=>C(t).length-C(e).length).forEach(e=>{let t,n,r;let a=0;for(;;){t=et(0,11),n=et(0,11);let l=((r=ea())+6)%8;if(X(e,t,n,l)){er[r]--,eo--,16-er[r]>16/3&&(eo-=er[r],er[r]=0);break}if(a>256)throw o++,Error("Failed to populate a word to the board. Try increasing the size of the board or reducing the number of words. Retrying...");a++}})}catch(e){console.error(e),M();return}t&&t.completions.forEach(([e,t,n,r])=>{!function(e,t,n,r){let o=W(e,t,n,r),a=ey(eg(e,t,n,r));ee(j.querySelector(`li[data-word="${a}"]`),o),B.appendChild(o)}(e,t,n,r)});let el=document.getElementById("dark-mode-toggle");el.addEventListener("click",()=>{let e;let t=el.dataset.mode,n=el.dataset.modeOptions.split("|"),r=n[(n.indexOf(t)+1)%n.length];e=()=>{el.dataset.mode=r,document.documentElement.dataset.mode=r},document.startViewTransition?document.startViewTransition(e):e(),localStorage.darkMode=r},{passive:!0});let ei=localStorage.darkMode;ei&&U(()=>{el.dataset.mode=ei,document.documentElement.dataset.mode=ei},el);let es=new Proxy({value:!1},{set:(e,t,n)=>{if("value"===t&&"boolean"==typeof n)return Reflect.set(e,t,n)}}),ec=[-1,-1],ed=-1,eu=[-1,-1];function em(e,t){let[n,o]=e,[a,l]=t;if(!(n===a||o===l||Math.abs(n-a)===Math.abs(o-l)))return -1;let[i,s]=[a-n,l-o];return r[Math.sign(s)+1][Math.sign(i)+1]}let ep=null;function eh(){if(!(-1!==ec[0]&&-1!==ec[1]))return;let e=null!==ep,[t,n]=ec,[r,o]=-1===eu[0]&&-1===eu[1]?[t,n]:eu;ep=W(t,n,r,o,ep),e||B.appendChild(ep)}function ef(e,t){let n=e<t?1:-1,r=[];for(let o=0;o<=Math.abs(t-e);o++)r.push(o*n+e);return r}function eg(e,t,n,r){let o=ef(e,n),a=ef(t,r);return Array.from({length:Math.max(o.length,a.length)},(n,r)=>{var l,i;return[null!==(l=o[r])&&void 0!==l?l:e,null!==(i=a[r])&&void 0!==i?i:t]}).map(([e,t])=>J[12*t+e].dataset.jamo).join("")}function ey(e){return P.find(t=>{let[n,r]=Y(t,()=>{let e=C(t);return[e.join(""),e.toReversed().join("")]});return n===e||r===e})}function ev(e){e.animate({opacity:0},{duration:150,easing:"ease-out"}).finished.then(()=>{e.remove()})}function eb(e,t,n){return e<t?t-e:e>n?e-n:0}function eE(e,t){let[n,r,o,a,l,i]=function(){let e=J[0],t=J[1],n=J[12],r=e.getBoundingClientRect(),o=t.getBoundingClientRect(),a=n.getBoundingClientRect(),l=o.left-r.left,i=a.top-r.top,s=r.left;return[s,r.top,l,i,o.left-r.right,a.top-r.bottom]}(),[s,c]=[e-(n-l/2),t-(r-i/2)],[d,u]=[Math.floor(s/o),Math.floor(c/a)];return[d,u]}function ew(e,t,n){return Math.min(n,Math.max(t,e))}B.addEventListener("pointerdown",e=>{if(e.target.matches("#jamo-board>i")){if(es.value=!0,(ec=eE(e.clientX,e.clientY))[0]<0||ec[0]>=12||ec[1]<0||ec[1]>=12){ec=[-1,-1];return}eu=[-1,-1],ed=-1,eh()}},{passive:!0}),document.addEventListener("pointerup",e=>{$=2===e.button,es.value=!1,ec[0]===eu[0]&&ec[1]===eu[1]?(null==ep||ep.remove(),ep=null):-1!==eu[0]&&-1!==eu[1]&&function(e){try{if(null===e)return;let[t,n]=e.dataset.start.split(",").map(Number),[r,o]=e.dataset.end.split(",").map(Number);if(t===r&&n===o){ev(e);return}let a=em([t,n],[r,o]);if(-1===a){ev(e);return}let l=eg(t,n,r,o),i=ey(l);if(i){let t=j.querySelector(`li[data-word="${i}"]`);t&&(t.classList.contains("found")?ev(e):ee(t,e))}else ev(e)}finally{ep=null}}(ep)},{passive:!0}),document.addEventListener("pointermove",e=>{if(!es.value)return;e.preventDefault();let t=eE(e.clientX,e.clientY);if(t[0]=ew(t[0],0,11),t[1]=ew(t[1],0,11),ec[0]===t[0]&&ec[1]===t[1])return;let n=em(ec,t);if(-1!==n?(eu=t,ed=n):-1!==eu[0]&&-1!==eu[1]&&(eu=function(e,t,n){let[r,o]=e,[a,l]=t,[i,s]=[a-r,l-o],[c,d]=[a-r,l-o],[u,m]=[a-r,l-o];Math.abs(c)<Math.abs(d)?c=0:d=0,(u+m)%2&&(Math.abs(u)<Math.abs(m)?m-=Math.sign(m):u-=Math.sign(u));{let e=Math.abs(Math.abs(u)-Math.abs(m))/2;Math.abs(u)<Math.abs(m)?(u+=Math.sign(u)*e,m-=Math.sign(m)*e):(u-=Math.sign(u)*e,m+=Math.sign(m)*e)}let p=Math.abs(i-c)+Math.abs(s-d),h=Math.abs(i-u)+Math.abs(s-m);return p===h?n%2==0?[r+c,o+d]:[r+u,o+m]:p<h?[r+c,o+d]:[r+u,o+m]}(ec,t,ed)),!(-1!==eu[0]&&-1!==eu[1]))return;let[r,o]=eu;if(r<0||r>=12||o<0||o>=12){let e=em(ec,[r,o]),t=eb(r,0,11),n=eb(o,0,11);[r,o]=H(r,o,e,-Math.max(t,n))}J[12*(eu=[r,o])[1]+eu[0]],eh()}),t||O("gameState",q()),window.serializeGameState=q;let eM=document.querySelector("main"),eS=()=>{let e=screen.availWidth,t=screen.availHeight;eM.style.transform="scale(1)",eM.style.margin="0";let{width:n,height:r}=eM.getBoundingClientRect(),o=Math.min(1,e/n,t/r);eM.style.transform=`scale(${o})`,eM.style.margin=`${r*(o-1)/2}px ${n*(o-1)/2}px`};function eL(e){let t=new Uint8Array(atob(e).split("").map(e=>e.charCodeAt(0)));return new TextDecoder().decode(t)}eS(),window.addEventListener("resize",eS,{passive:!0}),b.forEach(e=>{document.addEventListener(e,y,{passive:!0})}),window.addEventListener("beforeunload",()=>{v(),history.state&&O("historyState",history.state)},{passive:!0});let eO=document.getElementById("settings-panel"),ex=document.getElementById("export-game-state-plaintext");document.getElementById("show-settings-panel").addEventListener("click",()=>{eO.showModal()}),document.getElementById("settings-panel").addEventListener("open",()=>{var e;v(),ex.value=(e=S("gameState"),btoa(String.fromCharCode(...new TextEncoder().encode(e))))}),document.querySelectorAll("button[data-success-report]").forEach(e=>{e.addEventListener("click",()=>{if(!e.successReport){let t=document.getElementById("success-report-template").content.cloneNode(!0).querySelector("input");t.setCustomValidity(e.dataset.successReport),e.appendChild(t),e.successReport=t}e.successReport.reportValidity()})}),document.getElementById("copy-game-state-plaintext").addEventListener("click",()=>{navigator.clipboard.writeText(ex.value)}),document.getElementById("download-game-state-plaintext").addEventListener("click",()=>{let e=document.createElement("a"),t=new Blob([ex.value],{type:"text/plain"});e.href=URL.createObjectURL(t),e.download=`save-${new Date().toISOString().replace(/\D/g,"")}.hwgsave`,e.hidden=!0,document.body.appendChild(e),e.click(),e.remove(),URL.revokeObjectURL(e.href)});let eC=document.getElementById("import-game-state-plaintext"),eP=document.getElementById("file-wrapper");function ej(e){eC.setCustomValidity(e),eC.reportValidity()}document.getElementById("select-game-state-file").addEventListener("input",e=>{let t=e.target.files[0];eP.dataset.filename=t.name;let n=new FileReader;n.onload=e=>{let t=e.target.result;try{let e=eL(t);T(e)}catch(e){console.error("Failed to parse game state from file",e),ej("선택한 파일의 구문 분석에 실패했습니다.")}eC.value=t},n.readAsText(t)}),document.getElementById("apply-imported-game-state").addEventListener("click",()=>{try{let e=eL(eC.value);T(e),O("gameState",e),M()}catch(e){console.error("Failed to parse game state from text",e),ej("올바른 형식의 게임 상태 텍스트가 아닙니다.")}});let eI=new CustomEvent("open"),eB=new MutationObserver(e=>{e.forEach(({target:e,type:t,attributeName:n})=>{"attributes"===t&&"open"===n&&e.open&&e.dispatchEvent(eI)})});document.querySelectorAll("dialog").forEach(e=>{let{id:t}=e;eB.observe(e,{attributes:!0,attributeFilter:["open"],subtree:!1}),e.addEventListener("click",t=>{let n=e.getBoundingClientRect();e.open&&(n.left>t.clientX||n.right<t.clientX||n.top>t.clientY||n.bottom<t.clientY)&&(e.classList.remove("bump"),requestAnimationFrame(()=>{e.classList.add("bump")}))}),e.addEventListener("open",()=>{if(location.hash!==`#${t}`){let e=`#${t}`;history.pushState({open:t,url:e},"",e)}}),e.addEventListener("close",()=>{e.classList.remove("bump");let t=new URL(location);t.hash="",history.pushState({open:null,url:t.toString()},"",t)})});let eA=null,e$=document.getElementById("clear-game-state");e$.addEventListener("contextmenu",e=>{e.preventDefault()}),e$.addEventListener("pointerdown",()=>{eA=setTimeout(()=>{L("gameState"),location.reload()},2e3)}),e$.addEventListener("pointerup",()=>{clearTimeout(eA)}),e$.addEventListener("pointerleave",()=>{clearTimeout(eA)}),document.querySelectorAll("form[method=dialog]>button[type=submit]").forEach(e=>{e.addEventListener("click",t=>{var n,r;t.preventDefault(),null===(r=e.closest("dialog"))||void 0===r||null===(n=r.close)||void 0===n||n.call(r)})}),performance.now();let ek=S("historyState");(null==ek?void 0:ek.url)&&history.replaceState(ek,"",ek.url),x(location.hash.slice(1))})).apply(this,arguments)}function M(){[...document.body.children].filter(e=>e!==document.currentScript).forEach(e=>e.remove()),document.body.innerHTML=r,window.hwgInitialized=!1,b.forEach(e=>{document.removeEventListener(e,y)}),E()}function S(e,t){let n=localStorage[e];return"string"==typeof n?JSON.parse(n):null!=t?t:null}function L(e){delete localStorage[e]}function O(e,t){localStorage[e]=JSON.stringify(t)}function x(e){var t,n,r,o;e?null===(n=document.getElementById(e))||void 0===n||null===(t=n.show)||void 0===t||t.call(n):null===(o=document.querySelector("dialog[open]"))||void 0===o||null===(r=o.close)||void 0===r||r.call(o)}performance.now(),E(),window.addEventListener("popstate",e=>{x(e.state.open)})})();
//# sourceMappingURL=js.js.map