@sjtdev/koishi-plugin-dota2tracker 1.1.5-beta.2 → 1.1.5-beta.4
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/lib/index.js +88 -19
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1323,12 +1323,12 @@ async function apply(ctx, config) {
|
|
|
1323
1323
|
let steamId = flagBindedPlayer?.steamId ?? input_data;
|
|
1324
1324
|
let player;
|
|
1325
1325
|
try {
|
|
1326
|
-
let queryRes = await query(PLAYER_INFO_WITH_25_MATCHES(steamId, hero
|
|
1326
|
+
let queryRes = await query(PLAYER_INFO_WITH_25_MATCHES(steamId, hero?.id));
|
|
1327
1327
|
if (queryRes.status == 200) {
|
|
1328
1328
|
player = queryRes.data.data.player;
|
|
1329
1329
|
} else
|
|
1330
1330
|
throw 0;
|
|
1331
|
-
let queryRes2 = await query(PLAYER_EXTRA_INFO(steamId, player.matchCount, Object.keys(dotaconstants3.heroes).length, hero
|
|
1331
|
+
let queryRes2 = await query(PLAYER_EXTRA_INFO(steamId, player.matchCount, Object.keys(dotaconstants3.heroes).length, hero?.id));
|
|
1332
1332
|
if (queryRes2.status == 200) {
|
|
1333
1333
|
let playerExtra = queryRes2.data.data.player;
|
|
1334
1334
|
let filteredDotaPlus = {};
|
|
@@ -1456,7 +1456,12 @@ async function apply(ctx, config) {
|
|
|
1456
1456
|
function findingHero(input) {
|
|
1457
1457
|
if (!input)
|
|
1458
1458
|
return;
|
|
1459
|
-
let dc_heroes = Object.values(dotaconstants3.heroes).map((hero) => ({
|
|
1459
|
+
let dc_heroes = Object.values(dotaconstants3.heroes).map((hero) => ({
|
|
1460
|
+
id: hero["id"],
|
|
1461
|
+
name: hero["name"],
|
|
1462
|
+
shortName: hero["name"].match(/^npc_dota_hero_(.+)$/)[1],
|
|
1463
|
+
localized_name: hero["localized_name"].toLowerCase().replace(/\s+/g, "")
|
|
1464
|
+
}));
|
|
1460
1465
|
let cn_heroes = Object.keys(HEROES_CHINESE).map((key) => ({
|
|
1461
1466
|
id: parseInt(key),
|
|
1462
1467
|
names_cn: HEROES_CHINESE[key]
|
|
@@ -1475,34 +1480,98 @@ async function apply(ctx, config) {
|
|
|
1475
1480
|
return heroes3.find((hero) => hero.names_cn.includes(input) || hero.shortName === input.toLowerCase() || hero.id == input);
|
|
1476
1481
|
}
|
|
1477
1482
|
__name(findingHero, "findingHero");
|
|
1478
|
-
ctx.command("7.36 <input_data>", "查询7.36改动").usage("可查询英雄改动并生成图片返回").example("7.36 小松许").action(async ({ session }, input_data) => {
|
|
1483
|
+
ctx.command("7.36 <input_data>", "查询7.36改动").option("refresh", "-f 重新获取数据").usage("可查询英雄改动并生成图片返回").example("7.36 小松许").action(async ({ session, options }, input_data) => {
|
|
1484
|
+
if (!("dt_7_36" in ctx.database.tables) || options.refresh) {
|
|
1485
|
+
session.send((!("dt_7_36" in ctx.database.tables) ? "初次使用," : "") + "正在获取数据……");
|
|
1486
|
+
await ctx.model.extend("dt_7_36", { id: "integer", data: "string" });
|
|
1487
|
+
const page = await ctx.puppeteer.page();
|
|
1488
|
+
await page.setExtraHTTPHeaders({
|
|
1489
|
+
"Accept-Language": "zh-CN,zh;q=0.9"
|
|
1490
|
+
});
|
|
1491
|
+
await page.goto("https://www.dota2.com/patches/7.36");
|
|
1492
|
+
await page.waitForSelector("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div:nth-of-type(1)");
|
|
1493
|
+
await page.evaluate(() => {
|
|
1494
|
+
const scripts = document.querySelectorAll("script");
|
|
1495
|
+
scripts.forEach((script) => script.remove());
|
|
1496
|
+
});
|
|
1497
|
+
const result = await page.evaluate(() => {
|
|
1498
|
+
const divs = document.querySelectorAll("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div");
|
|
1499
|
+
const divArray = [];
|
|
1500
|
+
divs.forEach((div) => {
|
|
1501
|
+
const subDiv = div.querySelector("a > div");
|
|
1502
|
+
const match = subDiv?.style.backgroundImage.match(/url\("https:\/\/cdn\.cloudflare\.steamstatic\.com\/apps\/dota2\/images\/dota_react\/heroes\/([^"]+)\.png"\)/);
|
|
1503
|
+
divArray.push({ heroName: match[1], div: div.outerHTML });
|
|
1504
|
+
});
|
|
1505
|
+
document.querySelectorAll("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div:not(:first-of-type)").forEach((node) => node.remove());
|
|
1506
|
+
document.querySelector("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div").classList.add("placeholder");
|
|
1507
|
+
const remainingContent = document.documentElement.outerHTML;
|
|
1508
|
+
return {
|
|
1509
|
+
divArray,
|
|
1510
|
+
remainingContent
|
|
1511
|
+
};
|
|
1512
|
+
});
|
|
1513
|
+
const heroes3 = [];
|
|
1514
|
+
result.divArray.forEach((hero) => {
|
|
1515
|
+
const res = Object.values(dotaconstants3.heroes).find((Chero) => Chero.name.match(/^npc_dota_hero_(.+)$/)[1] == hero.heroName);
|
|
1516
|
+
heroes3.push({ id: res.id, data: hero.div });
|
|
1517
|
+
});
|
|
1518
|
+
heroes3.push({ id: 0, data: result.remainingContent });
|
|
1519
|
+
await ctx.database.upsert("dt_7_36", (row) => heroes3);
|
|
1520
|
+
import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/remainingContent.html", result.remainingContent);
|
|
1521
|
+
await session.send("数据获取完成。");
|
|
1522
|
+
await page.close();
|
|
1523
|
+
}
|
|
1479
1524
|
if (input_data) {
|
|
1480
1525
|
await session.send("正在查询,请耐心等待……");
|
|
1481
1526
|
const page = await ctx.puppeteer.page();
|
|
1482
|
-
await page.
|
|
1483
|
-
await page.waitForSelector("body > div:nth-of-type(
|
|
1527
|
+
await page.setContent((await ctx.database.get("dt_7_36", [0]))[0].data);
|
|
1528
|
+
await page.waitForSelector("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div:nth-of-type(1)");
|
|
1484
1529
|
const hero = findingHero(input_data);
|
|
1485
|
-
await page.
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1530
|
+
const placeholder = await page.$("div.placeholder");
|
|
1531
|
+
await page.waitForSelector("div.placeholder");
|
|
1532
|
+
const newHeroHTML = (await ctx.database.get("dt_7_36", [hero.id]))[0].data;
|
|
1533
|
+
await page.evaluate(
|
|
1534
|
+
(element, html) => {
|
|
1535
|
+
element.outerHTML = html;
|
|
1536
|
+
},
|
|
1537
|
+
placeholder,
|
|
1538
|
+
newHeroHTML
|
|
1539
|
+
);
|
|
1540
|
+
await page.evaluate(async () => {
|
|
1541
|
+
const images = Array.from(document.querySelectorAll("img"));
|
|
1542
|
+
await Promise.all(
|
|
1543
|
+
images.map((img) => {
|
|
1544
|
+
if (img.complete) {
|
|
1545
|
+
return Promise.resolve();
|
|
1546
|
+
} else {
|
|
1547
|
+
return new Promise((resolve, reject) => {
|
|
1548
|
+
img.onload = resolve;
|
|
1549
|
+
img.onerror = () => {
|
|
1550
|
+
const placeholderSrc = "https://cdn.cloudflare.steamstatic.com/apps/dota2/images/dota_react/icons/innate_icon.png";
|
|
1551
|
+
img.src = placeholderSrc;
|
|
1552
|
+
img.onload = resolve;
|
|
1553
|
+
img.onerror = resolve;
|
|
1554
|
+
};
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1557
|
+
})
|
|
1558
|
+
);
|
|
1559
|
+
});
|
|
1560
|
+
const testE = await page.$("body > div:nth-of-type(2) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(3) > div:nth-of-type(5) > div:nth-of-type(2) > div");
|
|
1496
1561
|
const res = await testE.screenshot();
|
|
1497
1562
|
const base64String = Buffer.from(res).toString("base64");
|
|
1498
1563
|
const imgTag = `<img src="data:image/png;base64,${base64String}" alt="Image" />`;
|
|
1499
1564
|
if (process.env.NODE_ENV === "development")
|
|
1500
1565
|
import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/temp.png", res);
|
|
1566
|
+
if (process.env.NODE_ENV === "development")
|
|
1567
|
+
import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/temp.html", await page.content());
|
|
1501
1568
|
session.send(imgTag);
|
|
1569
|
+
page.close();
|
|
1502
1570
|
} else
|
|
1503
|
-
session.send("https://www.dota2.com
|
|
1571
|
+
session.send("https://www.dota2.com/patches/7.36");
|
|
1504
1572
|
});
|
|
1505
|
-
ctx.command("test <input_data>").action(async ({ session }, input_data) => {
|
|
1573
|
+
ctx.command("test <input_data>").option("a", "a").action(async ({ session, options }, input_data) => {
|
|
1574
|
+
console.log((await ctx.database.get("dt_7_36", [0]))[0].data);
|
|
1506
1575
|
});
|
|
1507
1576
|
ctx.on("ready", async () => {
|
|
1508
1577
|
const tables = await ctx.database.tables;
|