UNPKG

auto-champion-select

Version:
11 lines (10 loc) 10.2 kB
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".dropdown-champions-default{position:inherit;width:-webkit-fill-available}.auto-select-checkboxes-div{display:flex;border-top:thin solid #1e282d}.auto-select-checkbox{margin:auto}.auto-select-checkbox:last-child{margin-right:17px}")),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})(); /** * @author balaclava * @name auto-champion-select * @link https://github.com/controlado/auto-champion-select#readme * @description Pick or ban automatically! 🐧 * @version 1.3.4 * @license GPL-3.0-or-later */ import{sleep as u,request as a,linkEndpoint as P}from"https://cdn.skypack.dev/balaclava-utils@latest";const T={enabled:!1},E={enabled:!1,force:!1,champions:[21,21]},$={enabled:!1,force:!1,champions:[21,21]},p={controladoAutoAccept:T,controladoPick:E,controladoBan:$};class I{constructor(){this.session=null,this.actions=null,this.localPlayerCellId=null,this.teamIntents=null,this.allPicks=null,this.allBans=null,this.mounted=!1,this.watch()}mount(){this.mounted=!0}unmount(){this.mounted=!1}async watch(){for(;;)this.mounted&&(await this.updateProperties(),await this.task()),await u(300)}async updateProperties(){const e=await a("GET","/lol-champ-select/v1/session");this.session=await e.json(),this.actions=this.session.actions,this.localPlayerCellId=this.session.localPlayerCellId,this.allPicks=[...this.session.myTeam,...this.session.theirTeam],this.allBans=[...this.session.bans.myTeamBans,...this.session.bans.theirTeamBans],this.teamIntents=this.session.myTeam.map(t=>t.championPickIntent)}async task(){const e=DataStore.get("controladoPick")||p.controladoPick,t=DataStore.get("controladoBan")||p.controladoBan;if(!e.enabled&&!t.enabled)return;const o=this.getLocalPlayerSubActions();if(o.length===0){console.debug("auto-champion-select: No local player sub actions found, skipping..."),this.unmount();return}for(const s of o){const c=s.type==="pick"?e:t;if(c.enabled)for(const i of c.champions){if(this.allBans.some(r=>r==i)){console.debug(`auto-champion-select: Banning ${i} but it's already banned, skipping...`);continue}if(s.type==="ban"&&this.teamIntents.some(r=>r==i))if(c.force===!0)console.debug(`auto-champion-select: Banning ${i} but it's already picked, forcing...`);else{console.debug(`auto-champion-select: Banning ${i} but it's already picked, skipping...`);continue}if(s.type==="pick"&&this.allPicks.some(r=>r.championId==i))if(c.force===!0)console.debug(`auto-champion-select: Picking ${i} but it's already picked, forcing...`);else{console.debug(`auto-champion-select: Picking ${i} but it's already picked, skipping...`);continue}if(console.debug(`auto-champion-select: Trying to ${s.type} ${i}...`),(await this.selectChampion(s.id,i)).ok)break;return}}}getLocalPlayerSubActions(){return this.actions.flat().filter(e=>e.actorCellId===this.localPlayerCellId&&e.completed===!1).sort((e,t)=>{const o=e.type==="pick"?0:1,s=t.type==="pick"?0:1;return o-s})}selectChampion(e,t){const o=`/lol-champ-select/v1/session/actions/${e}`;return a("PATCH",o,{body:{championId:t,completed:!0}})}}class m{constructor(e,t,o,s){this.element=document.createElement("lol-uikit-framed-dropdown"),this.element.classList.add("dropdown-champions-default"),this.text=e,this.config=null,this.configKey=t,this.configIndex=o,this.championsFunction=s,this.champions=null}async setup(){this.champions=await this.championsFunction(),this.config=DataStore.get(this.configKey)||p[this.configKey],this.champions.some(t=>this.config.champions[this.configIndex]===t.id)||(this.config.champions[this.configIndex]=this.champions[0].id,DataStore.set(this.configKey,this.config));const e=[];for(const t of this.champions){if(e.includes(t.name))continue;e.push(t.name);const o=this.getNewOption(t);this.element.appendChild(o)}if(!this.element.shadowRoot.querySelector("#controlado-placeholder")){const t=this.element.shadowRoot.querySelector(".ui-dropdown-current");t.style="display: flex; justify-content: space-between;";const o=this.getNewPlaceholder();t.appendChild(o)}}getNewOption(e){const t=document.createElement("lol-uikit-dropdown-option");return t.setAttribute("slot","lol-uikit-dropdown-option"),t.addEventListener("click",()=>{this.config.champions[this.configIndex]=e.id,DataStore.set(this.configKey,this.config)}),this.config.champions[this.configIndex]===e.id&&t.setAttribute("selected","true"),t.innerText=e.name,t}getNewPlaceholder(){const e=document.createElement("div");return e.classList.add("ui-dropdown-current-content"),e.style="width: unset; overflow: visible;",e.id="controlado-placeholder",e.innerText=this.text,e}refresh(){this.element.innerHTML="",this.setup()}}class b{constructor(e,t){this.element=document.createElement("lol-uikit-radio-input-option"),this.element.classList.add("lol-settings-voice-input-mode-option","auto-select-checkbox"),this.element.innerText=e,this.config=null,this.configKey=t}setup(){this.config=DataStore.get(this.configKey)||p[this.configKey],this.config.enabled&&this.element.setAttribute("selected","true"),this.element.addEventListener("click",()=>this.toggle())}toggle(){return console.debug("auto-champion-select: Toggling",this.configKey),this.config.enabled=!this.config.enabled,DataStore.set(this.configKey,this.config),this.element.toggleAttribute("selected"),this.config.enabled}}class R{constructor(e,...t){this.element=document.createElement("lol-social-roster-group"),this.element.addEventListener("post-render",()=>this.onPostRender()),this.element.addEventListener("click",()=>this.onClick()),this.label=e,this.hiddableElements=t,this.waitRender()}waitRender(){new MutationObserver((e,t)=>{if(this.element.querySelector("span")){const o=new Event("post-render");this.element.dispatchEvent(o),t.disconnect()}}).observe(this.element,{childList:!0})}onPostRender(){this.element.querySelector("span").innerText=this.label,this.element.querySelector(".group-header").removeAttribute("graggable")}onClick(){this.hiddableElements.forEach(e=>e.classList.toggle("hidden")),this.element.querySelector(".arrow").toggleAttribute("open")}}const l="Balaclava: Auto Champion Select";class k{constructor(e,t,o,s,c,i,y){this.id=e,this.name=t,this.legend=o,this.tags=s,this.group=c,this.perform=this.perform.bind(this),this.callback=i,this.toasts=y}perform(){if(typeof this.callback.then=="function")return Toast.promise(this.callback,{loading:this.toasts.loading,success:this.toasts.success,error:this.toasts.error});try{const e=this.callback();Toast.success(this.toasts.success||(e?this.toasts.on:this.toasts.off))}catch(e){Toast.error(this.toasts.error),console.error(e)}}}class D extends k{constructor(e,t,o){super(`${t}Switch`,()=>{var s;return`Auto ${e} [${(s=DataStore.get(t))!=null&&s.enabled?"ON":"OFF"}]`},()=>{var s;return(s=DataStore.get(t))!=null&&s.enabled?"Turn OFF":"Turn ON"},[l,t,"switch"],l,o,{on:`Auto ${e} is ON!`,off:`Auto ${e} is OFF!`,error:`Failed to toggle Auto ${e}. Check console.`})}}class x extends k{constructor(e,t){super(`${t}ForceSwitch`,()=>{var o;return`Force ${e} [${(o=DataStore.get(t))!=null&&o.force?"ON":"OFF"}]`},()=>`Ignore team intent and force ${e} the selected champion`,[l,t,"force","switch"],l,()=>this.switchDataStore(t),{on:`Force ${e} is ON!`,off:`Force ${e} is OFF!`,error:`Failed to toggle Force ${e}. Check console.`})}switchDataStore(e){const t=DataStore.get(e);return t.force=!t.force,DataStore.set(e,t),t.force}}class L extends k{constructor(e){super("RefreshDropdowns",()=>"Refresh Dropdowns",()=>"Normally dropdowns refresh automatically...",[l,"refresh"],l,()=>this.refreshDropdowns(e),{success:"Refreshed Dropdowns!",error:"Failed to refresh Dropdowns. Check console."})}refreshDropdowns(e){for(let t of e)t.refresh()}}class O extends D{constructor(e){super("Pick","controladoPick",e)}}class N extends D{constructor(e){super("Ban","controladoBan",e)}}class q extends x{constructor(){super("Pick","controladoPick")}}class j extends x{constructor(){super("Ban","controladoBan")}}function G(n){for(let e of n)CommandBar.addAction(e)}const K="1.3.4";const A=new I,w=new b("Accept","controladoAutoAccept"),f=new b("Pick","controladoPick"),h=new m("1st pick","controladoPick",0,B),d=new m("2nd pick","controladoPick",1,B),g=new b("Ban","controladoBan"),S=new m("1st ban","controladoBan",0,F),C=new m("2nd ban","controladoBan",1,F);function v(){return document.querySelector(".lol-social-roster")}async function B(){let n=await a("GET","/lol-champions/v1/owned-champions-minimal");for(;!n.ok;)console.debug("auto-champion-select(owned-champions-minimal): Retrying..."),n=await a("GET","/lol-champions/v1/owned-champions-minimal"),await u(1e3);const e=await n.json();return e.sort((t,o)=>t.name.localeCompare(o.name)),e}async function F(){const e=await(await a("GET","/lol-game-data/assets/v1/champion-summary.json")).json();return e.sort((t,o)=>t.name.localeCompare(o.name)),e}async function H(){w.config.enabled===!0&&(console.debug("auto-champion-select(auto-accept): Ready check detected, accepting in 2 seconds..."),await u(2e3),await M())}async function M(){const n=await a("POST","/lol-matchmaking/v1/ready-check/accept");n.ok?console.debug("auto-champion-select(auto-accept): Accepted ready check"):console.error("auto-champion-select(auto-accept): Failed to accept ready check",n)}window.addEventListener("load",async()=>{let n=v();for(;!n;)await u(200),n=v();Promise.all([w.setup(),f.setup(),g.setup(),h.setup(),d.setup(),S.setup(),C.setup()]),G([new O(()=>f.toggle()),new N(()=>g.toggle()),new q,new j,new L([h,d])]),P("/lol-inventory/v1/wallet",s=>{s.eventType==="Update"&&(console.debug("auto-champion-select(wallet): Refreshing dropdowns..."),Promise.all([h.refresh(),d.refresh()]))}),P("/lol-gameflow/v1/gameflow-phase",s=>{s.data==="ReadyCheck"&&H(),s.data==="ChampSelect"?A.mount():A.unmount()});const e=document.createElement("div"),t=document.createElement("div");t.classList.add("auto-select-checkboxes-div"),t.append(w.element,f.element,g.element),e.append(h.element,d.element),e.append(S.element,C.element);const o=new R("Auto champion select",e,t);n.append(o.element,t,e),console.debug(`auto-champion-select(${K}): Report bugs to Balaclava#1912`)});