dota2-team-randomizer
Version:
A React component for randomizing Dota 2 teams with animations and sound effects
3 lines (2 loc) • 9.03 kB
JavaScript
import e,{useState as r,useRef as n,useEffect as t}from"react";var a=function(){return a=Object.assign||function(e){for(var r,n=1,t=arguments.length;n<t;n++)for(var a in r=arguments[n])Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a]);return e},a.apply(this,arguments)};function o(e,r,n,t){return new(n||(n=Promise))((function(a,o){function i(e){try{u(t.next(e))}catch(e){o(e)}}function c(e){try{u(t.throw(e))}catch(e){o(e)}}function u(e){var r;e.done?a(e.value):(r=e.value,r instanceof n?r:new n((function(e){e(r)}))).then(i,c)}u((t=t.apply(e,r||[])).next())}))}function i(e,r){var n,t,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(i=0)),i;)try{if(n=1,t&&(a=2&c[0]?t.return:c[0]?t.throw||((a=t.return)&&a.call(t),0):t.next)&&!(a=a.call(t,c[1])).done)return a;switch(t=0,a&&(c=[2&c[0],a.value]),c[0]){case 0:case 1:a=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,t=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!a||c[1]>a[0]&&c[1]<a[3])){i.label=c[1];break}if(6===c[0]&&i.label<a[1]){i.label=a[1],a=c;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(c);break}a[2]&&i.ops.pop(),i.trys.pop();continue}c=r.call(e,i)}catch(e){c=[6,e],t=0}finally{n=a=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}function c(e,r,n){if(n||2===arguments.length)for(var t,a=0,o=r.length;a<o;a++)!t&&a in r||(t||(t=Array.prototype.slice.call(r,0,a)),t[a]=r[a]);return e.concat(t||Array.prototype.slice.call(r))}!function(e,r){void 0===r&&(r={});var n=r.insertAt;if(e&&"undefined"!=typeof document){var t=document.head||document.getElementsByTagName("head")[0],a=document.createElement("style");a.type="text/css","top"===n&&t.firstChild?t.insertBefore(a,t.firstChild):t.appendChild(a),a.styleSheet?a.styleSheet.cssText=e:a.appendChild(document.createTextNode(e))}}(".dota2-team-randomizer {\r\n text-align: center;\r\n padding: 20px;\r\n max-width: 1200px;\r\n margin: 0 auto;\r\n background-color: #000000;\r\n min-height: 100vh;\r\n color: #ffffff;\r\n box-sizing: border-box;\r\n}\r\n\r\n/* Copy all your existing CSS styles here, but prefix them with .dota2-team-randomizer */\r\n.dota2-team-randomizer h1 {\r\n color: #ffffff;\r\n margin-bottom: 30px;\r\n font-size: clamp(1.8rem, 4vw, 2.5rem);\r\n text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);\r\n}\r\n\r\n/* ... rest of your CSS styles ... */ ");var u="dota2_heroes_cache",l=function(){var l=r([]),s=l[0],m=l[1],f=r([]),d=f[0],p=f[1],h=r(!0),v=h[0],y=h[1],g=r(null),b=g[0],w=g[1],x=r(!1),E=x[0],T=x[1],N=r([]),S=N[0],k=N[1],C=r([]),_=C[0],z=C[1],R=n(null),M=n(new Audio("https://cdn.pixabay.com/audio/2022/03/15/audio_bd91fe76ba.mp3")),A=n(new Audio("https://assets.mixkit.co/active_storage/sfx/2018/2018-preview.mp3")),I=n(new Audio("https://assets.mixkit.co/active_storage/sfx/2016/2016-preview.mp3")),O=function(e){return void 0===e&&(e=0),o(void 0,void 0,void 0,(function(){var r,n,t,a,o;return i(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,6]),[4,fetch("https://api.opendota.com/api/heroes")];case 1:if(!(r=i.sent()).ok)throw new Error("HTTP error! status: ".concat(r.status));return[4,r.json()];case 2:return n=i.sent(),t=n.map((function(e){var r="Support";e.roles.includes("Carry")?r="Carry":e.roles.includes("Nuker")||e.roles.includes("Escape")?r="Mid":e.roles.includes("Durable")||e.roles.includes("Initiator")||e.roles.includes("Jungler")?r="Offlane":e.roles.includes("Support")&&(r="Support");var n=e.name.replace("npc_dota_hero_","");return"dawnbreaker"===n&&(n="dawn_breaker"),"marci"===n&&(n="marci"),"primal_beast"===n&&(n="primal_beast"),{name:e.localized_name,roles:e.roles,primaryRole:r,image:"https://cdn.dota2.com/apps/dota2/images/heroes/".concat(n,"_full.png")}})),a={heroes:t,timestamp:Date.now()},localStorage.setItem(u,JSON.stringify(a)),[2,t];case 3:return o=i.sent(),e<3?[4,(c=1e3*(e+1),new Promise((function(e){return setTimeout(e,c)})))]:[3,5];case 4:return i.sent(),[2,O(e+1)];case 5:throw o;case 6:return[2]}var c}))}))};t((function(){o(void 0,void 0,void 0,(function(){var e,r,n;return i(this,(function(t){switch(t.label){case 0:if(t.trys.push([0,7,,8]),e=function(){try{var e=localStorage.getItem(u);if(e){var r=JSON.parse(e),n=r.heroes,t=r.timestamp;if(!(Date.now()-t>864e5))return n}return null}catch(e){return null}}(),!e)return[3,5];m(e),y(!1),t.label=1;case 1:return t.trys.push([1,3,,4]),[4,O()];case 2:return r=t.sent(),m(r),[3,4];case 3:return t.sent(),[3,4];case 4:return[2];case 5:return[4,O()];case 6:return n=t.sent(),m(n),y(!1),[3,8];case 7:return t.sent(),w("Failed to fetch heroes data. Please check your internet connection and try again."),y(!1),[3,8];case 8:return[2]}}))}))}),[]),t((function(){return E?(M.current.loop=!0,M.current.volume=.3,M.current.play().catch((function(){}))):(M.current.pause(),M.current.currentTime=0),function(){M.current.pause(),M.current.currentTime=0,A.current.pause(),A.current.currentTime=0,I.current.pause(),I.current.currentTime=0}}),[E]),t((function(){S.length>0&&(A.current.currentTime=0,A.current.volume=.3,A.current.play().catch((function(){})))}),[S]),t((function(){5===S.length&&setTimeout((function(){I.current.currentTime=0,I.current.volume=.5,I.current.play().catch((function(){})),setTimeout((function(){I.current.pause(),I.current.currentTime=0}),1e4)}),1e3)}),[S]);t((function(){return function(){R.current&&clearInterval(R.current)}}),[]);return v?e.createElement("div",{className:"dota2-team-randomizer"},e.createElement("div",{className:"loading"},e.createElement("div",{className:"loading-spinner"}),e.createElement("div",null,"Loading heroes data..."))):b?e.createElement("div",{className:"dota2-team-randomizer"},e.createElement("div",{className:"error"},e.createElement("div",{className:"error-icon"},"⚠️"),e.createElement("div",null,b),e.createElement("button",{onClick:function(){return window.location.reload()},className:"retry-button"},"Retry"))):e.createElement("div",{className:"dota2-team-randomizer"},e.createElement("h1",null,"Dota 2 Team Randomizer"),e.createElement("button",{onClick:function(){if(0!==s.length){T(!0),k([]);var e=c([],s,!0).sort((function(){return.5-Math.random()})).slice(0,5),r=["Carry","Mid","Offlane","Support","Hard Support"],n=e.map((function(e,n){return a(a({},e),{primaryRole:r[n]})})).sort((function(){return.5-Math.random()}));p(n);var t=Array(5).fill(null).map((function(){var e=s[Math.floor(Math.random()*s.length)];return a(a({},e),{primaryRole:"Spinning..."})}));z(t),R.current&&clearInterval(R.current);var o=0;R.current=setInterval((function(){if(o>=5)return clearInterval(R.current),void T(!1);o>=5||z((function(e){var r,t=c([],e,!0),i=s[Math.floor(Math.random()*s.length)];return i&&(t[o]=a(a({},i),{primaryRole:(null===(r=n[o])||void 0===r?void 0:r.primaryRole)||"Spinning..."})),t}))}),50),setTimeout((function(){k([0]),z((function(e){var r=c([],e,!0);return n[0]&&(r[0]=n[0]),r})),o=1}),1e3),setTimeout((function(){k((function(e){return c(c([],e,!0),[1],!1)})),z((function(e){var r=c([],e,!0);return n[1]&&(r[1]=n[1]),r})),o=2}),2e3),setTimeout((function(){k((function(e){return c(c([],e,!0),[2],!1)})),z((function(e){var r=c([],e,!0);return n[2]&&(r[2]=n[2]),r})),o=3}),3e3),setTimeout((function(){k((function(e){return c(c([],e,!0),[3],!1)})),z((function(e){var r=c([],e,!0);return n[3]&&(r[3]=n[3]),r})),o=4}),4e3),setTimeout((function(){k((function(e){return c(c([],e,!0),[4],!1)})),z((function(e){var r=c([],e,!0);return n[4]&&(r[4]=n[4]),r})),o=5}),5e3)}},className:"randomize-button",disabled:E},E?"Spinning...":"Randomize Team"),e.createElement("div",{className:"heroes-container"},(E?_:d).slice(0,5).map((function(r,n){return e.createElement("div",{key:n,className:"hero-card ".concat(E&&!S.includes(n)?"spinning":"")},e.createElement("div",{className:"position-number"},"Pick ",n+1),e.createElement("div",{className:"hero-image-container"},e.createElement("img",{src:(null==r?void 0:r.image)||"",alt:(null==r?void 0:r.name)||"Hero",className:"hero-image ".concat(E&&!S.includes(n)?"spinning":"")})),e.createElement("h3",null,(null==r?void 0:r.name)||"Loading..."),e.createElement("div",{className:"hero-primary-role"},e.createElement("span",{className:"role-badge primary"},(null==r?void 0:r.primaryRole)||"Loading...")),e.createElement("button",{onClick:function(){return function(e){if(0!==s.length){var r=d[e].primaryRole,n=s.filter((function(e){return e.primaryRole===r&&!d.some((function(r){return r.name===e.name}))}));if(n.length>0){var t=n[Math.floor(Math.random()*n.length)],a=c([],d,!0);a[e]=t,p(a)}}}(n)},className:"replace-button",disabled:E},"Replace Hero"))}))))};export{l as default};
//# sourceMappingURL=index.esm.js.map