puzzle/build/public/assets/index.2f18bf13.js

1 line
45 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{d as e,c as t,a as n,w as o,b as l,r as i,o as s,e as a,t as r,F as d,f as c,g as u,v as p,h,i as g,j as y,k as m,l as f,m as w,n as v}from"./vendor.00b608ff.js";var x=e({name:"app",computed:{showNav(){return!["game","replay"].includes(String(this.$route.name))}}});const b={id:"app"},A={key:0,class:"nav"},z=a("Index"),T=a("New game");x.render=function(e,a,r,d,c,u){const p=i("router-link"),h=i("router-view");return s(),t("div",b,[e.showNav?(s(),t("ul",A,[n("li",null,[n(p,{class:"btn",to:{name:"index"}},{default:o((()=>[z])),_:1})]),n("li",null,[n(p,{class:"btn",to:{name:"new-game"}},{default:o((()=>[T])),_:1})])])):l("",!0),n(h)])};const S=864e5,C=e=>{const t=Math.floor(e/S);e%=S;const n=Math.floor(e/36e5);e%=36e5;const o=Math.floor(e/6e4);e%=6e4;return`${t}d ${n}h ${o}m ${Math.floor(e/1e3)}s`};var k=1e3,I=()=>{const e=new Date;return Date.UTC(e.getUTCFullYear(),e.getUTCMonth(),e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),e.getUTCMilliseconds())},P=(e,t)=>C(t-e),_=C,D=e({name:"game-teaser",props:{game:{type:Object,required:!0}},computed:{style(){return{"background-image":`url("${this.game.imageUrl.replace("uploads/","uploads/r/")+"-375x210.webp"}")`}}},methods:{time(e,t){const n=t?"🏁":"⏳",o=e,l=t||I();return`${n} ${P(o,l)}`}}});const B={class:"game-info-text"},O=n("br",null,null,-1),E=n("br",null,null,-1),M=n("br",null,null,-1);D.render=function(e,d,c,u,p,h){const g=i("router-link");return s(),t("div",{class:"game-teaser",style:e.style},[n(g,{class:"game-info",to:{name:"game",params:{id:e.game.id}}},{default:o((()=>[n("span",B,[a(" 🧩 "+r(e.game.tilesFinished)+"/"+r(e.game.tilesTotal),1),O,a(" 👥 "+r(e.game.players),1),E,a(" "+r(e.time(e.game.started,e.game.finished)),1),M])])),_:1},8,["to"]),l("",!0)],4)};var N=e({components:{GameTeaser:D},data:()=>({gamesRunning:[],gamesFinished:[]}),async created(){const e=await fetch("/api/index-data"),t=await e.json();this.gamesRunning=t.gamesRunning,this.gamesFinished=t.gamesFinished}});const U=n("h1",null,"Running games",-1),R=n("h1",null,"Finished games",-1);function $(e,t){const n=e.x-t.x,o=e.y-t.y;return Math.sqrt(n*n+o*o)}function G(e){return{x:e.x+e.w/2,y:e.y+e.h/2}}N.render=function(e,o,l,a,r,u){const p=i("game-teaser");return s(),t("div",null,[U,(s(!0),t(d,null,c(e.gamesRunning,((e,o)=>(s(),t("div",{class:"game-teaser-wrap",key:o},[n(p,{game:e},null,8,["game"])])))),128)),R,(s(!0),t(d,null,c(e.gamesFinished,((e,o)=>(s(),t("div",{class:"game-teaser-wrap",key:o},[n(p,{game:e},null,8,["game"])])))),128))])};var V={pointSub:function(e,t){return{x:e.x-t.x,y:e.y-t.y}},pointAdd:function(e,t){return{x:e.x+t.x,y:e.y+t.y}},pointDistance:$,pointInBounds:function(e,t){return e.x>=t.x&&e.x<=t.x+t.w&&e.y>=t.y&&e.y<=t.y+t.h},rectCenter:G,rectMoved:function(e,t,n){return{x:e.x+t,y:e.y+n,w:e.w,h:e.h}},rectCenterDistance:function(e,t){return $(G(e),G(t))},rectsOverlap:function(e,t){return!(t.x>e.x+e.w||e.x>t.x+t.w||t.y>e.y+e.h||e.y>t.y+t.h)}};var j=1,W=4,F=5,H=2,L=3,Q=6,Y=2,q=4,Z=3,K=9,J=1,X=2,ee=3,te=4,ne=5,oe=6,le=7,ie=8,se=10,ae=1,re=2,de=3;class ce{constructor(e){this.rand_high=e||3735929054,this.rand_low=1231121986^e}random(e,t){return this.rand_high=(this.rand_high<<16)+(this.rand_high>>16)+this.rand_low&4294967295,this.rand_low=this.rand_low+this.rand_high&4294967295,e+(this.rand_high>>>0)/4294967295*(t-e+1)|0}choice(e){return e[this.random(0,e.length-1)]}shuffle(e){const t=e.slice();for(let n=0;n<=t.length-2;n++){const e=this.random(n,t.length-1),o=t[n];t[n]=t[e],t[e]=o}return t}static serialize(e){return{rand_high:e.rand_high,rand_low:e.rand_low}}static unserialize(e){const t=new ce(0);return t.rand_high=e.rand_high,t.rand_low=e.rand_low,t}}const ue=(e,t)=>{const n=`${e}`;return n.length>=t.length?n:t.substr(0,t.length-n.length)+n},pe=(...e)=>{const t=t=>(...n)=>{const o=new Date,l=ue(o.getHours(),"00"),i=ue(o.getMinutes(),"00"),s=ue(o.getSeconds(),"00");console[t](`${l}:${i}:${s}`,...e,...n)};return{log:t("log"),error:t("error"),info:t("info")}};var he={hash:e=>{let t=0;for(let n=0;n<e.length;n++){t=(t<<5)-t+e.charCodeAt(n),t|=0}return t},uniqId:()=>Date.now().toString(36)+Math.random().toString(36).substring(2),encodeShape:function(e){return"number"==typeof e?e:e.top+1<<0|e.right+1<<2|e.bottom+1<<4|e.left+1<<6},decodeShape:function(e){return"number"!=typeof e?e:{top:(e>>0&3)-1,right:(e>>2&3)-1,bottom:(e>>4&3)-1,left:(e>>6&3)-1}},encodeTile:function(e){return Array.isArray(e)?e:[e.idx,e.pos.x,e.pos.y,e.z,e.owner,e.group]},decodeTile:function(e){return Array.isArray(e)?{idx:e[0],pos:{x:e[1],y:e[2]},z:e[3],owner:e[4],group:e[5]}:e},encodePlayer:function(e){return Array.isArray(e)?e:[e.id,e.x,e.y,e.d,e.name,e.color,e.bgcolor,e.points,e.ts]},decodePlayer:function(e){return Array.isArray(e)?{id:e[0],x:e[1],y:e[2],d:e[3],name:e[4],color:e[5],bgcolor:e[6],points:e[7],ts:e[8]}:e},encodeGame:function(e){return Array.isArray(e)?e:[e.id,e.rng.type,ce.serialize(e.rng.obj),e.puzzle,e.players,e.evtInfos,e.scoreMode]},decodeGame:function(e){return Array.isArray(e)?{id:e[0],rng:{type:e[1],obj:ce.unserialize(e[2])},puzzle:e[3],players:e[4],evtInfos:e[5],scoreMode:e[6]}:e},coordByTileIdx:function(e,t){const n=e.width/e.tileSize;return{x:t%n,y:Math.floor(t/n)}}};const ge={};function ye(e,t){return{id:e,x:0,y:0,d:0,name:null,color:null,bgcolor:null,points:0,ts:t}}function me(e,t){let n=0;for(let o of ge[e].players){if(he.decodePlayer(o).id===t)return n;n++}return-1}function fe(e,t){let n=me(e,t);return he.decodePlayer(ge[e].players[n])}function we(e,t,n){let o=me(e,t);-1===o?ge[e].players.push(he.encodePlayer(n)):ge[e].players[o]=he.encodePlayer(n)}function ve(e,t){return-1!==me(e,t)}function xe(e){return ge[e]?ge[e].players.map(he.decodePlayer):[]}function be(e){return ge[e].puzzle.tiles.length}function Ae(e){return ge[e].scoreMode||0}function ze(e){return Te(e)===be(e)}function Te(e){let t=0;for(let n of ge[e].puzzle.tiles)-1===he.decodeTile(n).owner&&t++;return t}function Se(e,t,n){const o=fe(e,t);for(let l of Object.keys(n))o[l]=n[l];we(e,t,o)}function Ce(e,t){for(let n of Object.keys(t))ge[e].puzzle.data[n]=t[n]}function ke(e,t,n){for(let o of Object.keys(n)){const l=he.decodeTile(ge[e].puzzle.tiles[t]);l[o]=n[o],ge[e].puzzle.tiles[t]=he.encodeTile(l)}}const Ie=(e,t)=>he.decodeTile(ge[e].puzzle.tiles[t]),Pe=(e,t)=>Ie(e,t).group,_e=(e,t)=>{const n=ge[e].puzzle.info,o={x:(n.table.width-n.width)/2,y:(n.table.height-n.height)/2},l=function(e,t){const n=ge[e].puzzle.info,o=he.coordByTileIdx(n,t),l=o.x*n.tileSize,i=o.y*n.tileSize;return{x:l,y:i}}(e,t);return V.pointAdd(o,l)},De=(e,t)=>Ie(e,t).pos,Be=e=>{const t=Qe(e),n=Ye(e),o=Math.round(t/4),l=Math.round(n/4);return{x:0-o,y:0-l,w:t+2*o,h:n+2*l}},Oe=(e,t)=>{const n=Ue(e),o=Ie(e,t);return{x:o.pos.x,y:o.pos.y,w:n,h:n}},Ee=(e,t)=>Ie(e,t).z,Me=(e,t)=>{for(let n of ge[e].puzzle.tiles){const e=he.decodeTile(n);if(e.owner===t)return e.idx}return-1},Ne=e=>ge[e].puzzle.info.tileDrawSize,Ue=e=>ge[e].puzzle.info.tileSize,Re=e=>ge[e].puzzle.data.maxGroup,$e=e=>ge[e].puzzle.data.maxZ;function Ge(e,t){const n=ge[e].puzzle.info,o=he.coordByTileIdx(n,t);return[o.y>0?t-n.tilesX:-1,o.x<n.tilesX-1?t+1:-1,o.y<n.tilesY-1?t+n.tilesX:-1,o.x>0?t-1:-1]}const Ve=(e,t,n)=>{for(let o of t)ke(e,o,{z:n})},je=(e,t,n)=>{const o=De(e,t);ke(e,t,{pos:V.pointAdd(o,n)})},We=(e,t,n)=>{const o=Ne(e),l=Be(e),i=n;for(let s of t){const t=Ie(e,s);t.pos.x+n.x<l.x?i.x=Math.max(l.x-t.pos.x,i.x):t.pos.x+o+n.x>l.x+l.w&&(i.x=Math.min(l.x+l.w-t.pos.x+o,i.x)),t.pos.y+n.y<l.y?i.y=Math.max(l.y-t.pos.y,i.y):t.pos.y+o+n.y>l.y+l.h&&(i.y=Math.min(l.y+l.h-t.pos.y+o,i.y))}for(let s of t)je(e,s,i)},Fe=(e,t,n)=>{for(let o of t)ke(e,o,{owner:n})};function He(e,t){const n=ge[e].puzzle.tiles,o=he.decodeTile(n[t]),l=[];if(o.group)for(let i of n){const e=he.decodeTile(i);e.group===o.group&&l.push(e.idx)}else l.push(o.idx);return l}const Le=(e,t)=>{const n=fe(e,t);return n?n.points:null},Qe=e=>ge[e].puzzle.info.table.width,Ye=e=>ge[e].puzzle.info.table.height;var qe={__createPlayerObject:ye,setGame:function(e,t){ge[e]=t},exists:function(e){return!!ge[e]||!1},playerExists:ve,getActivePlayers:function(e,t){const n=t-30*k;return xe(e).filter((e=>e.ts>=n))},getIdlePlayers:function(e,t){const n=t-30*k;return xe(e).filter((e=>e.ts<n&&e.points>0))},addPlayer:function(e,t,n){ve(e,t)?Se(e,t,{ts:n}):we(e,t,ye(t,n))},getFinishedTileCount:Te,getTileCount:be,getImageUrl:function(e){return ge[e].puzzle.info.imageUrl},setImageUrl:function(e,t){ge[e].puzzle.info.imageUrl=t},get:function(e){return ge[e]},getAllGames:function(){return Object.values(ge).sort(((e,t)=>ze(e.id)===ze(t.id)?t.puzzle.data.started-e.puzzle.data.started:ze(e.id)?1:-1))},getPlayerBgColor:(e,t)=>{const n=fe(e,t);return n?n.bgcolor:null},getPlayerColor:(e,t)=>{const n=fe(e,t);return n?n.color:null},getPlayerName:(e,t)=>{const n=fe(e,t);return n?n.name:null},getPlayerIndexById:me,getPlayerIdByIndex:function(e,t){return ge[e].players.length>t?he.decodePlayer(ge[e].players[t]).id:null},changePlayer:Se,setPlayer:we,setTile:function(e,t,n){ge[e].puzzle.tiles[t]=he.encodeTile(n)},setPuzzleData:function(e,t){ge[e].puzzle.data=t},getTableWidth:Qe,getTableHeight:Ye,getPuzzle:e=>ge[e].puzzle,getRng:e=>ge[e].rng.obj,getPuzzleWidth:e=>ge[e].puzzle.info.width,getPuzzleHeight:e=>ge[e].puzzle.info.height,getTilesSortedByZIndex:function(e){return ge[e].puzzle.tiles.map(he.decodeTile).sort(((e,t)=>e.z-t.z))},getFirstOwnedTile:(e,t)=>{const n=Me(e,t);return n<0?null:ge[e].puzzle.tiles[n]},getTileDrawOffset:e=>ge[e].puzzle.info.tileDrawOffset,getTileDrawSize:Ne,getFinalTilePos:_e,getStartTs:e=>ge[e].puzzle.data.started,getFinishTs:e=>ge[e].puzzle.data.finished,handleInput:function(e,t,n,o){const l=ge[e].puzzle,i=function(e,t){return t in ge[e].evtInfos?ge[e].evtInfos[t]:{_last_mouse:null,_last_mouse_down:null}}(e,t),s=[],a=()=>{s.push([ae,l.data])},r=t=>{s.push([re,he.encodeTile(Ie(e,t))])},d=e=>{for(const t of e)r(t)},c=()=>{s.push([de,he.encodePlayer(fe(e,t))])},u=n[0];if(u===oe){const l=n[1];Se(e,t,{bgcolor:l,ts:o}),c()}else if(u===le){const l=n[1];Se(e,t,{color:l,ts:o}),c()}else if(u===ie){const l=`${n[1]}`.substr(0,16);Se(e,t,{name:l,ts:o}),c()}else if(u===J){const l={x:n[1],y:n[2]};Se(e,t,{d:1,ts:o}),c(),i._last_mouse_down=l;const s=((e,t)=>{let n=ge[e].puzzle.info,o=ge[e].puzzle.tiles,l=-1,i=-1;for(let s=0;s<o.length;s++){const e=he.decodeTile(o[s]);if(0!==e.owner)continue;const a={x:e.pos.x,y:e.pos.y,w:n.tileSize,h:n.tileSize};V.pointInBounds(t,a)&&(-1===l||e.z>l)&&(l=e.z,i=s)}return i})(e,l);if(s>=0){let n=$e(e)+1;Ce(e,{maxZ:n}),a();const o=He(e,s);Ve(e,o,$e(e)),Fe(e,o,t),d(o)}i._last_mouse=l}else if(u===ee){const l=n[1],s=n[2],a={x:l,y:s};if(null===i._last_mouse_down)Se(e,t,{x:l,y:s,ts:o}),c();else{let n=Me(e,t);if(n>=0){Se(e,t,{x:l,y:s,ts:o}),c();const r=He(e,n);let u=V.pointInBounds(a,Be(e))&&V.pointInBounds(i._last_mouse_down,Be(e));for(let t of r){const n=Oe(e,t);if(V.pointInBounds(a,n)){u=!0;break}}if(u){const t=l-i._last_mouse_down.x,n=s-i._last_mouse_down.y;We(e,r,{x:t,y:n}),d(r)}}else Se(e,t,{ts:o}),c();i._last_mouse_down=a}i._last_mouse=a}else if(u===X){const s={x:n[1],y:n[2]},u=0;i._last_mouse_down=null;let p=Me(e,t);if(p>=0){let n=He(e,p);Fe(e,n,0),d(n);let i=De(e,p),s=_e(e,p);if(V.pointDistance(s,i)<l.info.snapDistance){let l=V.pointSub(s,i);We(e,n,l),((e,t)=>{for(let n of t)ke(e,n,{owner:-1,z:1})})(e,n),d(n);let r=Le(e,t);0===Ae(e)?r+=n.length:1===Ae(e)&&(r+=1),Se(e,t,{d:u,ts:o,points:r}),c(),Te(e)===be(e)&&(Ce(e,{finished:o}),a())}else{const n=(e,t,n,o)=>{let l=ge[e].puzzle.info;if(n<0)return!1;if(((e,t,n)=>{const o=Pe(e,t),l=Pe(e,n);return o&&o===l})(e,t,n))return!1;const i=De(e,t),s=V.pointAdd(De(e,n),{x:o[0]*l.tileSize,y:o[1]*l.tileSize});if(V.pointDistance(i,s)<l.snapDistance){let o=V.pointSub(s,i),l=He(e,t);We(e,l,o),((e,t,n)=>{const o=ge[e].puzzle.tiles,l=Pe(e,t),i=Pe(e,n);let s;const d=[];l&&d.push(l),i&&d.push(i),l?s=l:i?s=i:(Ce(e,{maxGroup:Re(e)+1}),a(),s=Re(e));if(ke(e,t,{group:s}),r(t),ke(e,n,{group:s}),r(n),d.length>0)for(const a of o){const t=he.decodeTile(a);d.includes(t.group)&&(ke(e,t.idx,{group:s}),r(t.idx))}})(e,t,n),l=He(e,t);const c=((e,t)=>{let n=0;for(let o of t){let t=Ee(e,o);t>n&&(n=t)}return n})(e,l);return Ve(e,l,c),d(l),!0}return!1};let l=!1;for(let t of He(e,p)){let o=Ge(e,t);if(n(e,t,o[0],[0,1])||n(e,t,o[1],[-1,0])||n(e,t,o[2],[0,-1])||n(e,t,o[3],[1,0])){l=!0;break}}if(l&&1===Ae(e)){const n=Le(e,t)+1;Se(e,t,{d:u,ts:o,points:n}),c()}else Se(e,t,{d:u,ts:o}),c()}}else Se(e,t,{d:u,ts:o}),c();i._last_mouse=s}else if(u===te){const l=n[1],s=n[2];Se(e,t,{x:l,y:s,ts:o}),c(),i._last_mouse={x:l,y:s}}else if(u===ne){const l=n[1],s=n[2];Se(e,t,{x:l,y:s,ts:o}),c(),i._last_mouse={x:l,y:s}}else Se(e,t,{ts:o}),c();return function(e,t,n){ge[e].evtInfos[t]=n}(e,t,i),s},SCORE_MODE_FINAL:0,SCORE_MODE_ANY:1},Ze=e({name:"upload",props:{accept:String,label:String},methods:{async upload(e){const t=e.target;if(!t.files)return;const n=t.files[0];if(!n)return;const o=new FormData;o.append("file",n,n.name);const l=await fetch("/upload",{method:"post",body:o}),i=await l.json();this.$emit("uploaded",i)}}});const Ke={class:"btn"};Ze.render=function(e,o,l,i,a,d){return s(),t("label",null,[n("input",{type:"file",style:{display:"none"},onChange:o[1]||(o[1]=(...t)=>e.upload&&e.upload(...t)),accept:e.accept},null,40,["accept"]),n("span",Ke,r(e.label||"Upload File"),1)])};var Je=e({name:"image-teaser",props:{image:{type:Object,required:!0}},computed:{style(){return{backgroundImage:`url("${this.image.url.replace("uploads/","uploads/r/")+"-150x100.webp"}")`}}},methods:{onClick(){this.$emit("click")}}});Je.render=function(e,n,o,l,i,a){return s(),t("div",{class:"imageteaser",style:e.style,onClick:n[1]||(n[1]=(...t)=>e.onClick&&e.onClick(...t))},null,4)};var Xe=e({name:"new-game-dialog",components:{Upload:Ze,ImageTeaser:Je},props:{images:Array},emits:{newGame:null},data:()=>({tiles:1e3,image:"",scoreMode:qe.SCORE_MODE_ANY}),methods:{mediaImgUploaded(e){this.image=e.image},canStartNewGame(){return!!(this.tilesInt&&this.image&&[0,1].includes(this.scoreModeInt))},onNewGameClick(){this.$emit("newGame",{tiles:this.tilesInt,image:this.image,scoreMode:this.scoreModeInt})}},computed:{scoreModeInt(){return parseInt(`${this.scoreMode}`,10)},tilesInt(){return parseInt(`${this.tiles}`,10)}}});const et=n("h1",null,"New game",-1),tt=n("td",null,[n("label",null,"Pieces: ")],-1),nt=n("td",null,[n("label",null,"Scoring: ")],-1),ot=a(" Any (Score when pieces are connected to each other or on final location)"),lt=n("br",null,null,-1),it=a(" Final (Score when pieces are put to their final location)"),st=n("td",null,[n("label",null,"Image: ")],-1),at={key:0},rt=a(" or "),dt={key:1},ct=a(" (or select from below) "),ut={colspan:"2"},pt=n("h1",null,"Image lib",-1);Xe.render=function(e,o,l,a,r,g){const y=i("upload"),m=i("image-teaser");return s(),t("div",null,[et,n("table",null,[n("tr",null,[tt,n("td",null,[u(n("input",{type:"text","onUpdate:modelValue":o[1]||(o[1]=t=>e.tiles=t)},null,512),[[p,e.tiles]])])]),n("tr",null,[nt,n("td",null,[n("label",null,[u(n("input",{type:"radio","onUpdate:modelValue":o[2]||(o[2]=t=>e.scoreMode=t),value:"1"},null,512),[[h,e.scoreMode]]),ot]),lt,n("label",null,[u(n("input",{type:"radio","onUpdate:modelValue":o[3]||(o[3]=t=>e.scoreMode=t),value:"0"},null,512),[[h,e.scoreMode]]),it])])]),n("tr",null,[st,n("td",null,[e.image?(s(),t("span",at,[n("img",{src:e.image.url,style:{width:"150px"}},null,8,["src"]),rt,n(y,{onUploaded:o[4]||(o[4]=t=>e.mediaImgUploaded(t)),accept:"image/*",label:"Upload an image"})])):(s(),t("span",dt,[n(y,{onUploaded:o[5]||(o[5]=t=>e.mediaImgUploaded(t)),accept:"image/*",label:"Upload an image"}),ct]))])]),n("tr",null,[n("td",ut,[n("button",{class:"btn",disabled:!e.canStartNewGame,onClick:o[6]||(o[6]=(...t)=>e.onNewGameClick&&e.onNewGameClick(...t))},"Start new game",8,["disabled"])])])]),pt,n("div",null,[(s(!0),t(d,null,c(e.images,((n,o)=>(s(),t(m,{image:n,onClick:t=>e.image=n,key:o},null,8,["image","onClick"])))),128))])])};var ht=e({components:{NewGameDialog:Xe},data:()=>({images:[]}),async created(){const e=await fetch("/api/newgame-data"),t=await e.json();this.images=t.images},methods:{async onNewGame(e){const t=await fetch("/newgame",{method:"post",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify(e)});if(200===t.status){const e=await t.json();this.$router.push({name:"game",params:{id:e.id}})}}}});ht.render=function(e,o,l,a,r,d){const c=i("new-game-dialog");return s(),t("div",null,[n(c,{images:e.images,onNewGame:e.onNewGame},null,8,["images","onNewGame"])])};var gt=e({name:"scores",props:{activePlayers:{type:Array,required:!0},idlePlayers:{type:Array,required:!0}},computed:{actives(){return this.activePlayers.sort(((e,t)=>t.points-e.points)),this.activePlayers},idles(){return this.idlePlayers.sort(((e,t)=>t.points-e.points)),this.idlePlayers}}});const yt={class:"scores"},mt=n("div",null,"Scores",-1),ft=n("td",null,"⚡",-1),wt=n("td",null,"💤",-1);gt.render=function(e,o,l,i,a,u){return s(),t("div",yt,[mt,n("table",null,[(s(!0),t(d,null,c(e.actives,((e,o)=>(s(),t("tr",{key:o,style:{color:e.color}},[ft,n("td",null,r(e.name),1),n("td",null,r(e.points),1)],4)))),128)),(s(!0),t(d,null,c(e.idles,((e,o)=>(s(),t("tr",{key:o,style:{color:e.color}},[wt,n("td",null,r(e.name),1),n("td",null,r(e.points),1)],4)))),128))])])};var vt=e({name:"puzzle-status",props:{finished:{type:Boolean,required:!0},duration:{type:Number,required:!0},piecesDone:{type:Number,required:!0},piecesTotal:{type:Number,required:!0}},computed:{icon(){return this.finished?"🏁":"⏳"},durationStr(){return _(this.duration)}}});const xt={class:"timer"};vt.render=function(e,o,l,i,a,d){return s(),t("div",xt,[n("div",null," 🧩 "+r(e.piecesDone)+"/"+r(e.piecesTotal),1),n("div",null,r(e.icon)+" "+r(e.durationStr),1),g(e.$slots,"default")])};var bt=e({name:"settings-overlay",emits:{bgclick:null,"update:modelValue":null},props:{modelValue:Object},created(){this.$watch("modelValue",(e=>{this.$emit("update:modelValue",e)}),{deep:!0})}});const At=n("td",null,[n("label",null,"Background: ")],-1),zt=n("td",null,[n("label",null,"Color: ")],-1),Tt=n("td",null,[n("label",null,"Name: ")],-1);bt.render=function(e,o,l,i,a,r){return s(),t("div",{class:"overlay transparent",onClick:o[5]||(o[5]=t=>e.$emit("bgclick"))},[n("table",{class:"overlay-content settings",onClick:o[4]||(o[4]=y((()=>{}),["stop"]))},[n("tr",null,[At,n("td",null,[u(n("input",{type:"color","onUpdate:modelValue":o[1]||(o[1]=t=>e.modelValue.background=t)},null,512),[[p,e.modelValue.background]])])]),n("tr",null,[zt,n("td",null,[u(n("input",{type:"color","onUpdate:modelValue":o[2]||(o[2]=t=>e.modelValue.color=t)},null,512),[[p,e.modelValue.color]])])]),n("tr",null,[Tt,n("td",null,[u(n("input",{type:"text",maxLength:"16","onUpdate:modelValue":o[3]||(o[3]=t=>e.modelValue.name=t)},null,512),[[p,e.modelValue.name]])])])])])};var St=e({name:"preview-overlay",props:{img:String},emits:{bgclick:null},computed:{previewStyle(){return{backgroundImage:`url('${this.img}')`}}}});const Ct={class:"preview"};St.render=function(e,o,l,i,a,r){return s(),t("div",{class:"overlay",onClick:o[1]||(o[1]=t=>e.$emit("bgclick"))},[n("div",Ct,[n("div",{class:"img",style:e.previewStyle},null,4)])])};const kt=pe("Communication.js");let It,Pt=e=>{},_t=e=>{};let Dt=0;const Bt=e=>{Dt!==e&&(Dt=e,_t(e))};function Ot(e){if(2===Dt)try{It.send(JSON.stringify(e))}catch(t){kt.info("unable to send message.. maybe because ws is invalid?")}}let Et,Mt;var Nt={connect:function(e,t,n){return Et=0,Mt={},Bt(3),new Promise((o=>{It=new WebSocket(e,n+"|"+t),It.onopen=e=>{Bt(2),Ot([L])},It.onmessage=e=>{const t=JSON.parse(e.data),l=t[0];if(l===W){const e=t[1];o(e)}else{if(l!==j)throw`[ 2021-05-09 invalid connect msgType ${l} ]`;{const e=t[1],o=t[2];if(e===n&&Mt[o])return void delete Mt[o];Pt(t)}}},It.onerror=e=>{throw Bt(1),"[ 2021-05-15 onerror ]"},It.onclose=e=>{4e3===e.code||1001===e.code?Bt(4):Bt(1)}}))},connectReplay:function(e,t,n){return Et=0,Mt={},Bt(3),new Promise((o=>{It=new WebSocket(e,n+"|"+t),It.onopen=e=>{Bt(2),Ot([Q])},It.onmessage=e=>{const t=JSON.parse(e.data),n=t[0];if(n!==F)throw`[ 2021-05-09 invalid connectReplay msgType ${n} ]`;{const e=t[1],n=t[2];o({game:e,log:n})}},It.onerror=e=>{throw Bt(1),"[ 2021-05-15 onerror ]"},It.onclose=e=>{4e3===e.code||1001===e.code?Bt(4):Bt(1)}}))},disconnect:function(){It&&It.close(4e3),Et=0,Mt={}},sendClientEvent:function(e){Et++,Mt[Et]=e,Ot([H,Et,Mt[Et]])},onServerChange:function(e){Pt=e},onConnectionStateChange:function(e){_t=e},CODE_CUSTOM_DISCONNECT:4e3,CONN_STATE_NOT_CONNECTED:0,CONN_STATE_DISCONNECTED:1,CONN_STATE_CLOSED:4,CONN_STATE_CONNECTED:2,CONN_STATE_CONNECTING:3},Ut=e({name:"connection-overlay",emits:{reconnect:null},props:{connectionState:Number},computed:{lostConnection(){return this.connectionState===Nt.CONN_STATE_DISCONNECTED},connecting(){return this.connectionState===Nt.CONN_STATE_CONNECTING},show(){return!(!this.lostConnection&&!this.connecting)}}});const Rt={key:0,class:"overlay connection-lost"},$t={key:0,class:"overlay-content"},Gt=n("div",null,"⁉️ LOST CONNECTION ⁉️",-1),Vt={key:1,class:"overlay-content"},jt=n("div",null,"Connecting...",-1);Ut.render=function(e,o,i,a,r,d){return e.show?(s(),t("div",Rt,[e.lostConnection?(s(),t("div",$t,[Gt,n("span",{class:"btn",onClick:o[1]||(o[1]=t=>e.$emit("reconnect"))},"Reconnect")])):l("",!0),e.connecting?(s(),t("div",Vt,[jt])):l("",!0)])):l("",!0)};var Wt=e({name:"help-overlay",emits:{bgclick:null}});const Ft=n("tr",null,[n("td",null,"⬆️ Move up:"),n("td",null,[n("div",null,[n("kbd",null,"W"),a("/"),n("kbd",null,"↑"),a("/🖱️")])])],-1),Ht=n("tr",null,[n("td",null,"⬇️ Move down:"),n("td",null,[n("div",null,[n("kbd",null,"S"),a("/"),n("kbd",null,"↓"),a("/🖱️")])])],-1),Lt=n("tr",null,[n("td",null,"⬅️ Move left:"),n("td",null,[n("div",null,[n("kbd",null,"A"),a("/"),n("kbd",null,"←"),a("/🖱️")])])],-1),Qt=n("tr",null,[n("td",null,"➡️ Move right:"),n("td",null,[n("div",null,[n("kbd",null,"D"),a("/"),n("kbd",null,"→"),a("/🖱️")])])],-1),Yt=n("tr",null,[n("td"),n("td",null,[n("div",null,[a("Move faster by holding "),n("kbd",null,"Shift")])])],-1),qt=n("tr",null,[n("td",null,"🔍+ Zoom in:"),n("td",null,[n("div",null,[n("kbd",null,"E"),a("/🖱️-Wheel")])])],-1),Zt=n("tr",null,[n("td",null,"🔍- Zoom out:"),n("td",null,[n("div",null,[n("kbd",null,"Q"),a("/🖱️-Wheel")])])],-1),Kt=n("tr",null,[n("td",null,"🖼️ Toggle preview:"),n("td",null,[n("div",null,[n("kbd",null,"Space")])])],-1),Jt=n("tr",null,[n("td",null,"🧩✔️ Toggle fixed pieces:"),n("td",null,[n("div",null,[n("kbd",null,"F")])])],-1),Xt=n("tr",null,[n("td",null,"🧩❓ Toggle loose pieces:"),n("td",null,[n("div",null,[n("kbd",null,"G")])])],-1);Wt.render=function(e,o,l,i,a,r){return s(),t("div",{class:"overlay transparent",onClick:o[2]||(o[2]=t=>e.$emit("bgclick"))},[n("table",{class:"overlay-content help",onClick:o[1]||(o[1]=y((()=>{}),["stop"]))},[Ft,Ht,Lt,Qt,Yt,qt,Zt,Kt,Jt,Xt])])};var en=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAW0lEQVQ4je1RywrAIAxLxP//5exixRWlVgZelpOKeTQFfnDypgy3eLIkSLLL8mxGPoHsU2hPAgDHBLvRX6hZZw/fwT0BtlLSONqCbWAmEIqMZOCDDlaDR3N03gOyDCn+y4DWmAAAAABJRU5ErkJggg=="}),tn=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAARElEQVQ4jWNgGAU0Af+hmBCbgYGBgYERhwHEAEYGBgYGJtIdiApYyLAZBVDsAqoagC1ACQJyY4ERg0GCISh6KA4DigEAou8LC+LnIJoAAAAASUVORK5CYII="}),nn=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAcUlEQVQ4ja1TQQ7AIAgD///n7jCozA2Hbk00jbG1KIrcARszTugoBs49qioZj7r2kKACptkyAOCJsJuA+GzglwHjvMSSWFVaENWVASxh5eRLiq5fN/ASjI89VcP2K3hHpq1cEXNaMfnrL3TDZP2tDuoOA6MzCCXWr38AAAAASUVORK5CYII="}),on=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAU0lEQVQ4jWNgoAH4D8X42HDARKlt5BoAd82AuQAOGLGIYQQUPv0wF5CiCQUge4EsQ5C9QI4BjMguwBYeBAElscCIy1ZivMKIwSDBEBQ9FCckigEAU3QOD7TGvY4AAAAASUVORK5CYII="});function ln(){let e=0,t=0,n=1;const o=(o,l)=>{e+=o/n,t+=l/n},l=e=>{const t=n+.05*n*("in"===e?1:-1);return Math.min(Math.max(t,.1),6)},i=o=>({x:o.x/n-e,y:o.y/n-t}),s=o=>({x:(o.x+e)*n,y:(o.y+t)*n}),a=e=>({w:e.w*n,h:e.h*n});return{move:o,canZoom:e=>n!=l(e),zoom:(e,t)=>((e,t)=>{if(n==e)return!1;const l=1-n/e;return o(-t.x*l,-t.y*l),n=e,!0})(l(e),t),worldToViewport:e=>{const{x:t,y:n}=s(e);return{x:Math.round(t),y:Math.round(n)}},worldToViewportRaw:s,worldDimToViewport:e=>{const{w:t,h:n}=a(e);return{w:Math.round(t),h:Math.round(n)}},worldDimToViewportRaw:a,viewportToWorld:e=>{const{x:t,y:n}=i(e);return{x:Math.round(t),y:Math.round(n)}},viewportToWorldRaw:i}}function sn(e=0,t=0){const n=document.createElement("canvas");return n.width=e,n.height=t,n}var an={createCanvas:sn,loadImageToBitmap:async function(e){return new Promise((t=>{const n=new Image;n.onload=()=>{createImageBitmap(n).then(t)},n.src=e}))},resizeBitmap:async function(e,t,n){const o=sn(t,n);return o.getContext("2d").drawImage(e,0,0,e.width,e.height,0,0,t,n),await createImageBitmap(o)},colorize:async function(e,t,n){const o=sn(e.width,e.height),l=o.getContext("2d");return l.save(),l.drawImage(t,0,0),l.fillStyle=n,l.globalCompositeOperation="source-in",l.fillRect(0,0,t.width,t.height),l.restore(),l.save(),l.globalCompositeOperation="destination-over",l.drawImage(e,0,0),l.restore(),await createImageBitmap(o)}};const rn=pe("Debug.js");let dn=0,cn=0;var un=e=>{dn=performance.now(),cn=e},pn=e=>{const t=performance.now(),n=t-dn;n>cn&&rn.log(e+": "+n),dn=t};const hn=pe("PuzzleGraphics.js");function gn(e,t){const n=he.coordByTileIdx(e,t);return{x:n.x*e.tileSize,y:n.y*e.tileSize,w:e.tileSize,h:e.tileSize}}var yn={loadPuzzleBitmaps:async function(e){const t=await an.loadImageToBitmap(e.info.imageUrl),n=await an.resizeBitmap(t,e.info.width,e.info.height);return await async function(e,t,n){hn.log("start createPuzzleTileBitmaps");var o=n.tileSize,l=n.tileMarginWidth,i=n.tileDrawSize,s=o/100,a=[0,0,40,15,37,5,37,5,40,0,38,-5,38,-5,20,-20,50,-20,50,-20,80,-20,62,-5,62,-5,60,0,63,5,63,5,65,15,100,0];const r=new Array(t.length),d={};function c(e){const t=`${e.top}${e.right}${e.left}${e.bottom}`;if(d[t])return d[t];const n=new Path2D,i={x:l,y:l},r=V.pointAdd(i,{x:o,y:0}),c=V.pointAdd(r,{x:0,y:o}),u=V.pointSub(c,{x:o,y:0});if(n.moveTo(i.x,i.y),0!==e.top)for(let o=0;o<a.length/6;o++){const t=V.pointAdd(i,{x:a[6*o+0]*s,y:e.top*a[6*o+1]*s}),l=V.pointAdd(i,{x:a[6*o+2]*s,y:e.top*a[6*o+3]*s}),r=V.pointAdd(i,{x:a[6*o+4]*s,y:e.top*a[6*o+5]*s});n.bezierCurveTo(t.x,t.y,l.x,l.y,r.x,r.y)}else n.lineTo(r.x,r.y);if(0!==e.right)for(let o=0;o<a.length/6;o++){const t=V.pointAdd(r,{x:-e.right*a[6*o+1]*s,y:a[6*o+0]*s}),l=V.pointAdd(r,{x:-e.right*a[6*o+3]*s,y:a[6*o+2]*s}),i=V.pointAdd(r,{x:-e.right*a[6*o+5]*s,y:a[6*o+4]*s});n.bezierCurveTo(t.x,t.y,l.x,l.y,i.x,i.y)}else n.lineTo(c.x,c.y);if(0!==e.bottom)for(let o=0;o<a.length/6;o++){let t=V.pointSub(c,{x:a[6*o+0]*s,y:e.bottom*a[6*o+1]*s}),l=V.pointSub(c,{x:a[6*o+2]*s,y:e.bottom*a[6*o+3]*s}),i=V.pointSub(c,{x:a[6*o+4]*s,y:e.bottom*a[6*o+5]*s});n.bezierCurveTo(t.x,t.y,l.x,l.y,i.x,i.y)}else n.lineTo(u.x,u.y);if(0!==e.left)for(let o=0;o<a.length/6;o++){let t=V.pointSub(u,{x:-e.left*a[6*o+1]*s,y:a[6*o+0]*s}),l=V.pointSub(u,{x:-e.left*a[6*o+3]*s,y:a[6*o+2]*s}),i=V.pointSub(u,{x:-e.left*a[6*o+5]*s,y:a[6*o+4]*s});n.bezierCurveTo(t.x,t.y,l.x,l.y,i.x,i.y)}else n.lineTo(i.x,i.y);return d[t]=n,n}const u=an.createCanvas(i,i),p=u.getContext("2d"),h=an.createCanvas(i,i),g=h.getContext("2d");for(let y of t){const t=he.decodeTile(y),o=gn(n,t.idx),s=c(he.decodeShape(n.shapes[t.idx]));p.clearRect(0,0,i,i),p.save(),p.lineWidth=2,p.stroke(s),p.globalCompositeOperation="source-in",p.drawImage(e,o.x-l,o.y-l,i,i,0,0,i,i),p.restore(),p.save(),p.globalCompositeOperation="source-in",p.globalAlpha=.2,p.fillStyle="black",p.fillRect(0,0,u.width,u.height),p.restore(),p.save(),p.clip(s),p.drawImage(e,o.x-l,o.y-l,i,i,0,0,i,i),p.restore(),p.save(),p.clip(s),p.strokeStyle="rgba(0,0,0,.4)",p.lineWidth=0,p.shadowColor="black",p.shadowBlur=2,p.shadowOffsetX=-1,p.shadowOffsetY=-1,p.stroke(s),p.restore(),p.save(),p.clip(s),p.strokeStyle="rgba(255,255,255,.4)",p.lineWidth=0,p.shadowColor="white",p.shadowBlur=2,p.shadowOffsetX=1,p.shadowOffsetY=1,p.stroke(s),p.restore(),g.clearRect(0,0,i,i),g.save(),g.lineWidth=1,g.stroke(s),g.globalCompositeOperation="source-in",g.drawImage(e,o.x-l,o.y-l,i,i,0,0,i,i),g.restore(),p.drawImage(h,0,0),r[t.idx]=await createImageBitmap(u)}return hn.log("end createPuzzleTileBitmaps"),r}(n,e.tiles,e.info)}};let mn=-10,fn=20,wn=2,vn=15;class xn{constructor(e){this.radius=10,this.previousRadius=10,this.explodingDuration=100,this.hasExploded=!1,this.alive=!0,this.color=function(e){return"rgba("+e.random(0,255)+","+e.random(0,255)+","+e.random(0,255)+", 0.8)"}(e),this.px=window.innerWidth/4+Math.random()*window.innerWidth/2,this.py=window.innerHeight,this.vx=mn+Math.random()*fn,this.vy=-1*(wn+Math.random()*vn),this.duration=0}update(e){if(this.hasExploded){const e=200-this.radius;this.previousRadius=this.radius,this.radius+=e/10,this.explodingDuration--,0==this.explodingDuration&&(this.alive=!1)}else this.vx+=0,this.vy+=1,this.vy>=0&&e&&this.explode(e),this.px+=this.vx,this.py+=this.vy}draw(e){e.beginPath(),e.arc(this.px,this.py,this.previousRadius,0,2*Math.PI,!1),this.hasExploded||(e.fillStyle=this.color,e.lineWidth=1,e.fill())}explode(e){this.hasExploded=!0;const t=3+Math.floor(3*Math.random());for(let n=0;n<t;n++){const t=10+Math.floor(21*Math.random()),n=5+5*Math.random(),o=2*Math.PI/t,l=Math.random()*o;for(let i=0;i<t;i++)e.push(new bn(this,i*o+l,n))}}}class bn{constructor(e,t,n){this.px=e.px,this.py=e.py,this.vx=Math.cos(t)*n,this.vy=Math.sin(t)*n,this.color=e.color,this.duration=40+Math.floor(20*Math.random()),this.alive=!0,this.radius=0}update(){this.vx+=0,this.vy+=.1,this.px+=this.vx,this.py+=this.vy,this.radius=3,this.duration--,this.duration<=0&&(this.alive=!1)}draw(e){e.beginPath(),e.arc(this.px,this.py,this.radius,0,2*Math.PI,!1),e.fillStyle=this.color,e.lineWidth=1,e.fill()}}class An{constructor(e,t){this.canvas=e,this.rng=t,this.ctx=this.canvas.getContext("2d"),this.resize(),this.readyBombs=[],this.explodedBombs=[],this.particles=[],window.addEventListener("resize",(()=>{this.resize()}))}setSpeedParams(){let e=0,t=0;for(;e<this.canvas.height&&t>=0;)t+=1,e+=t;wn=t/2,vn=t-wn;const n=1/4*this.canvas.width/(t/2);mn=-n,fn=2*n}resize(){this.setSpeedParams()}init(){this.readyBombs=[],this.explodedBombs=[],this.particles=[];for(let e=0;e<1;e++)this.readyBombs.push(new xn(this.rng))}update(){100*Math.random()<5&&this.readyBombs.push(new xn(this.rng));const e=[];for(;this.explodedBombs.length>0;){const t=this.explodedBombs.shift();if(!t)break;t.update(),t.alive&&e.push(t)}this.explodedBombs=e;const t=[];for(;this.readyBombs.length>0;){const e=this.readyBombs.shift();if(!e)break;e.update(this.particles),e.hasExploded?this.explodedBombs.push(e):t.push(e)}this.readyBombs=t;const n=[];for(;this.particles.length>0;){const e=this.particles.shift();if(!e)break;e.update(),e.alive&&n.push(e)}this.particles=n}render(){this.ctx.beginPath(),this.ctx.fillStyle="rgba(0, 0, 0, 0.1)",this.ctx.fillRect(0,0,this.canvas.width,this.canvas.height);for(let e=0;e<this.readyBombs.length;e++)this.readyBombs[e].draw(this.ctx);for(let e=0;e<this.explodedBombs.length;e++)this.explodedBombs[e].draw(this.ctx);for(let e=0;e<this.particles.length;e++)this.particles[e].draw(this.ctx)}}const zn={"./grab.png":en,"./grab_mask.png":tn,"./hand.png":nn,"./hand_mask.png":on};let Tn=!0,Sn=!0;let Cn=!0;async function kn(e,t,n,o,l,i){void 0===window.DEBUG&&(window.DEBUG=!1);const s=await an.loadImageToBitmap(zn["./grab.png"].default),a=await an.loadImageToBitmap(zn["./hand.png"].default),r=await an.loadImageToBitmap(zn["./grab_mask.png"].default),d=await an.loadImageToBitmap(zn["./hand_mask.png"].default),c=s.width,u=Math.round(c/2),p=s.height,h=Math.round(p/2),g={},y=async e=>{const t=e.color+" "+e.d;if(!g[t]){const n=e.d?s:a;if(e.color){const o=e.d?r:d;g[t]=await an.colorize(n,o,e.color)}else g[t]=n}return g[t]},m=function(e,t){return t.width=window.innerWidth,t.height=window.innerHeight,e.appendChild(t),window.addEventListener("resize",(()=>{t.width=window.innerWidth,t.height=window.innerHeight,Cn=!0})),t}(l,an.createCanvas()),f={log:[],logIdx:0,speeds:[.5,1,2,5,10,20,50],speedIdx:1,paused:!1,lastRealTs:0,lastGameTs:0,gameStartTs:0};Nt.onConnectionStateChange((e=>{i.setConnectionState(e)}));let w=()=>0;const v=async()=>{if("play"===o){const o=await Nt.connect(n,e,t),l=he.decodeGame(o);qe.setGame(l.id,l),w=()=>I()}else{if("replay"!==o)throw"[ 2020-12-22 MODE invalid, must be play|replay ]";{const o=await Nt.connectReplay(n,e,t),l=he.decodeGame(o.game);qe.setGame(l.id,l),f.log=o.log,f.lastRealTs=I(),f.gameStartTs=parseInt(f.log[0][f.log[0].length-2],10),f.lastGameTs=f.gameStartTs,w=()=>f.lastGameTs}}Cn=!0};await v();const x=qe.getTileDrawOffset(e),b=qe.getTileDrawSize(e),A=qe.getPuzzleWidth(e),z=qe.getPuzzleHeight(e),T=qe.getTableWidth(e),S=qe.getTableHeight(e),C={x:(T-A)/2,y:(S-z)/2},k={w:A,h:z},P={w:b,h:b},_=await yn.loadPuzzleBitmaps(qe.getPuzzle(e)),D=new An(m,qe.getRng(e));D.init();const B=m.getContext("2d");m.classList.add("loaded");const O=ln();O.move(-(T-m.width)/2,-(S-m.height)/2);const E=function(e,t,n){let o=[],l=!0,i=!1,s=!1,a=!1,r=!1,d=!1,c=!1,u=!1;const p=(e,t)=>{const o=n.viewportToWorld({x:e,y:t});return[o.x,o.y]},h=e=>p(e.offsetX,e.offsetY),g=()=>p(e.width/2,e.height/2),y=(e,t)=>{l&&("Shift"===t.key?u=e:"ArrowUp"===t.key||"w"===t.key||"W"===t.key?a=e:"ArrowDown"===t.key||"s"===t.key||"S"===t.key?r=e:"ArrowLeft"===t.key||"a"===t.key||"A"===t.key?i=e:"ArrowRight"===t.key||"d"===t.key||"D"===t.key?s=e:"q"===t.key?c=e:"e"===t.key&&(d=e))};e.addEventListener("mousedown",(e=>{0===e.button&&m([J,...h(e)])})),e.addEventListener("mouseup",(e=>{0===e.button&&m([X,...h(e)])})),e.addEventListener("mousemove",(e=>{m([ee,...h(e)])})),e.addEventListener("wheel",(e=>{if(n.canZoom(e.deltaY<0?"in":"out")){const t=e.deltaY<0?te:ne;m([t,...h(e)])}})),t.addEventListener("keydown",(e=>y(!0,e))),t.addEventListener("keyup",(e=>y(!1,e))),t.addEventListener("keypress",(e=>{l&&(" "===e.key&&m([se]),"F"!==e.key&&"f"!==e.key||(Tn=!Tn,Cn=!0),"G"!==e.key&&"g"!==e.key||(Sn=!Sn,Cn=!0))}));const m=e=>{o.push(e)};return{addEvent:m,consumeAll:()=>{if(0===o.length)return[];const e=o.slice();return o=[],e},createKeyEvents:()=>{const e=u?20:10,t=(i?e:0)-(s?e:0),o=(a?e:0)-(r?e:0);0===t&&0===o||m([K,t,o]),d&&c||(d?n.canZoom("in")&&m([te,...g()]):c&&n.canZoom("out")&&m([ne,...g()]))},setHotkeys:e=>{l=e}}}(m,window,O),M=qe.getImageUrl(e),N=()=>{const t=qe.getStartTs(e),n=qe.getFinishTs(e),o=w();i.setFinished(!!n),i.setDuration((n||o)-t)};N(),i.setPiecesDone(qe.getFinishedTileCount(e)),i.setPiecesTotal(qe.getTileCount(e));const U=w();i.setActivePlayers(qe.getActivePlayers(e,U)),i.setIdlePlayers(qe.getIdlePlayers(e,U));const R=!!qe.getFinishTs(e);let $=R;const G=()=>$&&!R,V=()=>qe.getPlayerBgColor(e,t)||localStorage.getItem("bg_color")||"#222222",j=()=>{i.setReplaySpeed&&i.setReplaySpeed(f.speeds[f.speedIdx]),i.setReplayPaused&&i.setReplayPaused(f.paused)};if("play"===o?setInterval(N,1e3):"replay"===o&&j(),"play"===o)Nt.onServerChange((n=>{n[0],n[1],n[2];const o=n[3];for(const[l,i]of o)switch(l){case de:{const n=he.decodePlayer(i);n.id!==t&&(qe.setPlayer(e,n.id,n),Cn=!0)}break;case re:{const t=he.decodeTile(i);qe.setTile(e,t.idx,t),Cn=!0}break;case ae:qe.setPuzzleData(e,i),Cn=!0}$=!!qe.getFinishTs(e)}));else if("replay"===o){let t=setInterval((()=>{const n=I();if(f.paused)return void(f.lastRealTs=n);const o=(n-f.lastRealTs)*f.speeds[f.speedIdx],l=f.lastGameTs+o;for(;;){if(f.paused)break;const n=f.logIdx+1;if(n>=f.log.length){clearInterval(t);break}const o=f.log[n],i=f.gameStartTs+o[o.length-1];if(i>l)break;const s=o.slice();if(s[0]===Y){const t=s[1];qe.addPlayer(e,t,i),Cn=!0}else if(s[0]===q){const t=qe.getPlayerIdByIndex(e,s[1]);qe.addPlayer(e,t,i),Cn=!0}else if(s[0]===Z){const t=qe.getPlayerIdByIndex(e,s[1]),n=s[2];qe.handleInput(e,t,n,i),Cn=!0}f.logIdx=n}f.lastRealTs=n,f.lastGameTs=l,N()}),50)}let W=null;return(e=>{const t=e.fps||60,n=e.slow||1,o=e.update,l=e.render,i=window.requestAnimationFrame,s=1/t,a=n*s;let r,d=0,c=window.performance.now();const u=()=>{for(r=window.performance.now(),d+=Math.min(1,(r-c)/1e3);d>a;)d-=a,o(s);l(d/n),c=r,i(u)};i(u)})({update:()=>{E.createKeyEvents();for(const n of E.consumeAll())if("play"===o){const o=n[0];if(o===K){const e=n[1],t=n[2];Cn=!0,O.move(e,t)}else if(o===ee){if(W&&!qe.getFirstOwnedTile(e,t)){const e={x:n[1],y:n[2]},t=O.worldToViewport(e),o=Math.round(t.x-W.x),l=Math.round(t.y-W.y);Cn=!0,O.move(o,l),W=t}}else if(o===J){const e={x:n[1],y:n[2]};W=O.worldToViewport(e)}else if(o===X)W=null;else if(o===te){const e={x:n[1],y:n[2]};Cn=!0,O.zoom("in",O.worldToViewport(e))}else if(o===ne){const e={x:n[1],y:n[2]};Cn=!0,O.zoom("out",O.worldToViewport(e))}else o===se&&i.togglePreview();const l=w();qe.handleInput(e,t,n,l).length>0&&(Cn=!0),Nt.sendClientEvent(n)}else if("replay"===o){const e=n[0];if(e===K){const e=n[1],t=n[2];Cn=!0,O.move(e,t)}else if(e===ee){if(W){const e={x:n[1],y:n[2]},t=O.worldToViewport(e),o=Math.round(t.x-W.x),l=Math.round(t.y-W.y);Cn=!0,O.move(o,l),W=t}}else if(e===J){const e={x:n[1],y:n[2]};W=O.worldToViewport(e)}else if(e===X)W=null;else if(e===te){const e={x:n[1],y:n[2]};Cn=!0,O.zoom("in",O.worldToViewport(e))}else if(e===ne){const e={x:n[1],y:n[2]};Cn=!0,O.zoom("out",O.worldToViewport(e))}else e===se&&i.togglePreview()}$=!!qe.getFinishTs(e),G()&&(D.update(),Cn=!0)},render:async()=>{if(!Cn)return;const n=w();let l,s,a;window.DEBUG&&un(0),B.fillStyle=V(),B.fillRect(0,0,m.width,m.height),window.DEBUG&&pn("clear done"),l=O.worldToViewportRaw(C),s=O.worldDimToViewportRaw(k),B.fillStyle="rgba(255, 255, 255, .3)",B.fillRect(l.x,l.y,s.w,s.h),window.DEBUG&&pn("board done");const r=qe.getTilesSortedByZIndex(e);window.DEBUG&&pn("get tiles done"),s=O.worldDimToViewportRaw(P);for(const e of r)(-1===e.owner?Tn:Sn)&&(a=_[e.idx],l=O.worldToViewportRaw({x:x+e.pos.x,y:x+e.pos.y}),B.drawImage(a,0,0,a.width,a.height,l.x,l.y,s.w,s.h));window.DEBUG&&pn("tiles done");const d=[];for(const i of qe.getActivePlayers(e,n))a=await y(i),l=O.worldToViewport(i),B.drawImage(a,l.x-u,l.y-h),c=i,("replay"===o||c.id!==t)&&d.push([`${i.name} (${i.points})`,l.x,l.y+p]);var c;B.fillStyle="white",B.textAlign="center";for(const[e,t,o]of d)B.fillText(e,t,o);window.DEBUG&&pn("players done"),i.setActivePlayers(qe.getActivePlayers(e,n)),i.setIdlePlayers(qe.getIdlePlayers(e,n)),i.setPiecesDone(qe.getFinishedTileCount(e)),window.DEBUG&&pn("HUD done"),G()&&D.render(),Cn=!1}}),{setHotkeys:e=>{E.setHotkeys(e)},onBgChange:e=>{localStorage.setItem("bg_color",e),E.addEvent([oe,e])},onColorChange:e=>{localStorage.setItem("player_color",e),E.addEvent([le,e])},onNameChange:e=>{localStorage.setItem("player_name",e),E.addEvent([ie,e])},replayOnSpeedUp:()=>{f.speedIdx+1<f.speeds.length&&(f.speedIdx++,j())},replayOnSpeedDown:()=>{f.speedIdx>=1&&(f.speedIdx--,j())},replayOnPauseToggle:()=>{f.paused=!f.paused,j()},previewImageUrl:M,player:{background:V(),color:qe.getPlayerColor(e,t)||localStorage.getItem("player_color")||"#ffffff",name:qe.getPlayerName(e,t)||localStorage.getItem("player_name")||"anon"},disconnect:Nt.disconnect,connect:v}}var In=e({name:"game",components:{PuzzleStatus:vt,Scores:gt,SettingsOverlay:bt,PreviewOverlay:St,ConnectionOverlay:Ut,HelpOverlay:Wt},data:()=>({activePlayers:[],idlePlayers:[],finished:!1,duration:0,piecesDone:0,piecesTotal:0,overlay:"",connectionState:0,g:{player:{background:"",color:"",name:""},previewImageUrl:"",setHotkeys:e=>{},onBgChange:e=>{},onColorChange:e=>{},onNameChange:e=>{},disconnect:()=>{},connect:()=>{}}}),async mounted(){this.$route.params.id&&(this.$watch((()=>this.g.player.background),(e=>{this.g.onBgChange(e)})),this.$watch((()=>this.g.player.color),(e=>{this.g.onColorChange(e)})),this.$watch((()=>this.g.player.name),(e=>{this.g.onNameChange(e)})),this.g=await kn(`${this.$route.params.id}`,this.$clientId,this.$config.WS_ADDRESS,"play",this.$el,{setActivePlayers:e=>{this.activePlayers=e},setIdlePlayers:e=>{this.idlePlayers=e},setFinished:e=>{this.finished=e},setDuration:e=>{this.duration=e},setPiecesDone:e=>{this.piecesDone=e},setPiecesTotal:e=>{this.piecesTotal=e},setConnectionState:e=>{this.connectionState=e},togglePreview:()=>{this.toggle("preview",!1)}}))},unmounted(){this.g.disconnect()},methods:{reconnect(){this.g.connect()},toggle(e,t){""===this.overlay?(this.overlay=e,t&&this.g.setHotkeys(!1)):(this.overlay="",t&&this.g.setHotkeys(!0))}}});const Pn={id:"game"},_n={class:"menu"},Dn={class:"tabs"},Bn=a("🧩 Puzzles");In.render=function(e,l,a,r,d,c){const p=i("settings-overlay"),h=i("preview-overlay"),g=i("help-overlay"),y=i("connection-overlay"),f=i("puzzle-status"),w=i("router-link"),v=i("scores");return s(),t("div",Pn,[u(n(p,{onBgclick:l[1]||(l[1]=t=>e.toggle("settings",!0)),modelValue:e.g.player,"onUpdate:modelValue":l[2]||(l[2]=t=>e.g.player=t)},null,8,["modelValue"]),[[m,"settings"===e.overlay]]),u(n(h,{onBgclick:l[3]||(l[3]=t=>e.toggle("preview",!1)),img:e.g.previewImageUrl},null,8,["img"]),[[m,"preview"===e.overlay]]),u(n(g,{onBgclick:l[4]||(l[4]=t=>e.toggle("help",!0))},null,512),[[m,"help"===e.overlay]]),n(y,{connectionState:e.connectionState,onReconnect:e.reconnect},null,8,["connectionState","onReconnect"]),n(f,{finished:e.finished,duration:e.duration,piecesDone:e.piecesDone,piecesTotal:e.piecesTotal},null,8,["finished","duration","piecesDone","piecesTotal"]),n("div",_n,[n("div",Dn,[n(w,{class:"opener",to:{name:"index"},target:"_blank"},{default:o((()=>[Bn])),_:1}),n("div",{class:"opener",onClick:l[5]||(l[5]=t=>e.toggle("preview",!1))},"🖼️ Preview"),n("div",{class:"opener",onClick:l[6]||(l[6]=t=>e.toggle("settings",!0))},"🛠️ Settings"),n("div",{class:"opener",onClick:l[7]||(l[7]=t=>e.toggle("help",!0))}," Help")])]),n(v,{activePlayers:e.activePlayers,idlePlayers:e.idlePlayers},null,8,["activePlayers","idlePlayers"])])};var On=e({name:"replay",components:{PuzzleStatus:vt,Scores:gt,SettingsOverlay:bt,PreviewOverlay:St,HelpOverlay:Wt},data:()=>({activePlayers:[],idlePlayers:[],finished:!1,duration:0,piecesDone:0,piecesTotal:0,overlay:"",connectionState:0,g:{player:{background:"",color:"",name:""},previewImageUrl:"",setHotkeys:e=>{},onBgChange:e=>{},onColorChange:e=>{},onNameChange:e=>{},replayOnSpeedUp:()=>{},replayOnSpeedDown:()=>{},replayOnPauseToggle:()=>{},disconnect:()=>{}},replay:{speed:1,paused:!1}}),async mounted(){this.$route.params.id&&(this.$watch((()=>this.g.player.background),(e=>{this.g.onBgChange(e)})),this.$watch((()=>this.g.player.color),(e=>{this.g.onColorChange(e)})),this.$watch((()=>this.g.player.name),(e=>{this.g.onNameChange(e)})),this.g=await kn(`${this.$route.params.id}`,this.$clientId,this.$config.WS_ADDRESS,"replay",this.$el,{setActivePlayers:e=>{this.activePlayers=e},setIdlePlayers:e=>{this.idlePlayers=e},setFinished:e=>{this.finished=e},setDuration:e=>{this.duration=e},setPiecesDone:e=>{this.piecesDone=e},setPiecesTotal:e=>{this.piecesTotal=e},togglePreview:()=>{this.toggle("preview",!1)},setConnectionState:e=>{this.connectionState=e},setReplaySpeed:e=>{this.replay.speed=e},setReplayPaused:e=>{this.replay.paused=e}}))},unmounted(){this.g.disconnect()},methods:{toggle(e,t){""===this.overlay?(this.overlay=e,t&&this.g.setHotkeys(!1)):(this.overlay="",t&&this.g.setHotkeys(!0))}},computed:{replayText(){return"Replay-Speed: "+this.replay.speed+"x"+(this.replay.paused?" Paused":"")}}});const En={id:"replay"},Mn={class:"menu"},Nn={class:"tabs"},Un=a("🧩 Puzzles");On.render=function(e,l,a,d,c,p){const h=i("settings-overlay"),g=i("preview-overlay"),y=i("help-overlay"),f=i("puzzle-status"),w=i("router-link"),v=i("scores");return s(),t("div",En,[u(n(h,{onBgclick:l[1]||(l[1]=t=>e.toggle("settings",!0)),modelValue:e.g.player,"onUpdate:modelValue":l[2]||(l[2]=t=>e.g.player=t)},null,8,["modelValue"]),[[m,"settings"===e.overlay]]),u(n(g,{onBgclick:l[3]||(l[3]=t=>e.toggle("preview",!1)),img:e.g.previewImageUrl},null,8,["img"]),[[m,"preview"===e.overlay]]),u(n(y,{onBgclick:l[4]||(l[4]=t=>e.toggle("help",!0))},null,512),[[m,"help"===e.overlay]]),n(f,{finished:e.finished,duration:e.duration,piecesDone:e.piecesDone,piecesTotal:e.piecesTotal},{default:o((()=>[n("div",null,[n("div",null,r(e.replayText),1),n("button",{class:"btn",onClick:l[5]||(l[5]=t=>e.g.replayOnSpeedUp())},"⏫"),n("button",{class:"btn",onClick:l[6]||(l[6]=t=>e.g.replayOnSpeedDown())},"⏬"),n("button",{class:"btn",onClick:l[7]||(l[7]=t=>e.g.replayOnPauseToggle())},"⏸️")])])),_:1},8,["finished","duration","piecesDone","piecesTotal"]),n("div",Mn,[n("div",Nn,[n(w,{class:"opener",to:{name:"index"},target:"_blank"},{default:o((()=>[Un])),_:1}),n("div",{class:"opener",onClick:l[8]||(l[8]=t=>e.toggle("preview",!1))},"🖼️ Preview"),n("div",{class:"opener",onClick:l[9]||(l[9]=t=>e.toggle("settings",!0))},"🛠️ Settings"),n("div",{class:"opener",onClick:l[10]||(l[10]=t=>e.toggle("help",!0))}," Help")])]),n(v,{activePlayers:e.activePlayers,idlePlayers:e.idlePlayers},null,8,["activePlayers","idlePlayers"])])},(async()=>{const e=await fetch("/api/conf"),t=await e.json();const n=f({history:w(),routes:[{name:"index",path:"/",component:N},{name:"new-game",path:"/new-game",component:ht},{name:"game",path:"/g/:id",component:In},{name:"replay",path:"/replay/:id",component:On}]});n.beforeEach(((e,t)=>{t.name&&document.documentElement.classList.remove(`view-${String(t.name)}`),document.documentElement.classList.add(`view-${String(e.name)}`)}));const o=v(x);o.config.globalProperties.$config=t,o.config.globalProperties.$clientId=function(){let e=localStorage.getItem("ID");return e||(e=he.uniqId(),localStorage.setItem("ID",e)),e}(),o.use(n),o.mount("#app")})();