@sjtdev/koishi-plugin-dota2tracker 1.1.5-beta.2 → 1.1.5-beta.3

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.
Files changed (2) hide show
  1. package/lib/index.js +85 -19
  2. 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.id));
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.id));
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) => ({ id: hero["id"], name: hero["name"], shortName: hero["name"].match(/^npc_dota_hero_(.+)$/)[1] }));
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,95 @@ 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.goto("https://www.dota2.com/patches/7.36");
1489
+ 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)");
1490
+ await page.evaluate(() => {
1491
+ const scripts = document.querySelectorAll("script");
1492
+ scripts.forEach((script) => script.remove());
1493
+ });
1494
+ const result = await page.evaluate(() => {
1495
+ 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");
1496
+ const divArray = [];
1497
+ divs.forEach((div) => {
1498
+ const subDiv = div.querySelector("a > div");
1499
+ const match = subDiv?.style.backgroundImage.match(/url\("https:\/\/cdn\.cloudflare\.steamstatic\.com\/apps\/dota2\/images\/dota_react\/heroes\/([^"]+)\.png"\)/);
1500
+ divArray.push({ heroName: match[1], div: div.outerHTML });
1501
+ });
1502
+ 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());
1503
+ 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");
1504
+ const remainingContent = document.documentElement.outerHTML;
1505
+ return {
1506
+ divArray,
1507
+ remainingContent
1508
+ };
1509
+ });
1510
+ const heroes3 = [];
1511
+ result.divArray.forEach((hero) => {
1512
+ const res = Object.values(dotaconstants3.heroes).find((Chero) => Chero.name.match(/^npc_dota_hero_(.+)$/)[1] == hero.heroName);
1513
+ heroes3.push({ id: res.id, data: hero.div });
1514
+ });
1515
+ heroes3.push({ id: 0, data: result.remainingContent });
1516
+ await ctx.database.upsert("dt_7_36", (row) => heroes3);
1517
+ import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/remainingContent.html", result.remainingContent);
1518
+ await session.send("数据获取完成。");
1519
+ await page.close();
1520
+ }
1479
1521
  if (input_data) {
1480
1522
  await session.send("正在查询,请耐心等待……");
1481
1523
  const page = await ctx.puppeteer.page();
1482
- await page.goto("https://www.dota2.com.cn/patches/7.36");
1483
- await page.waitForSelector("body > div:nth-of-type(3) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(5) > div:nth-of-type(2) > div:nth-of-type(1)");
1524
+ await page.setContent((await ctx.database.get("dt_7_36", [0]))[0].data);
1525
+ 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
1526
  const hero = findingHero(input_data);
1485
- await page.evaluate((hero2) => {
1486
- const divs = document.querySelectorAll("body > div:nth-of-type(3) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(5) > div:nth-of-type(2) > div");
1487
- for (const div of divs) {
1488
- const firstChild = div.firstElementChild;
1489
- if (firstChild && firstChild.tagName === "A" && firstChild.getAttribute("href") === "/hero/" + hero2.shortName) {
1490
- div.classList.add("selector");
1491
- }
1492
- }
1493
- return null;
1494
- }, hero);
1495
- const testE = await page.$("body > div:nth-of-type(3) > div:first-of-type > div:nth-of-type(2) > div:nth-of-type(2) > div:nth-of-type(5) > div:nth-of-type(2) > div.selector");
1527
+ const placeholder = await page.$("div.placeholder");
1528
+ await page.waitForSelector("div.placeholder");
1529
+ const newHeroHTML = (await ctx.database.get("dt_7_36", [hero.id]))[0].data;
1530
+ await page.evaluate(
1531
+ (element, html) => {
1532
+ element.outerHTML = html;
1533
+ },
1534
+ placeholder,
1535
+ newHeroHTML
1536
+ );
1537
+ await page.evaluate(async () => {
1538
+ const images = Array.from(document.querySelectorAll("img"));
1539
+ await Promise.all(
1540
+ images.map((img) => {
1541
+ if (img.complete) {
1542
+ return Promise.resolve();
1543
+ } else {
1544
+ return new Promise((resolve, reject) => {
1545
+ img.onload = resolve;
1546
+ img.onerror = () => {
1547
+ const placeholderSrc = "https://cdn.cloudflare.steamstatic.com/apps/dota2/images/dota_react/icons/innate_icon.png";
1548
+ img.src = placeholderSrc;
1549
+ img.onload = resolve;
1550
+ img.onerror = resolve;
1551
+ };
1552
+ });
1553
+ }
1554
+ })
1555
+ );
1556
+ });
1557
+ 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
1558
  const res = await testE.screenshot();
1497
1559
  const base64String = Buffer.from(res).toString("base64");
1498
1560
  const imgTag = `<img src="data:image/png;base64,${base64String}" alt="Image" />`;
1499
1561
  if (process.env.NODE_ENV === "development")
1500
1562
  import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/temp.png", res);
1563
+ if (process.env.NODE_ENV === "development")
1564
+ import_fs2.default.writeFileSync("./node_modules/@sjtdev/koishi-plugin-dota2tracker/temp.html", await page.content());
1501
1565
  session.send(imgTag);
1566
+ page.close();
1502
1567
  } else
1503
- session.send("https://www.dota2.com.cn/patches/7.36");
1568
+ session.send("https://www.dota2.com/patches/7.36");
1504
1569
  });
1505
- ctx.command("test <input_data>").action(async ({ session }, input_data) => {
1570
+ ctx.command("test <input_data>").option("a", "a").action(async ({ session, options }, input_data) => {
1571
+ console.log((await ctx.database.get("dt_7_36", [0]))[0].data);
1506
1572
  });
1507
1573
  ctx.on("ready", async () => {
1508
1574
  const tables = await ctx.database.tables;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sjtdev/koishi-plugin-dota2tracker",
3
3
  "description": "",
4
- "version": "1.1.5-beta.2",
4
+ "version": "1.1.5-beta.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [