1 line
51 KiB
JavaScript
1 line
51 KiB
JavaScript
import{d as e,c as t,a as n,w as o,b as l,r as i,o as a,e as s,t as r,F as d,f as c,g as u,v as p,h as g,i as h,j as y,k as m,l as f,m as w,n as v,p as b}from"./vendor.8616a479.js";var x=e({name:"app",computed:{showNav(){return!["game","replay"].includes(String(this.$route.name))}}});const C={id:"app"},A={key:0,class:"nav"},k=s("Index"),T=s("New game");x.render=function(e,s,r,d,c,u){const p=i("router-link"),g=i("router-view");return a(),t("div",C,[e.showNav?(a(),t("ul",A,[n("li",null,[n(p,{class:"btn",to:{name:"index"}},{default:o((()=>[k])),_:1})]),n("li",null,[n(p,{class:"btn",to:{name:"new-game"}},{default:o((()=>[T])),_:1})])])):l("",!0),n(g)])};const z=864e5,S=e=>{const t=Math.floor(e/z);e%=z;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 I=1e3,P=()=>{const e=new Date;return Date.UTC(e.getUTCFullYear(),e.getUTCMonth(),e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),e.getUTCMilliseconds())},_=(e,t)=>S(t-e),D=S,B=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||P();return`${n} ${_(o,l)}`}}});const O={class:"game-info-text"},E=n("br",null,null,-1),M=n("br",null,null,-1),N=n("br",null,null,-1);B.render=function(e,d,c,u,p,g){const h=i("router-link");return a(),t("div",{class:"game-teaser",style:e.style},[n(h,{class:"game-info",to:{name:"game",params:{id:e.game.id}}},{default:o((()=>[n("span",O,[s(" 🧩 "+r(e.game.tilesFinished)+"/"+r(e.game.tilesTotal),1),E,s(" 👥 "+r(e.game.players),1),M,s(" "+r(e.time(e.game.started,e.game.finished)),1),N])])),_:1},8,["to"]),l("",!0)],4)};var U=e({components:{GameTeaser:B},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 G=n("h1",null,"Running games",-1),R=n("h1",null,"Finished games",-1);U.render=function(e,o,l,s,r,u){const p=i("game-teaser");return a(),t("div",null,[G,(a(!0),t(d,null,c(e.gamesRunning,((e,o)=>(a(),t("div",{class:"game-teaser-wrap",key:o},[n(p,{game:e},null,8,["game"])])))),128)),R,(a(!0),t(d,null,c(e.gamesFinished,((e,o)=>(a(),t("div",{class:"game-teaser-wrap",key:o},[n(p,{game:e},null,8,["game"])])))),128))])};var $=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")}}});$.render=function(e,n,o,l,i,s){return a(),t("div",{class:"imageteaser",style:e.style,onClick:n[1]||(n[1]=(...t)=>e.onClick&&e.onClick(...t))},null,4)};var V=e({name:"image-library",components:{ImageTeaser:$},props:{images:{type:Array,required:!0}},emits:{imageClicked:null},methods:{imageClicked(e){this.$emit("imageClicked",e)}}});V.render=function(e,n,o,l,s,r){const u=i("image-teaser");return a(),t("div",null,[(a(!0),t(d,null,c(e.images,((n,o)=>(a(),t(u,{image:n,onClick:t=>e.imageClicked(n),key:o},null,8,["image","onClick"])))),128))])};var j=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 F={class:"btn"};j.render=function(e,o,l,i,s,d){return a(),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",F,r(e.label||"Upload File"),1)])};const W={name:"responsive-image",props:{src:String,title:{type:String,default:""},height:{type:String,default:"100%"},width:{type:String,default:"100%"}},computed:{style(){return{display:"inline-block",verticalAlign:"text-bottom",backgroundImage:`url('${this.src}')`,backgroundRepeat:"no-repeat",backgroundSize:"contain",backgroundPosition:"center",width:this.width,height:this.height}}}};W.render=function(e,n,o,l,i,s){return a(),t("div",{style:s.style,title:o.title},null,12,["title"])};var L=e({name:"new-image-dialog",components:{Upload:j,ResponsiveImage:W},emits:{bgclick:null,setupGameClick:null,postToGalleryClick:null},data:()=>({previewUrl:"",file:null,title:"",category:""}),computed:{canPostToGallery(){return!(!this.previewUrl||!this.file)},canSetupGameClick(){return!(!this.previewUrl||!this.file)}},methods:{preview(e){const t=e.target;if(!t.files)return;const n=t.files[0];if(!n)return;const o=new FileReader;o.readAsDataURL(n),o.onload=e=>{this.previewUrl=e.target.result,this.file=n}},postToGallery(){this.$emit("postToGalleryClick",{file:this.file,title:this.title,category:this.category})},setupGameClick(){this.$emit("setupGameClick",{file:this.file,title:this.title,category:this.category})}}});const H={key:0,class:"has-image"},Q={key:1},Y={class:"upload"},q={class:"btn"},Z={class:"area-settings"},K=n("td",null,[n("label",null,"Title")],-1),J=n("tr",null,[n("td",{colspan:"2"},[n("div",{class:"hint"},"Feel free to leave a credit to the artist/photographer in the title :)")])],-1),X=n("td",null,[n("label",null,"Category")],-1),ee={class:"area-buttons"},te=s("🧩 Post to gallery "),ne=n("br",null,null,-1),oe=s(" + set up game");function le(e,t){const n=e.x-t.x,o=e.y-t.y;return Math.sqrt(n*n+o*o)}function ie(e){return{x:e.x+e.w/2,y:e.y+e.h/2}}L.render=function(e,o,l,s,d,c){const h=i("responsive-image");return a(),t("div",{class:"overlay new-image-dialog",onClick:o[8]||(o[8]=t=>e.$emit("bgclick"))},[n("div",{class:"overlay-content",onClick:o[7]||(o[7]=g((()=>{}),["stop"]))},[n("div",{class:["area-image",{"has-image":!!e.previewUrl,"no-image":!e.previewUrl}]},[e.previewUrl?(a(),t("div",H,[n("span",{class:"remove btn",onClick:o[1]||(o[1]=t=>e.previewUrl="")},"X"),n(h,{src:e.previewUrl},null,8,["src"])])):(a(),t("div",Q,[n("label",Y,[n("input",{type:"file",style:{display:"none"},onChange:o[2]||(o[2]=(...t)=>e.preview&&e.preview(...t)),accept:"image/*"},null,32),n("span",q,r(e.label||"Upload File"),1)])]))],2),n("div",Z,[n("table",null,[n("tr",null,[K,n("td",null,[u(n("input",{type:"text","onUpdate:modelValue":o[3]||(o[3]=t=>e.title=t),placeholder:"Flower by @artist"},null,512),[[p,e.title]])])]),J,n("tr",null,[X,n("td",null,[u(n("input",{type:"text","onUpdate:modelValue":o[4]||(o[4]=t=>e.category=t),placeholder:"Plants"},null,512),[[p,e.category]])])])])]),n("div",ee,[n("button",{class:"btn",disabled:!e.canPostToGallery,onClick:o[5]||(o[5]=(...t)=>e.postToGallery&&e.postToGallery(...t))},"🖼️ Post to gallery",8,["disabled"]),n("button",{class:"btn",disabled:!e.canSetupGameClick,onClick:o[6]||(o[6]=(...t)=>e.setupGameClick&&e.setupGameClick(...t))},[te,ne,oe],8,["disabled"])])])])};var ae={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:le,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:ie,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 le(ie(e),ie(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 se=1,re=4,de=5,ce=2,ue=3,pe=6,ge=2,he=4,ye=3,me=9,fe=1,we=2,ve=3,be=4,xe=5,Ce=6,Ae=7,ke=8,Te=10,ze=1,Se=2,Ie=3;class Pe{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 Pe(0);return t.rand_high=e.rand_high,t.rand_low=e.rand_low,t}}const _e=(e,t)=>{const n=`${e}`;return n.length>=t.length?n:t.substr(0,t.length-n.length)+n},De=(...e)=>{const t=t=>(...n)=>{const o=new Date,l=_e(o.getHours(),"00"),i=_e(o.getMinutes(),"00"),a=_e(o.getSeconds(),"00");console[t](`${l}:${i}:${a}`,...e,...n)};return{log:t("log"),error:t("error"),info:t("info")}};var Be,Oe,Ee,Me,Ne={hash:e=>{let t=0;for(let n=0;n<e.length;n++){t=(t<<5)-t+e.charCodeAt(n),t|=0}return t},slug:e=>{let t=e.toLowerCase();return t=t.replace(/[^a-z0-9]+/g,"-"),t=t.replace(/^-|-$/,""),t},uniqId:()=>Date.now().toString(36)+Math.random().toString(36).substring(2),encodeShape:function(e){return e.top+1<<0|e.right+1<<2|e.bottom+1<<4|e.left+1<<6},decodeShape:function(e){return{top:(e>>0&3)-1,right:(e>>2&3)-1,bottom:(e>>4&3)-1,left:(e>>6&3)-1}},encodeTile:function(e){return[e.idx,e.pos.x,e.pos.y,e.z,e.owner,e.group]},decodeTile:function(e){return{idx:e[0],pos:{x:e[1],y:e[2]},z:e[3],owner:e[4],group:e[5]}},encodePlayer:function(e){return[e.id,e.x,e.y,e.d,e.name,e.color,e.bgcolor,e.points,e.ts]},decodePlayer:function(e){return{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]}},encodeGame:function(e){return Array.isArray(e)?e:[e.id,e.rng.type,Pe.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:Pe.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)}},asQueryArgs:function(e){const t=[];for(let n in e){const o=[n,e[n]].map(encodeURIComponent);t.push(o.join("="))}return 0===t.length?"":`?${t.join("&")}`}};(Oe=Be||(Be={}))[Oe.Flat=0]="Flat",Oe[Oe.Out=1]="Out",Oe[Oe.In=-1]="In",(Me=Ee||(Ee={}))[Me.FINAL=0]="FINAL",Me[Me.ANY=1]="ANY";const Ue={};function Ge(e,t){return{id:e,x:0,y:0,d:0,name:null,color:null,bgcolor:null,points:0,ts:t}}function Re(e,t){let n=0;for(let o of Ue[e].players){if(Ne.decodePlayer(o).id===t)return n;n++}return-1}function $e(e,t){const n=Re(e,t);return Ne.decodePlayer(Ue[e].players[n])}function Ve(e,t,n){const o=Re(e,t);-1===o?Ue[e].players.push(Ne.encodePlayer(n)):Ue[e].players[o]=Ne.encodePlayer(n)}function je(e,t){return-1!==Re(e,t)}function Fe(e){return Ue[e]?Ue[e].players.map(Ne.decodePlayer):[]}function We(e){return Ue[e].puzzle.tiles.length}function Le(e){return Ue[e].scoreMode||0}function He(e){return Qe(e)===We(e)}function Qe(e){let t=0;for(let n of Ue[e].puzzle.tiles)-1===Ne.decodeTile(n).owner&&t++;return t}function Ye(e,t,n){const o=$e(e,t);for(let l of Object.keys(n))o[l]=n[l];Ve(e,t,o)}function qe(e,t){for(let n of Object.keys(t))Ue[e].puzzle.data[n]=t[n]}function Ze(e,t,n){for(let o of Object.keys(n)){const l=Ne.decodeTile(Ue[e].puzzle.tiles[t]);l[o]=n[o],Ue[e].puzzle.tiles[t]=Ne.encodeTile(l)}}const Ke=(e,t)=>Ne.decodeTile(Ue[e].puzzle.tiles[t]),Je=(e,t)=>Ke(e,t).group,Xe=(e,t)=>{const n=Ue[e].puzzle.info,o={x:(n.table.width-n.width)/2,y:(n.table.height-n.height)/2},l=function(e,t){const n=Ue[e].puzzle.info,o=Ne.coordByTileIdx(n,t),l=o.x*n.tileSize,i=o.y*n.tileSize;return{x:l,y:i}}(e,t);return ae.pointAdd(o,l)},et=(e,t)=>Ke(e,t).pos,tt=e=>{const t=mt(e),n=ft(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}},nt=(e,t)=>{const n=at(e),o=Ke(e,t);return{x:o.pos.x,y:o.pos.y,w:n,h:n}},ot=(e,t)=>Ke(e,t).z,lt=(e,t)=>{for(let n of Ue[e].puzzle.tiles){const e=Ne.decodeTile(n);if(e.owner===t)return e.idx}return-1},it=e=>Ue[e].puzzle.info.tileDrawSize,at=e=>Ue[e].puzzle.info.tileSize,st=e=>Ue[e].puzzle.data.maxGroup,rt=e=>Ue[e].puzzle.data.maxZ;function dt(e,t){const n=Ue[e].puzzle.info,o=Ne.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 ct=(e,t,n)=>{for(let o of t)Ze(e,o,{z:n})},ut=(e,t,n)=>{const o=et(e,t);Ze(e,t,{pos:ae.pointAdd(o,n)})},pt=(e,t,n)=>{const o=it(e),l=tt(e),i=n;for(let a of t){const t=Ke(e,a);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 a of t)ut(e,a,i)},gt=(e,t,n)=>{for(let o of t)Ze(e,o,{owner:n})};function ht(e,t){const n=Ue[e].puzzle.tiles,o=Ne.decodeTile(n[t]),l=[];if(o.group)for(let i of n){const e=Ne.decodeTile(i);e.group===o.group&&l.push(e.idx)}else l.push(o.idx);return l}const yt=(e,t)=>{const n=$e(e,t);return n?n.points:0},mt=e=>Ue[e].puzzle.info.table.width,ft=e=>Ue[e].puzzle.info.table.height;var wt={__createPlayerObject:Ge,setGame:function(e,t){Ue[e]=t},exists:function(e){return!!Ue[e]||!1},playerExists:je,getActivePlayers:function(e,t){const n=t-30*I;return Fe(e).filter((e=>e.ts>=n))},getIdlePlayers:function(e,t){const n=t-30*I;return Fe(e).filter((e=>e.ts<n&&e.points>0))},addPlayer:function(e,t,n){je(e,t)?Ye(e,t,{ts:n}):Ve(e,t,Ge(t,n))},getFinishedTileCount:Qe,getTileCount:We,getImageUrl:function(e){return Ue[e].puzzle.info.imageUrl},setImageUrl:function(e,t){Ue[e].puzzle.info.imageUrl=t},get:function(e){return Ue[e]},getAllGames:function(){return Object.values(Ue).sort(((e,t)=>He(e.id)===He(t.id)?t.puzzle.data.started-e.puzzle.data.started:He(e.id)?1:-1))},getPlayerBgColor:(e,t)=>{const n=$e(e,t);return n?n.bgcolor:null},getPlayerColor:(e,t)=>{const n=$e(e,t);return n?n.color:null},getPlayerName:(e,t)=>{const n=$e(e,t);return n?n.name:null},getPlayerIndexById:Re,getPlayerIdByIndex:function(e,t){return Ue[e].players.length>t?Ne.decodePlayer(Ue[e].players[t]).id:null},changePlayer:Ye,setPlayer:Ve,setTile:function(e,t,n){Ue[e].puzzle.tiles[t]=Ne.encodeTile(n)},setPuzzleData:function(e,t){Ue[e].puzzle.data=t},getTableWidth:mt,getTableHeight:ft,getPuzzle:e=>Ue[e].puzzle,getRng:e=>Ue[e].rng.obj,getPuzzleWidth:e=>Ue[e].puzzle.info.width,getPuzzleHeight:e=>Ue[e].puzzle.info.height,getTilesSortedByZIndex:function(e){return Ue[e].puzzle.tiles.map(Ne.decodeTile).sort(((e,t)=>e.z-t.z))},getFirstOwnedTile:(e,t)=>{const n=lt(e,t);return n<0?null:Ue[e].puzzle.tiles[n]},getTileDrawOffset:e=>Ue[e].puzzle.info.tileDrawOffset,getTileDrawSize:it,getFinalTilePos:Xe,getStartTs:e=>Ue[e].puzzle.data.started,getFinishTs:e=>Ue[e].puzzle.data.finished,handleInput:function(e,t,n,o){const l=Ue[e].puzzle,i=function(e,t){return t in Ue[e].evtInfos?Ue[e].evtInfos[t]:{_last_mouse:null,_last_mouse_down:null}}(e,t),a=[],s=()=>{a.push([ze,l.data])},r=t=>{a.push([Se,Ne.encodeTile(Ke(e,t))])},d=e=>{for(const t of e)r(t)},c=()=>{a.push([Ie,Ne.encodePlayer($e(e,t))])},u=n[0];if(u===Ce){const l=n[1];Ye(e,t,{bgcolor:l,ts:o}),c()}else if(u===Ae){const l=n[1];Ye(e,t,{color:l,ts:o}),c()}else if(u===ke){const l=`${n[1]}`.substr(0,16);Ye(e,t,{name:l,ts:o}),c()}else if(u===fe){const l={x:n[1],y:n[2]};Ye(e,t,{d:1,ts:o}),c(),i._last_mouse_down=l;const a=((e,t)=>{let n=Ue[e].puzzle.info,o=Ue[e].puzzle.tiles,l=-1,i=-1;for(let a=0;a<o.length;a++){const e=Ne.decodeTile(o[a]);if(0!==e.owner)continue;const s={x:e.pos.x,y:e.pos.y,w:n.tileSize,h:n.tileSize};ae.pointInBounds(t,s)&&(-1===l||e.z>l)&&(l=e.z,i=a)}return i})(e,l);if(a>=0){let n=rt(e)+1;qe(e,{maxZ:n}),s();const o=ht(e,a);ct(e,o,rt(e)),gt(e,o,t),d(o)}i._last_mouse=l}else if(u===ve){const l=n[1],a=n[2],s={x:l,y:a};if(null===i._last_mouse_down)Ye(e,t,{x:l,y:a,ts:o}),c();else{let n=lt(e,t);if(n>=0){Ye(e,t,{x:l,y:a,ts:o}),c();const r=ht(e,n);let u=ae.pointInBounds(s,tt(e))&&ae.pointInBounds(i._last_mouse_down,tt(e));for(let t of r){const n=nt(e,t);if(ae.pointInBounds(s,n)){u=!0;break}}if(u){const t=l-i._last_mouse_down.x,n=a-i._last_mouse_down.y;pt(e,r,{x:t,y:n}),d(r)}}else Ye(e,t,{ts:o}),c();i._last_mouse_down=s}i._last_mouse=s}else if(u===we){const a={x:n[1],y:n[2]},u=0;i._last_mouse_down=null;let p=lt(e,t);if(p>=0){let n=ht(e,p);gt(e,n,0),d(n);let i=et(e,p),a=Xe(e,p);if(ae.pointDistance(a,i)<l.info.snapDistance){let l=ae.pointSub(a,i);pt(e,n,l),((e,t)=>{for(let n of t)Ze(e,n,{owner:-1,z:1})})(e,n),d(n);let r=yt(e,t);0===Le(e)?r+=n.length:1===Le(e)&&(r+=1),Ye(e,t,{d:u,ts:o,points:r}),c(),Qe(e)===We(e)&&(qe(e,{finished:o}),s())}else{const n=(e,t,n,o)=>{let l=Ue[e].puzzle.info;if(n<0)return!1;if(((e,t,n)=>{const o=Je(e,t),l=Je(e,n);return o&&o===l})(e,t,n))return!1;const i=et(e,t),a=ae.pointAdd(et(e,n),{x:o[0]*l.tileSize,y:o[1]*l.tileSize});if(ae.pointDistance(i,a)<l.snapDistance){let o=ae.pointSub(a,i),l=ht(e,t);pt(e,l,o),((e,t,n)=>{const o=Ue[e].puzzle.tiles,l=Je(e,t),i=Je(e,n);let a;const d=[];l&&d.push(l),i&&d.push(i),l?a=l:i?a=i:(qe(e,{maxGroup:st(e)+1}),s(),a=st(e));if(Ze(e,t,{group:a}),r(t),Ze(e,n,{group:a}),r(n),d.length>0)for(const s of o){const t=Ne.decodeTile(s);d.includes(t.group)&&(Ze(e,t.idx,{group:a}),r(t.idx))}})(e,t,n),l=ht(e,t);const c=((e,t)=>{let n=0;for(let o of t){let t=ot(e,o);t>n&&(n=t)}return n})(e,l);return ct(e,l,c),d(l),!0}return!1};let l=!1;for(let t of ht(e,p)){let o=dt(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===Le(e)){const n=yt(e,t)+1;Ye(e,t,{d:u,ts:o,points:n}),c()}else Ye(e,t,{d:u,ts:o}),c()}}else Ye(e,t,{d:u,ts:o}),c();i._last_mouse=a}else if(u===be){const l=n[1],a=n[2];Ye(e,t,{x:l,y:a,ts:o}),c(),i._last_mouse={x:l,y:a}}else if(u===xe){const l=n[1],a=n[2];Ye(e,t,{x:l,y:a,ts:o}),c(),i._last_mouse={x:l,y:a}}else Ye(e,t,{ts:o}),c();return function(e,t,n){Ue[e].evtInfos[t]=n}(e,t,i),a}},vt=e({name:"new-game-dialog",components:{ResponsiveImage:W},props:{image:{type:Object,required:!0}},emits:{newGame:null,bgclick:null},data:()=>({tiles:1e3,scoreMode:Ee.ANY}),methods:{onNewGameClick(){this.$emit("newGame",{tiles:this.tilesInt,image:this.image,scoreMode:this.scoreModeInt})}},computed:{canStartNewGame(){return!!(this.tilesInt&&this.image&&this.image.url&&[0,1].includes(this.scoreModeInt))},scoreModeInt(){return parseInt(`${this.scoreMode}`,10)},tilesInt(){return parseInt(`${this.tiles}`,10)}}});const bt={class:"area-image"},xt={class:"has-image"},Ct={class:"area-settings"},At=n("td",null,[n("label",null,"Pieces")],-1),kt=n("td",null,[n("label",null,"Scoring: ")],-1),Tt=s(" Any (Score when pieces are connected to each other or on final location)"),zt=n("br",null,null,-1),St=s(" Final (Score when pieces are put to their final location)"),It={class:"area-buttons"};vt.render=function(e,o,l,s,r,d){const c=i("responsive-image");return a(),t("div",{class:"overlay new-game-dialog",onClick:o[6]||(o[6]=t=>e.$emit("bgclick"))},[n("div",{class:"overlay-content",onClick:o[5]||(o[5]=g((()=>{}),["stop"]))},[n("div",bt,[n("div",xt,[n(c,{src:e.image.url,title:e.image.title},null,8,["src","title"])])]),n("div",Ct,[n("table",null,[n("tr",null,[At,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,[kt,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]]),Tt]),zt,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]]),St])])])])]),n("div",It,[n("button",{class:"btn",disabled:!e.canStartNewGame,onClick:o[4]||(o[4]=(...t)=>e.onNewGameClick&&e.onNewGameClick(...t))}," 🧩 Generate Puzzle ",8,["disabled"])])])])};var Pt=e({components:{ImageLibrary:V,NewImageDialog:L,NewGameDialog:vt},data:()=>({filters:{sort:"date_desc",category:""},images:[],categories:[],image:{id:0,filename:"",file:"",url:"",title:"",categories:[],created:0},dialog:""}),async created(){await this.loadImages()},methods:{async loadImages(){const e=await fetch(`/api/newgame-data${Ne.asQueryArgs(this.filters)}`),t=await e.json();this.images=t.images,this.categories=t.categories},async filtersChanged(){await this.loadImages()},imageClicked(e){this.image=e,this.dialog="new-game"},async uploadImage(e){const t=new FormData;t.append("file",e.file,e.file.name),t.append("title",e.title),t.append("category",e.category);const n=await fetch("/upload",{method:"post",body:t});return await n.json()},async postToGalleryClick(e){await this.uploadImage(e),this.dialog="",await this.loadImages()},async setupGameClick(e){const t=await this.uploadImage(e);this.loadImages(),this.image=t,this.dialog="new-game"},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}})}}}});const _t={class:"upload-image-teaser"},Dt=n("div",{class:"hint"},"(The image you upload will be added to the public gallery.)",-1),Bt={key:0},Ot=s(" Category: "),Et=n("option",{value:""},"All",-1),Mt=s(" Sort by: "),Nt=n("option",{value:"date_desc"},"Newest first",-1),Ut=n("option",{value:"date_asc"},"Oldest first",-1),Gt=n("option",{value:"alpha_asc"},"A-Z",-1),Rt=n("option",{value:"alpha_desc"},"Z-A",-1);Pt.render=function(e,o,s,p,g,h){const m=i("image-library"),f=i("new-image-dialog"),w=i("new-game-dialog");return a(),t("div",null,[n("div",_t,[n("div",{class:"btn btn-big",onClick:o[1]||(o[1]=t=>e.dialog="new-image")},"Upload your image"),Dt]),n("div",null,[e.categories.length>0?(a(),t("label",Bt,[Ot,u(n("select",{"onUpdate:modelValue":o[2]||(o[2]=t=>e.filters.category=t),onChange:o[3]||(o[3]=(...t)=>e.filtersChanged&&e.filtersChanged(...t))},[Et,(a(!0),t(d,null,c(e.categories,((e,n)=>(a(),t("option",{key:n,value:e.slug},r(e.title),9,["value"])))),128))],544),[[y,e.filters.category]])])):l("",!0),n("label",null,[Mt,u(n("select",{"onUpdate:modelValue":o[4]||(o[4]=t=>e.filters.sort=t),onChange:o[5]||(o[5]=(...t)=>e.filtersChanged&&e.filtersChanged(...t))},[Nt,Ut,Gt,Rt],544),[[y,e.filters.sort]])])]),n(m,{images:e.images,onImageClicked:e.imageClicked},null,8,["images","onImageClicked"]),"new-image"===e.dialog?(a(),t(f,{key:0,onBgclick:o[6]||(o[6]=t=>e.dialog=""),onPostToGalleryClick:e.postToGalleryClick,onSetupGameClick:e.setupGameClick},null,8,["onPostToGalleryClick","onSetupGameClick"])):l("",!0),e.image&&"new-game"===e.dialog?(a(),t(w,{key:1,onBgclick:o[7]||(o[7]=t=>e.dialog=""),onNewGame:e.onNewGame,image:e.image},null,8,["onNewGame","image"])):l("",!0)])};var $t=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 Vt={class:"scores"},jt=n("div",null,"Scores",-1),Ft=n("td",null,"⚡",-1),Wt=n("td",null,"💤",-1);$t.render=function(e,o,l,i,s,u){return a(),t("div",Vt,[jt,n("table",null,[(a(!0),t(d,null,c(e.actives,((e,o)=>(a(),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)),(a(!0),t(d,null,c(e.idles,((e,o)=>(a(),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 Lt=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 D(this.duration)}}});const Ht={class:"timer"};Lt.render=function(e,o,l,i,s,d){return a(),t("div",Ht,[n("div",null," 🧩 "+r(e.piecesDone)+"/"+r(e.piecesTotal),1),n("div",null,r(e.icon)+" "+r(e.durationStr),1),m(e.$slots,"default")])};var Qt=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 Yt=n("td",null,[n("label",null,"Background: ")],-1),qt=n("td",null,[n("label",null,"Color: ")],-1),Zt=n("td",null,[n("label",null,"Name: ")],-1);Qt.render=function(e,o,l,i,s,r){return a(),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]=g((()=>{}),["stop"]))},[n("tr",null,[Yt,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,[qt,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,[Zt,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 Kt=e({name:"preview-overlay",props:{img:String},emits:{bgclick:null},computed:{previewStyle(){return{backgroundImage:`url('${this.img}')`}}}});const Jt={class:"preview"};Kt.render=function(e,o,l,i,s,r){return a(),t("div",{class:"overlay",onClick:o[1]||(o[1]=t=>e.$emit("bgclick"))},[n("div",Jt,[n("div",{class:"img",style:e.previewStyle},null,4)])])};const Xt=De("Communication.js");let en,tn=e=>{},nn=e=>{};let on=0;const ln=e=>{on!==e&&(on=e,nn(e))};function an(e){if(2===on)try{en.send(JSON.stringify(e))}catch(t){Xt.info("unable to send message.. maybe because ws is invalid?")}}let sn,rn;var dn={connect:function(e,t,n){return sn=0,rn={},ln(3),new Promise((o=>{en=new WebSocket(e,n+"|"+t),en.onopen=e=>{ln(2),an([ue])},en.onmessage=e=>{const t=JSON.parse(e.data),l=t[0];if(l===re){const e=t[1];o(e)}else{if(l!==se)throw`[ 2021-05-09 invalid connect msgType ${l} ]`;{const e=t[1],o=t[2];if(e===n&&rn[o])return void delete rn[o];tn(t)}}},en.onerror=e=>{throw ln(1),"[ 2021-05-15 onerror ]"},en.onclose=e=>{4e3===e.code||1001===e.code?ln(4):ln(1)}}))},connectReplay:function(e,t,n){return sn=0,rn={},ln(3),new Promise((o=>{en=new WebSocket(e,n+"|"+t),en.onopen=e=>{ln(2),an([pe])},en.onmessage=e=>{const t=JSON.parse(e.data),n=t[0];if(n!==de)throw`[ 2021-05-09 invalid connectReplay msgType ${n} ]`;{const e=t[1],n=t[2];o({game:e,log:n})}},en.onerror=e=>{throw ln(1),"[ 2021-05-15 onerror ]"},en.onclose=e=>{4e3===e.code||1001===e.code?ln(4):ln(1)}}))},disconnect:function(){en&&en.close(4e3),sn=0,rn={}},sendClientEvent:function(e){sn++,rn[sn]=e,an([ce,sn,rn[sn]])},onServerChange:function(e){tn=e},onConnectionStateChange:function(e){nn=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},cn=e({name:"connection-overlay",emits:{reconnect:null},props:{connectionState:Number},computed:{lostConnection(){return this.connectionState===dn.CONN_STATE_DISCONNECTED},connecting(){return this.connectionState===dn.CONN_STATE_CONNECTING},show(){return!(!this.lostConnection&&!this.connecting)}}});const un={key:0,class:"overlay connection-lost"},pn={key:0,class:"overlay-content"},gn=n("div",null,"⁉️ LOST CONNECTION ⁉️",-1),hn={key:1,class:"overlay-content"},yn=n("div",null,"Connecting...",-1);cn.render=function(e,o,i,s,r,d){return e.show?(a(),t("div",un,[e.lostConnection?(a(),t("div",pn,[gn,n("span",{class:"btn",onClick:o[1]||(o[1]=t=>e.$emit("reconnect"))},"Reconnect")])):l("",!0),e.connecting?(a(),t("div",hn,[yn])):l("",!0)])):l("",!0)};var mn=e({name:"help-overlay",emits:{bgclick:null}});const fn=n("tr",null,[n("td",null,"⬆️ Move up:"),n("td",null,[n("div",null,[n("kbd",null,"W"),s("/"),n("kbd",null,"↑"),s("/🖱️")])])],-1),wn=n("tr",null,[n("td",null,"⬇️ Move down:"),n("td",null,[n("div",null,[n("kbd",null,"S"),s("/"),n("kbd",null,"↓"),s("/🖱️")])])],-1),vn=n("tr",null,[n("td",null,"⬅️ Move left:"),n("td",null,[n("div",null,[n("kbd",null,"A"),s("/"),n("kbd",null,"←"),s("/🖱️")])])],-1),bn=n("tr",null,[n("td",null,"➡️ Move right:"),n("td",null,[n("div",null,[n("kbd",null,"D"),s("/"),n("kbd",null,"→"),s("/🖱️")])])],-1),xn=n("tr",null,[n("td"),n("td",null,[n("div",null,[s("Move faster by holding "),n("kbd",null,"Shift")])])],-1),Cn=n("tr",null,[n("td",null,"🔍+ Zoom in:"),n("td",null,[n("div",null,[n("kbd",null,"E"),s("/🖱️-Wheel")])])],-1),An=n("tr",null,[n("td",null,"🔍- Zoom out:"),n("td",null,[n("div",null,[n("kbd",null,"Q"),s("/🖱️-Wheel")])])],-1),kn=n("tr",null,[n("td",null,"🖼️ Toggle preview:"),n("td",null,[n("div",null,[n("kbd",null,"Space")])])],-1),Tn=n("tr",null,[n("td",null,"🧩✔️ Toggle fixed pieces:"),n("td",null,[n("div",null,[n("kbd",null,"F")])])],-1),zn=n("tr",null,[n("td",null,"🧩❓ Toggle loose pieces:"),n("td",null,[n("div",null,[n("kbd",null,"G")])])],-1);mn.render=function(e,o,l,i,s,r){return a(),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]=g((()=>{}),["stop"]))},[fn,wn,vn,bn,xn,Cn,An,kn,Tn,zn])])};var Sn=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAW0lEQVQ4je1RywrAIAxLxP//5exixRWlVgZelpOKeTQFfnDypgy3eLIkSLLL8mxGPoHsU2hPAgDHBLvRX6hZZw/fwT0BtlLSONqCbWAmEIqMZOCDDlaDR3N03gOyDCn+y4DWmAAAAABJRU5ErkJggg=="}),In=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAARElEQVQ4jWNgGAU0Af+hmBCbgYGBgYERhwHEAEYGBgYGJtIdiApYyLAZBVDsAqoagC1ACQJyY4ERg0GCISh6KA4DigEAou8LC+LnIJoAAAAASUVORK5CYII="}),Pn=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAcUlEQVQ4ja1TQQ7AIAgD///n7jCozA2Hbk00jbG1KIrcARszTugoBs49qioZj7r2kKACptkyAOCJsJuA+GzglwHjvMSSWFVaENWVASxh5eRLiq5fN/ASjI89VcP2K3hHpq1cEXNaMfnrL3TDZP2tDuoOA6MzCCXWr38AAAAASUVORK5CYII="}),_n=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",default:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAU0lEQVQ4jWNgoAH4D8X42HDARKlt5BoAd82AuQAOGLGIYQQUPv0wF5CiCQUge4EsQ5C9QI4BjMguwBYeBAElscCIy1ZivMKIwSDBEBQ9FCckigEAU3QOD7TGvY4AAAAASUVORK5CYII="});function Dn(){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}),a=o=>({x:(o.x+e)*n,y:(o.y+t)*n}),s=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}=a(e);return{x:Math.round(t),y:Math.round(n)}},worldToViewportRaw:a,worldDimToViewport:e=>{const{w:t,h:n}=s(e);return{w:Math.round(t),h:Math.round(n)}},worldDimToViewportRaw:s,viewportToWorld:e=>{const{x:t,y:n}=i(e);return{x:Math.round(t),y:Math.round(n)}},viewportToWorldRaw:i}}function Bn(e=0,t=0){const n=document.createElement("canvas");return n.width=e,n.height=t,n}var On={createCanvas:Bn,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=Bn(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=Bn(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 En=De("Debug.js");let Mn=0,Nn=0;var Un=e=>{Mn=performance.now(),Nn=e},Gn=e=>{const t=performance.now(),n=t-Mn;n>Nn&&En.log(e+": "+n),Mn=t};const Rn=De("PuzzleGraphics.js");function $n(e,t){const n=Ne.coordByTileIdx(e,t);return{x:n.x*e.tileSize,y:n.y*e.tileSize,w:e.tileSize,h:e.tileSize}}var Vn={loadPuzzleBitmaps:async function(e){const t=await On.loadImageToBitmap(e.info.imageUrl),n=await On.resizeBitmap(t,e.info.width,e.info.height);return await async function(e,t,n){Rn.log("start createPuzzleTileBitmaps");var o=n.tileSize,l=n.tileMarginWidth,i=n.tileDrawSize,a=o/100,s=[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=ae.pointAdd(i,{x:o,y:0}),c=ae.pointAdd(r,{x:0,y:o}),u=ae.pointSub(c,{x:o,y:0});if(n.moveTo(i.x,i.y),0!==e.top)for(let o=0;o<s.length/6;o++){const t=ae.pointAdd(i,{x:s[6*o+0]*a,y:e.top*s[6*o+1]*a}),l=ae.pointAdd(i,{x:s[6*o+2]*a,y:e.top*s[6*o+3]*a}),r=ae.pointAdd(i,{x:s[6*o+4]*a,y:e.top*s[6*o+5]*a});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<s.length/6;o++){const t=ae.pointAdd(r,{x:-e.right*s[6*o+1]*a,y:s[6*o+0]*a}),l=ae.pointAdd(r,{x:-e.right*s[6*o+3]*a,y:s[6*o+2]*a}),i=ae.pointAdd(r,{x:-e.right*s[6*o+5]*a,y:s[6*o+4]*a});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<s.length/6;o++){let t=ae.pointSub(c,{x:s[6*o+0]*a,y:e.bottom*s[6*o+1]*a}),l=ae.pointSub(c,{x:s[6*o+2]*a,y:e.bottom*s[6*o+3]*a}),i=ae.pointSub(c,{x:s[6*o+4]*a,y:e.bottom*s[6*o+5]*a});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<s.length/6;o++){let t=ae.pointSub(u,{x:-e.left*s[6*o+1]*a,y:s[6*o+0]*a}),l=ae.pointSub(u,{x:-e.left*s[6*o+3]*a,y:s[6*o+2]*a}),i=ae.pointSub(u,{x:-e.left*s[6*o+5]*a,y:s[6*o+4]*a});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=On.createCanvas(i,i),p=u.getContext("2d"),g=On.createCanvas(i,i),h=g.getContext("2d");for(const y of t){const t=Ne.decodeTile(y),o=$n(n,t.idx),a=c(Ne.decodeShape(n.shapes[t.idx]));p.clearRect(0,0,i,i),p.save(),p.lineWidth=2,p.stroke(a),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(a),p.drawImage(e,o.x-l,o.y-l,i,i,0,0,i,i),p.restore(),p.save(),p.clip(a),p.strokeStyle="rgba(0,0,0,.4)",p.lineWidth=0,p.shadowColor="black",p.shadowBlur=2,p.shadowOffsetX=-1,p.shadowOffsetY=-1,p.stroke(a),p.restore(),p.save(),p.clip(a),p.strokeStyle="rgba(255,255,255,.4)",p.lineWidth=0,p.shadowColor="white",p.shadowBlur=2,p.shadowOffsetX=1,p.shadowOffsetY=1,p.stroke(a),p.restore(),h.clearRect(0,0,i,i),h.save(),h.lineWidth=1,h.stroke(a),h.globalCompositeOperation="source-in",h.drawImage(e,o.x-l,o.y-l,i,i,0,0,i,i),h.restore(),p.drawImage(g,0,0),r[t.idx]=await createImageBitmap(u)}return Rn.log("end createPuzzleTileBitmaps"),r}(n,e.tiles,e.info)}};let jn=-10,Fn=20,Wn=2,Ln=15;class Hn{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=jn+Math.random()*Fn,this.vy=-1*(Wn+Math.random()*Ln),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 Qn(this,i*o+l,n))}}}class Qn{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 Yn{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,Ln=t-Wn;const n=1/4*this.canvas.width/(t/2);jn=-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 Hn(this.rng))}update(){100*Math.random()<5&&this.readyBombs.push(new Hn(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 qn={"./grab.png":Sn,"./grab_mask.png":In,"./hand.png":Pn,"./hand_mask.png":_n};let Zn=!0,Kn=!0;let Jn=!0;async function Xn(e,t,n,o,l,i){void 0===window.DEBUG&&(window.DEBUG=!1);const a=await On.loadImageToBitmap(qn["./grab.png"].default),s=await On.loadImageToBitmap(qn["./hand.png"].default),r=await On.loadImageToBitmap(qn["./grab_mask.png"].default),d=await On.loadImageToBitmap(qn["./hand_mask.png"].default),c=a.width,u=Math.round(c/2),p=a.height,g=Math.round(p/2),h={},y=async e=>{const t=e.color+" "+e.d;if(!h[t]){const n=e.d?a:s;if(e.color){const o=e.d?r:d;h[t]=await On.colorize(n,o,e.color)}else h[t]=n}return h[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,Jn=!0})),t}(l,On.createCanvas()),f={log:[],logIdx:0,speeds:[.5,1,2,5,10,20,50],speedIdx:1,paused:!1,lastRealTs:0,lastGameTs:0,gameStartTs:0};dn.onConnectionStateChange((e=>{i.setConnectionState(e)}));let w=()=>0;const v=async()=>{if("play"===o){const o=await dn.connect(n,e,t),l=Ne.decodeGame(o);wt.setGame(l.id,l),w=()=>P()}else{if("replay"!==o)throw"[ 2020-12-22 MODE invalid, must be play|replay ]";{const o=await dn.connectReplay(n,e,t),l=Ne.decodeGame(o.game);wt.setGame(l.id,l),f.log=o.log,f.lastRealTs=P(),f.gameStartTs=parseInt(f.log[0][f.log[0].length-2],10),f.lastGameTs=f.gameStartTs,w=()=>f.lastGameTs}}Jn=!0};await v();const b=wt.getTileDrawOffset(e),x=wt.getTileDrawSize(e),C=wt.getPuzzleWidth(e),A=wt.getPuzzleHeight(e),k=wt.getTableWidth(e),T=wt.getTableHeight(e),z={x:(k-C)/2,y:(T-A)/2},S={w:C,h:A},I={w:x,h:x},_=await Vn.loadPuzzleBitmaps(wt.getPuzzle(e)),D=new Yn(m,wt.getRng(e));D.init();const B=m.getContext("2d");m.classList.add("loaded");const O=Dn();O.move(-(k-m.width)/2,-(T-m.height)/2);const E=function(e,t,n){let o=[],l=!0,i=!1,a=!1,s=!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]},g=e=>p(e.offsetX,e.offsetY),h=()=>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?s=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?a=e:"q"===t.key?c=e:"e"===t.key&&(d=e))};e.addEventListener("mousedown",(e=>{0===e.button&&m([fe,...g(e)])})),e.addEventListener("mouseup",(e=>{0===e.button&&m([we,...g(e)])})),e.addEventListener("mousemove",(e=>{m([ve,...g(e)])})),e.addEventListener("wheel",(e=>{if(n.canZoom(e.deltaY<0?"in":"out")){const t=e.deltaY<0?be:xe;m([t,...g(e)])}})),t.addEventListener("keydown",(e=>y(!0,e))),t.addEventListener("keyup",(e=>y(!1,e))),t.addEventListener("keypress",(e=>{l&&(" "===e.key&&m([Te]),"F"!==e.key&&"f"!==e.key||(Zn=!Zn,Jn=!0),"G"!==e.key&&"g"!==e.key||(Kn=!Kn,Jn=!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)-(a?e:0),o=(s?e:0)-(r?e:0);0===t&&0===o||m([me,t,o]),d&&c||(d?n.canZoom("in")&&m([be,...h()]):c&&n.canZoom("out")&&m([xe,...h()]))},setHotkeys:e=>{l=e}}}(m,window,O),M=wt.getImageUrl(e),N=()=>{const t=wt.getStartTs(e),n=wt.getFinishTs(e),o=w();i.setFinished(!!n),i.setDuration((n||o)-t)};N(),i.setPiecesDone(wt.getFinishedTileCount(e)),i.setPiecesTotal(wt.getTileCount(e));const U=w();i.setActivePlayers(wt.getActivePlayers(e,U)),i.setIdlePlayers(wt.getIdlePlayers(e,U));const G=!!wt.getFinishTs(e);let R=G;const $=()=>R&&!G,V=()=>wt.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)dn.onServerChange((n=>{n[0],n[1],n[2];const o=n[3];for(const[l,i]of o)switch(l){case Ie:{const n=Ne.decodePlayer(i);n.id!==t&&(wt.setPlayer(e,n.id,n),Jn=!0)}break;case Se:{const t=Ne.decodeTile(i);wt.setTile(e,t.idx,t),Jn=!0}break;case ze:wt.setPuzzleData(e,i),Jn=!0}R=!!wt.getFinishTs(e)}));else if("replay"===o){let t=setInterval((()=>{const n=P();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 a=o.slice();if(a[0]===ge){const t=a[1];wt.addPlayer(e,t,i),Jn=!0}else if(a[0]===he){const t=wt.getPlayerIdByIndex(e,a[1]);if(!t)throw"[ 2021-05-17 player not found (update player) ]";wt.addPlayer(e,t,i),Jn=!0}else if(a[0]===ye){const t=wt.getPlayerIdByIndex(e,a[1]);if(!t)throw"[ 2021-05-17 player not found (handle input) ]";const n=a[2];wt.handleInput(e,t,n,i),Jn=!0}f.logIdx=n}f.lastRealTs=n,f.lastGameTs=l,N()}),50)}let F=null;return(e=>{const t=e.fps||60,n=e.slow||1,o=e.update,l=e.render,i=window.requestAnimationFrame,a=1/t,s=n*a;let r,d=0,c=window.performance.now();const u=()=>{for(r=window.performance.now(),d+=Math.min(1,(r-c)/1e3);d>s;)d-=s,o(a);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===me){const e=n[1],t=n[2];Jn=!0,O.move(e,t)}else if(o===ve){if(F&&!wt.getFirstOwnedTile(e,t)){const e={x:n[1],y:n[2]},t=O.worldToViewport(e),o=Math.round(t.x-F.x),l=Math.round(t.y-F.y);Jn=!0,O.move(o,l),F=t}}else if(o===fe){const e={x:n[1],y:n[2]};F=O.worldToViewport(e)}else if(o===we)F=null;else if(o===be){const e={x:n[1],y:n[2]};Jn=!0,O.zoom("in",O.worldToViewport(e))}else if(o===xe){const e={x:n[1],y:n[2]};Jn=!0,O.zoom("out",O.worldToViewport(e))}else o===Te&&i.togglePreview();const l=w();wt.handleInput(e,t,n,l).length>0&&(Jn=!0),dn.sendClientEvent(n)}else if("replay"===o){const e=n[0];if(e===me){const e=n[1],t=n[2];Jn=!0,O.move(e,t)}else if(e===ve){if(F){const e={x:n[1],y:n[2]},t=O.worldToViewport(e),o=Math.round(t.x-F.x),l=Math.round(t.y-F.y);Jn=!0,O.move(o,l),F=t}}else if(e===fe){const e={x:n[1],y:n[2]};F=O.worldToViewport(e)}else if(e===we)F=null;else if(e===be){const e={x:n[1],y:n[2]};Jn=!0,O.zoom("in",O.worldToViewport(e))}else if(e===xe){const e={x:n[1],y:n[2]};Jn=!0,O.zoom("out",O.worldToViewport(e))}else e===Te&&i.togglePreview()}R=!!wt.getFinishTs(e),$()&&(D.update(),Jn=!0)},render:async()=>{if(!Jn)return;const n=w();let l,a,s;window.DEBUG&&Un(0),B.fillStyle=V(),B.fillRect(0,0,m.width,m.height),window.DEBUG&&Gn("clear done"),l=O.worldToViewportRaw(z),a=O.worldDimToViewportRaw(S),B.fillStyle="rgba(255, 255, 255, .3)",B.fillRect(l.x,l.y,a.w,a.h),window.DEBUG&&Gn("board done");const r=wt.getTilesSortedByZIndex(e);window.DEBUG&&Gn("get tiles done"),a=O.worldDimToViewportRaw(I);for(const e of r)(-1===e.owner?Zn:Kn)&&(s=_[e.idx],l=O.worldToViewportRaw({x:b+e.pos.x,y:b+e.pos.y}),B.drawImage(s,0,0,s.width,s.height,l.x,l.y,a.w,a.h));window.DEBUG&&Gn("tiles done");const d=[];for(const i of wt.getActivePlayers(e,n))s=await y(i),l=O.worldToViewport(i),B.drawImage(s,l.x-u,l.y-g),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&&Gn("players done"),i.setActivePlayers(wt.getActivePlayers(e,n)),i.setIdlePlayers(wt.getIdlePlayers(e,n)),i.setPiecesDone(wt.getFinishedTileCount(e)),window.DEBUG&&Gn("HUD done"),$()&&D.render(),Jn=!1}}),{setHotkeys:e=>{E.setHotkeys(e)},onBgChange:e=>{localStorage.setItem("bg_color",e),E.addEvent([Ce,e])},onColorChange:e=>{localStorage.setItem("player_color",e),E.addEvent([Ae,e])},onNameChange:e=>{localStorage.setItem("player_name",e),E.addEvent([ke,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:wt.getPlayerColor(e,t)||localStorage.getItem("player_color")||"#ffffff",name:wt.getPlayerName(e,t)||localStorage.getItem("player_name")||"anon"},disconnect:dn.disconnect,connect:v}}var eo=e({name:"game",components:{PuzzleStatus:Lt,Scores:$t,SettingsOverlay:Qt,PreviewOverlay:Kt,ConnectionOverlay:cn,HelpOverlay:mn},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 Xn(`${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 to={id:"game"},no={class:"menu"},oo={class:"tabs"},lo=s("🧩 Puzzles");eo.render=function(e,l,s,r,d,c){const p=i("settings-overlay"),g=i("preview-overlay"),h=i("help-overlay"),y=i("connection-overlay"),m=i("puzzle-status"),w=i("router-link"),v=i("scores");return a(),t("div",to,[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"]),[[f,"settings"===e.overlay]]),u(n(g,{onBgclick:l[3]||(l[3]=t=>e.toggle("preview",!1)),img:e.g.previewImageUrl},null,8,["img"]),[[f,"preview"===e.overlay]]),u(n(h,{onBgclick:l[4]||(l[4]=t=>e.toggle("help",!0))},null,512),[[f,"help"===e.overlay]]),n(y,{connectionState:e.connectionState,onReconnect:e.reconnect},null,8,["connectionState","onReconnect"]),n(m,{finished:e.finished,duration:e.duration,piecesDone:e.piecesDone,piecesTotal:e.piecesTotal},null,8,["finished","duration","piecesDone","piecesTotal"]),n("div",no,[n("div",oo,[n(w,{class:"opener",to:{name:"index"},target:"_blank"},{default:o((()=>[lo])),_: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 io=e({name:"replay",components:{PuzzleStatus:Lt,Scores:$t,SettingsOverlay:Qt,PreviewOverlay:Kt,HelpOverlay:mn},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 Xn(`${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 ao={id:"replay"},so={class:"menu"},ro={class:"tabs"},co=s("🧩 Puzzles");io.render=function(e,l,s,d,c,p){const g=i("settings-overlay"),h=i("preview-overlay"),y=i("help-overlay"),m=i("puzzle-status"),w=i("router-link"),v=i("scores");return a(),t("div",ao,[u(n(g,{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"]),[[f,"settings"===e.overlay]]),u(n(h,{onBgclick:l[3]||(l[3]=t=>e.toggle("preview",!1)),img:e.g.previewImageUrl},null,8,["img"]),[[f,"preview"===e.overlay]]),u(n(y,{onBgclick:l[4]||(l[4]=t=>e.toggle("help",!0))},null,512),[[f,"help"===e.overlay]]),n(m,{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",so,[n("div",ro,[n(w,{class:"opener",to:{name:"index"},target:"_blank"},{default:o((()=>[co])),_: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=w({history:v(),routes:[{name:"index",path:"/",component:U},{name:"new-game",path:"/new-game",component:Pt},{name:"game",path:"/g/:id",component:eo},{name:"replay",path:"/replay/:id",component:io}]});n.beforeEach(((e,t)=>{t.name&&document.documentElement.classList.remove(`view-${String(t.name)}`),document.documentElement.classList.add(`view-${String(e.name)}`)}));const o=b(x);o.config.globalProperties.$config=t,o.config.globalProperties.$clientId=function(){let e=localStorage.getItem("ID");return e||(e=Ne.uniqId(),localStorage.setItem("ID",e)),e}(),o.use(n),o.mount("#app")})();
|