@onceworlds/cli 0.11.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/cli.js +92 -19
- package/dist/emulator.js +211 -150
- package/dist/sdk.js +3 -3
- package/package.json +1 -1
package/dist/sdk.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";(()=>{var pn=Object.defineProperty;var fn=(n,e,t)=>e in n?pn(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var p=(n,e,t)=>fn(n,typeof e!="symbol"?e+"":e,t);var gn=["#f6dcc5","#ebc29f","#d4a07a","#b07a55","#875839","#5e3d29","#93c9bb","#a8c595","#94bcd3","#ab9bd9","#dca3ad","#b3bccb"],Ve=["#c95f56","#dc8a4f","#e2bd52","#9cc27a","#5a8a5e","#4f9a8e","#6fb7c8","#5b87c5","#465089","#8b68a8","#dc8fae","#f2eee6","#a9a8ad","#5d5c63","#2b2a30","#80604d"],hn=["Happy","Grin","Shades","Wink","Surprised","Determined","Joyful","Shy"],yn=["None","Sprout","Cap","Beanie","Crown","Headphones","Horns","Antenna","Bow","Halo","Party hat","Cat ears","Short hair","Tuft","Bob","Spikes","Bun","Curls","Long","Mop"];var bn=["Plain","Band","Spots","Stripes","Star"];var wn={skin:gn.length,shirt:Ve.length,pants:Ve.length,face:hn.length,hat:yn.length,top:bn.length};function We(n){if(!n||typeof n!="object")return null;let e=n,t={};for(let[r,s]of Object.entries(wn)){let o=e[r],i=typeof o=="number"?o:typeof o=="string"&&/^[0-9]{1,3}$/.test(o)?Number(o):NaN;if(!Number.isInteger(i)||i<0||i>=s)return null;t[r]=i}return t}var S={roomMaxPlayersDefault:16,roomMaxPlayersCap:30,teamsMax:8,messageBytes:16*1024,presenceBytes:1024,stateKeyLength:64,stateValueBytes:16*1024,stateKeys:256,privateKeys:16,privateValueBytes:4*1024,privateBytesPerPlayer:8*1024,messagesPerSecond:60,messagesPerSecondMin:10,roomBytesPerSecond:128*1024,roomBytesPerSecondMin:16*1024,presenceHz:20,presenceHzMin:5,roomMessagesPerSecond:900,roomBytesPerSecondTotal:2*1024*1024,matchCountdownMs:3e3,matchCountdownMaxMs:1e4,matchIdleMs:45e3,matchAbandonMs:12e4,idleAfterMs:3e4,idleAbandonMs:5*6e4,rankedPairMatchesPerDay:5,chatLength:200,chatIntervalMs:750,chatHistory:50,chatChannelsPerPlayer:10,saveValueBytes:64*1024,saveKeysPerGame:100,saveBytesPerGame:1024*1024,guestSaveBytesPerGame:128*1024,serverDataKeys:128,serverDataBytes:512*1024,serverDataServersPerGame:5e3,serverDataBytesPerGame:64*1024*1024,serverDataIdleDays:120,gameDataKeys:32,gameDataListMax:100,gameDataItemBytes:1024,gameDataValueBytes:8*1024,gameDataMapFields:1e3,gameDataMapPerPlayer:100,gameDataNumberMax:1e15,gameDataWritesPerMinute:30,deployFiles:2e3,deployFileBytes:25*1024*1024,deployHtmlBytes:10*1024*1024,deployPathLength:200,deployTotalBytes:100*1024*1024,envVars:50,envValueBytes:4*1024,envTotalBytes:16*1024,proxyRequestBytes:1024*1024,proxyTextRequestBytes:256*1024,proxySpeechRequestBytes:16*1024,proxyResponseBytes:10*1024*1024,proxyMaxOutputTokens:4096,proxyCallsPerDay:2e3,proxyCallsPerDayMax:1e5,proxyCallsPerPlayerPerDay:100,proxyCallsPerNetworkPerDay:1e3,proxyMediaCallsPerDay:100,proxyMediaCallsPerDayMax:1e4,proxyMediaCallsPerPlayerPerDay:20,proxyMediaCallsPerNetworkPerDay:25,proxyUnitsPerDay:2e6,proxyUnitsPerDayMax:1e8,proxyUnitsPerPlayerPerDay:25e4,proxyUnitsPerGuestPerDay:1e5,proxyUnitsPerNetworkPerDay:5e5,errorReportsPerPage:10,consoleLineLength:2e3,consoleLinesPerSecond:50,gameNameLength:60,gameDescriptionLength:1e3,galleryItems:5,mediaImageBytes:5*1024*1024,mediaVideoBytes:30*1024*1024,mediaGifBytes:1024*1024,mediaGifSide:512,mediaUploadsPerDay:200,mediaBytesPerDay:500*1024*1024,badgesPerGame:50,controlHints:12,controlHintKeys:4,controlKeyLength:12,controlActionLength:40,helpLength:400,badgeNameLength:50,badgeDescriptionLength:200,leaderboardsPerGame:25,leaderboardTop:100,gamesPerOwner:50,gamesCreatedPerCreatorPerDay:3,uploadAccountAgeMs:10080*6e4,deploysPerOwnerPerDay:100,draftsPerOwnerPerDay:1e3,draftsKeptPerGame:3,cliTokensPerAccount:10,cliDeviceCodeSeconds:600,deploymentsKeptPerGame:10,deployBytesPerOwnerPerDay:2*1024*1024*1024,deployEntriesPerOwnerPerDay:1e5,bioLength:500,exportRows:2e3,exportSaveChars:4*1024*1024,exportEveryMs:60*6e4,friendsMax:200,friendRequestsMax:100,friendRequestsIncomingMax:50,friendRequestsPerDay:20,friendRequestsPerNetworkPerDay:120,friendRequestPushesPerDay:5,passwordMinLength:8,passwordMaxLength:72,signupsPerNetworkPerDay:100,signupsPerWidePerDay:300,guestsPerNetworkPerDay:300,guestsPerWidePerDay:1500,visitsPerNetworkPerDay:10,visitSeconds:10,errorsPerDeployment:100,controlButtons:4,controlLabelLength:12,partySize:6,partyChatHistory:50,messageLength:500,messagesPerPage:50,groupChatMembers:20,groupChatNameLength:40,conversationsListed:50,notificationsListed:50,invitesPerRequest:10,inviteLiveMs:30*6e4,partyInviteMs:60*6e4};var kn="host:",H="data:";function J(n){return n.startsWith(kn)||n.startsWith(H)}var Ye=/^(?!__proto__$)[A-Za-z0-9_.:-]{1,59}$/;function ke(n){return n.startsWith(H)}function ve(n){return H+n}function Qe(n){return n.startsWith("private:")||n.startsWith("room:")}function Se(n){let e=n.length;for(let t=0;t<n.length;t++){let r=n.charCodeAt(t);r<128||(r<2048?e+=1:r>=55296&&r<=56319&&t+1<n.length&&(n.charCodeAt(t+1)&64512)===56320?(e+=2,t++):e+=2)}return e}var xe=/^[A-Za-z][A-Za-z0-9_.-]{0,31}$/;var vn={counter:["add"],list:["push"],record:["best"],map:["claim","release"],value:["set"]},Sn={add:"add(key, n)",push:"push(key, item)",best:"best(key, n)",claim:"claim(key, field)",release:"release(key, field)",set:"set(key, value)"};function xn(n){return vn[n].map(e=>`ow.data.game.${Sn[e]}`).join(" or ")}function Me(n,e){let r=Math.floor(e/864e5);if(n==="daily")return{start:r*864e5,next:(r+1)*864e5};let s=r-(r+3)%7;return{start:s*864e5,next:(s+7)*864e5}}function Mn(n,e){switch(n.type){case"counter":return{type:"counter",value:n.min,...n.reset?{resetsAt:Me(n.reset,e).next}:{}};case"list":return{type:"list",items:[]};case"record":return{type:"record",value:null,by:null,at:null};case"map":return{type:"map",fields:{}};case"value":return{type:"value",value:null,at:null}}}function Rn(n,e,t){if(!e||e.type!==n.type)return Mn(n,t);if(e.type==="counter"&&n.type==="counter"){if(!n.reset)return{type:"counter",value:e.value};let o=Me(n.reset,t);return{type:"counter",value:(e.period??0)>=o.start?e.value:n.min,resetsAt:o.next}}let{period:r,...s}=e;return s}function Ze(n,e,t,r,s){if(r.guest&&!n.guests)return{code:"sign_in",message:`Log in to change '${t.key}': guests can't (a ${n.type} is for signed-in players unless onceworlds.json says "guests": true).`};let o={id:r.id,name:r.name},i=Rn(n,e,s);switch(i.type){case"counter":{if(t.op!=="add"||n.type!=="counter")break;let l=Math.min(n.max,Math.max(n.min,i.value+t.n)),c=n.reset?Me(n.reset,s):null;return{next:{type:"counter",value:l,...c?{resetsAt:c.next,period:c.start}:{}},result:{type:"counter",value:l}}}case"list":{if(t.op!=="push"||n.type!=="list")break;return{next:{type:"list",items:[{value:t.item,by:o,at:s},...i.items].slice(0,n.max)},result:{type:"list",ok:!0}}}case"record":{if(t.op!=="best"||n.type!=="record")break;return i.value!==null&&i.by&&i.at!==null&&!(n.order==="max"?t.n>i.value:t.n<i.value)?{next:i,result:{type:"record",value:i.value,by:i.by,at:i.at,record:!1}}:{next:{type:"record",value:t.n,by:o,at:s},result:{type:"record",value:t.n,by:o,at:s,record:!0}}}case"map":{if(t.op!=="claim"&&t.op!=="release"||n.type!=="map")break;let l={...i.fields},c=Object.hasOwn(l,t.field)?l[t.field]:void 0;return t.op==="release"?!c||c.by.id!==r.id?{next:i,result:{type:"map",ok:!1,...c?{holder:c.by}:{}}}:(delete l[t.field],{next:{type:"map",fields:l},result:{type:"map",ok:!0}}):c?{next:i,result:{type:"map",ok:c.by.id===r.id,holder:c.by}}:Object.values(l).filter(d=>d.by.id===r.id).length>=n.perPlayer?{code:"limit",message:`claim('${t.key}'): a player holds at most ${n.perPlayer} (onceworlds.json's "perPlayer"). Release one first.`}:Object.keys(l).length>=n.fields?{code:"full",message:`claim('${t.key}'): all ${n.fields} fields are taken (onceworlds.json's "fields").`}:(l[t.field]={by:o,at:s},{next:{type:"map",fields:l},result:{type:"map",ok:!0}})}case"value":{if(t.op!=="set"||n.type!=="value")break;return r.host?{next:{type:"value",value:t.value,at:s},result:{type:"value",ok:!0}}:{code:"not_host",message:`set('${t.key}'): only the host of one of the game's rooms sets a value. Join a room, and set it from the host's page (room.isHost).`}}}return{code:"wrong_op",message:`'${t.key}' is a ${n.type}: use ${xn(n.type)}.`}}var Je=n=>n.replace(/[\u0000-\u001f\u007f]/g," ").replace(/\s+/g," ").trim();var En=/^[a-z0-9][a-z0-9-]{0,19}$/;function Xe(n){if(!n||typeof n!="object")return null;let e=n,t=[];for(let s of Array.isArray(e.buttons)?e.buttons:[]){if(t.length>=S.controlButtons)break;let{id:o,label:i}=s??{};if(typeof o!="string"||!En.test(o)||t.some(c=>c.id===o))continue;let l=typeof i=="string"?i.replace(/[\u0000-\u001f\u007f]/g,"").replace(/\s+/g," ").trim().slice(0,S.controlLabelLength).trim():"";t.push({id:o,label:l||o})}let r=e.stick===!0;return r||t.length>0?{stick:r,zone:e.zone==="corner"?"corner":"wide",buttons:t}:null}var Y="host:ow.settings",Pn=/^[a-z][a-zA-Z0-9]{0,15}$/;function Re(n){if(!Array.isArray(n))return[];let e=[];for(let t of n){if(e.length>=6)break;let{id:r,label:s,options:o,default:i}=t??{};if(typeof r!="string"||!Pn.test(r)||e.some(d=>d.id===r)||!Array.isArray(o))continue;let l=[];for(let d of o){let m=typeof d=="object"&&d!==null?d.value:d;if(typeof m!="string"&&typeof m!="number"||typeof m=="number"&&!Number.isFinite(m)||l.length>=8||l.some(f=>f.value===m))continue;let h=typeof d=="object"&&d!==null&&typeof d.label=="string"?d.label:String(m);l.push({value:m,label:Je(h).slice(0,20)||String(m)})}if(l.length<2)continue;let c=typeof s=="string"?Je(s).slice(0,24):"";e.push({id:r,label:c||r,options:l,default:l.some(d=>d.value===i)?i:l[0].value})}return e}function et(n,e){let t=e!==null&&typeof e=="object"?e[n.id]:void 0;return n.options.some(r=>r.value===t)?t:n.default}var tt="onceworlds:hello",nt="onceworlds:connect",rt=/^[a-z0-9]{1,32}~(?:(?:0|[1-9]\d{0,3})\.(?:0|[1-9]\d{0,4})\.(?:0|[1-9]\d{0,5}))?~[0-9a-z]{1,11}~[lp]~[A-Za-z0-9_-]{43}$/;var ot=["log","info","warn","error"],st="onceworlds-console";var it=/^[A-Za-z0-9-]{1,64}$/;function at(){let n=1,e=!1,t=()=>e?0:n,r=new Map;if(typeof AudioNode<"u"&&typeof AudioDestinationNode<"u"){let d=AudioNode.prototype.connect,m=AudioNode.prototype.disconnect,h=w=>typeof OfflineAudioContext>"u"||!(w instanceof OfflineAudioContext),f=w=>{let y=r.get(w);return y||(y=w.createGain(),y.gain.value=t(),d.call(y,w.destination),r.set(w,y)),y};AudioNode.prototype.connect=function(w,...y){return w instanceof AudioDestinationNode&&h(w.context)&&r.get(w.context)!==this?(d.call(this,f(w.context),...y),w):d.call(this,w,...y)},AudioNode.prototype.disconnect=function(...w){let y=w[0];if(y instanceof AudioDestinationNode){let k=r.get(y.context);k&&k!==this&&(w[0]=k)}return m.apply(this,w)}}let s=new WeakMap,o=new Set,i=typeof HTMLMediaElement<"u"?Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype,"volume"):void 0,l=d=>i?.set?.call(d,Ee((s.get(d)??1)*t())),c=d=>{s.has(d)||!i?.get||(s.set(d,i.get.call(d)),o.add(new WeakRef(d)))};if(i?.get&&i.set){Object.defineProperty(HTMLMediaElement.prototype,"volume",{configurable:!0,enumerable:i.enumerable,get(){return s.has(this)?s.get(this):i.get.call(this)},set(m){c(this),s.set(this,Ee(Number(m))),l(this)}});let d=HTMLMediaElement.prototype.play;HTMLMediaElement.prototype.play=function(){return c(this),l(this),d.call(this)}}return{get volume(){return n},get muted(){return e},apply(d){n=Ee(d.volume),e=d.muted;for(let[m,h]of r)h.gain.setTargetAtTime(t(),m.currentTime,.02);for(let m of o){let h=m.deref();h?l(h):o.delete(m)}}}}function Ee(n){return Number.isFinite(n)?Math.min(1,Math.max(0,n)):1}function lt(n){try{let e=n?.getAttribute("data-build");return e&&rt.test(e)?e:null}catch{return null}}var O={ms:9e3,warmupMs:1e3,flowSettleMs:1500,playSettleMs:2e3,motionGraceMs:5e3,ceilingMs:25e3,graceMs:12e3,maxBytes:6*1024*1024,posterBytes:1024*1024,long:1280,short:720,fps:30,bitsPerSecond:25e5};var $n=["video/webm;codecs=vp9","video/webm;codecs=vp8","video/webm","video/mp4;codecs=avc1","video/mp4"],Cn=3e3,ct=250,Ln="host:ow.flow";function ut(n){let e=n?.[Ln],t=e&&typeof e=="object"?e.phase:void 0;return typeof t=="string"?t:void 0}var Pe=250,oe=32,se=18,_n=.02,Ae={needed:6,of:8};function Dn(){return typeof window>"u"||typeof document>"u"?null:{document,MediaRecorder:globalThis.MediaRecorder,requestAnimationFrame:n=>window.requestAnimationFrame(n),setTimeout:(n,e)=>window.setTimeout(n,e),clearTimeout:n=>window.clearTimeout(n)}}var F=(n,e,t,r)=>typeof n=="number"&&Number.isFinite(n)?Math.min(t,Math.max(e,Math.round(n))):r;function On(n){if(!n||typeof n!="object")return null;let e=n;return e.t==="stop"?{t:"stop"}:e.t!=="start"?null:{t:"start",ms:F(e.ms,1e3,15e3,O.ms),long:F(e.long,160,1920,O.long),short:F(e.short,90,1080,O.short),fps:F(e.fps,5,60,O.fps),bitsPerSecond:F(e.bitsPerSecond,25e4,8e6,O.bitsPerSecond),maxBytes:F(e.maxBytes,64*1024,O.maxBytes,O.maxBytes)}}function ie(n){let e=null,t=0;for(let r of Array.from(n))try{if(r.isConnected===!1||r.width<64||r.height<64)continue;let s=typeof r.getBoundingClientRect=="function"?r.getBoundingClientRect():null;if(s&&(s.width<32||s.height<32))continue;let o=s?s.width*s.height:r.width*r.height;o>t&&(e=r,t=o)}catch{}return e}function Tn(n,e,t,r){let s=n>=e,o=Math.min(1,(s?t:r)/n,(s?r:t)/e);if(o>=1)return{width:n,height:e,scale:1};let i=l=>Math.max(2,Math.floor(l*o/2)*2);return{width:i(n),height:i(e),scale:o}}function In(n,e){return new Promise(t=>{e.requestAnimationFrame(()=>{try{let r=n.width,s=n.height,o=Math.min(O.long,Math.max(480,r)),i=Math.round(o*9/16),l=e.document.createElement("canvas");l.width=o,l.height=i;let c=l.getContext("2d");if(!c)return t(null);c.fillStyle="#000",c.fillRect(0,0,o,i);let d=r/s,m=d>=1.25&&d<=2.4?Math.max(o/r,i/s):Math.min(o/r,i/s);c.drawImage(n,(o-r*m)/2,(i-s*m)/2,r*m,s*m),l.toBlob(h=>h?h.arrayBuffer().then(t,()=>t(null)):t(null),"image/jpeg",.85)}catch{t(null)}})})}function Bn(n,e){let t=null,r=null;return s=>{e.requestAnimationFrame(()=>{try{if(!t){let c=e.document.createElement("canvas");c.width=oe,c.height=se,t=c.getContext("2d",{willReadFrequently:!0})}if(!t)return s(null);t.drawImage(n,0,0,oe,se);let o=t.getImageData(0,0,oe,se).data,i=r;if(r=new Uint8ClampedArray(o),!i)return s(null);let l=0;for(let c=0;c+2<o.length;c+=4){let d=o[c]*2+o[c+1]*5+o[c+2],m=i[c]*2+i[c+1]*5+i[c+2];Math.abs(d-m)>128&&l++}s(l/(oe*se))}catch{s(null)}})}}function Nn(n,e,t,r){return new Promise(s=>{let o=Bn(n,e),i=[],l=0,c=0,d=!1,m=()=>{if(r()||l>=O.ceilingMs)return s(!1);let h=null;try{h=t()}catch{}h&&h.phase!=="lobby"&&(d=!0);let f=h?.flow;if(h?.phase==="playing"&&h.paused!==!0&&(f===void 0||f==="playing")){if(c+=Pe,c>(f===void 0?O.playSettleMs:O.flowSettleMs))return s(!0)}else c=0;if(!d){if(l>=O.motionGraceMs&&i.length>=Ae.of&&i.filter(Boolean).length>=Ae.needed)return s(!0);o(w=>{i.push(w!==null&&w>=_n),i.length>Ae.of&&i.shift()})}l+=Pe,e.setTimeout(m,Pe)};m()})}function dt(n,e){let t=e.MediaRecorder;return typeof n.captureStream!="function"||typeof t!="function"?null:$n.find(r=>{try{return t.isTypeSupported(r)}catch{return!1}})??null}function mt(n,e=Dn(),t=()=>null){let r=null,s=l=>{try{n(l)}catch{}},o=l=>s({t:"none",reason:l});function i(l){if(r)return;if(!e)return o("unsupported");let c=e,d=!1,m=null;r={cancel(){d=!0,m!==null&&c.clearTimeout(m)}};let h=y=>{r=null,!d&&y&&s(y)},f=0,w=()=>{if(d)return h(null);let y=ie(c.document.querySelectorAll("canvas"));if(y){if(m=null,!dt(y,c))return h({t:"none",reason:"unsupported"});Nn(y,c,t,()=>d).then(k=>{if(d)return null;if(!k)return{t:"none",reason:"not-playing"};let E=ie(c.document.querySelectorAll("canvas"))??y;return Gn(E,l,c,()=>d,()=>!d&&s({t:"started"}))}).then(h,()=>h({t:"none",reason:"failed"}))}else++f>Cn/ct?h({t:"none",reason:"no-canvas"}):m=c.setTimeout(w,ct)};w()}return{receive(l){try{let c=On(l);if(!c)return;if(c.t==="stop"){r?.cancel(),r=null;return}i(c)}catch{r=null,o("failed")}}}}function Gn(n,e,t,r,s){let o=t.MediaRecorder,i=dt(n,t);if(!o||!i)return Promise.resolve({t:"none",reason:"unsupported"});let l=Tn(n.width,n.height,e.long,e.short),c=!1,d=n;if(l.scale<1){let f=t.document.createElement("canvas");f.width=l.width,f.height=l.height;let w=f.getContext("2d");if(!w)return Promise.resolve({t:"none",reason:"unsupported"});c=!0;let y=()=>{if(c){try{w.drawImage(n,0,0,l.width,l.height)}catch{}t.requestAnimationFrame(y)}};y(),d=f}let m,h;try{m=d.captureStream(e.fps),h=new o(m,{mimeType:i,videoBitsPerSecond:e.bitsPerSecond})}catch{return c=!1,Promise.resolve({t:"none",reason:"failed"})}return new Promise(f=>{let w=[],y=0,k=Promise.resolve(null),E=t.setTimeout(()=>{k=In(n,t)},Math.round(e.ms/2)),_=null,T=()=>{try{h.state!=="inactive"&&h.stop()}catch{}},M=()=>{c=!1,t.clearTimeout(E),_!==null&&t.clearTimeout(_);try{for(let L of m.getTracks())L.stop()}catch{}};h.ondataavailable=L=>{!L.data||L.data.size===0||(w.push(L.data),y+=L.data.size,y>e.maxBytes*.85&&T())},h.onerror=()=>{M(),f(r()?null:{t:"none",reason:"failed"})},h.onstop=()=>{if(M(),r())return f(null);let L=new Blob(w,{type:h.mimeType||i});Promise.all([L.arrayBuffer(),k.catch(()=>null)]).then(([U,N])=>{if(U.byteLength===0)return f({t:"none",reason:"failed"});if(U.byteLength>e.maxBytes)return f({t:"none",reason:"too-large"});f({t:"done",type:L.type||i,video:U,poster:N,width:l.width,height:l.height})}).catch(()=>f({t:"none",reason:"failed"}))};try{h.start(1e3)}catch{return M(),f({t:"none",reason:"failed"})}s(),_=t.setTimeout(()=>{_=null,T()},e.ms);let P=()=>{if(h.state!=="inactive"){if(r())return T();t.setTimeout(P,200)}};t.setTimeout(P,200)})}var jn=200,pt=2,K=20,ft=200;function gt(n){try{return new URLSearchParams(n).get(st)==="1"}catch{return!1}}function ht(n){return typeof n=="object"&&n!==null&&n.solo===!0?"standalone":"platform"}function yt(n){return typeof n=="object"&&n!==null&&n.demo===!0}function bt(n){try{let e=new URLSearchParams(n).get("demo");return e!==null&&e!=="0"&&e!=="false"}catch{return!1}}function wt(n,e,t=Date.now,r=setTimeout){let s=null,o=[],i=-1/0,l=0,c=0,d=!1,m=!1,h=y=>{s?s(y):o.length<jn&&o.push(y)},f=()=>{let y=t();return y-i>=1e3&&(i=y,l=0),l<S.consoleLinesPerSecond?(l++,!0):(c++,d||(d=!0,r(()=>{d=!1;let k=c;c=0,k>0&&f()&&h({level:"warn",text:`${k} more ${k===1?"line":"lines"} left out`,at:t()})},Math.max(0,1e3-(y-i)))),!1)},w=(y,k,E)=>{if(!f())return;let _=kt(e?k.split(`${e}/`).join(""):k);h({level:y,text:_,at:t(),...E?{source:E.source.slice(0,300),line:E.line,column:E.column}:{}})};for(let y of ot){let k=n[y];typeof k=="function"&&(n[y]=function(...E){if(k.apply(n,E),!m){m=!0;try{w(y,Hn(E))}catch{}finally{m=!1}}})}return{error(y,k){if(!m){m=!0;try{w("error",y,k)}catch{}finally{m=!1}}},connect(y){s=y;for(let k of o.splice(0))y(k)}}}function Hn(n){let e="",t=n;if(typeof n[0]=="string"){let r=1;e=n[0].replace(/%[sdifoOc%]/g,s=>{if(s==="%%")return"%";if(r>=n.length)return s;let o=n[r++];return s==="%c"?"":s==="%d"||s==="%i"?String(typeof o=="number"?Math.trunc(o):parseInt(Q(o),10)):s==="%f"?String(typeof o=="number"?o:parseFloat(Q(o))):s==="%s"?Q(o):V(o)}),t=n.slice(r)}for(let r of t)e+=e?` ${Q(r)}`:Q(r);return kt(e)}function Q(n){return typeof n=="string"?n:V(n)}function V(n,e=0){try{switch(typeof n){case"string":return JSON.stringify(n.length>ft?`${n.slice(0,ft)}\u2026`:n);case"bigint":return`${n}n`;case"symbol":return n.toString();case"function":return`\u0192 ${n.name||"anonymous"}()`;case"object":break;default:return String(n)}if(n===null)return"null";if(n instanceof Error)return ae(n);if(n instanceof Date)return Number.isNaN(n.getTime())?"Invalid Date":n.toISOString();if(n instanceof RegExp)return String(n);if(n instanceof Map||n instanceof Set)return`${n.constructor.name}(${n.size})`;if(ArrayBuffer.isView(n))return`${n.constructor.name}(${"length"in n?n.length:n.byteLength})`;if(typeof Element<"u"&&n instanceof Element)return`<${n.tagName.toLowerCase()}>`;if(Array.isArray(n))return e>=pt?`Array(${n.length})`:`[${n.slice(0,K).map(i=>V(i,e+1)).join(", ")}${n.length>K?`, \u2026${n.length-K} more`:""}]`;let t=typeof n.constructor=="function"&&n.constructor!==Object?`${n.constructor.name} `:"";if(e>=pt)return`${t}{\u2026}`;let r=Object.keys(n),s=r.slice(0,K).map(o=>`${/^[A-Za-z_$][\w$]*$/.test(o)?o:JSON.stringify(o)}: ${V(n[o],e+1)}`);return`${t}{${s.join(", ")}${r.length>K?`, \u2026${r.length-K} more`:""}}`}catch{return Object.prototype.toString.call(n)}}function ae(n){try{let e=`${n.name||"Error"}: ${n.message}`,t=typeof n.stack=="string"?n.stack:"";return t?t.startsWith(e)?t:`${e}
|
|
2
|
-
${t}`:e}catch{return"Error"}}function kt(n){return n.length>S.consoleLineLength?`${n.slice(0,S.consoleLineLength-1)}\u2026`:n}var qn={wasd:{up:"w",down:"s",left:"a",right:"d"},arrows:{up:"ArrowUp",down:"ArrowDown",left:"ArrowLeft",right:"ArrowRight"}},zn=.45,Un=.3,$e={" ":["Space",32],Enter:["Enter",13],Shift:["ShiftLeft",16],Control:["ControlLeft",17],Alt:["AltLeft",18],Escape:["Escape",27],Tab:["Tab",9],Backspace:["Backspace",8],ArrowLeft:["ArrowLeft",37],ArrowUp:["ArrowUp",38],ArrowRight:["ArrowRight",39],ArrowDown:["ArrowDown",40]},le=class{constructor(e,t){p(this,"touch");p(this,"stick",{x:0,y:0});p(this,"held",new Set);p(this,"downs",new Map);p(this,"stickMode");p(this,"buttonKeys",new Map);p(this,"down",new Map);p(this,"show");p(this,"reportTouch");this.show=e,this.reportTouch=t,this.touch=typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches,window.addEventListener("pointerdown",r=>r.isTrusted&&this.setTouch(r.pointerType!=="mouse"),!0),window.addEventListener("keydown",r=>r.isTrusted&&this.setTouch(!1),!0),window.addEventListener("blur",()=>this.release()),document.addEventListener("visibilitychange",()=>document.hidden&&this.release())}set(e){let t=e??{};this.stickMode=t.stick==="wasd"||t.stick==="arrows"||t.stick==="analog"?t.stick:void 0;let r=Array.isArray(t.buttons)?t.buttons:[],s=e?Xe({stick:this.stickMode!==void 0,zone:t.zone,buttons:r}):null;this.buttonKeys=new Map;for(let o of s?.buttons??[]){let i=r.find(l=>l?.id===o.id)?.key;typeof i=="string"&&i&&this.buttonKeys.set(o.id,i)}this.show(s),s?this.update():this.release()}receive(e){let t=s=>typeof s=="number"&&Number.isFinite(s)?Math.max(-1,Math.min(1,s)):0;this.stick.x=t(e?.x),this.stick.y=t(e?.y);let r=new Set(Array.isArray(e?.buttons)?e.buttons.filter(s=>typeof s=="string"):[]);for(let s of r)this.held.has(s)||this.downs.set(s,(this.downs.get(s)??0)+1);this.held=r,this.update()}pressed(e){return this.held.has(e)}presses(e){return this.downs.get(e)??0}update(){let e=new Map;if(this.stickMode==="wasd"||this.stickMode==="arrows"){let t=qn[this.stickMode],r=(s,o)=>{o>(this.down.has(s)?Un:zn)&&e.set(s,vt(s))};r(t.up,-this.stick.y),r(t.down,this.stick.y),r(t.left,-this.stick.x),r(t.right,this.stick.x)}for(let t of this.held){let r=this.buttonKeys.get(t);r&&e.set(r,vt(r))}for(let[t,r]of this.down)e.has(t)||(this.down.delete(t),St(r,!1));for(let[t,r]of e)this.down.has(t)||(this.down.set(t,r),St(r,!0))}release(){this.stick.x=0,this.stick.y=0,this.held.clear(),this.update()}setTouch(e){e!==this.touch&&(this.touch=e,e||this.release(),this.reportTouch(e))}};function vt(n){let e=n==="Space"?" ":n;return Object.prototype.hasOwnProperty.call($e,e)?{key:e,code:$e[e][0],keyCode:$e[e][1]}:/^[a-z]$/i.test(e)?{key:e.toLowerCase(),code:`Key${e.toUpperCase()}`,keyCode:e.toUpperCase().charCodeAt(0)}:/^[0-9]$/.test(e)?{key:e,code:`Digit${e}`,keyCode:48+Number(e)}:{key:e,code:"",keyCode:0}}function St(n,e){try{let t={key:n.key,code:n.code,keyCode:n.keyCode,which:n.keyCode,bubbles:!0,cancelable:!0},r=new KeyboardEvent(e?"keydown":"keyup",t);r.keyCode!==n.keyCode&&(Object.defineProperty(r,"keyCode",{get:()=>n.keyCode}),Object.defineProperty(r,"which",{get:()=>n.keyCode})),(document.activeElement??document.body??document).dispatchEvent(r)}catch{}}function Fn(n){switch(n.type){case"counter":return n.value;case"list":return n.items;case"record":return n.value===null?null:{value:n.value,by:n.by,at:n.at};case"map":return n.fields;case"value":return n.value}}function xt(n=e=>console.warn(`[onceworlds] ${e}`)){let e=new Map;return t=>{let r=Date.now();r-(e.get(t)??-1/0)<5e3||(e.size>200&&e.clear(),e.set(t,r),n(t))}}function Mt(n,e){let t=new Set,r=async(s,o)=>{let i=`onceworlds.data.server.${o===null?"delete":"set"}('${String(s).slice(0,40)}')`,l=n();if(typeof s!="string"||!Ye.test(s))return e(`${i} did nothing: a key is 1-59 letters, digits or _ . : -`),!1;if(!l||l.closed)return e(`${i} did nothing: you're not in a room. Join one first (await onceworlds.rooms.join({ private: true })): server data belongs to a server.`),!1;if(!l.connected)return e(`${i} did nothing: the connection is down and the platform is reconnecting. Write it again after room.on('reconnect').`),!1;if(!l.isHost)return e(`${i} did nothing: only the room's host writes server data. Check onceworlds.data.server.canWrite, or send the change to the host (room.send) and let it write.`),!1;let c;try{c=JSON.stringify(o??null)}catch{c=void 0}return c===void 0?(e(`${i} did nothing: the value must be plain JSON data (numbers, text, lists, objects).`),!1):Se(c)>S.stateValueBytes-128?(e(`${i} did nothing: the value is ${Math.ceil(Se(c)/1024)} KB, and a key holds 16 KB. Split it into keys by area or chunk (a server keeps ${S.serverDataKeys} keys, ${S.serverDataBytes/1024} KB), or let the engine's Persist() save the world for you.`),!1):await l._writeAcked(ve(s),o??null)};return{get(s){let o=n();if(!o||typeof s!="string")return null;let i=o.state[ve(s)];return i===void 0?null:i},all(){let s=n(),o={};if(!s)return o;for(let[i,l]of Object.entries(s.state))i.startsWith("data:")&&(o[i.slice(5)]=l);return o},set:(s,o)=>r(s,o??null),delete:s=>r(s,null),get kept(){return n()?.kept??!1},get canWrite(){let s=n();return!!(s&&!s.closed&&s.isHost)},async reset(){let s=n();return s?s.owner!==s.me.id&&s.kind!=="solo"?(e("onceworlds.data.server.reset() did nothing: only a private server's owner starts its world over (room.owner)."),!1):s._resetData():(e("onceworlds.data.server.reset() did nothing: you're not in a room."),!1)},on(s,o){return s!=="change"||typeof o!="function"?()=>{}:(t.add(o),()=>t.delete(o))},_changed(s,o,i){for(let l of t)try{l(s,o??null,i)}catch(c){console.error('[onceworlds] error in data.server "change" listener',c)}}}}function Rt(n,e){let t=async(s,o,i,l)=>{if(typeof s.key!="string"||!xe.test(s.key))return e(`${o} did nothing: '${String(s.key).slice(0,40)}' isn't a game data key (1-32 letters, digits, _ . or -, declared in onceworlds.json's "data").`),l;try{return i(await(await n()).op(s))}catch(c){return e(`${o} did nothing: ${c instanceof Error?c.message:String(c)}`),l}},r=(s,o)=>`onceworlds.data.game.${s}('${String(o).slice(0,40)}')`;return{async get(s){if(typeof s!="string"||!xe.test(s))return e(`${r("get",s)}: '${String(s).slice(0,40)}' isn't a game data key (1-32 letters, digits, _ . or -).`),null;try{return Fn(await(await n()).read(s))}catch(o){return e(`${r("get",s)} read nothing: ${o instanceof Error?o.message:String(o)}`),null}},add:(s,o)=>t({key:s,op:"add",n:Number(o)},r("add",s),i=>i.type==="counter"?i.value:null,null),push:(s,o)=>t({key:s,op:"push",item:o},r("push",s),i=>i.type==="list",!1),best:(s,o)=>t({key:s,op:"best",n:Number(o)},r("best",s),i=>i.type==="record"?{value:i.value,by:i.by,at:i.at,record:i.record}:null,null),claim:(s,o)=>t({key:s,op:"claim",field:String(o)},r("claim",s),i=>i.type==="map"&&i.ok,!1),release:(s,o)=>t({key:s,op:"release",field:String(o)},r("release",s),i=>i.type==="map"&&i.ok,!1),set:(s,o)=>t({key:s,op:"set",value:o},r("set",s),i=>i.type==="value",!1)}}function Et(n,e){let t=S.gameDataNumberMax,r=o=>{switch(o.op){case"add":return{type:"counter",min:0,max:t,add:[-t,t],guests:!0};case"push":return{type:"list",max:S.gameDataListMax,bytes:S.gameDataItemBytes,guests:!0};case"best":return{type:"record",order:"max",min:-t,max:t,guests:!0};case"claim":case"release":return{type:"map",fields:S.gameDataMapFields,perPlayer:S.gameDataMapPerPlayer,guests:!0};case"set":return{type:"value",bytes:S.gameDataValueBytes,guests:!0}}},s=o=>{try{let i=e.get(`onceworlds:game:${o}`);return i?JSON.parse(i):null}catch{return null}};return{async read(o){let i=s(o);if(!i)throw new Error(`nothing is stored under '${o}' on this page yet (on its own, game data lives in this browser).`);return i},async op(o){let i=await n(),l=s(o.key),c=r(o);if(l&&l.type!==c.type)throw new Error(`'${o.key}' holds a ${l.type} on this page: it was first used that way.`);let d=Ze(c,l,JSON.parse(JSON.stringify(o??null)),{id:i.id,name:i.name,host:!0},Date.now());if("code"in d)throw new Error(d.message);return e.set(`onceworlds:game:${o.key}`,JSON.stringify(d.next)),d.result}}}function Pt(n){return{messagesPerSecond:n.messagesPerSecond*.85,messageBurst:n.messagesPerSecond*2*.85,bytesPerSecond:n.bytesPerSecond*.85,byteBurst:n.bytesPerSecond*4*.85}}var Kn=60,Vn=100,Wn=5e3,ce=class{constructor(e={}){p(this,"queue",[]);p(this,"rates",Pt({messagesPerSecond:S.messagesPerSecond,bytesPerSecond:S.roomBytesPerSecond}));p(this,"messages",this.rates.messageBurst);p(this,"bytes",this.rates.byteBurst);p(this,"budget",{messagesPerSecond:S.messagesPerSecond,bytesPerSecond:S.roomBytesPerSecond});p(this,"refilledAt");p(this,"timer",null);p(this,"warnedAt",-1/0);p(this,"dropped",0);p(this,"now");p(this,"setTimer");p(this,"clearTimer");p(this,"warn");this.now=e.now??Date.now,this.setTimer=e.setTimer??((t,r)=>setTimeout(t,r)),this.clearTimer=e.clearTimer??(t=>clearTimeout(t)),this.warn=e.warn??(t=>console.warn(`[onceworlds] ${t}`)),this.refilledAt=this.now(),e.budget&&this.setBudget(e.budget)}setBudget(e){this.refill(),this.budget={messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond},this.rates=Pt(this.budget),this.messages=Math.min(this.messages,this.rates.messageBurst),this.bytes=Math.min(this.bytes,this.rates.byteBurst),this.drain(!1)}get waiting(){return this.queue.length}event(e,t){this.queue.push({kind:"event",size:e,send:t}),this.settle()}urgent(e,t){this.queue.push({kind:"event",size:e,send:t}),this.drain(!0)}state(e,t,r){for(let s=this.queue.length-1;s>=0;s--){let o=this.queue[s];if(o.kind!=="state")break;if(o.key===e)return o.size=t,o.send=r,this.settle()}this.queue.push({kind:"state",key:e,size:t,send:r}),this.settle()}spend(e){this.refill(),this.bytes-=e}clear(){this.queue=[],this.timer!==null&&this.clearTimer(this.timer),this.timer=null}settle(){this.queue.length>this.waitingLimit(Kn,12)&&(this.squeeze(),this.tell(`${this.queue.length} messages are waiting: a room takes about ${this.budget.messagesPerSecond} messages and ${Math.round(this.budget.bytesPerSecond/1024)} KB a second from each player. State is sent at its newest value and other messages in order. Send less: batch events, write state at 10-20 Hz, and only what changed.`)),this.drain(!1)}waitingLimit(e,t){return Math.max(t,Math.round(e*this.rates.messagesPerSecond/(S.messagesPerSecond*.85)))}refill(){let e=this.now(),t=Math.max(0,(e-this.refilledAt)/1e3);this.refilledAt=e,this.messages=Math.min(this.rates.messageBurst,this.messages+t*this.rates.messagesPerSecond),this.bytes=Math.min(this.rates.byteBurst,this.bytes+t*this.rates.bytesPerSecond)}drain(e){for(this.timer!==null&&(this.clearTimer(this.timer),this.timer=null),this.refill();this.queue.length>0;){let i=this.queue[0],l=Math.min(i.size(),this.rates.byteBurst);if(!e&&(this.messages<1||this.bytes<l))break;this.queue.shift(),this.messages-=1,this.bytes-=l;try{i.send()}catch(c){console.error("[onceworlds] a message could not be sent",c)}}if(this.queue.length===0)return;let t=Math.min(this.queue[0].size(),this.rates.byteBurst),r=Math.max(0,1-this.messages)/this.rates.messagesPerSecond,s=Math.max(0,t-this.bytes)/this.rates.bytesPerSecond,o=Math.min(250,Math.max(8,Math.ceil(Math.max(r,s)*1e3)));this.timer=this.setTimer(()=>{this.timer=null,this.drain(!1)},o)}squeeze(){let e=new Set;for(let s=this.queue.length-1;s>=0;s--){let o=this.queue[s];o.kind==="state"&&(e.has(o.key)?this.queue.splice(s,1):e.add(o.key))}let t=this.queue.filter(s=>s.kind==="event").length,r=this.waitingLimit(Vn,20);for(let s=0;t>r&&s<this.queue.length;)this.queue[s].kind==="event"?(this.queue.splice(s,1),t--,this.dropped++):s++;this.dropped>0&&this.tell(`${this.dropped} messages were dropped so the rest arrive fresh: the game sends much more than a room takes (${this.budget.messagesPerSecond} messages, ${Math.round(this.budget.bytesPerSecond/1024)} KB a second per player). Send less.`)}tell(e){let t=this.now();t-this.warnedAt<Wn||(this.warnedAt=t,this.warn(e))}};function q(n){try{let e=JSON.stringify(n)??"null",t=0;for(let r=0;r<e.length;r++)e.charCodeAt(r)>127&&(t+=2);return e.length+t+32}catch{return 1024}}var Ce=Math.PI*2;function Le(n,e,t,r={},s=""){if(typeof n=="number"&&typeof e=="number"){if(r.angles?.includes(s)){let o=((e-n)%Ce+Ce+Math.PI)%Ce-Math.PI;return n+o*t}return n+(e-n)*t}if(Array.isArray(n)&&Array.isArray(e)&&n.length===e.length)return e.map((o,i)=>Le(n[i],o,t,r,s));if(n&&e&&typeof n=="object"&&typeof e=="object"&&!Array.isArray(n)&&!Array.isArray(e)){let o={};for(let i of Object.keys(e))o[i]=i in n?Le(n[i],e[i],t,r,i):e[i];return o}return t<.5?n:e}function Jn(n,e,t){if(t===void 0||!n||!e||typeof n!="object"||typeof e!="object")return!1;for(let[r,s]of Object.entries(e)){let o=n[r];if(typeof s=="number"&&typeof o=="number"&&Math.abs(s-o)>t)return!0}return!1}function At(n,e,t={}){if(n.length===0)return;let r=n[0],s=n[n.length-1];if(e<=r.at)return r.d;if(e>=s.at)return s.d;let o=n.length-2;for(;o>0&&n[o].at>e;)o--;let i=n[o],l=n[o+1];return Jn(i.d,l.d,t.snap)?l.d:Le(i.d,l.d,(e-i.at)/Math.max(1,l.at-i.at),t)}var $t=8,Ct=2e3;var Yn=3e3,Qn=15e3,Zn=5e3;function Lt(n){let e={};for(let t of Object.keys(n))J(t)&&(e[t]=n[t]);return e}var I=()=>48,Z=class{constructor(e,t,r=Date.now,s=[]){p(this,"id");p(this,"kind");p(this,"invite");p(this,"teams");p(this,"open");p(this,"ranked");p(this,"kept");p(this,"me");p(this,"owner");p(this,"players",new Map);p(this,"state",{});p(this,"host");p(this,"closed",!1);p(this,"connected",!0);p(this,"match");p(this,"listeners",new Map);p(this,"transport");p(this,"outbox",new ce);p(this,"samples",new Map);p(this,"presenceTimer",null);p(this,"pendingPresence",null);p(this,"writes",0);p(this,"written",new Map);p(this,"confirmed",{});p(this,"shares",{messagesPerSecond:S.messagesPerSecond,presenceHz:S.presenceHz,bytesPerSecond:S.roomBytesPerSecond});p(this,"clock");p(this,"schema");p(this,"frozenSettings",null);p(this,"secrets",new Map);p(this,"offlineReady");p(this,"complained",new Map);p(this,"acks",new Map);this.transport=t,this.clock=r,this.schema=s,this.match=e.match??{phase:"lobby",n:0,min:1},this.applyBudget(e.budget),this.id=e.room,this.kind=e.room==="solo"?"solo":e.room.startsWith("private:")?"private":e.room.startsWith("room:")?"named":"public",this.invite=e.invite??null,this.teams=e.teams??0,this.open=e.open??!0,this.ranked=e.ranked??!1,this.kept=e.kept??Qe(e.room),this.owner=e.owner??null,this.host=e.host;for(let o of e.players)this.players.set(o.id,{...o}),o.presence!==null&&o.presence!==void 0&&this.samples.set(o.id,[{at:e.now??this.clock(),d:o.presence}]);this.me=this.players.get(e.you)??{id:e.you,name:"You",presence:null,team:0},this.players.set(this.me.id,this.me),this.state={...e.state},this.confirmed=Lt(e.state);for(let[o,i]of Object.entries(e.private??{}))this.secrets.set(o,{...i});this.match.phase!=="lobby"&&(this.frozenSettings=this.chosenSettings())}get budget(){return this.shares}get settings(){return this.frozenSettings??this.chosenSettings()}setSetting(e,t){if(this.closed||!this.isHost||this.match.phase!=="lobby")return;let r=this.schema.find(o=>o.id===e);if(!r||!r.options.some(o=>o.value===t)||this.chosenSettings()[e]===t)return;let s=this.state[Y];this.setState(Y,{...s!==null&&typeof s=="object"?s:{},[e]:t}),this.clearReady(),this.emit("settings",this.settings)}chosenSettings(){let e=this.state[Y];return Object.fromEntries(this.schema.map(t=>[t.id,et(t,e)]))}get isHost(){return this.connected&&this.host===this.me.id}get online(){return[...this.players.values()].filter(e=>e.connected!==!1)}get participants(){let e=this.match.participants;return this.match.phase==="lobby"||!e?[]:e.map(t=>this.players.get(t)).filter(t=>!!t)}get spectators(){let e=this.match.participants;return this.match.phase==="lobby"||!e?[]:this.online.filter(t=>!e.includes(t.id))}get leftOut(){let e=this.match.left;return this.match.phase==="lobby"||!e?.length?[]:this.spectators.filter(t=>e.includes(t.id))}get private(){return this.secrets.get(this.me.id)??{}}privateOf(e){return e===this.me.id||this.isHost?this.secrets.get(e)??{}:{}}setPrivate(e,t){this.closed||(this.setSecret(this.me.id,e,t??null),this.outbox.event(()=>q(t),()=>this.transport.private(e,t??null)))}setPrivateFor(e,t,r){if(!this.closed){if(e===this.me.id)return this.setPrivate(t,r);this.outbox.event(()=>q(r),()=>this.transport.private(t,r??null,String(e)))}}presenceAt(e,t={}){if(e===this.me.id)return this.me.presence;let r=this.samples.get(e);return!r||r.length===0?this.players.get(e)?.presence??null:At(r,this.clock()-(t.delay??100),t)}keep(e,t,r){let s=this.samples.get(e)??[],o=s.length;for(;o>0&&s[o-1].at>t;)o--;for(s.splice(o,0,{at:t,d:r});s.length>$t||s.length>2&&s[0].at<t-Ct;)s.shift();this.samples.set(e,s)}isParticipant(e=this.me.id){return this.match.phase!=="lobby"&&!!this.match.participants?.includes(e)}get spectating(){return this.match.phase!=="lobby"&&!this.isParticipant()}get running(){return this.match.phase==="playing"&&!this.match.paused}matchNow(){let e=this.match;if(e.phase!=="playing"||e.startedAt===void 0)return 0;let t=this.clock(),r=(e.pausedMs??0)+(e.paused?Math.max(0,t-e.paused.since):0);return Math.max(0,t-e.startedAt-r)}get canStart(){return this.isHost&&this.kind!=="public"&&this.match.phase==="lobby"&&this.online.length>=this.match.min&&this.allReady}get allReady(){return this.notReady.length===0}get notReady(){return this.online.filter(e=>e.id!==this.host&&!e.ready)}setReady(e=!0){this.closed||!!this.me.ready===e||(e?this.me.ready=!0:delete this.me.ready,this.connected||(this.offlineReady=e),this.outbox.urgent(I,()=>this.transport.ready(e)))}startMatch(e={}){if(!this.reachable("startMatch"))return;let t=Number(e.countdown),r={...Number.isFinite(t)?{countdown:Math.round(t*1e3)}:{},...e.open===!0?{open:!0}:{}};this.outbox.urgent(I,()=>this.transport.start(r))}endMatch(){this.reachable("endMatch")&&this.outbox.urgent(I,()=>this.transport.end())}pauseMatch(e=!0){this.reachable("pauseMatch")&&this.outbox.urgent(I,()=>this.transport.pause(e))}admit(e){let t=(Array.isArray(e)?e:[e]).map(String);t.length&&this.reachable("admit")&&this.outbox.urgent(I,()=>this.transport.admit(t))}reachable(e){let[t,r]=this.closed?["closed","this room is closed (you left it, were removed, or moved to another one)"]:this.connected?["",""]:["disconnected","the connection to the room is down and the platform is reconnecting: call it again after room.on('reconnect')"];return t?(this.complain(e,t,r),!1):!0}dropHostWrites(){for(let e of[...this.written.keys()]){if(!J(e))continue;this.written.delete(e);let t=this.confirmed[e]??null;JSON.stringify(this.state[e]??null)!==JSON.stringify(t)&&(t===null?delete this.state[e]:this.state[e]=t,this.emit("state",e,t,""))}}complain(e,t,r){let s=this.clock(),o=`${e}:${t}`;if(s-(this.complained.get(o)??-1/0)<Zn)return;this.complained.set(o,s);let i=`${e}() did nothing: ${r}.`;console.warn(`[onceworlds] ${i} (${t})`),this.emit("error",{code:t,message:i})}transferHost(e){this.closed||this.outbox.urgent(I,()=>this.transport.transfer(String(e)))}hideLobby(e=!0){this.closed||this.transport.hideLobby?.(!!e)}clearReady(){if(!(this.closed||!this.isHost)){for(let e of this.players.values())delete e.ready;this.outbox.urgent(I,()=>this.transport.unready())}}send(e,t={}){this.closed||this.outbox.event(()=>q(e),()=>this.transport.send(e,t.to))}setPresence(e){if(!this.closed){if(this.me.presence=e,this.presenceTimer){this.pendingPresence={value:e};return}this.transport.presence(e),this.outbox.spend(q(e)),this.presenceTimer=setTimeout(()=>this.flushPresence(),1e3/this.shares.presenceHz)}}setState(e,t){this.writeState(e,t)}writeState(e,t){if(this.closed)return 0;if(J(e)&&this.connected&&this.host!==this.me.id)return this.complain("setState","not_host",`only the host writes ${e}`),0;t==null?delete this.state[e]:this.state[e]=t;let r=++this.writes;return this.written.set(e,{w:r,at:this.clock()}),this.outbox.state(e,()=>q(t)+e.length,()=>this.transport.state(e,t??null,r)),r}_writeAcked(e,t){if(this.closed||!this.connected)return Promise.resolve(!1);let r=this.writeState(e,t);return r?this.kind==="solo"?Promise.resolve(!0):new Promise(s=>{let o={w:r,done:l=>{clearTimeout(i),s(l)}},i=setTimeout(()=>this.settleAcks(e,l=>l===o,!1),Qn);this.acks.set(e,[...this.acks.get(e)??[],o])}):Promise.resolve(!1)}_resetData(){return this.closed||!this.connected||!this.transport.dataReset?!1:(this.outbox.urgent(I,()=>this.transport.dataReset()),!0)}settleAcks(e,t,r){let s=this.acks.get(e);if(!s)return;let o=s.filter(i=>!t(i));o.length>0?this.acks.set(e,o):this.acks.delete(e);for(let i of s)t(i)&&i.done(r)}failAcks(){for(let e of[...this.acks.keys()])this.settleAcks(e,()=>!0,!1)}setTeam(e,t){this.closed||this.outbox.urgent(I,()=>this.transport.team(e,t))}setOpen(e){this.closed||this.outbox.urgent(I,()=>this.transport.open(e))}reportResult(e){this.closed||this.outbox.urgent(()=>q(e),()=>this.transport.result(e))}kick(e){this.closed||this.outbox.urgent(I,()=>this.transport.kick(String(e)))}voteKick(e){this.closed||this.outbox.urgent(I,()=>this.transport.voteKick(String(e)))}on(e,t){let r=this.listeners.get(e);return r||this.listeners.set(e,r=new Set),r.add(t),()=>r.delete(t)}leave(){this.closed||(this.transport.leave(),this.close("left"))}_receive(e){switch(e.t){case"welcome":return this.resync(e);case"away":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(t.connected=!1,this.emit("away",t))}case"back":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(delete t.connected,this.emit("back",t,e.fresh===!0))}case"ready":{let t=this.players.get(e.id);return!t||!!t.ready==(e.ready===!0)?void 0:(e.ready===!0?t.ready=!0:delete t.ready,this.emit("ready",t))}case"match":return this.applyMatch(e.match);case"idle":{let t=this.players.get(e.id);return!t||t===this.me||!!t.idle==(e.idle===!0)?void 0:(e.idle===!0?t.idle=!0:delete t.idle,this.emit("idle",t))}case"private":{let t=JSON.stringify(this.secrets.get(e.id)?.[e.k]??null);this.setSecret(e.id,e.k,e.v??null),t!==JSON.stringify(e.v??null)&&this.emit("private",e.k,e.v??null,e.id);return}case"privates":return this.applySecrets(e.all??{});case"unready":{for(let t of this.players.values())t.ready&&(delete t.ready,this.emit("ready",t));return}case"join":{let t={...e.player};return this.players.set(t.id,t),this.emit("join",t)}case"leave":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(this.players.delete(e.id),this.samples.delete(e.id),this.emit("leave",t,e.kicked===!0))}case"votekick":{let t=this.players.get(e.id);t&&this.emit("votekick",t,e.votes,e.needed);return}case"host":if(this.host=e.id,e.id!==this.me.id)for(let t of[...this.secrets.keys()])t!==this.me.id&&this.secrets.delete(t);return e.id!==this.me.id&&(this.dropHostWrites(),this.failAcks()),this.emit("host",e.id);case"msg":return this.emit("message",e.d,this.players.get(e.from)??{id:e.from,name:"Unknown",presence:null,team:0},typeof e.at=="number"?e.at:this.clock(),typeof e.mt=="number"?e.mt:void 0);case"team":{let t=this.players.get(e.id);return t?(t.team=e.team,this.emit("team",t)):void 0}case"name":{let t=e.id===this.me.id?this.me:this.players.get(e.id);return!t||typeof e.name!="string"||t.name===e.name?void 0:(t.name=e.name,this.emit("rename",t))}case"open":return this.open=e.open,this.emit("open",e.open);case"rated":for(let t of e.changes){let r=this.players.get(t.id);r&&(r.rating=t.rating)}return this.emit("rated",e.changes,e.disputed,e.capped===!0);case"presence":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(t.presence=e.d,this.keep(e.id,typeof e.at=="number"?e.at:this.clock(),e.d),this.emit("presence",t))}case"state":{e.from===this.me.id&&typeof e.w=="number"&&this.settleAcks(e.k,s=>s.w<=e.w,!0),J(e.k)&&(e.v===null?delete this.confirmed[e.k]:this.confirmed[e.k]=e.v);let t=this.written.get(e.k);if(t)if(e.from===this.me.id)if(typeof e.w!="number")this.written.delete(e.k);else{if(e.w<t.w)return;e.w===t.w&&this.written.delete(e.k)}else{if(this.clock()-t.at<Yn)return;this.written.delete(e.k)}let r=JSON.stringify(this.state[e.k]??null);e.v===null?delete this.state[e.k]:this.state[e.k]=e.v,(e.from!==this.me.id||r!==JSON.stringify(e.v??null))&&this.emit("state",e.k,e.v,e.from),e.k===Y&&this.schema.length>0&&this.frozenSettings===null&&r!==JSON.stringify(e.v??null)&&e.from!==this.me.id&&this.emit("settings",this.settings);return}case"chat":return this.emit("chat",e.entry);case"error":return typeof e.k=="string"&&typeof e.w=="number"&&this.settleAcks(e.k,t=>t.w===e.w,!1),e.code==="cooldown"&&this.me.ready&&(delete this.me.ready,this.emit("ready",this.me)),console.warn(`[onceworlds] ${e.message} (${e.code})`),this.emit("error",{code:e.code,message:e.message})}}_closed(e){this.close(e)}_disconnected(){this.closed||!this.connected||(this.connected=!1,this.outbox.clear(),this.failAcks(),this.emit("disconnect"))}close(e){this.closed||(this.closed=!0,this.outbox.clear(),this.failAcks(),this.presenceTimer&&clearTimeout(this.presenceTimer),this.emit("close",e))}flushPresence(){if(this.presenceTimer=null,!this.pendingPresence||this.closed)return;let{value:e}=this.pendingPresence;this.pendingPresence=null,this.transport.presence(e),this.outbox.spend(q(e)),this.presenceTimer=setTimeout(()=>this.flushPresence(),1e3/this.shares.presenceHz)}applyBudget(e){if(!e)return;let t=(r,s)=>typeof r=="number"&&Number.isFinite(r)&&r>0&&r<=s;t(e.presenceHz,S.presenceHz)&&(this.shares={...this.shares,presenceHz:e.presenceHz}),t(e.messagesPerSecond,S.messagesPerSecond)&&t(e.bytesPerSecond,S.roomBytesPerSecond)&&(this.shares={...this.shares,messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond},this.outbox.setBudget({messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond}))}setSecret(e,t,r){let s={...this.secrets.get(e)??{}};r===null?delete s[t]:s[t]=r,Object.keys(s).length>0?this.secrets.set(e,s):this.secrets.delete(e)}applySecrets(e){let t=[];for(let[r,s]of Object.entries(e))for(let[o,i]of Object.entries(s))JSON.stringify(this.secrets.get(r)?.[o]??null)!==JSON.stringify(i)&&t.push([r,o,i]);for(let[r,s]of this.secrets)for(let o of Object.keys(s))o in(e[r]??{})||t.push([r,o,null]);this.secrets=new Map(Object.entries(e).map(([r,s])=>[r,{...s}]));for(let[r,s,o]of t)this.emit("private",s,o,r)}applyMatch(e){let t=this.match;if(!(!e||JSON.stringify(t)===JSON.stringify(e))){if(this.match=e,e.phase!=="lobby"&&this.frozenSettings===null)this.frozenSettings=this.chosenSettings();else if(e.phase==="lobby"&&this.frozenSettings!==null){let r=JSON.stringify(this.frozenSettings);this.frozenSettings=null,r!==JSON.stringify(this.chosenSettings())&&queueMicrotask(()=>this.emit("settings",this.settings))}this.emit("match",e,t),t.phase!=="starting"&&e.phase==="starting"&&this.emit("starting",e),t.phase!=="playing"&&e.phase==="playing"&&this.emit("matchstart",e),t.phase!=="lobby"&&e.phase==="lobby"&&this.emit("matchend",e,t),t.phase==="playing"&&e.phase==="playing"&&(!t.paused&&e.paused?this.emit("matchpause",e):t.paused&&!e.paused&&this.emit("matchresume",e))}}resync(e){let t=this.connected,r=this.host!==e.host;this.connected=!0,this.host=e.host,this.written.clear(),this.confirmed=Lt(e.state),this.applyBudget(e.budget);let s=new Set;for(let i of e.players){if(s.add(i.id),i.id===this.me.id){this.offlineReady===void 0&&!!this.me.ready!=!!i.ready&&(i.ready?this.me.ready=!0:delete this.me.ready,this.emit("ready",this.me));continue}let l=this.players.get(i.id);if(l)l.presence=i.presence,i.presence!==null&&i.presence!==void 0&&this.keep(i.id,e.now??this.clock(),i.presence),l.team=i.team,l.name!==i.name&&(l.name=i.name,this.emit("rename",l)),!!l.ready!=!!i.ready&&(i.ready?l.ready=!0:delete l.ready,this.emit("ready",l)),!!l.idle!=!!i.idle&&(i.idle?l.idle=!0:delete l.idle,this.emit("idle",l)),l.connected===!1!=(i.connected===!1)&&(i.connected===!1?l.connected=!1:delete l.connected,i.connected===!1?this.emit("away",l):this.emit("back",l,!1));else{let c={...i};this.players.set(i.id,c),this.emit("join",c)}}for(let[i,l]of this.players)s.has(i)||l===this.me||(this.players.delete(i),this.emit("leave",l));let o=new Set([...Object.keys(this.state),...Object.keys(e.state)]);for(let i of o){let l=e.state[i]??null;JSON.stringify(this.state[i]??null)!==JSON.stringify(l)&&(l===null?delete this.state[i]:this.state[i]=l,this.emit("state",i,l,""))}r&&this.emit("host",e.host),this.teams=e.teams??this.teams,this.kept=e.kept??this.kept,e.open!==void 0&&e.open!==this.open&&(this.open=e.open,this.emit("open",e.open)),this.applyMatch(e.match??{phase:"lobby",n:0,min:1}),this.applySecrets(e.private??{}),this.me.presence!==null&&this.transport.presence(this.me.presence),this.offlineReady!==void 0&&!!e.players.find(i=>i.id===this.me.id)?.ready!==this.offlineReady&&this.transport.ready(this.offlineReady),this.offlineReady=void 0,t||this.emit("reconnect")}emit(e,...t){for(let r of this.listeners.get(e)??[])try{r(...t)}catch(s){console.error(`[onceworlds] error in room "${e}" listener`,s)}}};function _t(n){let e=null,t=s=>n()._receive({t:"match",match:s}),r=()=>{let o=n().match;if(o.phase!=="starting")return;let{startsAt:i,...l}=o;t({...l,phase:"playing",startedAt:Date.now(),pausedMs:0})};return{send(){},presence(){},state(){},team(){},open(){},result(){},kick(){},voteKick(){},ready(){},unready(){},transfer(){},start(s){let o=n();if(o.match.phase!=="lobby")return;let i=Math.min(S.matchCountdownMaxMs,Math.max(0,s.countdown??S.matchCountdownMs));t({phase:"starting",n:o.match.n+1,min:o.match.min,id:`solo${o.match.n+1}`,seed:Math.floor(Math.random()*2**32),participants:[o.me.id],startsAt:Date.now()+i}),i===0?r():e=setTimeout(r,i)},end(){let s=n();e&&clearTimeout(e),e=null,s.match.phase!=="lobby"&&t({phase:"lobby",n:s.match.n,min:s.match.min})},pause(s){let o=n().match;if(o.phase!=="playing")return;let i=Date.now();if(s&&!o.paused)t({...o,paused:{since:i,reason:"host"}});else if(!s&&o.paused){let{paused:l,...c}=o;t({...c,pausedMs:(o.pausedMs??0)+Math.max(0,i-l.since)})}},admit(){},private(){},leave(){e&&clearTimeout(e),e=null}}}var Dt=["pointerdown","pointermove","keydown","wheel","touchstart","touchmove","devicemotion","visibilitychange"],Xn=5e3;function Ot(n,e=S.idleAfterMs){let t=Date.now(),r=!1,s=()=>{t=Date.now(),r&&(r=!1,n(!1))},o=()=>{try{for(let l of navigator.getGamepads?.()??[])if(l&&(l.buttons.some(c=>c.pressed)||l.axes.some(c=>Math.abs(c)>.25)))return!0}catch{}return!1};for(let l of Dt)window.addEventListener(l,s,{capture:!0,passive:!0});let i=setInterval(()=>{if(o())return s();!r&&Date.now()-t>e&&(r=!0,n(!0))},Xn);return{get idle(){return r},stop(){clearInterval(i);for(let l of Dt)window.removeEventListener(l,s,{capture:!0})}}}var Tt=["pointerdown","keydown","touchstart","gamepadconnected"];function It(n,e){let t=r=>{if(!(!r.isTrusted||!n())){for(let s of Tt)window.removeEventListener(s,t,{capture:!0});e()}};for(let r of Tt)window.addEventListener(r,t,{capture:!0,passive:!0})}var Nt=["low","medium","high"],er={low:.6,medium:.8,high:1},tr=24,nr=18.5,rr=26,_e=6e3,Bt=2e4,or=5*6e4,De=class{constructor(e=2){p(this,"level",2);p(this,"slow",0);p(this,"smoothSince",0);p(this,"holdUntil",0);p(this,"probeAfter",Bt);p(this,"steppedUpAt",-1/0);this.level=e}window(e,t,r){return t>tr?(this.smoothSince=0,++this.slow>=2&&e>=this.holdUntil&&this.level>0&&(e-this.steppedUpAt<_e+Bt/2&&(this.probeAfter=Math.min(or,this.probeAfter*2)),this.level--,this.slow=0,this.holdUntil=e+_e)):t<=nr&&r<=rr?(this.slow=0,this.smoothSince||(this.smoothSince=e),this.level<Nt.length-1&&e>=this.holdUntil&&e-this.smoothSince>=this.probeAfter&&(this.level++,this.steppedUpAt=e,this.smoothSince=0,this.holdUntil=e+_e)):(this.slow=0,this.smoothSince=0),this.level}};function Gt(n){let e="auto",t=typeof matchMedia=="function"&&matchMedia("(prefers-reduced-motion: reduce)").matches,r=!1,s=new De(sr()?1:2),o=new Set,i=!1,l=!1,c=0,d=[],m=0,h=0,f=M=>{if(i){if(c&&!document.hidden){let P=M-c;P<250&&d.push(P)}if(c=document.hidden?0:M,m||(m=M),M-m>=1e3){if(d.length>=10){let P=[...d].sort((j,A)=>j-A),L=d.reduce((j,A)=>j+A,0)/d.length,U=P[Math.min(P.length-1,Math.floor(P.length*.95))],N=E();e==="auto"&&s.window(M,L,U),r&&M-h>=500&&(h=M,n.stats({fps:Math.round(1e3/L),frameMs:Math.round(L*10)/10,worstMs:Math.round(P[P.length-1]*10)/10})),E()!==N&&_()}d=[],m=M}w()}},w=()=>{l||!i||(l=!0,requestAnimationFrame(M=>{l=!1,f(M)}))},y=()=>{i||typeof requestAnimationFrame!="function"||(i=!0,c=0,m=0,w())},k=()=>{!r&&e!=="auto"&&i&&(i=!1)},E=()=>e==="auto"?Nt[s.level]:e,_=()=>{for(let M of o)try{M(T)}catch(P){console.error('[onceworlds] error in settings "change" listener',P)}},T={get choice(){return e},get quality(){return e==="auto"&&y(),E()},get scale(){return er[T.quality]},get reducedMotion(){return t},pixelRatio(M=2){let P=typeof devicePixelRatio=="number"&&devicePixelRatio>0?devicePixelRatio:1;return Math.max(.5,Math.min(P,M)*T.scale)},on(M,P){return M!=="change"?()=>{}:(o.add(P),e==="auto"&&y(),()=>o.delete(P))}};return{settings:T,apply(M){let P=`${E()}|${t}`;e=M.quality,t=M.reducedMotion,r=M.showStats,r||e==="auto"&&o.size>0?y():k(),`${E()}|${t}`!==P&&_()}}}function sr(){return typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches}var X={maxSide:1280,quality:.85,maxBytes:15e5,answerMs:2500,waitMs:3e3,perSecond:3};var ir=4,ar=250,lr=1e3;function cr(){return typeof window>"u"||typeof document>"u"?null:{document,requestAnimationFrame:n=>window.requestAnimationFrame(n),setTimeout:(n,e)=>window.setTimeout(n,e),clearTimeout:n=>window.clearTimeout(n),background:n=>window.getComputedStyle(n).backgroundColor,hidden:()=>document.visibilityState==="hidden"}}function ur(n){if(!n||typeof n!="object")return null;let e=n;if(typeof e.id!="number"||!Number.isSafeInteger(e.id)||e.id<0)return null;let t=typeof e.maxSide=="number"&&Number.isFinite(e.maxSide)?Math.min(X.maxSide,Math.max(64,Math.round(e.maxSide))):X.maxSide,r=typeof e.quality=="number"&&Number.isFinite(e.quality)?Math.min(.95,Math.max(.5,e.quality)):X.quality;return{id:e.id,maxSide:t,quality:r}}function dr(n,e,t){let r=Math.min(1,t/Math.max(n,e,1)),s=o=>Math.max(1,Math.min(t,Math.round(o*r)));return{width:s(n),height:s(e)}}function mr(n){for(let e=0;e+2<n.length;e+=4)if(n[e]!==0||n[e+1]!==0||n[e+2]!==0)return!1;return!0}var pr=/^(?:transparent|rgba\(.*,\s*0(?:\.0*)?\))$/;function fr(n,e){try{for(let t=n;t;t=t.parentElement){let r=e.background?.(t)?.trim();if(r&&!pr.test(r))return r}}catch{}return"#000"}function Oe(n,e,t,r,s){try{let o=r.document.createElement("canvas");o.width=e.width,o.height=e.height;let i=o.getContext("2d");if(!i)return{miss:"unsupported"};if(i.drawImage(n,0,0,e.width,e.height),mr(i.getImageData(0,0,e.width,e.height).data))return{miss:"blank"};i.globalCompositeOperation="destination-over",i.fillStyle=s(),i.fillRect(0,0,e.width,e.height);for(let l of[t.quality,.6]){let c=o.toDataURL("image/jpeg",l);if(!c.startsWith("data:image/jpeg;base64,"))return{miss:"unsupported"};if((c.length-23)*.75<=X.maxBytes)return{image:c}}return{miss:"failed"}}catch(o){return{miss:o?.name==="SecurityError"?"tainted":"failed"}}}function jt(n,e){let t=!1,r=()=>{t||(t=!0,e())};n.requestAnimationFrame(r),n.setTimeout(r,ar)}function gr(n,e,t){return new Promise(r=>{let s,o;try{if(typeof n.captureStream!="function")return r(null);s=n.captureStream(),o=e.document.createElement("video"),o.muted=!0,o.playsInline=!0,o.srcObject=s}catch{return r(null)}let i=!1,l=m=>{if(!i){i=!0,e.clearTimeout(c);try{o.srcObject=null;for(let h of s.getTracks())h.stop()}catch{}r(m)}},c=e.setTimeout(()=>l(null),lr),d=()=>{!i&&o.videoWidth>0&&o.videoHeight>0&&l(t(o))};try{o.addEventListener("loadeddata",d),o.addEventListener("playing",d),o.play()?.catch(()=>{})}catch{l(null)}})}function Ht(n,e=cr()){let t=!1,r=o=>{try{n(o)}catch{}};function s(o){let i=o.id;if(!e)return r({id:i,t:"none",reason:"unsupported"});if(t)return r({id:i,t:"none",reason:"busy"});let l=e,c=ie(l.document.querySelectorAll("canvas"));if(!c)return r({id:i,t:"none",reason:"no-canvas"});t=!0;let d=k=>{t=!1,r("image"in k?{id:i,t:"done",image:k.image}:{id:i,t:"none",reason:k.miss})},m=dr(c.width,c.height,o.maxSide),h=null,f=()=>h??(h=fr(c,l));if(l.hidden?.()){let k=Oe(c,m,o,l,f);return d("miss"in k&&k.miss==="blank"?{miss:"hidden"}:k)}let w=0,y=()=>{try{let k=Oe(c,m,o,l,f);if(!("miss"in k)||k.miss!=="blank")return d(k);if(++w<ir)return jt(l,y);gr(c,l,E=>Oe(E,m,o,l,f)).then(E=>d(E??{miss:"blank"}),()=>d({miss:"blank"}))}catch{d({miss:"failed"})}};jt(l,y)}return{receive(o){let i=null;try{i=ur(o),i&&s(i)}catch{t=!1,i&&r({id:i.id,t:"none",reason:"failed"})}}}}var qt=(n,e)=>Object.prototype.hasOwnProperty.call(n,e);function Te(n){try{return n(),!0}catch{return!1}}function hr(){let n=new Map,e={getItem:t=>n.has(String(t))?n.get(String(t)):null,setItem:(t,r)=>{n.set(String(t),String(r))},removeItem:t=>{n.delete(String(t))},clear:()=>n.clear(),key:t=>[...n.keys()][Number(t)]??null};return new Proxy(e,{get(t,r){if(r==="length")return n.size;if(r===Symbol.toStringTag)return"Storage";if(typeof r=="string")return qt(e,r)?t[r]:n.get(r)},set(t,r,s){return typeof r=="string"&&n.set(r,String(s)),!0},has:(t,r)=>typeof r=="string"&&(qt(e,r)||n.has(r)),deleteProperty(t,r){return typeof r=="string"&&n.delete(r),!0},ownKeys:()=>[...n.keys()],getOwnPropertyDescriptor:(t,r)=>typeof r=="string"&&n.has(r)?{value:n.get(r),writable:!0,enumerable:!0,configurable:!0}:void 0})}function yr(n=Date.now){let e=new Map;return{read:()=>[...e].map(([t,r])=>`${t}=${r}`).join("; "),write(t){let[r,...s]=String(t).split(";"),o=r.indexOf("="),i=(o<0?"":r.slice(0,o)).trim(),l=(o<0?r:r.slice(o+1)).trim();if(!i&&!l)return;let c=!1;for(let d of s){let[m,...h]=d.split("="),f=h.join("=").trim();m.trim().toLowerCase()==="max-age"&&Number(f)<=0&&(c=!0),m.trim().toLowerCase()==="expires"&&Date.parse(f)<=n()&&(c=!0)}c?e.delete(i):e.set(i,l)}}}function ee(n,e,t){try{Object.defineProperty(n,e,{configurable:!0,...t})}catch{}}function br(){if(typeof HTMLImageElement!="function")return;let n=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");if(!n?.get||!n.set)return;let{get:e,set:t}=n;ee(HTMLImageElement.prototype,"src",{enumerable:n.enumerable,get(){return e.call(this)},set(r){try{this.crossOrigin===null&&new URL(String(r),document.baseURI).origin===location.origin&&(this.crossOrigin="anonymous")}catch{}t.call(this,r)}})}function wr(){let n=window.Worker;if(typeof n!="function")return;let e=function(t,r){try{return new n(t,r)}catch(s){if(!(s instanceof DOMException&&s.name==="SecurityError")||r?.type==="module")throw s;let o;try{o=new URL(String(t),document.baseURI)}catch{throw s}if(o.origin!==location.origin)throw s;let l=`const base = ${JSON.stringify(o.href)};
|
|
1
|
+
"use strict";(()=>{var Qn=Object.defineProperty;var Zn=(n,e,t)=>e in n?Qn(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var c=(n,e,t)=>Zn(n,typeof e!="symbol"?e+"":e,t);var Xn=["#f6dcc5","#ebc29f","#d4a07a","#b07a55","#875839","#5e3d29","#93c9bb","#a8c595","#94bcd3","#ab9bd9","#dca3ad","#b3bccb"],vt=["#c95f56","#dc8a4f","#e2bd52","#9cc27a","#5a8a5e","#4f9a8e","#6fb7c8","#5b87c5","#465089","#8b68a8","#dc8fae","#f2eee6","#a9a8ad","#5d5c63","#2b2a30","#80604d"],er=["Happy","Grin","Shades","Wink","Surprised","Determined","Joyful","Shy"],tr=["None","Sprout","Cap","Beanie","Crown","Headphones","Horns","Antenna","Bow","Halo","Party hat","Cat ears","Short hair","Tuft","Bob","Spikes","Bun","Curls","Long","Mop"];var nr=["Plain","Band","Spots","Stripes","Star"];var rr={skin:Xn.length,shirt:vt.length,pants:vt.length,face:er.length,hat:tr.length,top:nr.length};function St(n){if(!n||typeof n!="object")return null;let e=n,t={};for(let[r,o]of Object.entries(rr)){let s=e[r],i=typeof s=="number"?s:typeof s=="string"&&/^[0-9]{1,3}$/.test(s)?Number(s):NaN;if(!Number.isInteger(i)||i<0||i>=o)return null;t[r]=i}return t}var E={roomMaxPlayersDefault:16,roomMaxPlayersCap:30,teamsMax:8,messageBytes:16*1024,presenceBytes:1024,stateKeyLength:64,stateValueBytes:16*1024,stateKeys:256,privateKeys:16,privateValueBytes:4*1024,privateBytesPerPlayer:8*1024,messagesPerSecond:60,messagesPerSecondMin:10,roomBytesPerSecond:128*1024,roomBytesPerSecondMin:16*1024,presenceHz:20,presenceHzMin:5,roomMessagesPerSecond:900,roomBytesPerSecondTotal:2*1024*1024,matchCountdownMs:3e3,matchCountdownMaxMs:1e4,matchIdleMs:45e3,matchAbandonMs:12e4,idleAfterMs:3e4,idleAbandonMs:5*6e4,rankedPairMatchesPerDay:5,chatLength:200,chatIntervalMs:750,chatHistory:50,chatChannelsPerPlayer:10,saveValueBytes:64*1024,saveKeysPerGame:100,saveBytesPerGame:1024*1024,guestSaveBytesPerGame:128*1024,serverDataKeys:128,serverDataBytes:512*1024,serverDataServersPerGame:5e3,serverDataBytesPerGame:64*1024*1024,serverDataIdleDays:120,gameDataKeys:32,gameDataListMax:100,gameDataItemBytes:1024,gameDataValueBytes:8*1024,gameDataMapFields:1e3,gameDataMapPerPlayer:100,gameDataNumberMax:1e15,gameDataWritesPerMinute:30,deployFiles:2e3,deployFileBytes:25*1024*1024,deployHtmlBytes:10*1024*1024,deployPathLength:200,deployTotalBytes:100*1024*1024,envVars:50,envValueBytes:4*1024,envTotalBytes:16*1024,proxyRequestBytes:1024*1024,proxyTextRequestBytes:256*1024,proxySpeechRequestBytes:16*1024,proxyResponseBytes:10*1024*1024,proxyMaxOutputTokens:4096,proxyCallsPerDay:2e3,proxyCallsPerDayMax:1e5,proxyCallsPerPlayerPerDay:100,proxyCallsPerNetworkPerDay:1e3,proxyMediaCallsPerDay:100,proxyMediaCallsPerDayMax:1e4,proxyMediaCallsPerPlayerPerDay:20,proxyMediaCallsPerNetworkPerDay:25,proxyUnitsPerDay:2e6,proxyUnitsPerDayMax:1e8,proxyUnitsPerPlayerPerDay:25e4,proxyUnitsPerGuestPerDay:1e5,proxyUnitsPerNetworkPerDay:5e5,errorReportsPerPage:10,consoleLineLength:2e3,consoleLinesPerSecond:50,gameNameLength:60,gameDescriptionLength:1e3,galleryItems:5,mediaImageBytes:5*1024*1024,mediaVideoBytes:30*1024*1024,mediaGifBytes:1024*1024,mediaGifSide:512,mediaUploadsPerDay:200,mediaBytesPerDay:500*1024*1024,badgesPerGame:50,controlHints:12,controlHintKeys:4,controlKeyLength:12,controlActionLength:40,helpLength:400,badgeNameLength:50,badgeDescriptionLength:200,leaderboardsPerGame:25,leaderboardTop:100,gamesPerOwner:50,gamesCreatedPerCreatorPerDay:3,uploadAccountAgeMs:10080*6e4,deploysPerOwnerPerDay:100,draftsPerOwnerPerDay:1e3,draftsKeptPerGame:3,cliTokensPerAccount:10,cliDeviceCodeSeconds:600,deploymentsKeptPerGame:10,deployBytesPerOwnerPerDay:2*1024*1024*1024,deployEntriesPerOwnerPerDay:1e5,bioLength:500,exportRows:2e3,exportSaveChars:4*1024*1024,exportEveryMs:60*6e4,friendsMax:200,friendRequestsMax:100,friendRequestsIncomingMax:50,friendRequestsPerDay:20,friendRequestsPerNetworkPerDay:120,friendRequestPushesPerDay:5,passwordMinLength:8,passwordMaxLength:72,signupsPerNetworkPerDay:100,signupsPerWidePerDay:300,guestsPerNetworkPerDay:300,guestsPerWidePerDay:1500,visitsPerNetworkPerDay:10,visitSeconds:10,errorsPerDeployment:100,controlButtons:4,controlLabelLength:12,partySize:6,partyChatHistory:50,messageLength:500,messagesPerPage:50,groupChatMembers:20,groupChatNameLength:40,conversationsListed:50,notificationsListed:50,invitesPerRequest:10,inviteLiveMs:30*6e4,partyInviteMs:60*6e4};var or="host:",F="data:";function te(n){return n.startsWith(or)||n.startsWith(F)}var Et=/^(?!__proto__$)[A-Za-z0-9_.:-]{1,59}$/;function Ke(n){return n.startsWith(F)}function He(n){return F+n}function Mt(n){return n.startsWith("private:")||n.startsWith("room:")}function qe(n){let e=n.length;for(let t=0;t<n.length;t++){let r=n.charCodeAt(t);r<128||(r<2048?e+=1:r>=55296&&r<=56319&&t+1<n.length&&(n.charCodeAt(t+1)&64512)===56320?(e+=2,t++):e+=2)}return e}var Ue=/^[A-Za-z][A-Za-z0-9_.-]{0,31}$/;var sr={counter:["add"],list:["push"],record:["best"],map:["claim","release"],value:["set"]},ir={add:"add(key, n)",push:"push(key, item)",best:"best(key, n)",claim:"claim(key, field)",release:"release(key, field)",set:"set(key, value)"};function ar(n){return sr[n].map(e=>`ow.data.game.${ir[e]}`).join(" or ")}function ze(n,e){let r=Math.floor(e/864e5);if(n==="daily")return{start:r*864e5,next:(r+1)*864e5};let o=r-(r+3)%7;return{start:o*864e5,next:(o+7)*864e5}}function lr(n,e){switch(n.type){case"counter":return{type:"counter",value:n.min,...n.reset?{resetsAt:ze(n.reset,e).next}:{}};case"list":return{type:"list",items:[]};case"record":return{type:"record",value:null,by:null,at:null};case"map":return{type:"map",fields:{}};case"value":return{type:"value",value:null,at:null}}}function ur(n,e,t){if(!e||e.type!==n.type)return lr(n,t);if(e.type==="counter"&&n.type==="counter"){if(!n.reset)return{type:"counter",value:e.value};let s=ze(n.reset,t);return{type:"counter",value:(e.period??0)>=s.start?e.value:n.min,resetsAt:s.next}}let{period:r,...o}=e;return o}function Rt(n,e,t,r,o){if(r.guest&&!n.guests)return{code:"sign_in",message:`Log in to change '${t.key}': guests can't (a ${n.type} is for signed-in players unless onceworlds.json says "guests": true).`};let s={id:r.id,name:r.name},i=ur(n,e,o);switch(i.type){case"counter":{if(t.op!=="add"||n.type!=="counter")break;let a=Math.min(n.max,Math.max(n.min,i.value+t.n)),u=n.reset?ze(n.reset,o):null;return{next:{type:"counter",value:a,...u?{resetsAt:u.next,period:u.start}:{}},result:{type:"counter",value:a}}}case"list":{if(t.op!=="push"||n.type!=="list")break;return{next:{type:"list",items:[{value:t.item,by:s,at:o},...i.items].slice(0,n.max)},result:{type:"list",ok:!0}}}case"record":{if(t.op!=="best"||n.type!=="record")break;return i.value!==null&&i.by&&i.at!==null&&!(n.order==="max"?t.n>i.value:t.n<i.value)?{next:i,result:{type:"record",value:i.value,by:i.by,at:i.at,record:!1}}:{next:{type:"record",value:t.n,by:s,at:o},result:{type:"record",value:t.n,by:s,at:o,record:!0}}}case"map":{if(t.op!=="claim"&&t.op!=="release"||n.type!=="map")break;let a={...i.fields},u=Object.hasOwn(a,t.field)?a[t.field]:void 0;return t.op==="release"?!u||u.by.id!==r.id?{next:i,result:{type:"map",ok:!1,...u?{holder:u.by}:{}}}:(delete a[t.field],{next:{type:"map",fields:a},result:{type:"map",ok:!0}}):u?{next:i,result:{type:"map",ok:u.by.id===r.id,holder:u.by}}:Object.values(a).filter(f=>f.by.id===r.id).length>=n.perPlayer?{code:"limit",message:`claim('${t.key}'): a player holds at most ${n.perPlayer} (onceworlds.json's "perPlayer"). Release one first.`}:Object.keys(a).length>=n.fields?{code:"full",message:`claim('${t.key}'): all ${n.fields} fields are taken (onceworlds.json's "fields").`}:(a[t.field]={by:s,at:o},{next:{type:"map",fields:a},result:{type:"map",ok:!0}})}case"value":{if(t.op!=="set"||n.type!=="value")break;return r.host?{next:{type:"value",value:t.value,at:o},result:{type:"value",ok:!0}}:{code:"not_host",message:`set('${t.key}'): only the host of one of the game's rooms sets a value. Join a room, and set it from the host's page (room.isHost).`}}}return{code:"wrong_op",message:`'${t.key}' is a ${n.type}: use ${ar(n.type)}.`}}var xt=n=>n.replace(/[\u0000-\u001f\u007f]/g," ").replace(/\s+/g," ").trim();var cr=/^[a-z0-9][a-z0-9-]{0,19}$/;function Pt(n){if(!n||typeof n!="object")return null;let e=n,t=[];for(let o of Array.isArray(e.buttons)?e.buttons:[]){if(t.length>=E.controlButtons)break;let{id:s,label:i}=o??{};if(typeof s!="string"||!cr.test(s)||t.some(u=>u.id===s))continue;let a=typeof i=="string"?i.replace(/[\u0000-\u001f\u007f]/g,"").replace(/\s+/g," ").trim().slice(0,E.controlLabelLength).trim():"";t.push({id:s,label:a||s})}let r=e.stick===!0;return r||t.length>0?{stick:r,zone:e.zone==="corner"?"corner":"wide",buttons:t}:null}var ne="host:ow.settings",dr=/^[a-z][a-zA-Z0-9]{0,15}$/;function Fe(n){if(!Array.isArray(n))return[];let e=[];for(let t of n){if(e.length>=6)break;let{id:r,label:o,options:s,default:i}=t??{};if(typeof r!="string"||!dr.test(r)||e.some(f=>f.id===r)||!Array.isArray(s))continue;let a=[];for(let f of s){let d=typeof f=="object"&&f!==null?f.value:f;if(typeof d!="string"&&typeof d!="number"||typeof d=="number"&&!Number.isFinite(d)||a.length>=8||a.some(h=>h.value===d))continue;let g=typeof f=="object"&&f!==null&&typeof f.label=="string"?f.label:String(d);a.push({value:d,label:xt(g).slice(0,20)||String(d)})}if(a.length<2)continue;let u=typeof o=="string"?xt(o).slice(0,24):"";e.push({id:r,label:u||r,options:a,default:a.some(f=>f.value===i)?i:a[0].value})}return e}function At(n,e){let t=e!==null&&typeof e=="object"?e[n.id]:void 0;return n.options.some(r=>r.value===t)?t:n.default}var Dt="onceworlds:hello",Ct="onceworlds:connect",$t=/^[a-z0-9]{1,32}~(?:(?:0|[1-9]\d{0,3})\.(?:0|[1-9]\d{0,4})\.(?:0|[1-9]\d{0,5}))?~[0-9a-z]{1,11}~[lp]~[A-Za-z0-9_-]{43}$/;var Tt=["log","info","warn","error"],Lt="onceworlds-console";var It=/^[A-Za-z0-9-]{1,64}$/;function Ot(){let n=1,e=!1,t=()=>e?0:n,r=new Map,o=new WeakSet;if(typeof AudioNode<"u"&&typeof AudioDestinationNode<"u"){let d=AudioNode.prototype.connect,g=AudioNode.prototype.disconnect,h=y=>typeof OfflineAudioContext>"u"||!(y instanceof OfflineAudioContext),A=y=>{let w=r.get(y);return w||(w=y.createGain(),w.gain.value=t(),d.call(w,y.destination),r.set(y,w)),w};AudioNode.prototype.connect=function(y,...w){return y instanceof AudioDestinationNode&&h(y.context)&&r.get(y.context)!==this?(d.call(this,A(y.context),...w),y):d.call(this,y,...w)},AudioNode.prototype.disconnect=function(...y){let w=y[0];if(w instanceof AudioDestinationNode){let M=r.get(w.context);M&&M!==this&&(y[0]=M)}return g.apply(this,y)}}let s=new WeakMap,i=new Set,a=typeof HTMLMediaElement<"u"?Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype,"volume"):void 0,u=d=>a?.set?.call(d,Ve((s.get(d)??1)*t())),f=d=>{s.has(d)||!a?.get||(s.set(d,a.get.call(d)),i.add(new WeakRef(d)))};if(a?.get&&a.set){Object.defineProperty(HTMLMediaElement.prototype,"volume",{configurable:!0,enumerable:a.enumerable,get(){return s.has(this)?s.get(this):a.get.call(this)},set(g){f(this),s.set(this,Ve(Number(g))),u(this)}});let d=HTMLMediaElement.prototype.play;HTMLMediaElement.prototype.play=function(){return f(this),u(this),d.call(this)}}return{get volume(){return n},get muted(){return e},apply(d){n=Ve(d.volume),e=d.muted;for(let[g,h]of r)h.gain.setTargetAtTime(t(),g.currentTime,.02);for(let g of i){let h=g.deref();h?u(h):i.delete(g)}},wake(){for(let d of r.keys())if(!o.has(d)&&(o.add(d),!(d.state==="running"||d.state==="closed")))try{d.resume?.().catch(()=>{})}catch{}}}}function Ve(n){return Number.isFinite(n)?Math.min(1,Math.max(0,n)):1}function _t(n){try{let e=n?.getAttribute("data-build");return e&&$t.test(e)?e:null}catch{return null}}var B={ms:9e3,warmupMs:1e3,flowSettleMs:1500,playSettleMs:2e3,motionGraceMs:5e3,ceilingMs:25e3,graceMs:12e3,maxBytes:6*1024*1024,posterBytes:1024*1024,long:1280,short:720,fps:30,bitsPerSecond:25e5};var fr=["video/webm;codecs=vp9","video/webm;codecs=vp8","video/webm","video/mp4;codecs=avc1","video/mp4"],mr=3e3,Bt=250,hr="host:ow.flow";function Nt(n){let e=n?.[hr],t=e&&typeof e=="object"?e.phase:void 0;return typeof t=="string"?t:void 0}var We=250,ye=32,be=18,gr=.02,Je={needed:6,of:8};function yr(){return typeof window>"u"||typeof document>"u"?null:{document,MediaRecorder:globalThis.MediaRecorder,requestAnimationFrame:n=>window.requestAnimationFrame(n),setTimeout:(n,e)=>window.setTimeout(n,e),clearTimeout:n=>window.clearTimeout(n)}}var Q=(n,e,t,r)=>typeof n=="number"&&Number.isFinite(n)?Math.min(t,Math.max(e,Math.round(n))):r;function br(n){if(!n||typeof n!="object")return null;let e=n;return e.t==="stop"?{t:"stop"}:e.t!=="start"?null:{t:"start",ms:Q(e.ms,1e3,15e3,B.ms),long:Q(e.long,160,1920,B.long),short:Q(e.short,90,1080,B.short),fps:Q(e.fps,5,60,B.fps),bitsPerSecond:Q(e.bitsPerSecond,25e4,8e6,B.bitsPerSecond),maxBytes:Q(e.maxBytes,64*1024,B.maxBytes,B.maxBytes)}}function we(n){let e=null,t=0;for(let r of Array.from(n))try{if(r.isConnected===!1||r.width<64||r.height<64)continue;let o=typeof r.getBoundingClientRect=="function"?r.getBoundingClientRect():null;if(o&&(o.width<32||o.height<32))continue;let s=o?o.width*o.height:r.width*r.height;s>t&&(e=r,t=s)}catch{}return e}function wr(n,e,t,r){let o=n>=e,s=Math.min(1,(o?t:r)/n,(o?r:t)/e);if(s>=1)return{width:n,height:e,scale:1};let i=a=>Math.max(2,Math.floor(a*s/2)*2);return{width:i(n),height:i(e),scale:s}}function kr(n,e){return new Promise(t=>{e.requestAnimationFrame(()=>{try{let r=n.width,o=n.height,s=Math.min(B.long,Math.max(480,r)),i=Math.round(s*9/16),a=e.document.createElement("canvas");a.width=s,a.height=i;let u=a.getContext("2d");if(!u)return t(null);u.fillStyle="#000",u.fillRect(0,0,s,i);let f=r/o,d=f>=1.25&&f<=2.4?Math.max(s/r,i/o):Math.min(s/r,i/o);u.drawImage(n,(s-r*d)/2,(i-o*d)/2,r*d,o*d),a.toBlob(g=>g?g.arrayBuffer().then(t,()=>t(null)):t(null),"image/jpeg",.85)}catch{t(null)}})})}function vr(n,e){let t=null,r=null;return o=>{e.requestAnimationFrame(()=>{try{if(!t){let u=e.document.createElement("canvas");u.width=ye,u.height=be,t=u.getContext("2d",{willReadFrequently:!0})}if(!t)return o(null);t.drawImage(n,0,0,ye,be);let s=t.getImageData(0,0,ye,be).data,i=r;if(r=new Uint8ClampedArray(s),!i)return o(null);let a=0;for(let u=0;u+2<s.length;u+=4){let f=s[u]*2+s[u+1]*5+s[u+2],d=i[u]*2+i[u+1]*5+i[u+2];Math.abs(f-d)>128&&a++}o(a/(ye*be))}catch{o(null)}})}}function Sr(n,e,t,r){return new Promise(o=>{let s=vr(n,e),i=[],a=0,u=0,f=!1,d=()=>{if(r()||a>=B.ceilingMs)return o(!1);let g=null;try{g=t()}catch{}g&&g.phase!=="lobby"&&(f=!0);let h=g?.flow;if(g?.phase==="playing"&&g.paused!==!0&&(h===void 0||h==="playing")){if(u+=We,u>(h===void 0?B.playSettleMs:B.flowSettleMs))return o(!0)}else u=0;if(!f){if(a>=B.motionGraceMs&&i.length>=Je.of&&i.filter(Boolean).length>=Je.needed)return o(!0);s(A=>{i.push(A!==null&&A>=gr),i.length>Je.of&&i.shift()})}a+=We,e.setTimeout(d,We)};d()})}function jt(n,e){let t=e.MediaRecorder;return typeof n.captureStream!="function"||typeof t!="function"?null:fr.find(r=>{try{return t.isTypeSupported(r)}catch{return!1}})??null}function Gt(n,e=yr(),t=()=>null){let r=null,o=a=>{try{n(a)}catch{}},s=a=>o({t:"none",reason:a});function i(a){if(r)return;if(!e)return s("unsupported");let u=e,f=!1,d=null;r={cancel(){f=!0,d!==null&&u.clearTimeout(d)}};let g=y=>{r=null,!f&&y&&o(y)},h=0,A=()=>{if(f)return g(null);let y=we(u.document.querySelectorAll("canvas"));if(y){if(d=null,!jt(y,u))return g({t:"none",reason:"unsupported"});Sr(y,u,t,()=>f).then(w=>{if(f)return null;if(!w)return{t:"none",reason:"not-playing"};let M=we(u.document.querySelectorAll("canvas"))??y;return xr(M,a,u,()=>f,()=>!f&&o({t:"started"}))}).then(g,()=>g({t:"none",reason:"failed"}))}else++h>mr/Bt?g({t:"none",reason:"no-canvas"}):d=u.setTimeout(A,Bt)};A()}return{receive(a){try{let u=br(a);if(!u)return;if(u.t==="stop"){r?.cancel(),r=null;return}i(u)}catch{r=null,s("failed")}}}}function xr(n,e,t,r,o){let s=t.MediaRecorder,i=jt(n,t);if(!s||!i)return Promise.resolve({t:"none",reason:"unsupported"});let a=wr(n.width,n.height,e.long,e.short),u=!1,f=n;if(a.scale<1){let h=t.document.createElement("canvas");h.width=a.width,h.height=a.height;let A=h.getContext("2d");if(!A)return Promise.resolve({t:"none",reason:"unsupported"});u=!0;let y=()=>{if(u){try{A.drawImage(n,0,0,a.width,a.height)}catch{}t.requestAnimationFrame(y)}};y(),f=h}let d,g;try{d=f.captureStream(e.fps),g=new s(d,{mimeType:i,videoBitsPerSecond:e.bitsPerSecond})}catch{return u=!1,Promise.resolve({t:"none",reason:"failed"})}return new Promise(h=>{let A=[],y=0,w=Promise.resolve(null),M=t.setTimeout(()=>{w=kr(n,t)},Math.round(e.ms/2)),L=null,O=()=>{try{g.state!=="inactive"&&g.stop()}catch{}},m=()=>{u=!1,t.clearTimeout(M),L!==null&&t.clearTimeout(L);try{for(let D of d.getTracks())D.stop()}catch{}};g.ondataavailable=D=>{!D.data||D.data.size===0||(A.push(D.data),y+=D.data.size,y>e.maxBytes*.85&&O())},g.onerror=()=>{m(),h(r()?null:{t:"none",reason:"failed"})},g.onstop=()=>{if(m(),r())return h(null);let D=new Blob(A,{type:g.mimeType||i});Promise.all([D.arrayBuffer(),w.catch(()=>null)]).then(([N,q])=>{if(N.byteLength===0)return h({t:"none",reason:"failed"});if(N.byteLength>e.maxBytes)return h({t:"none",reason:"too-large"});h({t:"done",type:D.type||i,video:N,poster:q,width:a.width,height:a.height})}).catch(()=>h({t:"none",reason:"failed"}))};try{g.start(1e3)}catch{return m(),h({t:"none",reason:"failed"})}o(),L=t.setTimeout(()=>{L=null,O()},e.ms);let k=()=>{if(g.state!=="inactive"){if(r())return O();t.setTimeout(k,200)}};t.setTimeout(k,200)})}var Er=200,Kt=2,Z=20,Ht=200;function qt(n){try{return new URLSearchParams(n).get(Lt)==="1"}catch{return!1}}function Ut(n){return typeof n=="object"&&n!==null&&n.solo===!0?"standalone":"platform"}function zt(n){return typeof n=="object"&&n!==null&&n.demo===!0}function Ft(n){try{let e=new URLSearchParams(n).get("demo");return e!==null&&e!=="0"&&e!=="false"}catch{return!1}}function Vt(n,e,t=Date.now,r=setTimeout){let o=null,s=[],i=-1/0,a=0,u=0,f=!1,d=!1,g=y=>{o?o(y):s.length<Er&&s.push(y)},h=()=>{let y=t();return y-i>=1e3&&(i=y,a=0),a<E.consoleLinesPerSecond?(a++,!0):(u++,f||(f=!0,r(()=>{f=!1;let w=u;u=0,w>0&&h()&&g({level:"warn",text:`${w} more ${w===1?"line":"lines"} left out`,at:t()})},Math.max(0,1e3-(y-i)))),!1)},A=(y,w,M)=>{if(!h())return;let L=Wt(e?w.split(`${e}/`).join(""):w);g({level:y,text:L,at:t(),...M?{source:M.source.slice(0,300),line:M.line,column:M.column}:{}})};for(let y of Tt){let w=n[y];typeof w=="function"&&(n[y]=function(...M){if(w.apply(n,M),!d){d=!0;try{A(y,Mr(M))}catch{}finally{d=!1}}})}return{error(y,w){if(!d){d=!0;try{A("error",y,w)}catch{}finally{d=!1}}},connect(y){o=y;for(let w of s.splice(0))y(w)}}}function Mr(n){let e="",t=n;if(typeof n[0]=="string"){let r=1;e=n[0].replace(/%[sdifoOc%]/g,o=>{if(o==="%%")return"%";if(r>=n.length)return o;let s=n[r++];return o==="%c"?"":o==="%d"||o==="%i"?String(typeof s=="number"?Math.trunc(s):parseInt(re(s),10)):o==="%f"?String(typeof s=="number"?s:parseFloat(re(s))):o==="%s"?re(s):X(s)}),t=n.slice(r)}for(let r of t)e+=e?` ${re(r)}`:re(r);return Wt(e)}function re(n){return typeof n=="string"?n:X(n)}function X(n,e=0){try{switch(typeof n){case"string":return JSON.stringify(n.length>Ht?`${n.slice(0,Ht)}\u2026`:n);case"bigint":return`${n}n`;case"symbol":return n.toString();case"function":return`\u0192 ${n.name||"anonymous"}()`;case"object":break;default:return String(n)}if(n===null)return"null";if(n instanceof Error)return ke(n);if(n instanceof Date)return Number.isNaN(n.getTime())?"Invalid Date":n.toISOString();if(n instanceof RegExp)return String(n);if(n instanceof Map||n instanceof Set)return`${n.constructor.name}(${n.size})`;if(ArrayBuffer.isView(n))return`${n.constructor.name}(${"length"in n?n.length:n.byteLength})`;if(typeof Element<"u"&&n instanceof Element)return`<${n.tagName.toLowerCase()}>`;if(Array.isArray(n))return e>=Kt?`Array(${n.length})`:`[${n.slice(0,Z).map(i=>X(i,e+1)).join(", ")}${n.length>Z?`, \u2026${n.length-Z} more`:""}]`;let t=typeof n.constructor=="function"&&n.constructor!==Object?`${n.constructor.name} `:"";if(e>=Kt)return`${t}{\u2026}`;let r=Object.keys(n),o=r.slice(0,Z).map(s=>`${/^[A-Za-z_$][\w$]*$/.test(s)?s:JSON.stringify(s)}: ${X(n[s],e+1)}`);return`${t}{${o.join(", ")}${r.length>Z?`, \u2026${r.length-Z} more`:""}}`}catch{return Object.prototype.toString.call(n)}}function ke(n){try{let e=`${n.name||"Error"}: ${n.message}`,t=typeof n.stack=="string"?n.stack:"";return t?t.startsWith(e)?t:`${e}
|
|
2
|
+
${t}`:e}catch{return"Error"}}function Wt(n){return n.length>E.consoleLineLength?`${n.slice(0,E.consoleLineLength-1)}\u2026`:n}var Rr={wasd:{up:"w",down:"s",left:"a",right:"d"},arrows:{up:"ArrowUp",down:"ArrowDown",left:"ArrowLeft",right:"ArrowRight"}},Pr=.45,Ar=.3,Ye={" ":["Space",32],Enter:["Enter",13],Shift:["ShiftLeft",16],Control:["ControlLeft",17],Alt:["AltLeft",18],Escape:["Escape",27],Tab:["Tab",9],Backspace:["Backspace",8],ArrowLeft:["ArrowLeft",37],ArrowUp:["ArrowUp",38],ArrowRight:["ArrowRight",39],ArrowDown:["ArrowDown",40]},ve=class{constructor(e,t){c(this,"touch");c(this,"stick",{x:0,y:0});c(this,"held",new Set);c(this,"downs",new Map);c(this,"stickMode");c(this,"buttonKeys",new Map);c(this,"down",new Map);c(this,"show");c(this,"reportTouch");this.show=e,this.reportTouch=t,this.touch=typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches,window.addEventListener("pointerdown",r=>r.isTrusted&&this.setTouch(r.pointerType!=="mouse"),!0),window.addEventListener("keydown",r=>r.isTrusted&&this.setTouch(!1),!0),window.addEventListener("blur",()=>this.release()),document.addEventListener("visibilitychange",()=>document.hidden&&this.release())}set(e){let t=e??{};this.stickMode=t.stick==="wasd"||t.stick==="arrows"||t.stick==="analog"?t.stick:void 0;let r=Array.isArray(t.buttons)?t.buttons:[],o=e?Pt({stick:this.stickMode!==void 0,zone:t.zone,buttons:r}):null;this.buttonKeys=new Map;for(let s of o?.buttons??[]){let i=r.find(a=>a?.id===s.id)?.key;typeof i=="string"&&i&&this.buttonKeys.set(s.id,i)}this.show(o),o?this.update():this.release()}receive(e){let t=o=>typeof o=="number"&&Number.isFinite(o)?Math.max(-1,Math.min(1,o)):0;this.stick.x=t(e?.x),this.stick.y=t(e?.y);let r=new Set(Array.isArray(e?.buttons)?e.buttons.filter(o=>typeof o=="string"):[]);for(let o of r)this.held.has(o)||this.downs.set(o,(this.downs.get(o)??0)+1);this.held=r,this.update()}pressed(e){return this.held.has(e)}presses(e){return this.downs.get(e)??0}update(){let e=new Map;if(this.stickMode==="wasd"||this.stickMode==="arrows"){let t=Rr[this.stickMode],r=(o,s)=>{s>(this.down.has(o)?Ar:Pr)&&e.set(o,Jt(o))};r(t.up,-this.stick.y),r(t.down,this.stick.y),r(t.left,-this.stick.x),r(t.right,this.stick.x)}for(let t of this.held){let r=this.buttonKeys.get(t);r&&e.set(r,Jt(r))}for(let[t,r]of this.down)e.has(t)||(this.down.delete(t),Yt(r,!1));for(let[t,r]of e)this.down.has(t)||(this.down.set(t,r),Yt(r,!0))}release(){this.stick.x=0,this.stick.y=0,this.held.clear(),this.update()}setTouch(e){e!==this.touch&&(this.touch=e,e||this.release(),this.reportTouch(e))}};function Jt(n){let e=n==="Space"?" ":n;return Object.prototype.hasOwnProperty.call(Ye,e)?{key:e,code:Ye[e][0],keyCode:Ye[e][1]}:/^[a-z]$/i.test(e)?{key:e.toLowerCase(),code:`Key${e.toUpperCase()}`,keyCode:e.toUpperCase().charCodeAt(0)}:/^[0-9]$/.test(e)?{key:e,code:`Digit${e}`,keyCode:48+Number(e)}:{key:e,code:"",keyCode:0}}function Yt(n,e){try{let t={key:n.key,code:n.code,keyCode:n.keyCode,which:n.keyCode,bubbles:!0,cancelable:!0},r=new KeyboardEvent(e?"keydown":"keyup",t);r.keyCode!==n.keyCode&&(Object.defineProperty(r,"keyCode",{get:()=>n.keyCode}),Object.defineProperty(r,"which",{get:()=>n.keyCode})),(document.activeElement??document.body??document).dispatchEvent(r)}catch{}}function Dr(n){switch(n.type){case"counter":return n.value;case"list":return n.items;case"record":return n.value===null?null:{value:n.value,by:n.by,at:n.at};case"map":return n.fields;case"value":return n.value}}function Qt(n=e=>console.warn(`[onceworlds] ${e}`)){let e=new Map;return t=>{let r=Date.now();r-(e.get(t)??-1/0)<5e3||(e.size>200&&e.clear(),e.set(t,r),n(t))}}function Zt(n,e){let t=new Set,r=async(o,s)=>{let i=`onceworlds.data.server.${s===null?"delete":"set"}('${String(o).slice(0,40)}')`,a=n();if(typeof o!="string"||!Et.test(o))return e(`${i} did nothing: a key is 1-59 letters, digits or _ . : -`),!1;if(!a||a.closed)return e(`${i} did nothing: you're not in a room. Join one first (await onceworlds.rooms.join({ private: true })): server data belongs to a server.`),!1;if(!a.connected)return e(`${i} did nothing: the connection is down and the platform is reconnecting. Write it again after room.on('reconnect').`),!1;if(!a.isHost)return e(`${i} did nothing: only the room's host writes server data. Check onceworlds.data.server.canWrite, or send the change to the host (room.send) and let it write.`),!1;let u;try{u=JSON.stringify(s??null)}catch{u=void 0}return u===void 0?(e(`${i} did nothing: the value must be plain JSON data (numbers, text, lists, objects).`),!1):qe(u)>E.stateValueBytes-128?(e(`${i} did nothing: the value is ${Math.ceil(qe(u)/1024)} KB, and a key holds 16 KB. Split it into keys by area or chunk (a server keeps ${E.serverDataKeys} keys, ${E.serverDataBytes/1024} KB), or let the engine's Persist() save the world for you.`),!1):await a._writeAcked(He(o),s??null)};return{get(o){let s=n();if(!s||typeof o!="string")return null;let i=s.state[He(o)];return i===void 0?null:i},all(){let o=n(),s={};if(!o)return s;for(let[i,a]of Object.entries(o.state))i.startsWith("data:")&&(s[i.slice(5)]=a);return s},set:(o,s)=>r(o,s??null),delete:o=>r(o,null),get kept(){return n()?.kept??!1},get canWrite(){let o=n();return!!(o&&!o.closed&&o.isHost)},async reset(){let o=n();return o?o.owner!==o.me.id&&o.kind!=="solo"?(e("onceworlds.data.server.reset() did nothing: only a private server's owner starts its world over (room.owner)."),!1):o._resetData():(e("onceworlds.data.server.reset() did nothing: you're not in a room."),!1)},on(o,s){return o!=="change"||typeof s!="function"?()=>{}:(t.add(s),()=>t.delete(s))},_changed(o,s,i){for(let a of t)try{a(o,s??null,i)}catch(u){console.error('[onceworlds] error in data.server "change" listener',u)}}}}function Xt(n,e){let t=async(o,s,i,a)=>{if(typeof o.key!="string"||!Ue.test(o.key))return e(`${s} did nothing: '${String(o.key).slice(0,40)}' isn't a game data key (1-32 letters, digits, _ . or -, declared in onceworlds.json's "data").`),a;try{return i(await(await n()).op(o))}catch(u){return e(`${s} did nothing: ${u instanceof Error?u.message:String(u)}`),a}},r=(o,s)=>`onceworlds.data.game.${o}('${String(s).slice(0,40)}')`;return{async get(o){if(typeof o!="string"||!Ue.test(o))return e(`${r("get",o)}: '${String(o).slice(0,40)}' isn't a game data key (1-32 letters, digits, _ . or -).`),null;try{return Dr(await(await n()).read(o))}catch(s){return e(`${r("get",o)} read nothing: ${s instanceof Error?s.message:String(s)}`),null}},add:(o,s)=>t({key:o,op:"add",n:Number(s)},r("add",o),i=>i.type==="counter"?i.value:null,null),push:(o,s)=>t({key:o,op:"push",item:s},r("push",o),i=>i.type==="list",!1),best:(o,s)=>t({key:o,op:"best",n:Number(s)},r("best",o),i=>i.type==="record"?{value:i.value,by:i.by,at:i.at,record:i.record}:null,null),claim:(o,s)=>t({key:o,op:"claim",field:String(s)},r("claim",o),i=>i.type==="map"&&i.ok,!1),release:(o,s)=>t({key:o,op:"release",field:String(s)},r("release",o),i=>i.type==="map"&&i.ok,!1),set:(o,s)=>t({key:o,op:"set",value:s},r("set",o),i=>i.type==="value",!1)}}function en(n,e){let t=E.gameDataNumberMax,r=s=>{switch(s.op){case"add":return{type:"counter",min:0,max:t,add:[-t,t],guests:!0};case"push":return{type:"list",max:E.gameDataListMax,bytes:E.gameDataItemBytes,guests:!0};case"best":return{type:"record",order:"max",min:-t,max:t,guests:!0};case"claim":case"release":return{type:"map",fields:E.gameDataMapFields,perPlayer:E.gameDataMapPerPlayer,guests:!0};case"set":return{type:"value",bytes:E.gameDataValueBytes,guests:!0}}},o=s=>{try{let i=e.get(`onceworlds:game:${s}`);return i?JSON.parse(i):null}catch{return null}};return{async read(s){let i=o(s);if(!i)throw new Error(`nothing is stored under '${s}' on this page yet (on its own, game data lives in this browser).`);return i},async op(s){let i=await n(),a=o(s.key),u=r(s);if(a&&a.type!==u.type)throw new Error(`'${s.key}' holds a ${a.type} on this page: it was first used that way.`);let f=Rt(u,a,JSON.parse(JSON.stringify(s??null)),{id:i.id,name:i.name,host:!0},Date.now());if("code"in f)throw new Error(f.message);return e.set(`onceworlds:game:${s.key}`,JSON.stringify(f.next)),f.result}}}function tn(n){return{messagesPerSecond:n.messagesPerSecond*.85,messageBurst:n.messagesPerSecond*2*.85,bytesPerSecond:n.bytesPerSecond*.85,byteBurst:n.bytesPerSecond*4*.85}}var Cr=60,$r=100,Tr=5e3,Se=class{constructor(e={}){c(this,"queue",[]);c(this,"rates",tn({messagesPerSecond:E.messagesPerSecond,bytesPerSecond:E.roomBytesPerSecond}));c(this,"messages",this.rates.messageBurst);c(this,"bytes",this.rates.byteBurst);c(this,"budget",{messagesPerSecond:E.messagesPerSecond,bytesPerSecond:E.roomBytesPerSecond});c(this,"refilledAt");c(this,"timer",null);c(this,"warnedAt",-1/0);c(this,"dropped",0);c(this,"now");c(this,"setTimer");c(this,"clearTimer");c(this,"warn");this.now=e.now??Date.now,this.setTimer=e.setTimer??((t,r)=>setTimeout(t,r)),this.clearTimer=e.clearTimer??(t=>clearTimeout(t)),this.warn=e.warn??(t=>console.warn(`[onceworlds] ${t}`)),this.refilledAt=this.now(),e.budget&&this.setBudget(e.budget)}setBudget(e){this.refill(),this.budget={messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond},this.rates=tn(this.budget),this.messages=Math.min(this.messages,this.rates.messageBurst),this.bytes=Math.min(this.bytes,this.rates.byteBurst),this.drain(!1)}get waiting(){return this.queue.length}event(e,t){this.queue.push({kind:"event",size:e,send:t}),this.settle()}urgent(e,t){this.queue.push({kind:"event",size:e,send:t}),this.drain(!0)}state(e,t,r){for(let o=this.queue.length-1;o>=0;o--){let s=this.queue[o];if(s.kind!=="state")break;if(s.key===e)return s.size=t,s.send=r,this.settle()}this.queue.push({kind:"state",key:e,size:t,send:r}),this.settle()}spend(e){this.refill(),this.bytes-=e}clear(){this.queue=[],this.timer!==null&&this.clearTimer(this.timer),this.timer=null}settle(){this.queue.length>this.waitingLimit(Cr,12)&&(this.squeeze(),this.tell(`${this.queue.length} messages are waiting: a room takes about ${this.budget.messagesPerSecond} messages and ${Math.round(this.budget.bytesPerSecond/1024)} KB a second from each player. State is sent at its newest value and other messages in order. Send less: batch events, write state at 10-20 Hz, and only what changed.`)),this.drain(!1)}waitingLimit(e,t){return Math.max(t,Math.round(e*this.rates.messagesPerSecond/(E.messagesPerSecond*.85)))}refill(){let e=this.now(),t=Math.max(0,(e-this.refilledAt)/1e3);this.refilledAt=e,this.messages=Math.min(this.rates.messageBurst,this.messages+t*this.rates.messagesPerSecond),this.bytes=Math.min(this.rates.byteBurst,this.bytes+t*this.rates.bytesPerSecond)}drain(e){for(this.timer!==null&&(this.clearTimer(this.timer),this.timer=null),this.refill();this.queue.length>0;){let i=this.queue[0],a=Math.min(i.size(),this.rates.byteBurst);if(!e&&(this.messages<1||this.bytes<a))break;this.queue.shift(),this.messages-=1,this.bytes-=a;try{i.send()}catch(u){console.error("[onceworlds] a message could not be sent",u)}}if(this.queue.length===0)return;let t=Math.min(this.queue[0].size(),this.rates.byteBurst),r=Math.max(0,1-this.messages)/this.rates.messagesPerSecond,o=Math.max(0,t-this.bytes)/this.rates.bytesPerSecond,s=Math.min(250,Math.max(8,Math.ceil(Math.max(r,o)*1e3)));this.timer=this.setTimer(()=>{this.timer=null,this.drain(!1)},s)}squeeze(){let e=new Set;for(let o=this.queue.length-1;o>=0;o--){let s=this.queue[o];s.kind==="state"&&(e.has(s.key)?this.queue.splice(o,1):e.add(s.key))}let t=this.queue.filter(o=>o.kind==="event").length,r=this.waitingLimit($r,20);for(let o=0;t>r&&o<this.queue.length;)this.queue[o].kind==="event"?(this.queue.splice(o,1),t--,this.dropped++):o++;this.dropped>0&&this.tell(`${this.dropped} messages were dropped so the rest arrive fresh: the game sends much more than a room takes (${this.budget.messagesPerSecond} messages, ${Math.round(this.budget.bytesPerSecond/1024)} KB a second per player). Send less.`)}tell(e){let t=this.now();t-this.warnedAt<Tr||(this.warnedAt=t,this.warn(e))}};function V(n){try{let e=JSON.stringify(n)??"null",t=0;for(let r=0;r<e.length;r++)e.charCodeAt(r)>127&&(t+=2);return e.length+t+32}catch{return 1024}}var Qe=Math.PI*2;function Ze(n,e,t,r={},o=""){if(typeof n=="number"&&typeof e=="number"){if(r.angles?.includes(o)){let s=((e-n)%Qe+Qe+Math.PI)%Qe-Math.PI;return n+s*t}return n+(e-n)*t}if(Array.isArray(n)&&Array.isArray(e)&&n.length===e.length)return e.map((s,i)=>Ze(n[i],s,t,r,o));if(n&&e&&typeof n=="object"&&typeof e=="object"&&!Array.isArray(n)&&!Array.isArray(e)){let s={};for(let i of Object.keys(e))s[i]=i in n?Ze(n[i],e[i],t,r,i):e[i];return s}return t<.5?n:e}function Lr(n,e,t){if(t===void 0||!n||!e||typeof n!="object"||typeof e!="object")return!1;for(let[r,o]of Object.entries(e)){let s=n[r];if(typeof o=="number"&&typeof s=="number"&&Math.abs(o-s)>t)return!0}return!1}function nn(n,e,t={}){if(n.length===0)return;let r=n[0],o=n[n.length-1];if(e<=r.at)return r.d;if(e>=o.at)return o.d;let s=n.length-2;for(;s>0&&n[s].at>e;)s--;let i=n[s],a=n[s+1];return Lr(i.d,a.d,t.snap)?a.d:Ze(i.d,a.d,(e-i.at)/Math.max(1,a.at-i.at),t)}var rn=8,on=2e3;var Ir=3e3,Or=15e3,_r=5e3;function sn(n){let e={};for(let t of Object.keys(n))te(t)&&(e[t]=n[t]);return e}var j=()=>48,oe=class{constructor(e,t,r=Date.now,o=[]){c(this,"id");c(this,"kind");c(this,"invite");c(this,"teams");c(this,"open");c(this,"ranked");c(this,"kept");c(this,"me");c(this,"owner");c(this,"players",new Map);c(this,"state",{});c(this,"host");c(this,"closed",!1);c(this,"connected",!0);c(this,"match");c(this,"listeners",new Map);c(this,"transport");c(this,"outbox",new Se);c(this,"samples",new Map);c(this,"presenceTimer",null);c(this,"pendingPresence",null);c(this,"writes",0);c(this,"written",new Map);c(this,"confirmed",{});c(this,"shares",{messagesPerSecond:E.messagesPerSecond,presenceHz:E.presenceHz,bytesPerSecond:E.roomBytesPerSecond});c(this,"clock");c(this,"schema");c(this,"frozenSettings",null);c(this,"secrets",new Map);c(this,"offlineReady");c(this,"complained",new Map);c(this,"acks",new Map);this.transport=t,this.clock=r,this.schema=o,this.match=e.match??{phase:"lobby",n:0,min:1},this.applyBudget(e.budget),this.id=e.room,this.kind=e.room==="solo"?"solo":e.room.startsWith("private:")?"private":e.room.startsWith("room:")?"named":"public",this.invite=e.invite??null,this.teams=e.teams??0,this.open=e.open??!0,this.ranked=e.ranked??!1,this.kept=e.kept??Mt(e.room),this.owner=e.owner??null,this.host=e.host;for(let s of e.players)this.players.set(s.id,{...s}),s.presence!==null&&s.presence!==void 0&&this.samples.set(s.id,[{at:e.now??this.clock(),d:s.presence}]);this.me=this.players.get(e.you)??{id:e.you,name:"You",presence:null,team:0},this.players.set(this.me.id,this.me),this.state={...e.state},this.confirmed=sn(e.state);for(let[s,i]of Object.entries(e.private??{}))this.secrets.set(s,{...i});this.match.phase!=="lobby"&&(this.frozenSettings=this.chosenSettings())}get budget(){return this.shares}get settings(){return this.frozenSettings??this.chosenSettings()}setSetting(e,t){if(this.closed||!this.isHost||this.match.phase!=="lobby")return;let r=this.schema.find(s=>s.id===e);if(!r||!r.options.some(s=>s.value===t)||this.chosenSettings()[e]===t)return;let o=this.state[ne];this.setState(ne,{...o!==null&&typeof o=="object"?o:{},[e]:t}),this.clearReady(),this.emit("settings",this.settings)}chosenSettings(){let e=this.state[ne];return Object.fromEntries(this.schema.map(t=>[t.id,At(t,e)]))}get isHost(){return this.connected&&this.host===this.me.id}get online(){return[...this.players.values()].filter(e=>e.connected!==!1)}get participants(){let e=this.match.participants;return this.match.phase==="lobby"||!e?[]:e.map(t=>this.players.get(t)).filter(t=>!!t)}get spectators(){let e=this.match.participants;return this.match.phase==="lobby"||!e?[]:this.online.filter(t=>!e.includes(t.id))}get leftOut(){let e=this.match.left;return this.match.phase==="lobby"||!e?.length?[]:this.spectators.filter(t=>e.includes(t.id))}get private(){return this.secrets.get(this.me.id)??{}}privateOf(e){return e===this.me.id||this.isHost?this.secrets.get(e)??{}:{}}setPrivate(e,t){this.closed||(this.setSecret(this.me.id,e,t??null),this.outbox.event(()=>V(t),()=>this.transport.private(e,t??null)))}setPrivateFor(e,t,r){if(!this.closed){if(e===this.me.id)return this.setPrivate(t,r);this.outbox.event(()=>V(r),()=>this.transport.private(t,r??null,String(e)))}}presenceAt(e,t={}){if(e===this.me.id)return this.me.presence;let r=this.samples.get(e);return!r||r.length===0?this.players.get(e)?.presence??null:nn(r,this.clock()-(t.delay??100),t)}keep(e,t,r){let o=this.samples.get(e)??[],s=o.length;for(;s>0&&o[s-1].at>t;)s--;for(o.splice(s,0,{at:t,d:r});o.length>rn||o.length>2&&o[0].at<t-on;)o.shift();this.samples.set(e,o)}isParticipant(e=this.me.id){return this.match.phase!=="lobby"&&!!this.match.participants?.includes(e)}get spectating(){return this.match.phase!=="lobby"&&!this.isParticipant()}get running(){return this.match.phase==="playing"&&!this.match.paused}matchNow(){let e=this.match;if(e.phase!=="playing"||e.startedAt===void 0)return 0;let t=this.clock(),r=(e.pausedMs??0)+(e.paused?Math.max(0,t-e.paused.since):0);return Math.max(0,t-e.startedAt-r)}get canStart(){return this.isHost&&this.kind!=="public"&&this.match.phase==="lobby"&&this.online.length>=this.match.min&&this.allReady}get allReady(){return this.notReady.length===0}get notReady(){return this.online.filter(e=>e.id!==this.host&&!e.ready)}setReady(e=!0){this.closed||!!this.me.ready===e||(e?this.me.ready=!0:delete this.me.ready,this.connected||(this.offlineReady=e),this.outbox.urgent(j,()=>this.transport.ready(e)))}startMatch(e={}){if(!this.reachable("startMatch"))return;let t=Number(e.countdown),r={...Number.isFinite(t)?{countdown:Math.round(t*1e3)}:{},...e.open===!0?{open:!0}:{}};this.outbox.urgent(j,()=>this.transport.start(r))}endMatch(){this.reachable("endMatch")&&this.outbox.urgent(j,()=>this.transport.end())}pauseMatch(e=!0){this.reachable("pauseMatch")&&this.outbox.urgent(j,()=>this.transport.pause(e))}admit(e){let t=(Array.isArray(e)?e:[e]).map(String);t.length&&this.reachable("admit")&&this.outbox.urgent(j,()=>this.transport.admit(t))}reachable(e){let[t,r]=this.closed?["closed","this room is closed (you left it, were removed, or moved to another one)"]:this.connected?["",""]:["disconnected","the connection to the room is down and the platform is reconnecting: call it again after room.on('reconnect')"];return t?(this.complain(e,t,r),!1):!0}dropHostWrites(){for(let e of[...this.written.keys()]){if(!te(e))continue;this.written.delete(e);let t=this.confirmed[e]??null;JSON.stringify(this.state[e]??null)!==JSON.stringify(t)&&(t===null?delete this.state[e]:this.state[e]=t,this.emit("state",e,t,""))}}complain(e,t,r){let o=this.clock(),s=`${e}:${t}`;if(o-(this.complained.get(s)??-1/0)<_r)return;this.complained.set(s,o);let i=`${e}() did nothing: ${r}.`;console.warn(`[onceworlds] ${i} (${t})`),this.emit("error",{code:t,message:i})}transferHost(e){this.closed||this.outbox.urgent(j,()=>this.transport.transfer(String(e)))}hideLobby(e=!0){}clearReady(){if(!(this.closed||!this.isHost)){for(let e of this.players.values())delete e.ready;this.outbox.urgent(j,()=>this.transport.unready())}}send(e,t={}){this.closed||this.outbox.event(()=>V(e),()=>this.transport.send(e,t.to))}setPresence(e){if(!this.closed){if(this.me.presence=e,this.presenceTimer){this.pendingPresence={value:e};return}this.transport.presence(e),this.outbox.spend(V(e)),this.presenceTimer=setTimeout(()=>this.flushPresence(),1e3/this.shares.presenceHz)}}setState(e,t){this.writeState(e,t)}writeState(e,t){if(this.closed)return 0;if(te(e)&&this.connected&&this.host!==this.me.id)return this.complain("setState","not_host",`only the host writes ${e}`),0;t==null?delete this.state[e]:this.state[e]=t;let r=++this.writes;return this.written.set(e,{w:r,at:this.clock()}),this.outbox.state(e,()=>V(t)+e.length,()=>this.transport.state(e,t??null,r)),r}_writeAcked(e,t){if(this.closed||!this.connected)return Promise.resolve(!1);let r=this.writeState(e,t);return r?this.kind==="solo"?Promise.resolve(!0):new Promise(o=>{let s={w:r,done:a=>{clearTimeout(i),o(a)}},i=setTimeout(()=>this.settleAcks(e,a=>a===s,!1),Or);this.acks.set(e,[...this.acks.get(e)??[],s])}):Promise.resolve(!1)}_resetData(){return this.closed||!this.connected||!this.transport.dataReset?!1:(this.outbox.urgent(j,()=>this.transport.dataReset()),!0)}settleAcks(e,t,r){let o=this.acks.get(e);if(!o)return;let s=o.filter(i=>!t(i));s.length>0?this.acks.set(e,s):this.acks.delete(e);for(let i of o)t(i)&&i.done(r)}failAcks(){for(let e of[...this.acks.keys()])this.settleAcks(e,()=>!0,!1)}setTeam(e,t){this.closed||this.outbox.urgent(j,()=>this.transport.team(e,t))}setOpen(e){this.closed||this.outbox.urgent(j,()=>this.transport.open(e))}reportResult(e){this.closed||this.outbox.urgent(()=>V(e),()=>this.transport.result(e))}kick(e){this.closed||this.outbox.urgent(j,()=>this.transport.kick(String(e)))}voteKick(e){this.closed||this.outbox.urgent(j,()=>this.transport.voteKick(String(e)))}on(e,t){let r=this.listeners.get(e);return r||this.listeners.set(e,r=new Set),r.add(t),()=>r.delete(t)}leave(){this.closed||(this.transport.leave(),this.close("left"))}_receive(e){switch(e.t){case"welcome":return this.resync(e);case"away":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(t.connected=!1,this.emit("away",t))}case"back":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(delete t.connected,this.emit("back",t,e.fresh===!0))}case"ready":{let t=this.players.get(e.id);return!t||!!t.ready==(e.ready===!0)?void 0:(e.ready===!0?t.ready=!0:delete t.ready,this.emit("ready",t))}case"match":return this.applyMatch(e.match);case"idle":{let t=this.players.get(e.id);return!t||t===this.me||!!t.idle==(e.idle===!0)?void 0:(e.idle===!0?t.idle=!0:delete t.idle,this.emit("idle",t))}case"private":{let t=JSON.stringify(this.secrets.get(e.id)?.[e.k]??null);this.setSecret(e.id,e.k,e.v??null),t!==JSON.stringify(e.v??null)&&this.emit("private",e.k,e.v??null,e.id);return}case"privates":return this.applySecrets(e.all??{});case"unready":{for(let t of this.players.values())t.ready&&(delete t.ready,this.emit("ready",t));return}case"join":{let t={...e.player};return this.players.set(t.id,t),this.emit("join",t)}case"leave":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(this.players.delete(e.id),this.samples.delete(e.id),this.emit("leave",t,e.kicked===!0))}case"votekick":{let t=this.players.get(e.id);t&&this.emit("votekick",t,e.votes,e.needed);return}case"host":if(this.host=e.id,e.id!==this.me.id)for(let t of[...this.secrets.keys()])t!==this.me.id&&this.secrets.delete(t);return e.id!==this.me.id&&(this.dropHostWrites(),this.failAcks()),this.emit("host",e.id);case"msg":return this.emit("message",e.d,this.players.get(e.from)??{id:e.from,name:"Unknown",presence:null,team:0},typeof e.at=="number"?e.at:this.clock(),typeof e.mt=="number"?e.mt:void 0);case"team":{let t=this.players.get(e.id);return t?(t.team=e.team,this.emit("team",t)):void 0}case"name":{let t=e.id===this.me.id?this.me:this.players.get(e.id);return!t||typeof e.name!="string"||t.name===e.name?void 0:(t.name=e.name,this.emit("rename",t))}case"open":return this.open=e.open,this.emit("open",e.open);case"rated":for(let t of e.changes){let r=this.players.get(t.id);r&&(r.rating=t.rating)}return this.emit("rated",e.changes,e.disputed,e.capped===!0);case"presence":{let t=this.players.get(e.id);return!t||t===this.me?void 0:(t.presence=e.d,this.keep(e.id,typeof e.at=="number"?e.at:this.clock(),e.d),this.emit("presence",t))}case"state":{e.from===this.me.id&&typeof e.w=="number"&&this.settleAcks(e.k,o=>o.w<=e.w,!0),te(e.k)&&(e.v===null?delete this.confirmed[e.k]:this.confirmed[e.k]=e.v);let t=this.written.get(e.k);if(t)if(e.from===this.me.id)if(typeof e.w!="number")this.written.delete(e.k);else{if(e.w<t.w)return;e.w===t.w&&this.written.delete(e.k)}else{if(this.clock()-t.at<Ir)return;this.written.delete(e.k)}let r=JSON.stringify(this.state[e.k]??null);e.v===null?delete this.state[e.k]:this.state[e.k]=e.v,(e.from!==this.me.id||r!==JSON.stringify(e.v??null))&&this.emit("state",e.k,e.v,e.from),e.k===ne&&this.schema.length>0&&this.frozenSettings===null&&r!==JSON.stringify(e.v??null)&&e.from!==this.me.id&&this.emit("settings",this.settings);return}case"chat":return this.emit("chat",e.entry);case"error":return typeof e.k=="string"&&typeof e.w=="number"&&this.settleAcks(e.k,t=>t.w===e.w,!1),e.code==="cooldown"&&this.me.ready&&(delete this.me.ready,this.emit("ready",this.me)),console.warn(`[onceworlds] ${e.message} (${e.code})`),this.emit("error",{code:e.code,message:e.message})}}_closed(e){this.close(e)}_disconnected(){this.closed||!this.connected||(this.connected=!1,this.outbox.clear(),this.failAcks(),this.emit("disconnect"))}close(e){this.closed||(this.closed=!0,this.outbox.clear(),this.failAcks(),this.presenceTimer&&clearTimeout(this.presenceTimer),this.emit("close",e))}flushPresence(){if(this.presenceTimer=null,!this.pendingPresence||this.closed)return;let{value:e}=this.pendingPresence;this.pendingPresence=null,this.transport.presence(e),this.outbox.spend(V(e)),this.presenceTimer=setTimeout(()=>this.flushPresence(),1e3/this.shares.presenceHz)}applyBudget(e){if(!e)return;let t=(r,o)=>typeof r=="number"&&Number.isFinite(r)&&r>0&&r<=o;t(e.presenceHz,E.presenceHz)&&(this.shares={...this.shares,presenceHz:e.presenceHz}),t(e.messagesPerSecond,E.messagesPerSecond)&&t(e.bytesPerSecond,E.roomBytesPerSecond)&&(this.shares={...this.shares,messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond},this.outbox.setBudget({messagesPerSecond:e.messagesPerSecond,bytesPerSecond:e.bytesPerSecond}))}setSecret(e,t,r){let o={...this.secrets.get(e)??{}};r===null?delete o[t]:o[t]=r,Object.keys(o).length>0?this.secrets.set(e,o):this.secrets.delete(e)}applySecrets(e){let t=[];for(let[r,o]of Object.entries(e))for(let[s,i]of Object.entries(o))JSON.stringify(this.secrets.get(r)?.[s]??null)!==JSON.stringify(i)&&t.push([r,s,i]);for(let[r,o]of this.secrets)for(let s of Object.keys(o))s in(e[r]??{})||t.push([r,s,null]);this.secrets=new Map(Object.entries(e).map(([r,o])=>[r,{...o}]));for(let[r,o,s]of t)this.emit("private",o,s,r)}applyMatch(e){let t=this.match;if(!(!e||JSON.stringify(t)===JSON.stringify(e))){if(this.match=e,e.phase!=="lobby"&&this.frozenSettings===null)this.frozenSettings=this.chosenSettings();else if(e.phase==="lobby"&&this.frozenSettings!==null){let r=JSON.stringify(this.frozenSettings);this.frozenSettings=null,r!==JSON.stringify(this.chosenSettings())&&queueMicrotask(()=>this.emit("settings",this.settings))}this.emit("match",e,t),t.phase!=="starting"&&e.phase==="starting"&&this.emit("starting",e),t.phase!=="playing"&&e.phase==="playing"&&this.emit("matchstart",e),t.phase!=="lobby"&&e.phase==="lobby"&&this.emit("matchend",e,t),t.phase==="playing"&&e.phase==="playing"&&(!t.paused&&e.paused?this.emit("matchpause",e):t.paused&&!e.paused&&this.emit("matchresume",e))}}resync(e){let t=this.connected,r=this.host!==e.host;this.connected=!0,this.host=e.host,this.written.clear(),this.confirmed=sn(e.state),this.applyBudget(e.budget);let o=new Set;for(let i of e.players){if(o.add(i.id),i.id===this.me.id){this.offlineReady===void 0&&!!this.me.ready!=!!i.ready&&(i.ready?this.me.ready=!0:delete this.me.ready,this.emit("ready",this.me));continue}let a=this.players.get(i.id);if(a)a.presence=i.presence,i.presence!==null&&i.presence!==void 0&&this.keep(i.id,e.now??this.clock(),i.presence),a.team=i.team,a.name!==i.name&&(a.name=i.name,this.emit("rename",a)),!!a.ready!=!!i.ready&&(i.ready?a.ready=!0:delete a.ready,this.emit("ready",a)),!!a.idle!=!!i.idle&&(i.idle?a.idle=!0:delete a.idle,this.emit("idle",a)),a.connected===!1!=(i.connected===!1)&&(i.connected===!1?a.connected=!1:delete a.connected,i.connected===!1?this.emit("away",a):this.emit("back",a,!1));else{let u={...i};this.players.set(i.id,u),this.emit("join",u)}}for(let[i,a]of this.players)o.has(i)||a===this.me||(this.players.delete(i),this.emit("leave",a));let s=new Set([...Object.keys(this.state),...Object.keys(e.state)]);for(let i of s){let a=e.state[i]??null;JSON.stringify(this.state[i]??null)!==JSON.stringify(a)&&(a===null?delete this.state[i]:this.state[i]=a,this.emit("state",i,a,""))}r&&this.emit("host",e.host),this.teams=e.teams??this.teams,this.kept=e.kept??this.kept,e.open!==void 0&&e.open!==this.open&&(this.open=e.open,this.emit("open",e.open)),this.applyMatch(e.match??{phase:"lobby",n:0,min:1}),this.applySecrets(e.private??{}),this.me.presence!==null&&this.transport.presence(this.me.presence),this.offlineReady!==void 0&&!!e.players.find(i=>i.id===this.me.id)?.ready!==this.offlineReady&&this.transport.ready(this.offlineReady),this.offlineReady=void 0,t||this.emit("reconnect")}emit(e,...t){for(let r of this.listeners.get(e)??[])try{r(...t)}catch(o){console.error(`[onceworlds] error in room "${e}" listener`,o)}}};function an(n){let e=null,t=o=>n()._receive({t:"match",match:o}),r=()=>{let s=n().match;if(s.phase!=="starting")return;let{startsAt:i,...a}=s;t({...a,phase:"playing",startedAt:Date.now(),pausedMs:0})};return{send(){},presence(){},state(){},team(){},open(){},result(){},kick(){},voteKick(){},ready(){},unready(){},transfer(){},start(o){let s=n();if(s.match.phase!=="lobby")return;let i=Math.min(E.matchCountdownMaxMs,Math.max(0,o.countdown??E.matchCountdownMs));t({phase:"starting",n:s.match.n+1,min:s.match.min,id:`solo${s.match.n+1}`,seed:Math.floor(Math.random()*2**32),participants:[s.me.id],startsAt:Date.now()+i}),i===0?r():e=setTimeout(r,i)},end(){let o=n();e&&clearTimeout(e),e=null,o.match.phase!=="lobby"&&t({phase:"lobby",n:o.match.n,min:o.match.min})},pause(o){let s=n().match;if(s.phase!=="playing")return;let i=Date.now();if(o&&!s.paused)t({...s,paused:{since:i,reason:"host"}});else if(!o&&s.paused){let{paused:a,...u}=s;t({...u,pausedMs:(s.pausedMs??0)+Math.max(0,i-a.since)})}},admit(){},private(){},leave(){e&&clearTimeout(e),e=null}}}var ln=["pointerdown","pointermove","keydown","wheel","touchstart","touchmove","devicemotion","visibilitychange"],Br=5e3;function un(n,e=E.idleAfterMs){let t=Date.now(),r=!1,o=()=>{t=Date.now(),r&&(r=!1,n(!1))},s=()=>{try{for(let a of navigator.getGamepads?.()??[])if(a&&(a.buttons.some(u=>u.pressed)||a.axes.some(u=>Math.abs(u)>.25)))return!0}catch{}return!1};for(let a of ln)window.addEventListener(a,o,{capture:!0,passive:!0});let i=setInterval(()=>{if(s())return o();!r&&Date.now()-t>e&&(r=!0,n(!0))},Br);return{get idle(){return r},stop(){clearInterval(i);for(let a of ln)window.removeEventListener(a,o,{capture:!0})}}}var cn=["pointerdown","keydown","touchstart","gamepadconnected"];function dn(n,e){let t=r=>{if(!(!r.isTrusted||!n())){for(let o of cn)window.removeEventListener(o,t,{capture:!0});e()}};for(let r of cn)window.addEventListener(r,t,{capture:!0,passive:!0})}var fn=["low","medium","high"],Nr={low:.6,medium:.8,high:1},jr=24,Gr=18.5,Kr=26,Xe=6e3,pn=2e4,Hr=5*6e4,et=class{constructor(e=2){c(this,"level",2);c(this,"slow",0);c(this,"smoothSince",0);c(this,"holdUntil",0);c(this,"probeAfter",pn);c(this,"steppedUpAt",-1/0);this.level=e}window(e,t,r){return t>jr?(this.smoothSince=0,++this.slow>=2&&e>=this.holdUntil&&this.level>0&&(e-this.steppedUpAt<Xe+pn/2&&(this.probeAfter=Math.min(Hr,this.probeAfter*2)),this.level--,this.slow=0,this.holdUntil=e+Xe)):t<=Gr&&r<=Kr?(this.slow=0,this.smoothSince||(this.smoothSince=e),this.level<fn.length-1&&e>=this.holdUntil&&e-this.smoothSince>=this.probeAfter&&(this.level++,this.steppedUpAt=e,this.smoothSince=0,this.holdUntil=e+Xe)):(this.slow=0,this.smoothSince=0),this.level}};function mn(n){let e="auto",t=typeof matchMedia=="function"&&matchMedia("(prefers-reduced-motion: reduce)").matches,r=!1,o=new et(qr()?1:2),s=new Set,i=!1,a=!1,u=0,f=[],d=0,g=0,h=m=>{if(i){if(u&&!document.hidden){let k=m-u;k<250&&f.push(k)}if(u=document.hidden?0:m,d||(d=m),m-d>=1e3){if(f.length>=10){let k=[...f].sort((z,C)=>z-C),D=f.reduce((z,C)=>z+C,0)/f.length,N=k[Math.min(k.length-1,Math.floor(k.length*.95))],q=M();e==="auto"&&o.window(m,D,N),r&&m-g>=500&&(g=m,n.stats({fps:Math.round(1e3/D),frameMs:Math.round(D*10)/10,worstMs:Math.round(k[k.length-1]*10)/10})),M()!==q&&L()}f=[],d=m}A()}},A=()=>{a||!i||(a=!0,requestAnimationFrame(m=>{a=!1,h(m)}))},y=()=>{i||typeof requestAnimationFrame!="function"||(i=!0,u=0,d=0,A())},w=()=>{!r&&e!=="auto"&&i&&(i=!1)},M=()=>e==="auto"?fn[o.level]:e,L=()=>{for(let m of s)try{m(O)}catch(k){console.error('[onceworlds] error in settings "change" listener',k)}},O={get choice(){return e},get quality(){return e==="auto"&&y(),M()},get scale(){return Nr[O.quality]},get reducedMotion(){return t},pixelRatio(m=2){let k=typeof devicePixelRatio=="number"&&devicePixelRatio>0?devicePixelRatio:1;return Math.max(.5,Math.min(k,m)*O.scale)},on(m,k){return m!=="change"?()=>{}:(s.add(k),e==="auto"&&y(),()=>s.delete(k))}};return{settings:O,apply(m){let k=`${M()}|${t}`;e=m.quality,t=m.reducedMotion,r=m.showStats,r||e==="auto"&&s.size>0?y():w(),`${M()}|${t}`!==k&&L()}}}function qr(){return typeof matchMedia=="function"&&matchMedia("(pointer: coarse)").matches}var se={maxSide:1280,quality:.85,maxBytes:15e5,answerMs:2500,waitMs:3e3,perSecond:3};var Ur=4,zr=250,Fr=1e3;function Vr(){return typeof window>"u"||typeof document>"u"?null:{document,requestAnimationFrame:n=>window.requestAnimationFrame(n),setTimeout:(n,e)=>window.setTimeout(n,e),clearTimeout:n=>window.clearTimeout(n),background:n=>window.getComputedStyle(n).backgroundColor,hidden:()=>document.visibilityState==="hidden"}}function Wr(n){if(!n||typeof n!="object")return null;let e=n;if(typeof e.id!="number"||!Number.isSafeInteger(e.id)||e.id<0)return null;let t=typeof e.maxSide=="number"&&Number.isFinite(e.maxSide)?Math.min(se.maxSide,Math.max(64,Math.round(e.maxSide))):se.maxSide,r=typeof e.quality=="number"&&Number.isFinite(e.quality)?Math.min(.95,Math.max(.5,e.quality)):se.quality;return{id:e.id,maxSide:t,quality:r}}function Jr(n,e,t){let r=Math.min(1,t/Math.max(n,e,1)),o=s=>Math.max(1,Math.min(t,Math.round(s*r)));return{width:o(n),height:o(e)}}function Yr(n){for(let e=0;e+2<n.length;e+=4)if(n[e]!==0||n[e+1]!==0||n[e+2]!==0)return!1;return!0}var Qr=/^(?:transparent|rgba\(.*,\s*0(?:\.0*)?\))$/;function Zr(n,e){try{for(let t=n;t;t=t.parentElement){let r=e.background?.(t)?.trim();if(r&&!Qr.test(r))return r}}catch{}return"#000"}function tt(n,e,t,r,o){try{let s=r.document.createElement("canvas");s.width=e.width,s.height=e.height;let i=s.getContext("2d");if(!i)return{miss:"unsupported"};if(i.drawImage(n,0,0,e.width,e.height),Yr(i.getImageData(0,0,e.width,e.height).data))return{miss:"blank"};i.globalCompositeOperation="destination-over",i.fillStyle=o(),i.fillRect(0,0,e.width,e.height);for(let a of[t.quality,.6]){let u=s.toDataURL("image/jpeg",a);if(!u.startsWith("data:image/jpeg;base64,"))return{miss:"unsupported"};if((u.length-23)*.75<=se.maxBytes)return{image:u}}return{miss:"failed"}}catch(s){return{miss:s?.name==="SecurityError"?"tainted":"failed"}}}function hn(n,e){let t=!1,r=()=>{t||(t=!0,e())};n.requestAnimationFrame(r),n.setTimeout(r,zr)}function Xr(n,e,t){return new Promise(r=>{let o,s;try{if(typeof n.captureStream!="function")return r(null);o=n.captureStream(),s=e.document.createElement("video"),s.muted=!0,s.playsInline=!0,s.srcObject=o}catch{return r(null)}let i=!1,a=d=>{if(!i){i=!0,e.clearTimeout(u);try{s.srcObject=null;for(let g of o.getTracks())g.stop()}catch{}r(d)}},u=e.setTimeout(()=>a(null),Fr),f=()=>{!i&&s.videoWidth>0&&s.videoHeight>0&&a(t(s))};try{s.addEventListener("loadeddata",f),s.addEventListener("playing",f),s.play()?.catch(()=>{})}catch{a(null)}})}function gn(n,e=Vr()){let t=!1,r=s=>{try{n(s)}catch{}};function o(s){let i=s.id;if(!e)return r({id:i,t:"none",reason:"unsupported"});if(t)return r({id:i,t:"none",reason:"busy"});let a=e,u=we(a.document.querySelectorAll("canvas"));if(!u)return r({id:i,t:"none",reason:"no-canvas"});t=!0;let f=w=>{t=!1,r("image"in w?{id:i,t:"done",image:w.image}:{id:i,t:"none",reason:w.miss})},d=Jr(u.width,u.height,s.maxSide),g=null,h=()=>g??(g=Zr(u,a));if(a.hidden?.()){let w=tt(u,d,s,a,h);return f("miss"in w&&w.miss==="blank"?{miss:"hidden"}:w)}let A=0,y=()=>{try{let w=tt(u,d,s,a,h);if(!("miss"in w)||w.miss!=="blank")return f(w);if(++A<Ur)return hn(a,y);Xr(u,a,M=>tt(M,d,s,a,h)).then(M=>f(M??{miss:"blank"}),()=>f({miss:"blank"}))}catch{f({miss:"failed"})}};hn(a,y)}return{receive(s){let i=null;try{i=Wr(s),i&&o(i)}catch{t=!1,i&&r({id:i.id,t:"none",reason:"failed"})}}}}var R=(n,e)=>new DOMException(e,n);function K(n,e=[]){if(typeof n=="number"&&!Number.isNaN(n)||typeof n=="string")return n;if(n instanceof Date&&!Number.isNaN(n.getTime()))return new Date(n.getTime());if(n instanceof ArrayBuffer)return n.slice(0);if(ArrayBuffer.isView(n))return n.buffer.slice(n.byteOffset,n.byteOffset+n.byteLength);if(Array.isArray(n)&&!e.includes(n))return n.map(t=>K(t,[...e,n]));throw R("DataError","The value is not a valid key.")}function st(n){try{return K(n)}catch{return}}var xe=n=>typeof n=="number"?1:n instanceof Date?2:typeof n=="string"?3:n instanceof ArrayBuffer?4:5;function I(n,e){if(xe(n)!==xe(e))return xe(n)<xe(e)?-1:1;if(n instanceof ArrayBuffer||Array.isArray(n)){let o=n instanceof ArrayBuffer?[...new Uint8Array(n)]:n,s=e instanceof ArrayBuffer?[...new Uint8Array(e)]:e;for(let i=0;i<Math.min(o.length,s.length);i++){let a=I(o[i],s[i]);if(a)return a}return o.length===s.length?0:o.length<s.length?-1:1}let t=n instanceof Date?n.getTime():n,r=e instanceof Date?e.getTime():e;return t<r?-1:t>r?1:0}function J(n){return typeof n=="number"?`n${n}`:typeof n=="string"?`s${n}`:n instanceof Date?`d${n.getTime()}`:n instanceof ArrayBuffer?`b${new Uint8Array(n).join()}`:`a${JSON.stringify(n.map(J))}`}function wn(n,e){let t=n;if(e==="")return t;for(let r of e.split(".")){if(t==null)return;t=t[r]}return t}function it(n,e){if(typeof e=="string")return st(wn(n,e));let t=e.map(r=>it(n,r));return t.includes(void 0)?void 0:t}function eo(n,e,t){let r=e.split("."),o=n;for(let s of r.slice(0,-1))o=o[s]??(o[s]={});o[r[r.length-1]]=t}var Y=n=>typeof structuredClone=="function"?structuredClone(n):n;function at(n){let e=[...n].sort();return e.item=t=>e[t]??null,e.contains=t=>e.includes(t),e}var nt=(()=>{if(typeof MessageChannel!="function")return t=>{setTimeout(t)};let n=[],e=new MessageChannel;return e.port1.onmessage=()=>n.shift()?.(),e.port1.unref?.(),t=>{n.push(t),e.port2.postMessage(0)}})(),ie=class{constructor(){c(this,"listeners",new Map)}addEventListener(e,t){if(!t)return;let r=this.listeners.get(e)??[];r.includes(t)||r.push(t),this.listeners.set(e,r)}removeEventListener(e,t){let r=this.listeners.get(e);r&&t&&r.includes(t)&&r.splice(r.indexOf(t),1)}dispatchEvent(e){return G(e,[this]),!e.defaultPrevented}run(e){let t=!1,r=this[`on${e.type}`];for(let o of[r,...this.listeners.get(e.type)??[]])try{typeof o=="function"?o.call(this,e):o?.handleEvent?.(e)}catch(s){t=!0,setTimeout(()=>{throw s})}return t}};function G(n,e){Object.defineProperty(n,"target",{configurable:!0,value:e[0]}),Object.defineProperty(n,"srcElement",{configurable:!0,value:e[0]});let t=!1;for(let r of e)if(Object.defineProperty(n,"currentTarget",{configurable:!0,value:r}),t=r.run(n)||t,n.cancelBubble)break;return t}function Ee(n,e,t){return typeof IDBVersionChangeEvent=="function"?new IDBVersionChangeEvent(n,{oldVersion:e,newVersion:t}):Object.assign(new Event(n),{oldVersion:e,newVersion:t})}var Me=class{constructor(e,t,r){c(this,"name",e);c(this,"keyPath",t);c(this,"autoIncrement",r);c(this,"records",new Map);c(this,"indexes",new Map);c(this,"nextKey",1)}schema(){return{keyPath:this.keyPath,autoIncrement:this.autoIncrement,nextKey:this.nextKey,indexes:[...this.indexes.values()]}}},Re=class{constructor(e,t,r){c(this,"name",e);c(this,"backend",t);c(this,"version",0);c(this,"stores",new Map);c(this,"connections",new Set);c(this,"queue",[]);c(this,"running",null);if(r){this.version=r.version;for(let[o,s]of Object.entries(r.stores)){let i=new Me(o,s.keyPath,s.autoIncrement);i.nextKey=s.nextKey;for(let a of s.indexes)i.indexes.set(a.name,a);this.stores.set(o,i)}for(let{store:o,key:s,value:i}of r.records){let a=st(s);a!==void 0&&this.stores.get(o)?.records.set(J(a),{key:a,value:i})}}}enqueue(e,t){this.queue.push({transaction:e,started:t}),nt(()=>this.next())}next(){if(this.running)return;let e=this.queue.shift();e&&(this.running=e.transaction,e.transaction.begin(e.started))}finished(){this.running=null,this.next()}drop(e){this.queue=this.queue.filter(t=>t.transaction!==e)}},ae=class extends ie{constructor(t,r){super();c(this,"source",t);c(this,"transaction",r);c(this,"result");c(this,"error",null);c(this,"readyState","pending");c(this,"onsuccess",null);c(this,"onerror",null)}},le=class extends ae{constructor(){super(...arguments);c(this,"onupgradeneeded",null);c(this,"onblocked",null)}},ue=class extends ie{constructor(t,r,o){super();c(this,"db",t);c(this,"scope",r);c(this,"mode",o);c(this,"error",null);c(this,"oncomplete",null);c(this,"onerror",null);c(this,"onabort",null);c(this,"ended",null);c(this,"undo",[]);c(this,"ops",[]);c(this,"schemaChanged",!1);c(this,"state","waiting");c(this,"work",[]);c(this,"stores",new Map);c(this,"scheduled",!1)}get objectStoreNames(){return at(this.scope)}widen(t){this.scope.includes(t)||this.scope.push(t)}forget(t){this.scope=this.scope.filter(r=>r!==t),this.stores.delete(t)}objectStore(t){if(this.state==="done")throw R("InvalidStateError","The transaction has finished.");let r=this.db.data.stores.get(t);if(!r||!this.scope.includes(t))throw R("NotFoundError",`No object store named ${t} in this transaction.`);let o=this.stores.get(t);return o||this.stores.set(t,o=new de(this,r)),o}begin(t){this.state="active",t?.(),this.schedule()}schedule(){this.scheduled||this.state!=="active"||(this.scheduled=!0,nt(()=>{if(this.scheduled=!1,this.state!=="active")return;let t=this.work.shift();t?(this.perform(t.request,t.run),this.schedule()):this.finish()}))}check(t=!1){if(this.state!=="active"&&this.state!=="waiting")throw R("TransactionInactiveError","The transaction has finished.");if(t&&this.mode==="readonly")throw R("ReadOnlyError","The transaction is read-only.")}request(t,r){this.check();let o=new ae(t,this);return this.work.push({request:o,run:r}),this.schedule(),o}again(t,r){this.check(),t.readyState="pending",this.work.push({request:t,run:r}),this.schedule()}path(t){return[t,this,this.db]}perform(t,r){let o;try{o=r()}catch(s){let i=s instanceof DOMException?s:R("UnknownError",String(s));t.readyState="done",t.error=i;let a=new Event("error",{bubbles:!0,cancelable:!0}),u=G(a,this.path(t));(u||!a.defaultPrevented)&&this.stop(u?R("AbortError","A handler threw."):i);return}t.readyState="done",t.result=o,G(new Event("success"),[t])&&this.stop(R("AbortError","A handler threw."))}async finish(){this.state="committing";let t=this.db.data;if((this.ops.length>0||this.schemaChanged)&&t.backend){let o=this.schemaChanged?Object.fromEntries([...t.stores].map(([s,i])=>[s,i.schema()])):void 0;try{await t.backend.commit({db:t.name,version:t.version,stores:o,ops:this.ops})}catch(s){let i=s instanceof Error?s.message:String(s);this.state="active",this.stop(i.includes("QuotaExceededError")?R("QuotaExceededError","This game has used its storage in this browser."):R("UnknownError",i));return}}this.state="done",G(new Event("complete"),[this]),this.ended?.(!0),t.finished()}stop(t){if(this.state==="done")return;let r=this.state==="waiting";this.state="done",this.error=t;for(let i of this.undo.reverse())i();let o=this.work;this.work=[];let s=this.db.data;r&&s.drop(this),nt(()=>{for(let{request:i}of o)i.readyState="done",i.error=R("AbortError","The transaction was aborted."),G(new Event("error",{bubbles:!0,cancelable:!0}),this.path(i));G(new Event("abort",{bubbles:!0}),[this,this.db]),this.ended?.(!1),r||s.finished()})}abort(){if(this.state==="done"||this.state==="committing")throw R("InvalidStateError","The transaction has finished.");this.stop(null)}commit(){this.check()}},Pe=class{constructor(e){c(this,"transaction",e)}select(e,t="next"){let r=lt(e),o=this.entries().filter(s=>r(s.key));return t.endsWith("unique")&&(o=o.filter((s,i)=>i===0||I(o[i-1].key,s.key)!==0)),t.startsWith("prev")&&(o=o.reverse()),o}value(e){return Y(this.storeData().records.get(J(e.primaryKey))?.value)}get(e){return rt(e),this.transaction.request(this,()=>{let[t]=this.select(e);return t?this.value(t):void 0})}getKey(e){return rt(e),this.transaction.request(this,()=>Y(this.select(e)[0]?.primaryKey))}getAll(e,t){return this.transaction.request(this,()=>yn(this.select(e),t).map(r=>this.value(r)))}getAllKeys(e,t){return this.transaction.request(this,()=>yn(this.select(e),t).map(r=>Y(r.primaryKey)))}count(e){return this.transaction.request(this,()=>this.select(e).length)}openCursor(e,t){return bn(this,e,t,!0)}openKeyCursor(e,t){return bn(this,e,t,!1)}};function rt(n){if(n==null)throw R("DataError","A key or key range is required.")}var yn=(n,e)=>e===void 0?n:n.slice(0,e);function lt(n){if(n==null)return()=>!0;if(typeof n=="object"&&"lowerOpen"in n){let t=n,r=t.lower===void 0?void 0:K(t.lower),o=t.upper===void 0?void 0:K(t.upper);return s=>{let i=r===void 0?1:I(s,r),a=o===void 0?-1:I(s,o);return(t.lowerOpen?i>0:i>=0)&&(t.upperOpen?a<0:a<=0)}}let e=K(n);return t=>I(t,e)===0}function bn(n,e,t="next",r){let o=e??void 0;lt(o);let s=n.transaction.request(n,()=>new(r?Ae:ce)(n,s,n.select(o,t),t).move());return s}var ce=class{constructor(e,t,r,o){c(this,"source",e);c(this,"request",t);c(this,"list",r);c(this,"direction",o);c(this,"key");c(this,"primaryKey");c(this,"at",-1);c(this,"ready",!0)}move(e=()=>!0){let t=this.source.storeData().records;for(this.ready=!0,this.at++;this.at<this.list.length;this.at++){let r=this.list[this.at];if(!(!t.has(J(r.primaryKey))||!e(r)))return this.key=Y(r.key),this.primaryKey=Y(r.primaryKey),this.show(r),this}return null}show(e){}step(e){if(this.source.transaction.check(),!this.ready)throw R("InvalidStateError","The cursor is already moving.");this.ready=!1,this.source.transaction.again(this.request,()=>this.move(e))}continue(e){if(e==null)return this.step();let t=K(e),r=this.direction.startsWith("prev");this.step(o=>r?I(o.key,t)<=0:I(o.key,t)>=0)}continuePrimaryKey(e,t){let r=K(e),o=K(t),s=this.direction.startsWith("prev");this.step(i=>{let a=I(i.key,r)||I(i.primaryKey,o);return s?a<=0:a>=0})}advance(e){if(!(e>0))throw new TypeError("advance needs a positive count.");let t=e;this.step(()=>--t===0)}store(){let e=this.source;return e instanceof de?e:e.objectStore}update(e){let t=this.store();return t.keyPath===null?t.put(e,this.primaryKey):t.put(e)}delete(){return this.store().delete(this.primaryKey)}},Ae=class extends ce{constructor(){super(...arguments);c(this,"value")}show(t){this.value=this.source.value(t)}},de=class extends Pe{constructor(t,r){super(t);c(this,"data",r)}get name(){return this.data.name}get keyPath(){return this.data.keyPath}get autoIncrement(){return this.data.autoIncrement}get indexNames(){return at(this.data.indexes.keys())}storeData(){return this.data}entries(){return[...this.data.records.values()].map(t=>({key:t.key,primaryKey:t.key})).sort((t,r)=>I(t.key,r.key))}select(t,r){if(t==null||typeof t=="object"&&"lowerOpen"in t)return super.select(t,r);let o=this.data.records.get(J(K(t)));return o?[{key:o.key,primaryKey:o.key}]:[]}put(t,r){return this.write(t,r,!1)}add(t,r){return this.write(t,r,!0)}write(t,r,o){let{transaction:s,data:i}=this;s.check(!0);let a=Y(t),u;if(i.keyPath!==null){if(r!==void 0)throw R("DataError","This store takes its keys from its values.");u=it(a,i.keyPath)}else r!==void 0&&(u=K(r));if(u===void 0&&!i.autoIncrement)throw R("DataError","The value has no key.");return s.request(this,()=>{let f=u,d=i.nextKey;f===void 0?(f=i.nextKey++,typeof i.keyPath=="string"&&eo(a,i.keyPath,f),s.schemaChanged=!0):i.autoIncrement&&typeof f=="number"&&f>=i.nextKey&&(i.nextKey=Math.floor(f)+1,s.schemaChanged=!0);let g=J(f),h=i.records.get(g);if(o&&h)throw R("ConstraintError","A record with this key already exists.");return this.checkUnique(g,a),i.records.set(g,{key:f,value:a}),s.undo.push(()=>{i.nextKey=d,h?i.records.set(g,h):i.records.delete(g)}),s.ops.push({store:i.name,put:f,value:a}),Y(f)})}checkUnique(t,r){for(let o of this.data.indexes.values())if(o.unique){for(let s of ot(o,r))for(let[i,a]of this.data.records)if(i!==t&&ot(o,a.value).some(u=>I(u,s)===0))throw R("ConstraintError",`Another record has this ${o.name}.`)}}delete(t){let{transaction:r,data:o}=this;return r.check(!0),rt(t),lt(t),r.request(this,()=>{for(let{primaryKey:s}of this.select(t)){let i=J(s),a=o.records.get(i);a&&(o.records.delete(i),r.undo.push(()=>o.records.set(i,a)),r.ops.push({store:o.name,remove:a.key}))}})}clear(){let{transaction:t,data:r}=this;return t.check(!0),t.request(this,()=>{let o=r.records;r.records=new Map,t.undo.push(()=>r.records=o),t.ops.push({store:r.name,clear:!0})})}upgrading(){if(this.transaction.mode!=="versionchange")throw R("InvalidStateError","Indexes change only while a database upgrades.");this.transaction.check(),this.transaction.schemaChanged=!0}createIndex(t,r,o){if(this.upgrading(),this.data.indexes.has(t))throw R("ConstraintError",`An index named ${t} already exists.`);let s=r;return this.data.indexes.set(t,{name:t,keyPath:Array.isArray(s)?[...s]:String(s),unique:o?.unique===!0,multiEntry:o?.multiEntry===!0}),this.transaction.undo.push(()=>this.data.indexes.delete(t)),this.index(t)}deleteIndex(t){this.upgrading();let r=this.data.indexes.get(t);if(!r)throw R("NotFoundError",`No index named ${t}.`);this.data.indexes.delete(t),this.transaction.undo.push(()=>this.data.indexes.set(t,r))}index(t){this.transaction.check();let r=this.data.indexes.get(t);if(!r)throw R("NotFoundError",`No index named ${t}.`);return new De(this,r)}};function ot(n,e){if(n.multiEntry&&typeof n.keyPath=="string"){let r=wn(e,n.keyPath);if(Array.isArray(r)){let o=r.map(s=>st(s)).filter(s=>s!==void 0);return o.filter((s,i)=>o.findIndex(a=>I(a,s)===0)===i)}}let t=it(e,n.keyPath);return t===void 0?[]:[t]}var De=class extends Pe{constructor(t,r){super(t.transaction);c(this,"objectStore",t);c(this,"index",r)}get name(){return this.index.name}get keyPath(){return this.index.keyPath}get unique(){return this.index.unique}get multiEntry(){return this.index.multiEntry}storeData(){return this.objectStore.data}entries(){let t=[];for(let r of this.objectStore.data.records.values())for(let o of ot(this.index,r.value))t.push({key:o,primaryKey:r.key});return t.sort((r,o)=>I(r.key,o.key)||I(r.primaryKey,o.primaryKey))}},pe=class extends ie{constructor(t){super();c(this,"data",t);c(this,"onversionchange",null);c(this,"onclose",null);c(this,"onerror",null);c(this,"onabort",null);c(this,"upgrade",null);c(this,"closed",!1);t.connections.add(this)}get name(){return this.data.name}get version(){return this.data.version}get objectStoreNames(){return at(this.data.stores.keys())}transaction(t,r="readonly"){if(this.closed)throw R("InvalidStateError","The database connection is closed.");if(this.upgrade)throw R("InvalidStateError","The database is upgrading.");if(r!=="readonly"&&r!=="readwrite")throw new TypeError(`Bad transaction mode: ${String(r)}`);let o=typeof t=="string"?[t]:[...t];if(o.length===0)throw R("InvalidAccessError","A transaction needs an object store.");for(let i of o)if(!this.data.stores.has(i))throw R("NotFoundError",`No object store named ${i}.`);let s=new ue(this,[...new Set(o)],r);return this.data.enqueue(s),s}createObjectStore(t,r){let o=this.upgrading();if(this.data.stores.has(t))throw R("ConstraintError",`An object store named ${t} already exists.`);let s=r?.keyPath??null;if(r?.autoIncrement&&(s===""||Array.isArray(s)))throw R("InvalidAccessError","A key generator needs a single key path.");let i=new Me(t,Array.isArray(s)?[...s]:s,r?.autoIncrement===!0);return this.data.stores.set(t,i),o.widen(t),o.undo.push(()=>{this.data.stores.delete(t),o.forget(t)}),o.objectStore(t)}deleteObjectStore(t){let r=this.upgrading(),o=this.data.stores.get(t);if(!o)throw R("NotFoundError",`No object store named ${t}.`);this.data.stores.delete(t),r.forget(t),r.ops.push({store:t,clear:!0}),r.undo.push(()=>{this.data.stores.set(t,o),r.widen(t)})}upgrading(){if(!this.upgrade)throw R("InvalidStateError","Object stores change only while a database upgrades.");return this.upgrade.check(),this.upgrade.schemaChanged=!0,this.upgrade}close(){this.closed=!0,this.data.connections.delete(this)}},Ce=class{constructor(e){c(this,"backend",e);c(this,"loading",new Map)}load(e){let t=this.loading.get(e);return t||(t=to(this.backend.open(e)).then(r=>new Re(e,this.backend,r),()=>new Re(e,null,null)),this.loading.set(e,t)),t}open(e,t){if(t!==void 0&&!(Math.floor(t)>=1))throw new TypeError("The version must be at least 1.");let r=new le(null,null);return this.load(String(e)).then(o=>this.connect(r,o,t===void 0?void 0:Math.floor(t))),r}connect(e,t,r){let o=r??(t.version||1),s=d=>{e.readyState="done",e.result=void 0,e.error=d,G(new Event("error",{bubbles:!0,cancelable:!0}),[e])},i=d=>{e.readyState="done",e.result=d,G(new Event("success"),[e])};if(o<t.version)return s(R("VersionError",`The database is at version ${t.version}.`));if(o===t.version)return i(new pe(t));let a=t.version;for(let d of[...t.connections])G(Ee("versionchange",a,o),[d]);let u=new pe(t),f=new ue(u,[...t.stores.keys()],"versionchange");f.schemaChanged=!0,u.upgrade=f,f.ended=d=>{if(u.upgrade=null,e.transaction=null,d)return i(u);u.close(),s(R("AbortError","The upgrade was aborted."))},t.enqueue(f,()=>{t.version=o,f.undo.push(()=>t.version=a),e.readyState="done",e.result=u,e.transaction=f,G(Ee("upgradeneeded",a,o),[e])&&f.abort()})}deleteDatabase(e){let t=new le(null,null);return this.load(String(e)).then(async r=>{let o=r.version;for(let s of[...r.connections])G(Ee("versionchange",o,null),[s]),s.close();await r.backend?.remove(r.name).catch(()=>{}),this.loading.delete(r.name),t.readyState="done",G(Ee("success",o,null),[t])}),t}async databases(){return this.backend.list().catch(()=>[])}cmp(e,t){return I(K(e),K(t))}};function to(n){return new Promise((e,t)=>{let r=setTimeout(()=>t(new Error("Timed out")),8e3);n.then(e,t).finally(()=>clearTimeout(r))})}function no(n){return new Ce(n)}function kn(n){try{if(window.origin!=="null")return;let e={indexedDB:no(n),IDBFactory:Ce,IDBDatabase:pe,IDBTransaction:ue,IDBObjectStore:de,IDBIndex:De,IDBCursor:ce,IDBCursorWithValue:Ae,IDBRequest:ae,IDBOpenDBRequest:le};for(let[t,r]of Object.entries(e))Object.defineProperty(window,t,{configurable:!0,writable:!0,value:r})}catch{}}function vn(n){return n.key==="Enter"&&!n.isComposing&&!n.ctrlKey&&!n.metaKey&&!n.altKey&&!n.shiftKey}var Sn=9;var ro=["pointerdown","pointerup","mousedown","mouseup","touchstart","touchend","click","dblclick","contextmenu","auxclick"],oo=500,so=100,io="all:initial!important;position:fixed!important;z-index:2147483647!important;display:block!important;pointer-events:auto!important;background:transparent!important;touch-action:manipulation!important;user-select:none!important;-webkit-user-select:none!important;-webkit-tap-highlight-color:transparent!important;";function xn(n){let e=n;if(!e||typeof e!="object")return null;let{x:t,y:r,w:o,h:s}=e;return Number.isFinite(t)&&Number.isFinite(r)&&Number.isFinite(o)&&Number.isFinite(s)&&o>0&&s>0?{x:t,y:r,w:o,h:s}:null}function ao(n){let e=n.composedPath?.()[0]??n.target;if(!e||typeof e.tagName!="string")return!1;if(e.isContentEditable||e.tagName==="INPUT"||e.tagName==="TEXTAREA"||e.tagName==="SELECT")return!0;try{return e.matches('button:focus-visible, a[href]:focus-visible, summary:focus-visible, [role="button"]:focus-visible')}catch{return!1}}function En(n){let e=!1,t=null,r=null,o=null,s=!1,i=null,a=null,u=!1,f=m=>{try{m()}catch{}},d=m=>{let k=t;return k!==null&&m.clientX>=k.x&&m.clientX<=k.x+k.w&&m.clientY>=k.y&&m.clientY<=k.y+k.h},g=()=>{f(n.gesture),f(n.press)},h=()=>f(()=>{let m=t;!m||!o||(o.parentNode!==document.documentElement&&document.documentElement.append(o),n.target({ok:document.elementFromPoint(m.x+m.w/2,m.y+m.h/2)===o,hover:s}))}),A=m=>{o&&o.style.setProperty("cursor",m?"pointer":"default","important"),s!==m&&(s=m,h())},y=()=>{let m=document.createElement("div");m.setAttribute("aria-hidden","true"),m.style.cssText=io;for(let D of ro)m.addEventListener(D,N=>N.stopPropagation());let k=!1;return m.addEventListener("touchstart",D=>{k=D.defaultPrevented}),m.addEventListener("touchend",D=>{let N=D.changedTouches?.[0];D.isTrusted&&(k||D.defaultPrevented)&&N&&d(N)&&g()}),m.addEventListener("pointermove",D=>A(D.pointerType==="mouse"&&d(D))),m.addEventListener("pointerleave",()=>A(!1)),m},w=()=>{let m=r;if(!t||!m){o?.remove(),o=null,s=!1,i!==null&&clearInterval(i),i=null;return}o??(o=y());let k=o.style;k.setProperty("left",`${m.x}px`,"important"),k.setProperty("top",`${m.y}px`,"important"),k.setProperty("width",`${m.w}px`,"important"),k.setProperty("height",`${m.h}px`,"important"),i??(i=setInterval(h,oo)),h()},M=()=>{try{for(let m of navigator.getGamepads?.()??[])if(m?.buttons[Sn]?.pressed)return!0}catch{}return!1},L=()=>{e!==(a!==null)&&(a!==null?(clearInterval(a),a=null):(u=!0,a=setInterval(()=>{let m=M();m&&!u&&f(n.key),u=m},so)))},O=m=>{!e||!m.isTrusted||!vn(m)||ao(m)||(m.preventDefault(),m.stopImmediatePropagation(),!(m.type!=="keydown"||m.repeat)&&(f(n.gesture),f(n.key)))};if(typeof window<"u"){for(let m of["keydown","keypress","keyup"])window.addEventListener(m,O,!0);window.addEventListener("click",m=>{o!==null&&m.target===o&&m.isTrusted&&d(m)&&g()},!0)}return{receive(m){f(()=>{let k=m;e=k?.keys===!0,t=xn(k?.button),r=xn(k?.cover),w(),L()})}}}var lo=new Set(["Escape","Tab","Shift","Control","Alt","Meta"]);function Mn(n=()=>Date.now()){if(typeof Element>"u"||typeof document>"u"||typeof Element.prototype.requestPointerLock!="function")return;let e=Element.prototype.requestPointerLock,t=null,r=null,o=()=>{t=null,r=null};Element.prototype.requestPointerLock=function(...i){return t={element:this,options:i[0]},r=null,e.apply(this,i)};let s=Document.prototype.exitPointerLock;typeof s=="function"&&(Document.prototype.exitPointerLock=function(){return o(),s.call(this)}),document.addEventListener("pointerlockerror",()=>{t&&(r=n())},!0),document.addEventListener("pointerlockchange",o,!0),window.addEventListener("keydown",i=>{if(r===null||!t||!i.isTrusted)return;if(i.key==="Escape")return o();if(lo.has(i.key))return;let a=i.target;if(a&&(a.isContentEditable||["INPUT","TEXTAREA","SELECT"].includes(a.tagName)))return;let{element:u,options:f}=t,d=n()-r>2e4;if(o(),!(d||!u.isConnected))try{e.call(u,f)?.catch?.(()=>{})}catch{}},!0)}var Rn=(n,e)=>Object.prototype.hasOwnProperty.call(n,e);function ut(n){try{return n(),!0}catch{return!1}}function uo(){let n=new Map,e={getItem:t=>n.has(String(t))?n.get(String(t)):null,setItem:(t,r)=>{n.set(String(t),String(r))},removeItem:t=>{n.delete(String(t))},clear:()=>n.clear(),key:t=>[...n.keys()][Number(t)]??null};return new Proxy(e,{get(t,r){if(r==="length")return n.size;if(r===Symbol.toStringTag)return"Storage";if(typeof r=="string")return Rn(e,r)?t[r]:n.get(r)},set(t,r,o){return typeof r=="string"&&n.set(r,String(o)),!0},has:(t,r)=>typeof r=="string"&&(Rn(e,r)||n.has(r)),deleteProperty(t,r){return typeof r=="string"&&n.delete(r),!0},ownKeys:()=>[...n.keys()],getOwnPropertyDescriptor:(t,r)=>typeof r=="string"&&n.has(r)?{value:n.get(r),writable:!0,enumerable:!0,configurable:!0}:void 0})}function co(n=Date.now){let e=new Map;return{read:()=>[...e].map(([t,r])=>`${t}=${r}`).join("; "),write(t){let[r,...o]=String(t).split(";"),s=r.indexOf("="),i=(s<0?"":r.slice(0,s)).trim(),a=(s<0?r:r.slice(s+1)).trim();if(!i&&!a)return;let u=!1;for(let f of o){let[d,...g]=f.split("="),h=g.join("=").trim();d.trim().toLowerCase()==="max-age"&&Number(h)<=0&&(u=!0),d.trim().toLowerCase()==="expires"&&Date.parse(h)<=n()&&(u=!0)}u?e.delete(i):e.set(i,a)}}}function fe(n,e,t){try{Object.defineProperty(n,e,{configurable:!0,...t})}catch{}}function po(){if(typeof HTMLImageElement!="function")return;let n=Object.getOwnPropertyDescriptor(HTMLImageElement.prototype,"src");if(!n?.get||!n.set)return;let{get:e,set:t}=n;fe(HTMLImageElement.prototype,"src",{enumerable:n.enumerable,get(){return e.call(this)},set(r){try{this.crossOrigin===null&&new URL(String(r),document.baseURI).origin===location.origin&&(this.crossOrigin="anonymous")}catch{}t.call(this,r)}})}function fo(){let n=window.Worker;if(typeof n!="function")return;let e=function(t,r){try{return new n(t,r)}catch(o){if(!(o instanceof DOMException&&o.name==="SecurityError")||r?.type==="module")throw o;let s;try{s=new URL(String(t),document.baseURI)}catch{throw o}if(s.origin!==location.origin)throw o;let a=`const base = ${JSON.stringify(s.href)};
|
|
3
3
|
const resolve = (input) => new URL(String(input), base).href;
|
|
4
4
|
const importScripts_ = self.importScripts.bind(self);
|
|
5
5
|
self.importScripts = (...urls) => importScripts_(...urls.map(resolve));
|
|
@@ -9,4 +9,4 @@ if (typeof XMLHttpRequest === 'function') {
|
|
|
9
9
|
const open_ = XMLHttpRequest.prototype.open;
|
|
10
10
|
XMLHttpRequest.prototype.open = function (method, url, ...rest) { return open_.call(this, method, resolve(url), ...rest); };
|
|
11
11
|
}
|
|
12
|
-
importScripts(base);`;return new n(URL.createObjectURL(new Blob([l],{type:"text/javascript"})),r)}};e.prototype=n.prototype,ee(window,"Worker",{value:e,writable:!0})}function zt(){try{for(let n of["localStorage","sessionStorage"])Te(()=>window[n].length)||ee(window,n,{value:hr()});if(!Te(()=>document.cookie)){let n=yr();ee(document,"cookie",{get:()=>n.read(),set:e=>n.write(e)})}if(window.origin==="null"&&(wr(),br()),window.origin==="null"||!Te(()=>history.replaceState(history.state,"",location.href)))for(let n of["pushState","replaceState"]){let e=history[n];ee(history,n,{value:function(...t){try{return e.apply(this,t)}catch(r){if(!(r instanceof DOMException&&r.name==="SecurityError"))throw r}}})}}catch{}}var kr=["menu","lobby","stick","buttons"];function Ft(n){if(!Array.isArray(n)||n.length>16)return null;let e=[];for(let t of n){if(!t||typeof t!="object")continue;let{kind:r,x:s,y:o,w:i,h:l}=t;kr.includes(r)&&(!ue(s)||!ue(o)||!ue(i)||!ue(l)||i<0||l<0||e.push({kind:r,x:s,y:o,w:i,h:l}))}return e}var ue=n=>typeof n=="number"&&Number.isFinite(n),Ie={entries:10,entryMs:1e3,totalMs:2e3};function Kt(n){let e=typeof n=="number"?[n]:Array.isArray(n)?n.slice(0,Ie.entries):null;if(!e||!e.every(s=>typeof s=="number"&&Number.isFinite(s)&&s>=0))return null;let t=[],r=Ie.totalMs;for(let s of e){let o=Math.min(Math.round(s),Ie.entryMs,r);if(t.push(o),r-=o,r<=0)break}return t.some((s,o)=>o%2===0&&s>0)?t:[]}var vr=/^#(?:[0-9a-f]{3}){1,2}$/i,te={panel:"#111216",text:"#f7f7f8",accent:"#d33e00"},Ne=4.5,Be=3,de=["#4ade80","#15803d"];function Vt(n){let e=[];if(n==null)return{look:null,notes:e};if(typeof n!="object")return{look:null,notes:["a look is { accent, panel, text } or null"]};let t=n,r=m=>{let h=t[m];return h==null?null:typeof h=="string"&&vr.test(h)?Wt(h):(e.push(`${m} is not #rgb or #rrggbb`),null)},s=r("panel"),o=r("text"),i=r("accent");if(s||o){let m=z(o??te.text,s??te.panel);m<Ne&&(e.push(`text on panel is ${m.toFixed(1)}:1, under ${Ne}:1: the platform's are used`),s=o=null)}let l=s??te.panel,c=o??te.text;if(i){let m=z(i,l);m<Be&&(e.push(`accent on panel is ${m.toFixed(1)}:1, under ${Be}:1: the platform's is used`),i=null)}if(!i&&s&&z(te.accent,s)<Be&&(i=c),!s&&!o&&!i)return{look:null,notes:e};let d=i?Sr(i):null;return{look:{panel:s,text:o,muted:s||o?xr(c,l):null,accent:i,accentHover:i?Jt(i,d==="#ffffff"?"#000000":"#ffffff",.14):null,onAccent:d,ok:s?z(de[0],s)>=z(de[1],s)?de[0]:de[1]:null},notes:e}}function Wt(n){let e=n.slice(1).toLowerCase();return`#${e.length===3?[...e].map(t=>t+t).join(""):e}`}function Ge(n){let e=Wt(n);return[parseInt(e.slice(1,3),16),parseInt(e.slice(3,5),16),parseInt(e.slice(5,7),16)]}function Ut(n){let[e,t,r]=Ge(n).map(s=>{let o=s/255;return o<=.04045?o/12.92:((o+.055)/1.055)**2.4});return .2126*e+.7152*t+.0722*r}function z(n,e){let[t,r]=[Ut(n),Ut(e)].sort((s,o)=>o-s);return(t+.05)/(r+.05)}function Sr(n){return z(n,"#000000")>z(n,"#ffffff")?"#000000":"#ffffff"}function Jt(n,e,t){let[r,s]=[Ge(n),Ge(e)];return`#${r.map((o,i)=>Math.round(o+(s[i]-o)*t).toString(16).padStart(2,"0")).join("")}`}function xr(n,e){for(let t=.3;t>.001;t-=.05){let r=Jt(n,e,t);if(z(r,e)>=Ne)return r}return n}var me=class{constructor(){p(this,"areas",null);p(this,"key","")}receive(e){let t=Ft(e);if(!t)return!1;let r=JSON.stringify(t);return this.areas&&r===this.key?!1:(this.key=r,this.areas=Object.freeze(t.map(s=>Object.freeze(s))),!0)}};function Yt(n){let e={look:null,notes:["a look is { accent, panel, text } or null"]};if(n==null)return{look:null,notes:[]};if(typeof n!="object")return e;let t={};try{for(let r of["accent","panel","text"]){let s=n[r];s!=null&&(t[r]=typeof s=="string"?s.slice(0,16):"")}}catch{return e}return{look:t,notes:Vt(t).notes}}function Qt(n,e,t){let r=typeof n.vibrate=="function"?n.vibrate.bind(n):null,s=o=>{try{if(!e())return r?r(o):!1;if(!r)return!1;let i=Kt(Array.isArray(o)?Array.from(o):o);return i?(t(i),!0):!1}catch{return!1}};try{Object.defineProperty(n,"vibrate",{value:s,configurable:!0,writable:!0})}catch{}}var Mr=["RTCPeerConnection","webkitRTCPeerConnection","mozRTCPeerConnection"];function Zt(n){if(n)for(let e of Mr)try{Object.defineProperty(n,e,{value:void 0,writable:!1,enumerable:!1,configurable:!1})}catch{}}function Xt(n,e){if(Zt(n),!!e)for(let t of["contentWindow","contentDocument"])try{let r=Object.getOwnPropertyDescriptor(e.prototype,t),s=r?.get;if(!r||!s)continue;Object.defineProperty(e.prototype,t,{...r,get(){let o=s.call(this);return Zt(t==="contentWindow"?o:o?.defaultView),o}})}catch{}}var en="0.1.0";function on(){let n=globalThis;return n.onceworlds?.__onceworlds||(n.onceworlds=Rr()),n.onceworlds}function Rr(){let n=lt(document.currentScript);zt(),Xt(window,typeof HTMLIFrameElement>"u"?void 0:HTMLIFrameElement);let e=at(),t=Gt({stats:a=>x("stats",a)}),r=window.parent!==window,s=r&>(location.search)?wt(console,location.origin):null,o="connecting",i=!r&&bt(location.search),l=null,c=null,d=1,m=new Map,h=new Map,f=null,w=[],y,k=null,E=0,_=new Set,T=new me,M=new Set,P=new Set,L=r?mt(a=>x("clip",a),void 0,()=>f?{phase:f.match.phase,paused:!!f.match.paused,flow:ut(f.state)}:null):null,U=s?Ht(a=>x("snapshot",a)):null,N=new le(a=>{o!=="standalone"&&R("ui.controls",a).catch(()=>{})},a=>x("input",{touch:a})),j,A=new Promise(a=>j=a),je=xt(),He=Mt(()=>f,je),sn={read:a=>R("data.game.get",{key:a}),op:a=>R("data.game.op",a)},an=Et(()=>G().then(a=>a.player),B),ln=Rt(async()=>(await A,o==="platform"?sn:an),je),be=new Set,we=[],qe=a=>{let u=`${a.message}|${a.source}|${a.line}`;be.has(u)||be.size>=S.errorReportsPerPage||(be.add(u),o==="platform"?x("error",a):o==="connecting"&&we.push(a))};if(window.addEventListener("error",a=>{if(!(a instanceof ErrorEvent))return;let u=String(a.message||"Error");if(qe({message:u,source:tn(a.filename),line:a.lineno||0,column:a.colno||0,stack:a.error instanceof Error?String(a.error.stack??""):""}),s){let g=a.error instanceof Error?`Uncaught ${ae(a.error)}`:/^Uncaught\b/.test(u)?u:`Uncaught ${u}`;s.error(g,{source:tn(a.filename),line:a.lineno||0,column:a.colno||0})}}),window.addEventListener("unhandledrejection",a=>{let u=a.reason;s?.error(`Uncaught (in promise) ${u instanceof Error?ae(u):V(u)}`),qe({message:`Unhandled promise rejection: ${u instanceof Error?u.message:String(u)}`,source:"",line:0,column:0,stack:u instanceof Error?String(u.stack??""):""})}),r){window.addEventListener("message",u=>{if(!(u.source!==window.parent||u.data?.type!==nt||!u.ports[0])&&o!=="standalone"){if(c?.close(),c=u.ports[0],c.onmessage=g=>cn(g.data),o=ht(u.data),i=o==="platform"&&yt(u.data),j(),o==="platform")for(let g of we.splice(0))x("error",g);else we.length=0;s?.connect(g=>x("console",g)),y||(x("alive",{}),y=setInterval(()=>x("alive",{}),2e3)),o==="platform"&&G().then(g=>{e.apply(g.audio),g.settings&&t.apply(g.settings)}).catch(()=>{})}});let a={type:tt,version:en,...n?{build:n}:{}};window.parent.postMessage(a,"*"),setTimeout(()=>{o==="connecting"&&(o="standalone",j())},5e3)}else o="standalone",j();if(window.addEventListener("keydown",a=>{a.key!=="/"||o!=="platform"||!f||Ar(a.target)||(a.preventDefault(),a.stopImmediatePropagation(),x("hotkey",{key:"/"}))},!0),r){try{document.documentElement.style.touchAction="pan-x pan-y"}catch{}for(let a of["gesturestart","gesturechange","gestureend"])document.addEventListener(a,u=>u.preventDefault(),{passive:!1})}if(r){let a=u=>{u.pointerType==="mouse"||o!=="platform"||(window.removeEventListener("pointerup",a,!0),R("ui.tapped",void 0).catch(()=>{}))};window.addEventListener("pointerup",a,!0)}if(r&&It(()=>o==="platform",()=>x("input.first",{})),r&&Qt(navigator,()=>o==="platform",a=>x("vibrate",{pattern:a})),r){let a=Element.prototype.requestFullscreen;Element.prototype.requestFullscreen=function(u){return o!=="platform"?a.call(this,u):(R("ui.fullscreen",void 0).catch(()=>{}),Promise.resolve())}}function cn(a){if(a.kind==="res"){let g=m.get(a.id);if(!g)return;m.delete(a.id),a.ok?g.resolve(a.result):g.reject(new Error(a.error));return}if(a.kind!=="event")return;let u=a.name;if(u==="room")f?._receive(a.data);else if(u==="room.connection")a.data?.connected===!1&&f?._disconnected();else if(u==="room.closed"){let g=f;f=null,g?._closed(a.data.reason)}else if(u==="room.moved"){let g=f;f=ze(a.data),g?._closed("moved");for(let v of _)try{v(f)}catch(D){console.error('[onceworlds] error in rooms "moved" listener',D)}}else if(u==="audio")e.apply(a.data),re("audio",a.data);else if(u==="settings")t.apply(a.data);else if(u==="pause"||u==="resume")re(u);else if(u==="controls")N.receive(a.data);else if(u==="input")N.touch=a.data?.touch===!0;else if(u==="reserved")T.receive(a.data)&&re("reserved",T.areas);else if(u==="clip")L?.receive(a.data);else if(u==="snapshot")U?.receive(a.data);else if(u==="debug")for(let g of P)try{g(a.data)}catch(v){console.error("[onceworlds] error in the debugger link",v)}else if(u==="player"){let g=a.data;G().then(v=>{g?.id!==v.player.id||typeof g.name!="string"||(v.player.name=g.name,re("player",v.player))}).catch(()=>{})}}async function R(a,u){await A;let g=c;if(!g)throw new Error("Not connected to Onceworlds");return new Promise((v,D)=>{let b=d++;m.set(b,{resolve:v,reject:D}),g.postMessage({kind:"req",id:b,method:a,params:u})})}function x(a,u){c?.postMessage({kind:"event",name:a,data:u})}function re(a,...u){for(let g of h.get(a)??[])try{g(...u)}catch(v){console.error(`[onceworlds] error in "${a}" listener`,v)}}function ze(a,u=w){let g={send:(b,$)=>x("room.send",{d:b,to:$}),presence:b=>x("room.presence",{d:b}),state:(b,$,C)=>x("room.state",{k:b,v:$,w:C}),hideLobby:b=>x("room.hideLobby",{hidden:b}),team:(b,$)=>x("room.team",{id:b,team:$}),open:b=>x("room.open",{open:b}),result:b=>x("room.result",{result:b}),kick:b=>x("room.kick",{id:b}),voteKick:b=>x("room.votekick",{id:b}),ready:b=>x("room.ready",{ready:b}),unready:()=>x("room.unready",{}),transfer:b=>x("room.transfer",{id:b}),start:b=>x("room.start",b),end:()=>x("room.end",{}),pause:b=>x("room.pause",{paused:b}),admit:b=>x("room.admit",{ids:b}),private:(b,$,C)=>x("room.private",{k:b,v:$,...C?{id:C}:{}}),dataReset:()=>x("room.datareset",{}),leave:()=>{f===v&&(f=null),R("room.leave",void 0).catch(()=>{})}};w=u;let v=new Z(a,g,()=>Date.now()+E,u);Ue(v);let D=Ot(b=>x("room.idle",{idle:b}));return v.on("reconnect",()=>D.idle&&x("room.idle",{idle:!0})),v.on("close",()=>D.stop()),v}function Ue(a){a.on("state",(u,g,v)=>{f===a&&ke(u)&&He._changed(u.slice(H.length),g,v)})}function un(a,u){let g={};for(let[C,W]of Object.entries(ge.all()))g[H+C]=W;let v={room:"solo",you:a.id,host:a.id,players:[{...a,presence:null,team:u.teams?1:0}],state:g,chat:[],teams:u.teams??0,open:!0,now:Date.now(),ranked:!1,kept:!0,owner:a.id},b={..._t(()=>$),state(C,W){ke(C)&&ge.set(C.slice(H.length),W)},dataReset(){for(let C of Object.keys(ge.all()))ge.set(C,null),$._receive({t:"state",k:H+C,v:null,from:""})}},$=new Z(v,b,()=>Date.now()+E,Re(u.settings));return Ue($),$}function G(){return k??(k=A.then(()=>o==="platform"?R("session.get",void 0):$r()).then(a=>(E=Number(a.timeOffset)||0,a))),k}G().catch(()=>{});let Fe={async get(a){return await A,o==="platform"?await R("save.get",{key:a}):pe.get(a)},async set(a,u){await A,o==="platform"?await R("save.set",{key:a,value:u}):pe.set(a,u)},async delete(a){await A,o==="platform"?await R("save.delete",{key:a}):pe.delete(a)},async list(){return await A,o==="platform"?R("save.list",void 0):pe.list()}},Ke={__onceworlds:!0,_debug:s?{send:a=>{c&&x("debug",a)},on:a=>(P.add(a),()=>P.delete(a))}:null,version:en,get mode(){return o},ready:()=>A,player:{get:()=>G().then(a=>a.player),async avatarUrl(a,u="full"){let g=await G().catch(()=>null);if(!g?.platformOrigin)return null;let v=typeof a=="string"&&a?a:g.player.id;return`${g.platformOrigin}/avatar/${encodeURIComponent(v)}${u==="head"?"/head":""}.svg`},async avatar(a){if(await A,o!=="platform")return null;let u=typeof a=="string"&&a?a:(await G()).player.id;return it.test(u)?We(await R("player.avatar",{id:u}).catch(()=>null)):null},async rename(){return await A,o!=="platform"?null:await R("player.rename",void 0).catch(()=>null)?(await G()).player:null}},game:{info:()=>G().then(a=>a.game)},save:Fe,data:{player:Fe,server:He,game:ln},rooms:{async join(a={}){await A;let u=a.party===!0&&f!==null&&o==="platform";if(u||(f?.leave(),f=null),o!=="platform"){let{player:D}=await G();return f=un(D,a),f}let g=await R("room.join",{name:a.name,maxPlayers:a.maxPlayers,mode:a.mode,teams:a.teams,private:a.private,ranked:a.ranked,party:a.party,minPlayers:a.minPlayers,minPlayersPrivate:a.minPlayersPrivate,settings:a.settings,countdown:a.countdown,lobby:a.lobby,...a.ownCountdown===!0?{ownCountdown:!0}:{}}),v=f;return f=ze(g,Re(a.settings)),u&&v?._closed("moved"),f},get current(){return f},on(a,u){return a!=="moved"?()=>{}:(_.add(u),()=>_.delete(u))}},ratings:{async get(a){return await A,o==="platform"?R("ratings.get",{mode:String(a)}):null},async top(a,u={}){return await A,o==="platform"?R("ratings.top",{mode:String(a),limit:u.limit}):[]}},ui:{setMenuPosition(a){R("ui.menuPosition",{position:a}).catch(()=>{})},get reserved(){return T.areas},setLook(a){let{look:u,notes:g}=Yt(a);for(let v of g)M.has(v)||(M.add(v),console.warn(`[onceworlds] ui.setLook: ${v}`));o!=="standalone"&&R("ui.look",u).catch(()=>{})},requestFullscreen(){o==="platform"?R("ui.fullscreen",void 0).catch(()=>{}):document.documentElement.requestFullscreen?.().catch(()=>{})},showInvite(){o==="platform"&&R("ui.invite",void 0).catch(()=>{})},setOrientation(a){a!=="any"&&a!=="landscape"&&a!=="portrait"||o!=="standalone"&&R("ui.orientation",{orientation:a}).catch(()=>{})}},controls:{set(a){N.set(a)},get stick(){return N.stick},pressed(a){return N.pressed(String(a))},presses(a){return N.presses(String(a))},get touch(){return N.touch}},now:()=>Date.now()+E,leaderboards:{async submit(a,u,g={}){if(await A,o!=="platform")return he.submit(String(a),Number(u),g.lowerIsBetter===!0);try{return await R("leaderboards.submit",{board:String(a),score:Number(u),lowerIsBetter:g.lowerIsBetter===!0})}catch(v){return console.warn(`[onceworlds] score for "${a}" was not saved: ${v instanceof Error?v.message:v}`),null}},async top(a,u={}){return await A,o!=="platform"?he.top(String(a)):R("leaderboards.top",{board:String(a),limit:u.limit,lowerIsBetter:u.lowerIsBetter===!0})}},settings:t.settings,audio:{get volume(){return e.volume},get muted(){return e.muted}},badges:{async award(a){if(await A,o!=="platform")return ye.add(String(a));try{return(await R("badges.award",{key:String(a)})).awarded}catch(u){return console.warn(`[onceworlds] badge "${a}" was not awarded: ${u instanceof Error?u.message:u}`),!1}},async list(){return await A,o==="platform"?R("badges.list",void 0):ye.list()},async has(a){return(await Ke.badges.list()).some(u=>u.key===a&&u.earned)}},demo:{get active(){return i},end(){return l??(l=(async()=>(await A,i?o!=="platform"?(console.info("[onceworlds] demo.end(): the end of the demo. On Onceworlds the platform shows its end-of-demo card here."),!0):R("demo.end",void 0).then(a=>a!==!1,()=>!0):!1))().finally(()=>l=null)),l}},env:Er(),async fetch(a,u={}){if(await A,o!=="platform")throw new Error("onceworlds.fetch only works on Onceworlds, where your secrets are stored. Run the game with `onceworlds dev`.");let g=a instanceof Request?a:null,v=new URL(g?g.url:String(a),location.href).href,D=(u.method??g?.method??"GET").toUpperCase(),b={};new Headers(u.headers??g?.headers).forEach((dn,mn)=>{b[mn]=dn});let $=u.body??null;$===null&&g&&D!=="GET"&&D!=="HEAD"&&($=await g.arrayBuffer()),$ instanceof URLSearchParams&&(b["content-type"]??(b["content-type"]="application/x-www-form-urlencoded;charset=UTF-8"),$=$.toString());let C=await R("net.fetch",{url:v,method:D,headers:b,body:await Pr($)}),W=[101,204,205,304].includes(C.status);return new Response(W?null:C.body,{status:C.status,statusText:C.statusText,headers:C.headers})},on(a,u){let g=h.get(a);return g||h.set(a,g=new Set),g.add(u),()=>g.delete(u)}};return Ke}function Er(){let n=globalThis.__ONCEWORLDS_ENV__,e={};if(n&&typeof n=="object")for(let[t,r]of Object.entries(n))typeof r=="string"&&(e[t]=r);return Object.freeze(e)}async function Pr(n){if(n==null)return null;if(typeof n=="string"||n instanceof ArrayBuffer)return n;if(ArrayBuffer.isView(n))return new Uint8Array(n.buffer,n.byteOffset,n.byteLength).slice().buffer;if(n instanceof Blob)return n.arrayBuffer();throw new TypeError("onceworlds.fetch bodies must be a string (e.g. JSON.stringify(...)), bytes or a Blob.")}function tn(n){if(!n)return"";try{let e=new URL(n);return e.origin===location.origin?e.pathname.slice(1)||"index.html":e.href}catch{return n}}function Ar(n){return n instanceof HTMLElement?n.isContentEditable||["INPUT","TEXTAREA","SELECT"].includes(n.tagName):!1}function $r(){let n=B.get("onceworlds:player-id");return n||(n=`local-${Math.random().toString(36).slice(2,10)}`,B.set("onceworlds:player-id",n)),{player:{id:n,name:"Player"},game:{slug:location.hostname.split(".")[0]||"local",name:document.title||"Game",owner:"local"},audio:{volume:1,muted:!1},timeOffset:0}}var nn="onceworlds:scores",he={all(){try{let n=JSON.parse(B.get(nn)??"{}");return n&&typeof n=="object"?n:{}}catch{return{}}},submit(n,e,t){let r=he.all(),s=r[n],o=s===void 0?e:t?Math.min(s,e):Math.max(s,e);return r[n]=o,B.set(nn,JSON.stringify(r)),{best:o,rank:1}},top(n){let e=he.all()[n];if(e===void 0)return{entries:[],me:null};let t={rank:1,player:{id:"local",name:"You"},score:e};return{entries:[t],me:t}}},ne="onceworlds:save:",rn="onceworlds:badges",ye={earned(){try{let n=JSON.parse(B.get(rn)??"{}");return n&&typeof n=="object"?n:{}}catch{return{}}},add(n){let e=ye.earned();return e[n]?!1:(e[n]=Date.now(),B.set(rn,JSON.stringify(e)),!0)},list(){return Object.entries(ye.earned()).map(([n,e])=>({key:n,name:n,description:"",icon:null,earned:!0,earnedAt:e}))}},pe={get(n){let e=B.get(ne+n);if(e===null)return null;try{return JSON.parse(e)}catch{return null}},set(n,e){B.set(ne+n,JSON.stringify(e??null))},delete(n){B.remove(ne+n)},list(){let n=[];try{for(let e=0;e<localStorage.length;e++){let t=localStorage.key(e);t?.startsWith(ne)&&n.push(t.slice(ne.length))}}catch{}return n.sort()}},fe="onceworlds:server:",ge={all(){let n={};try{for(let e=0;e<localStorage.length;e++){let t=localStorage.key(e);if(t?.startsWith(fe))try{n[t.slice(fe.length)]=JSON.parse(localStorage.getItem(t)??"null")}catch{}}}catch{}return n},set(n,e){e==null?B.remove(fe+n):B.set(fe+n,JSON.stringify(e))}},B={get(n){try{return localStorage.getItem(n)}catch{return null}},set(n,e){try{localStorage.setItem(n,e)}catch{}},remove(n){try{localStorage.removeItem(n)}catch{}}};on();})();
|
|
12
|
+
importScripts(base);`;return new n(URL.createObjectURL(new Blob([a],{type:"text/javascript"})),r)}};e.prototype=n.prototype,fe(window,"Worker",{value:e,writable:!0})}function Pn(){try{for(let n of["localStorage","sessionStorage"])ut(()=>window[n].length)||fe(window,n,{value:uo()});if(!ut(()=>document.cookie)){let n=co();fe(document,"cookie",{get:()=>n.read(),set:e=>n.write(e)})}if(window.origin==="null"&&(fo(),po(),Reflect.deleteProperty(Navigator.prototype,"serviceWorker")),window.origin==="null"||!ut(()=>history.replaceState(history.state,"",location.href)))for(let n of["pushState","replaceState"]){let e=history[n];fe(history,n,{value:function(...t){try{return e.apply(this,t)}catch(r){if(!(r instanceof DOMException&&r.name==="SecurityError"))throw r}}})}}catch{}}var mo=["menu","lobby","stick","buttons"];function Dn(n){if(!Array.isArray(n)||n.length>16)return null;let e=[];for(let t of n){if(!t||typeof t!="object")continue;let{kind:r,x:o,y:s,w:i,h:a}=t;mo.includes(r)&&(!$e(o)||!$e(s)||!$e(i)||!$e(a)||i<0||a<0||e.push({kind:r,x:o,y:s,w:i,h:a}))}return e}var $e=n=>typeof n=="number"&&Number.isFinite(n),ct={entries:10,entryMs:1e3,totalMs:2e3};function Cn(n){let e=typeof n=="number"?[n]:Array.isArray(n)?n.slice(0,ct.entries):null;if(!e||!e.every(o=>typeof o=="number"&&Number.isFinite(o)&&o>=0))return null;let t=[],r=ct.totalMs;for(let o of e){let s=Math.min(Math.round(o),ct.entryMs,r);if(t.push(s),r-=s,r<=0)break}return t.some((o,s)=>s%2===0&&o>0)?t:[]}var ho=/^#(?:[0-9a-f]{3}){1,2}$/i,me={panel:"#111216",text:"#f7f7f8",accent:"#d33e00"},pt=4.5,dt=3,Te=["#4ade80","#15803d"];function $n(n){let e=[];if(n==null)return{look:null,notes:e};if(typeof n!="object")return{look:null,notes:["a look is { accent, panel, text } or null"]};let t=n,r=d=>{let g=t[d];return g==null?null:typeof g=="string"&&ho.test(g)?Tn(g):(e.push(`${d} is not #rgb or #rrggbb`),null)},o=r("panel"),s=r("text"),i=r("accent");if(o||s){let d=W(s??me.text,o??me.panel);d<pt&&(e.push(`text on panel is ${d.toFixed(1)}:1, under ${pt}:1: the platform's are used`),o=s=null)}let a=o??me.panel,u=s??me.text;if(i){let d=W(i,a);d<dt&&(e.push(`accent on panel is ${d.toFixed(1)}:1, under ${dt}:1: the platform's is used`),i=null)}if(!i&&o&&W(me.accent,o)<dt&&(i=u),!o&&!s&&!i)return{look:null,notes:e};let f=i?go(i):null;return{look:{panel:o,text:s,muted:o||s?yo(u,a):null,accent:i,accentHover:i?Ln(i,f==="#ffffff"?"#000000":"#ffffff",.14):null,onAccent:f,ok:o?W(Te[0],o)>=W(Te[1],o)?Te[0]:Te[1]:null},notes:e}}function Tn(n){let e=n.slice(1).toLowerCase();return`#${e.length===3?[...e].map(t=>t+t).join(""):e}`}function ft(n){let e=Tn(n);return[parseInt(e.slice(1,3),16),parseInt(e.slice(3,5),16),parseInt(e.slice(5,7),16)]}function An(n){let[e,t,r]=ft(n).map(o=>{let s=o/255;return s<=.04045?s/12.92:((s+.055)/1.055)**2.4});return .2126*e+.7152*t+.0722*r}function W(n,e){let[t,r]=[An(n),An(e)].sort((o,s)=>s-o);return(t+.05)/(r+.05)}function go(n){return W(n,"#000000")>W(n,"#ffffff")?"#000000":"#ffffff"}function Ln(n,e,t){let[r,o]=[ft(n),ft(e)];return`#${r.map((s,i)=>Math.round(s+(o[i]-s)*t).toString(16).padStart(2,"0")).join("")}`}function yo(n,e){for(let t=.3;t>.001;t-=.05){let r=Ln(n,e,t);if(W(r,e)>=pt)return r}return n}var Le=class{constructor(){c(this,"areas",null);c(this,"key","")}receive(e){let t=Dn(e);if(!t)return!1;let r=JSON.stringify(t);return this.areas&&r===this.key?!1:(this.key=r,this.areas=Object.freeze(t.map(o=>Object.freeze(o))),!0)}};function In(n){let e={look:null,notes:["a look is { accent, panel, text } or null"]};if(n==null)return{look:null,notes:[]};if(typeof n!="object")return e;let t={};try{for(let r of["accent","panel","text"]){let o=n[r];o!=null&&(t[r]=typeof o=="string"?o.slice(0,16):"")}}catch{return e}return{look:t,notes:$n(t).notes}}function On(n,e,t){let r=typeof n.vibrate=="function"?n.vibrate.bind(n):null,o=s=>{try{if(!e())return r?r(s):!1;if(!r)return!1;let i=Cn(Array.isArray(s)?Array.from(s):s);return i?(t(i),!0):!1}catch{return!1}};try{Object.defineProperty(n,"vibrate",{value:o,configurable:!0,writable:!0})}catch{}}var bo=["RTCPeerConnection","webkitRTCPeerConnection","mozRTCPeerConnection"];function _n(n){if(n)for(let e of bo)try{Object.defineProperty(n,e,{value:void 0,writable:!1,enumerable:!1,configurable:!1})}catch{}}function Bn(n,e){if(_n(n),!!e)for(let t of["contentWindow","contentDocument"])try{let r=Object.getOwnPropertyDescriptor(e.prototype,t),o=r?.get;if(!r||!o)continue;Object.defineProperty(e.prototype,t,{...r,get(){let s=o.call(this);return _n(t==="contentWindow"?s:s?.defaultView),s}})}catch{}}var Nn="0.1.0";function Hn(){let n=globalThis;return n.onceworlds?.__onceworlds||(n.onceworlds=wo()),n.onceworlds}function wo(){let n=_t(document.currentScript);Pn(),kn({open:l=>P("storage.open",{db:l}),commit:l=>P("storage.commit",l),remove:l=>P("storage.delete",{db:l}),list:()=>P("storage.list",void 0)}),Bn(window,typeof HTMLIFrameElement>"u"?void 0:HTMLIFrameElement);let e=Ot(),t=mn({stats:l=>S("stats",l)}),r=window.parent!==window,o=r&&qt(location.search)?Vt(console,location.origin):null,s="connecting",i=!r&&Ft(location.search),a=null,u=null,f=1,d=new Map,g=new Map,h=null,A=[],y,w=null,M=0,L=new Set,O=new Le,m=new Set,k=new Set,D=r?Gt(l=>S("clip",l),void 0,()=>h?{phase:h.match.phase,paused:!!h.match.paused,flow:Nt(h.state)}:null):null,N=o?gn(l=>S("snapshot",l)):null,q=new ve(l=>{s!=="standalone"&&P("ui.controls",l).catch(()=>{})},l=>S("input",{touch:l})),z,C=new Promise(l=>z=l),mt=Qt(),ht=Zt(()=>h,mt),qn={read:l=>P("data.game.get",{key:l}),op:l=>P("data.game.op",l)},Un=en(()=>U().then(l=>l.player),H),zn=Xt(async()=>(await C,s==="platform"?qn:Un),mt),je=new Set,Ge=[],gt=l=>{let p=`${l.message}|${l.source}|${l.line}`;je.has(p)||je.size>=E.errorReportsPerPage||(je.add(p),s==="platform"?S("error",l):s==="connecting"&&Ge.push(l))};if(window.addEventListener("error",l=>{if(!(l instanceof ErrorEvent))return;let p=String(l.message||"Error");if(gt({message:p,source:jn(l.filename),line:l.lineno||0,column:l.colno||0,stack:l.error instanceof Error?String(l.error.stack??""):""}),o){let b=l.error instanceof Error?`Uncaught ${ke(l.error)}`:/^Uncaught\b/.test(p)?p:`Uncaught ${p}`;o.error(b,{source:jn(l.filename),line:l.lineno||0,column:l.colno||0})}}),window.addEventListener("unhandledrejection",l=>{let p=l.reason;o?.error(`Uncaught (in promise) ${p instanceof Error?ke(p):X(p)}`),gt({message:`Unhandled promise rejection: ${p instanceof Error?p.message:String(p)}`,source:"",line:0,column:0,stack:p instanceof Error?String(p.stack??""):""})}),r){window.addEventListener("message",p=>{if(!(p.source!==window.parent||p.data?.type!==Ct||!p.ports[0])&&s!=="standalone"){if(u?.close(),u=p.ports[0],u.onmessage=b=>Vn(b.data),s=Ut(p.data),i=s==="platform"&&zt(p.data),z(),s==="platform")for(let b of Ge.splice(0))S("error",b);else Ge.length=0;o?.connect(b=>S("console",b)),y||(S("alive",{}),y=setInterval(()=>S("alive",{}),2e3)),s==="platform"&&U().then(b=>{e.apply(b.audio),b.settings&&t.apply(b.settings)}).catch(()=>{})}});let l={type:Dt,version:Nn,...n?{build:n}:{}};window.parent.postMessage(l,"*"),setTimeout(()=>setTimeout(()=>{s==="connecting"&&(s="standalone",z())},250),15e3)}else s="standalone",z();window.addEventListener("keydown",l=>{l.key!=="/"||s!=="platform"||!h||So(l.target)||(l.preventDefault(),l.stopImmediatePropagation(),S("hotkey",{key:"/"}))},!0);let Fn=r?En({key:()=>S("hotkey",{key:"Enter"}),press:()=>S("lobby.press",{}),target:l=>S("lobby.target",l),gesture:()=>e.wake()}):null;if(r&&Mn(),r){try{document.documentElement.style.touchAction="pan-x pan-y"}catch{}for(let l of["gesturestart","gesturechange","gestureend"])document.addEventListener(l,p=>p.preventDefault(),{passive:!1})}if(r){let l=p=>{p.pointerType==="mouse"||s!=="platform"||(window.removeEventListener("pointerup",l,!0),P("ui.tapped",void 0).catch(()=>{}))};window.addEventListener("pointerup",l,!0)}if(r&&dn(()=>s==="platform",()=>S("input.first",{})),r&&On(navigator,()=>s==="platform",l=>S("vibrate",{pattern:l})),r){let l=Element.prototype.requestFullscreen;Element.prototype.requestFullscreen=function(p){return s!=="platform"?l.call(this,p):(P("ui.fullscreen",void 0).catch(()=>{}),Promise.resolve())}}function Vn(l){if(l.kind==="res"){let b=d.get(l.id);if(!b)return;d.delete(l.id),l.ok?b.resolve(l.result):b.reject(new Error(l.error));return}if(l.kind!=="event")return;let p=l.name;if(p==="room")h?._receive(l.data);else if(p==="room.connection")l.data?.connected===!1&&h?._disconnected();else if(p==="room.closed"){let b=h;h=null,b?._closed(l.data.reason)}else if(p==="room.moved"){let b=h;h=yt(l.data),b?._closed("moved");for(let x of L)try{x(h)}catch(_){console.error('[onceworlds] error in rooms "moved" listener',_)}}else if(p==="audio")e.apply(l.data),ge("audio",l.data);else if(p==="settings")t.apply(l.data);else if(p==="pause"||p==="resume")ge(p);else if(p==="controls")q.receive(l.data);else if(p==="input")q.touch=l.data?.touch===!0;else if(p==="reserved")O.receive(l.data)&&ge("reserved",O.areas);else if(p==="lobby")Fn?.receive(l.data);else if(p==="clip")D?.receive(l.data);else if(p==="snapshot")N?.receive(l.data);else if(p==="debug")for(let b of k)try{b(l.data)}catch(x){console.error("[onceworlds] error in the debugger link",x)}else if(p==="player"){let b=l.data;U().then(x=>{b?.id!==x.player.id||typeof b.name!="string"||(x.player.name=b.name,ge("player",x.player))}).catch(()=>{})}}async function P(l,p){await C;let b=u;if(!b)throw new Error("Not connected to Onceworlds");return new Promise((x,_)=>{let v=f++;d.set(v,{resolve:x,reject:_}),b.postMessage({kind:"req",id:v,method:l,params:p})})}function S(l,p){u?.postMessage({kind:"event",name:l,data:p})}function ge(l,...p){for(let b of g.get(l)??[])try{b(...p)}catch(x){console.error(`[onceworlds] error in "${l}" listener`,x)}}function yt(l,p=A){let b={send:(v,$)=>S("room.send",{d:v,to:$}),presence:v=>S("room.presence",{d:v}),state:(v,$,T)=>S("room.state",{k:v,v:$,w:T}),team:(v,$)=>S("room.team",{id:v,team:$}),open:v=>S("room.open",{open:v}),result:v=>S("room.result",{result:v}),kick:v=>S("room.kick",{id:v}),voteKick:v=>S("room.votekick",{id:v}),ready:v=>S("room.ready",{ready:v}),unready:()=>S("room.unready",{}),transfer:v=>S("room.transfer",{id:v}),start:v=>S("room.start",v),end:()=>S("room.end",{}),pause:v=>S("room.pause",{paused:v}),admit:v=>S("room.admit",{ids:v}),private:(v,$,T)=>S("room.private",{k:v,v:$,...T?{id:T}:{}}),dataReset:()=>S("room.datareset",{}),leave:()=>{h===x&&(h=null),P("room.leave",void 0).catch(()=>{})}};A=p;let x=new oe(l,b,()=>Date.now()+M,p);bt(x);let _=un(v=>S("room.idle",{idle:v}));return x.on("reconnect",()=>_.idle&&S("room.idle",{idle:!0})),x.on("close",()=>_.stop()),x}function bt(l){l.on("state",(p,b,x)=>{h===l&&Ke(p)&&ht._changed(p.slice(F.length),b,x)})}function Wn(l,p){let b={};for(let[T,ee]of Object.entries(_e.all()))b[F+T]=ee;let x={room:"solo",you:l.id,host:l.id,players:[{...l,presence:null,team:p.teams?1:0}],state:b,chat:[],teams:p.teams??0,open:!0,now:Date.now(),ranked:!1,kept:!0,owner:l.id},v={...an(()=>$),state(T,ee){Ke(T)&&_e.set(T.slice(F.length),ee)},dataReset(){for(let T of Object.keys(_e.all()))_e.set(T,null),$._receive({t:"state",k:F+T,v:null,from:""})}},$=new oe(x,v,()=>Date.now()+M,Fe(p.settings));return bt($),$}function U(){return w??(w=C.then(()=>s==="platform"?P("session.get",void 0):xo()).then(l=>(M=Number(l.timeOffset)||0,l))),w}U().catch(()=>{});let wt={async get(l){return await C,s==="platform"?await P("save.get",{key:l}):Ie.get(l)},async set(l,p){await C,s==="platform"?await P("save.set",{key:l,value:p}):Ie.set(l,p)},async delete(l){await C,s==="platform"?await P("save.delete",{key:l}):Ie.delete(l)},async list(){return await C,s==="platform"?P("save.list",void 0):Ie.list()}},kt={__onceworlds:!0,_debug:o?{send:l=>{u&&S("debug",l)},on:l=>(k.add(l),()=>k.delete(l))}:null,version:Nn,get mode(){return s},ready:()=>C,player:{get:()=>U().then(l=>l.player),async avatarUrl(l,p="full"){let b=await U().catch(()=>null);if(!b?.platformOrigin)return null;let x=typeof l=="string"&&l?l:b.player.id;return`${b.platformOrigin}/avatar/${encodeURIComponent(x)}${p==="head"?"/head":""}.svg`},async avatar(l){if(await C,s!=="platform")return null;let p=typeof l=="string"&&l?l:(await U()).player.id;return It.test(p)?St(await P("player.avatar",{id:p}).catch(()=>null)):null},async rename(){return await C,s!=="platform"?null:await P("player.rename",void 0).catch(()=>null)?(await U()).player:null}},game:{info:()=>U().then(l=>l.game)},save:wt,data:{player:wt,server:ht,game:zn},rooms:{async join(l={}){await C;let p=l.party===!0&&h!==null&&s==="platform";if(p||(h?.leave(),h=null),s!=="platform"){let{player:_}=await U();return h=Wn(_,l),h}let b=await P("room.join",{name:l.name,maxPlayers:l.maxPlayers,mode:l.mode,teams:l.teams,private:l.private,ranked:l.ranked,party:l.party,minPlayers:l.minPlayers,minPlayersPrivate:l.minPlayersPrivate,settings:l.settings,countdown:l.countdown,lobby:l.lobby,...l.ownCountdown===!0?{ownCountdown:!0}:{}}),x=h;return h=yt(b,Fe(l.settings)),p&&x?._closed("moved"),h},get current(){return h},on(l,p){return l!=="moved"?()=>{}:(L.add(p),()=>L.delete(p))}},ratings:{async get(l){return await C,s==="platform"?P("ratings.get",{mode:String(l)}):null},async top(l,p={}){return await C,s==="platform"?P("ratings.top",{mode:String(l),limit:p.limit}):[]}},ui:{setMenuPosition(l){P("ui.menuPosition",{position:l}).catch(()=>{})},get reserved(){return O.areas},setLook(l){let{look:p,notes:b}=In(l);for(let x of b)m.has(x)||(m.add(x),console.warn(`[onceworlds] ui.setLook: ${x}`));s!=="standalone"&&P("ui.look",p).catch(()=>{})},requestFullscreen(){s==="platform"?P("ui.fullscreen",void 0).catch(()=>{}):document.documentElement.requestFullscreen?.().catch(()=>{})},showInvite(){s==="platform"&&P("ui.invite",void 0).catch(()=>{})},setOrientation(l){l!=="any"&&l!=="landscape"&&l!=="portrait"||s!=="standalone"&&P("ui.orientation",{orientation:l}).catch(()=>{})}},controls:{set(l){q.set(l)},get stick(){return q.stick},pressed(l){return q.pressed(String(l))},presses(l){return q.presses(String(l))},get touch(){return q.touch}},now:()=>Date.now()+M,leaderboards:{async submit(l,p,b={}){if(await C,s!=="platform")return Be.submit(String(l),Number(p),b.lowerIsBetter===!0);try{return await P("leaderboards.submit",{board:String(l),score:Number(p),lowerIsBetter:b.lowerIsBetter===!0})}catch(x){return console.warn(`[onceworlds] score for "${l}" was not saved: ${x instanceof Error?x.message:x}`),null}},async top(l,p={}){return await C,s!=="platform"?Be.top(String(l)):P("leaderboards.top",{board:String(l),limit:p.limit,lowerIsBetter:p.lowerIsBetter===!0})}},settings:t.settings,audio:{get volume(){return e.volume},get muted(){return e.muted}},badges:{async award(l){if(await C,s!=="platform")return Ne.add(String(l));try{return(await P("badges.award",{key:String(l)})).awarded}catch(p){return console.warn(`[onceworlds] badge "${l}" was not awarded: ${p instanceof Error?p.message:p}`),!1}},async list(){return await C,s==="platform"?P("badges.list",void 0):Ne.list()},async has(l){return(await kt.badges.list()).some(p=>p.key===l&&p.earned)}},demo:{get active(){return i},end(){return a??(a=(async()=>(await C,i?s!=="platform"?(console.info("[onceworlds] demo.end(): the end of the demo. On Onceworlds the platform shows its end-of-demo card here."),!0):P("demo.end",void 0).then(l=>l!==!1,()=>!0):!1))().finally(()=>a=null)),a}},env:ko(),async fetch(l,p={}){if(await C,s!=="platform")throw new Error("onceworlds.fetch only works on Onceworlds, where your secrets are stored. Run the game with `onceworlds dev`.");let b=l instanceof Request?l:null,x=new URL(b?b.url:String(l),location.href).href,_=(p.method??b?.method??"GET").toUpperCase(),v={};new Headers(p.headers??b?.headers).forEach((Jn,Yn)=>{v[Yn]=Jn});let $=p.body??null;$===null&&b&&_!=="GET"&&_!=="HEAD"&&($=await b.arrayBuffer()),$ instanceof URLSearchParams&&(v["content-type"]??(v["content-type"]="application/x-www-form-urlencoded;charset=UTF-8"),$=$.toString());let T=await P("net.fetch",{url:x,method:_,headers:v,body:await vo($)}),ee=[101,204,205,304].includes(T.status);return new Response(ee?null:T.body,{status:T.status,statusText:T.statusText,headers:T.headers})},on(l,p){let b=g.get(l);return b||g.set(l,b=new Set),b.add(p),()=>b.delete(p)}};return kt}function ko(){let n=globalThis.__ONCEWORLDS_ENV__,e={};if(n&&typeof n=="object")for(let[t,r]of Object.entries(n))typeof r=="string"&&(e[t]=r);return Object.freeze(e)}async function vo(n){if(n==null)return null;if(typeof n=="string"||n instanceof ArrayBuffer)return n;if(ArrayBuffer.isView(n))return new Uint8Array(n.buffer,n.byteOffset,n.byteLength).slice().buffer;if(n instanceof Blob)return n.arrayBuffer();throw new TypeError("onceworlds.fetch bodies must be a string (e.g. JSON.stringify(...)), bytes or a Blob.")}function jn(n){if(!n)return"";try{let e=new URL(n);return e.origin===location.origin?e.pathname.slice(1)||"index.html":e.href}catch{return n}}function So(n){return n instanceof HTMLElement?n.isContentEditable||["INPUT","TEXTAREA","SELECT"].includes(n.tagName):!1}function xo(){let n=H.get("onceworlds:player-id");return n||(n=`local-${Math.random().toString(36).slice(2,10)}`,H.set("onceworlds:player-id",n)),{player:{id:n,name:"Player"},game:{slug:location.hostname.split(".")[0]||"local",name:document.title||"Game",owner:"local"},audio:{volume:1,muted:!1},timeOffset:0}}var Gn="onceworlds:scores",Be={all(){try{let n=JSON.parse(H.get(Gn)??"{}");return n&&typeof n=="object"?n:{}}catch{return{}}},submit(n,e,t){let r=Be.all(),o=r[n],s=o===void 0?e:t?Math.min(o,e):Math.max(o,e);return r[n]=s,H.set(Gn,JSON.stringify(r)),{best:s,rank:1}},top(n){let e=Be.all()[n];if(e===void 0)return{entries:[],me:null};let t={rank:1,player:{id:"local",name:"You"},score:e};return{entries:[t],me:t}}},he="onceworlds:save:",Kn="onceworlds:badges",Ne={earned(){try{let n=JSON.parse(H.get(Kn)??"{}");return n&&typeof n=="object"?n:{}}catch{return{}}},add(n){let e=Ne.earned();return e[n]?!1:(e[n]=Date.now(),H.set(Kn,JSON.stringify(e)),!0)},list(){return Object.entries(Ne.earned()).map(([n,e])=>({key:n,name:n,description:"",icon:null,earned:!0,earnedAt:e}))}},Ie={get(n){let e=H.get(he+n);if(e===null)return null;try{return JSON.parse(e)}catch{return null}},set(n,e){H.set(he+n,JSON.stringify(e??null))},delete(n){H.remove(he+n)},list(){let n=[];try{for(let e=0;e<localStorage.length;e++){let t=localStorage.key(e);t?.startsWith(he)&&n.push(t.slice(he.length))}}catch{}return n.sort()}},Oe="onceworlds:server:",_e={all(){let n={};try{for(let e=0;e<localStorage.length;e++){let t=localStorage.key(e);if(t?.startsWith(Oe))try{n[t.slice(Oe.length)]=JSON.parse(localStorage.getItem(t)??"null")}catch{}}}catch{}return n},set(n,e){e==null?H.remove(Oe+n):H.set(Oe+n,JSON.stringify(e))}},H={get(n){try{return localStorage.getItem(n)}catch{return null}},set(n,e){try{localStorage.setItem(n,e)}catch{}},remove(n){try{localStorage.removeItem(n)}catch{}}};Hn();})();
|