-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (115 loc) · 3.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { initSettingAutoAccept } from "./auto-accept";
import "./index.css";
import {
initUIPrePickInChampSelect,
prePickChampionEvent,
prePickChampionsUI,
} from "./pre-pick";
import { waitForElm } from "./utils";
console.log("Hello, League Client!, Ken Plugin is running");
/* load theme from URL */
function injectCSS(url) {
const link = document.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", url);
document.body.appendChild(link);
}
function subscribe() {
const uri = document.querySelector('link[rel="riot:plugins:websocket"]').href;
console.log("url socket", uri, DataStore);
if (uri) {
console.log({ uri });
handleGetSummonerData();
}
// ======== socket
const socket = new WebSocket(uri, "wamp");
socket.onopen = () => socket.send(JSON.stringify([5, "OnJsonApiEvent"]));
socket.onmessage = async (message) => {
const [_id, _event, info] = JSON.parse(message.data);
console.log("URL : ", info.uri);
// console.log(DataStore.get("summoner"));
// @todo process data
listenAutoAccept(info);
listenAfterMatch(info);
};
}
const handleGetSummonerData = async () => {
const res = await fetch("/lol-summoner/v1/current-summoner");
const summoner = await res.json();
console.log(summoner);
DataStore.set("summoner", summoner);
const championList = DataStore.get("champions");
const pool = DataStore.get("champPool");
// DataStore.set("champPool", []);
console.log("init pool", pool);
if (!championList) {
const resC = await fetch(
`/lol-champions/v1/inventories/${summoner.summonerId}/champions`
);
const champs = await resC.json();
// console.log({ champs });
const d = [...champs]
.map((v) => ({ ...v, checked: false }))
.sort((a, b) => b.id - a.id);
d.shift();
DataStore.set("champions", d);
}
// undefined
if (!pool) {
console.log("NO POOL");
if (!championList) {
DataStore.set("champPool", []);
} else {
// init
const p = championList.map((v) => v).filter((z) => z.checked);
DataStore.set("champPool", p);
}
} else {
// init
const p = championList.map((v) => v).filter((z) => z.checked);
DataStore.set("champPool", p);
}
};
const listenAfterMatch = ({ uri, data }) => {
const isPrePickMode = DataStore.get("prePickMode");
if (uri === "/lol-champ-select/v1/session") {
// console.log("SELECT 😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂");
// console.log(JSON.stringify(data));
// handle when lobby has changed
if (isPrePickMode) {
prePickChampionEvent(data);
}
}
};
const listenAutoAccept = ({ uri, data }) => {
const isAutoAcceptMode = DataStore.get("autoAcceptMode");
if (uri === "/lol-gameflow/v1/gameflow-phase") {
console.log("listenAutoAccept", { data, isAutoAcceptMode });
// logic goes here
if (data === "ReadyCheck" && isAutoAcceptMode) {
fetch("/lol-matchmaking/v1/ready-check/accept", {
method: "POST",
});
}
}
};
// +=========== window
window.addEventListener("load", () => {
const url =
"https://fonts.googleapis.com/css2?family=Dancing+Script&family=Lora:ital,wght@1,500&family=Montserrat:wght@100&display=swap";
/* ^----- put your link here */
/* the server must support HTTPS, otherwise the theme will be denied */
injectCSS(url);
subscribe();
// menu rendered
waitForElm(
"div.lol-social-lower-pane-container > lol-social-roster > lol-uikit-scrollable > div.list-content"
).then(() => {
initSettingAutoAccept();
prePickChampionsUI();
// initAramBoostUI();
initUIPrePickInChampSelect();
// /lol-champ-select/v1/session/my-selection/reroll
});
});