UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

1 lines 12.2 kB
(()=>{"use strict";var e={d:(t,r)=>{for(var i in r)e.o(r,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:r[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{TinyDayNightCycle:()=>r});const r=class{#e=!0;#t=86400;#r=3600;#i=60;#s=new Map;#n=[];#o;#h;#a={main:null};#u=0;#c=0;#y=0;#m="";#d=1;#f=1;#w=1;#S=!1;#g=[31,31,31,31,31,31,31,31,31,31,31,31];#p={default:{},day:{},night:{},hours:{},seasons:{}};#l={min:60,max:180};#b=0;get daySize(){return this.#t}get autoSizeAdjuste(){return this.#e}set autoSizeAdjuste(e){if("boolean"!=typeof e)throw new TypeError("autoSizeAdjuste must be a boolean.");this.#e=e}set daySize(e){if("number"!=typeof e||!Number.isFinite(e)||e<=0)throw new Error("daySize must be a positive finite number.");const t=this.#r/this.#t,r=this.#i/this.#t;this.#t=e,this.#e&&(this.#r=e*t,this.#i=e*r)}get hourSize(){return this.#r}set hourSize(e){if("number"!=typeof e||!Number.isFinite(e)||e<=0)throw new Error("hourSize must be a positive finite number.");const t=this.#t/this.#r,r=this.#i/this.#r;this.#r=e,this.#e&&(this.#t=e*t,this.#i=e*r)}get minuteSize(){return this.#i}set minuteSize(e){if("number"!=typeof e||!Number.isFinite(e)||e<=0)throw new Error("minuteSize must be a positive finite number.");const t=this.#r/this.#i,r=this.#t/this.#i;this.#i=e,this.#e&&(this.#r=e*t,this.#t=e*r)}get isDestroyed(){return this.#S}get currentSeconds(){return this.#u}set currentSeconds(e){if(this._checkDestroyed(),"number"!=typeof e||!Number.isFinite(e))throw new TypeError("currentSeconds must be a finite number, received "+typeof e);if(e<0||e>=this.#t)throw new RangeError(`currentSeconds must be between 0 and 86399, received ${e}`);this.#u=Math.floor(e),this.#c=Math.floor(e/this.#i),this.#y=Math.floor(e/this.#r)}get currentMinutes(){return this.#c}set currentMinutes(e){if(this._checkDestroyed(),"number"!=typeof e||!Number.isFinite(e))throw new TypeError("currentMinutes must be a finite number, received "+typeof e);if(e<0||e>=1440)throw new RangeError(`currentMinutes must be between 0 and 1439, received ${e}`);this.#c=Math.floor(e),this.#y=Math.floor(e/this.#i),this.#u=Math.floor(e*this.#i)}get currentHours(){return this.#y}set currentHours(e){if(this._checkDestroyed(),"number"!=typeof e||!Number.isFinite(e))throw new TypeError("currentHours must be a finite number, received "+typeof e);if(e<0||e>=24)throw new RangeError(`currentHours must be between 0 and less than 24, received ${e}`);this.#y=Math.floor(e),this.#c=Math.floor(e*this.#i),this.#u=Math.floor(e*this.#r)}get moons(){return this.#n.map(e=>this.getMoon(e))}get seasons(){return Array.from(this.#s.keys())}get dayStart(){return this.#o}get nightStart(){return this.#h}get weather(){return{...this.#a}}get currentSeason(){return this.#m}get currentDay(){return this.#d}get currentMonth(){return this.#f}get currentYear(){return this.#w}get monthDays(){return[...this.#g]}get weatherDuration(){return{...this.#l}}get weatherTimeLeft(){return this.#b}set dayStart(e){if(this._checkDestroyed(),"number"!=typeof e||e<1)throw new TypeError("dayStart must be a positive non-zero number, received "+typeof e);this.#o=e}set nightStart(e){if(this._checkDestroyed(),"number"!=typeof e||e<1)throw new TypeError("nightStart must be a positive non-zero number, received "+typeof e);this.#h=e}set weather(e){if(this._checkDestroyed(),"object"!=typeof e||null===e||Array.isArray(e)||!Object.values(e).every(e=>"string"==typeof e))throw new TypeError("weather must be a object with strings, received "+typeof e);this.#a=e}set currentSeason(e){if(this._checkDestroyed(),"string"!=typeof e||!this.#s.has(e))throw new TypeError(`currentSeason must be one of ${Array.from(this.#s).map(e=>e[0]).join(", ")}, received ${e}`);this.#m=e}set currentDay(e){if(this._checkDestroyed(),"number"!=typeof e||e<1||e>this.#g[this.#f-1])throw new TypeError("currentDay must be a valid day number, received "+typeof e);this.#d=e}set currentMonth(e){if(this._checkDestroyed(),"number"!=typeof e||"number"!=typeof this.#g[e-1])throw new TypeError("currentMonth must be a valid month number, received "+typeof e);this.#f=e}set currentYear(e){if(this._checkDestroyed(),"number"!=typeof e||e<1)throw new TypeError("currentYear must be a positive number non-zero, received "+typeof e);this.#w=e}set monthDays(e){if(this._checkDestroyed(),!Array.isArray(e))throw new TypeError("monthDays must be a array");if(!e.every(e=>"number"==typeof e))throw new TypeError("monthDays must have number values");this.#g=[...e]}set weatherDuration(e){if(this._checkDestroyed(),"object"!=typeof e||null===e)throw new TypeError("weatherDuration must be a non-null object");if("number"!=typeof e.min||"number"!=typeof e.max)throw new TypeError("weatherDuration.min and weatherDuration.max must be numbers");this.#l={...e}}set weatherTimeLeft(e){if(this._checkDestroyed(),"number"!=typeof e)throw new TypeError("weatherTimeLeft must be a number, received "+typeof e);this.#b=e}get weatherConfig(){const e={},t={};for(const t in this.#p.hours)e[t]={...this.#p.hours[t]};for(const e in this.#p.seasons)t[e]={...this.#p.seasons[e]};return{default:{...this.#p.default},day:{...this.#p.day},night:{...this.#p.night},hours:e,seasons:t}}set weatherConfig(e){if(this._checkDestroyed(),"object"!=typeof e||null===e)throw new TypeError(`weatherConfig must be a non-null object, received ${e}`);const t=["default","day","night","hours","seasons"];for(const r of t)if(!(r in e))throw new TypeError(`weatherConfig is missing required property "${r}".`);const r={},i={},s=(e,t)=>{if("object"!=typeof e||null===e)throw new TypeError(`${t} must be an object, received ${e}`);for(const r in e){const i=typeof e[r];if("number"!==i&&"function"!==i)throw new TypeError(`${t}["${r}"] must be a number or function, received ${i}`)}};for(const t in e.hours)s(e.hours[t],`hours["${t}"]`),r[t]={...e.hours[t]};for(const t in e.seasons)s(e.seasons[t],`seasons["${t}"]`),i[t]={...e.seasons[t]};const n=(e,t)=>(s(e,t),{...e});this.#p={default:n(e.default,"default"),day:n(e.day,"day"),night:n(e.night,"night"),hours:r,seasons:i}}constructor(e=6,t=18){this.#o=e,this.#h=t}addSeason(e,t){if(this._checkDestroyed(),"string"!=typeof e)throw new TypeError("Season name must be a string, received "+typeof e);if(!Array.isArray(t)||!t.every(e=>"number"==typeof e))throw new TypeError("Season values must be an array of numbers");this.#s.set(e,t)}removeSeason(e){if(this._checkDestroyed(),"string"!=typeof e)throw new TypeError("Season name must be a string, received "+typeof e);this.#s.delete(e),this.#m===e&&(this.#m="")}hasSeason(e){if("string"!=typeof e)throw new TypeError("Season name must be a string, received "+typeof e);return this.#s.has(e)}getSeason(e){if("string"!=typeof e)throw new TypeError("Season name must be a string, received "+typeof e);const t=this.#s.get(e);if(!t)throw new Error(`Season "${e}" not found`);return[...t]}setTime({hour:e=0,minute:t=0,second:r=0}){this._checkDestroyed(),this.currentSeconds=(e*this.#r+t*this.#i+r)%this.#t}addTime({hours:e=0,minutes:t=0,seconds:r=0}){this._checkDestroyed();let i=this.currentSeconds+e*this.#r+t*this.#i+r;for(;i>=this.#t;)i-=this.#t,this.nextDay();for(;i<0;)i+=this.#t,this.prevDay();this.currentSeconds=i,this.updateWeatherTimer((e*this.#r+t*this.#i+r)/this.#i)}getTime({withSeconds:e=!1,hourSize:t=this.#r,minuteSize:r=this.#i}={}){const i=Math.floor(this.currentSeconds/t),s=Math.floor(this.currentSeconds%t/r),n=this.currentSeconds%r;return{hour:i,minute:s,second:n,formatted:`${i.toString().padStart(2,"0")}:${s.toString().padStart(2,"0")}`+(e?`:${n.toString().padStart(2,"0")}`:"")}}isDay(){return this.#o<this.#h?this.#c>=this.#o*this.#i&&this.#c<this.#h*this.#i:this.#c>=this.#o*this.#i||this.#c<this.#h*this.#i}minutesUntilDay(){return this.timeUntil(this.#o,"minutes")}secondsUntilDay(){return this.timeUntil(this.#o,"seconds")}hoursUntilDay(){return this.timeUntil(this.#o,"hours")}minutesUntilNight(){return this.timeUntil(this.#h,"minutes")}secondsUntilNight(){return this.timeUntil(this.#h,"seconds")}hoursUntilNight(){return this.timeUntil(this.#h,"hours")}timeUntil(e,t){this._checkDestroyed();let r=e*this.#r-this.#u;switch(r<=0&&(r+=this.#t),t){case"minutes":return r/this.#i;case"hours":return r/this.#r;default:return r}}setTo(e){this._checkDestroyed(),"day"===e?this.setTime({hour:this.#o}):"night"===e&&this.setTime({hour:this.#h})}nextDay(e=1){this._checkDestroyed();for(let t=0;t<e;t++){this.#d++;const e=this.#g[this.#f-1];if(Number.isNaN(e)||!Number.isFinite(e))throw new Error("Invalid month day count: monthDays must be a finite number.");this.#d>e&&(this.#d=1,this.#f++,this.#f>this.#g.length&&(this.#f=1,this.#w++)),this.updateSeason(),this.advanceMoons(1)}}prevDay(e=1){this._checkDestroyed();for(let t=0;t<e;t++)this.#d--,this.#d<1&&(this.#f--,this.#f<1&&(this.#f=this.#g.length,this.#w--),this.#d=this.#g[this.#f-1]||30),this.updateSeason(),this.rewindMoons(1)}updateSeason(){this._checkDestroyed(),this.#s.forEach((e,t)=>{e.includes(this.#f)&&(this.#m=t)})}setWeatherConfig(e){this._checkDestroyed(),this.#p={...this.#p,...e}}setWeatherDuration(e,t){this._checkDestroyed(),this.#l.min=e,this.#l.max=t}updateWeatherTimer(e){this._checkDestroyed(),this.#b-=e,this.#b<=0&&this.chooseNewWeather()}forceWeather({where:e="main",type:t,duration:r=null}){this._checkDestroyed();const i=this.#a;i[e]=t,this.weather=i,this.#b=r??this._randomInRange(this.#l.min,this.#l.max)}chooseNewWeather({customWeather:e,where:t="main"}={}){this._checkDestroyed();let r={};const i=e=>{for(const[t,i]of Object.entries(e)){let e=i;"function"==typeof e&&(e=e({hour:Math.floor(this.#c/this.#i),minute:this.#c%this.#i,currentMinutes:this.#c,isDay:this.isDay(),season:this.#m,weather:this.#a})),"number"!=typeof e||Number.isNaN(e)||(r[t]=(r[t]||0)+e)}};i(this.#p.default||{});for(const e in this.#p.hours){const[t,r]=e.split("-").map(e=>{const[t,r]=e.split(":").map(Number);return t*this.#i+(r||0)}),s=this.#c;(t<=r?s>=t&&s<=r:s>=t||s<=r)&&i(this.#p.hours[e])}this.isDay()&&this.#p.day?i(this.#p.day):!this.isDay()&&this.#p.night&&i(this.#p.night),this.#p.seasons?.[this.#m]&&i(this.#p.seasons[this.#m]),e&&"object"==typeof e&&i(e);const s=Object.entries(r).filter(([,e])=>e>0);if(!s.length)return this.#a[t]=null,null;const n=s.reduce((e,[,t])=>e+t,0);let o=Math.random()*n;for(const[e,r]of s){if(o<r)return this.#a[t]=e,this.#b=this._randomInRange(this.#l.min,this.#l.max),e;o-=r}return this.#a[t]=null,null}_randomInRange(e,t){return Math.floor(Math.random()*(t-e+1))+e}addMoon(e,t,r,i=0){this._checkDestroyed();const s=Math.max(1,t);return this.#n.push({name:e,cycleLength:s,currentPhase:(i%s+s)%s,phaseNames:r}),this.#n.length-1}removeMoon(e){this._checkDestroyed(),this.#n=this.#n.filter(t=>t.name!==e)}advanceMoons(e=1){for(const t in this.#n)this.advanceMoon(parseInt(t),e)}rewindMoons(e=1){for(const t in this.#n)this.rewindMoon(parseInt(t),e)}advanceMoon(e,t=1){this._checkDestroyed();const r=this.#n[e];if(!r)throw new RangeError(`No moon found at index ${e}`);r.currentPhase=(r.currentPhase+t)%r.cycleLength}rewindMoon(e,t=1){this._checkDestroyed();const r=this.#n[e];if(!r)throw new RangeError(`No moon found at index ${e}`);r.currentPhase=(r.currentPhase-t+r.cycleLength)%r.cycleLength}moonExists(e){return!!this.#n[e]}getMoon(e){let t;if("number"==typeof e){if(t=this.#n[e],!t)throw new RangeError(`No moon found at index ${e}`)}else{if(!e||"object"!=typeof e||"string"!=typeof e.name||"number"!=typeof e.cycleLength||"number"!=typeof e.currentPhase)throw new TypeError("Invalid moon reference. Expected a number index or a MoonRaw object, received "+typeof e);t=e}return{name:t.name,phaseIndex:t.currentPhase,phaseName:t.phaseNames?t.phaseNames[t.currentPhase]??String(t.currentPhase):String(t.currentPhase),cycleLength:t.cycleLength}}_checkDestroyed(){if(this.#S)throw new Error("This instance has been destroyed and can no longer be used.")}destroy(){this.#S||(this.#s.clear(),this.#n.length=0,this.#o=0,this.#h=0,this.#u=0,this.#c=0,this.#y=0,this.#d=1,this.#f=1,this.#w=1,this.#b=0,this.#m="",this.#a={main:null},this.#g=[],this.#p.default={},this.#p.day={},this.#p.night={},this.#p.hours={},this.#p.seasons={},this.#l={min:0,max:0},this.#S=!0)}};window.TinyDayNightCycle=t.TinyDayNightCycle})();