@azimiao/koishi-plugin-cafe-bot-exp 0.0.1 → 0.0.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.
@@ -0,0 +1 @@
1
+ export declare function DailySeededName(uid: any): string;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * PseudoRandom 伪随机数类
3
+ */
4
+ declare class PseudoRandom {
5
+ private seed;
6
+ private randFunc;
7
+ constructor(seedStr: string);
8
+ next: () => number;
9
+ nextInt: (min: number, max: number) => number;
10
+ }
11
+ export default PseudoRandom;
package/lib/index.js CHANGED
@@ -1679,146 +1679,62 @@ __export(src_exports, {
1679
1679
  module.exports = __toCommonJS(src_exports);
1680
1680
  var import_koishi2 = require("koishi");
1681
1681
 
1682
- // src/DailySeededRandom.ts
1683
- function md5(str) {
1684
- function rotateLeft(lValue, iShiftBits) {
1685
- return lValue << iShiftBits | lValue >>> 32 - iShiftBits;
1686
- }
1687
- __name(rotateLeft, "rotateLeft");
1688
- function addUnsigned(lX, lY) {
1689
- const lX4 = lX & 1073741824;
1690
- const lY4 = lY & 1073741824;
1691
- const lX8 = lX & 2147483648;
1692
- const lY8 = lY & 2147483648;
1693
- const lResult = (lX & 1073741823) + (lY & 1073741823);
1694
- if (lX4 & lY4) return lResult ^ 2147483648 ^ lX8 ^ lY8;
1695
- if (lX4 | lY4) {
1696
- if (lResult & 1073741824) return lResult ^ 3221225472 ^ lX8 ^ lY8;
1697
- else return lResult ^ 1073741824 ^ lX8 ^ lY8;
1698
- } else {
1699
- return lResult ^ lX8 ^ lY8;
1700
- }
1701
- }
1702
- __name(addUnsigned, "addUnsigned");
1703
- function F(x2, y, z) {
1704
- return x2 & y | ~x2 & z;
1705
- }
1706
- __name(F, "F");
1707
- function G(x2, y, z) {
1708
- return x2 & z | y & ~z;
1709
- }
1710
- __name(G, "G");
1711
- function H(x2, y, z) {
1712
- return x2 ^ y ^ z;
1713
- }
1714
- __name(H, "H");
1715
- function I(x2, y, z) {
1716
- return y ^ (x2 | ~z);
1717
- }
1718
- __name(I, "I");
1719
- function FF(a2, b2, c2, d2, x2, s, ac) {
1720
- a2 = addUnsigned(a2, addUnsigned(addUnsigned(F(b2, c2, d2), x2), ac));
1721
- return addUnsigned(rotateLeft(a2, s), b2);
1722
- }
1723
- __name(FF, "FF");
1724
- function GG(a2, b2, c2, d2, x2, s, ac) {
1725
- a2 = addUnsigned(a2, addUnsigned(addUnsigned(G(b2, c2, d2), x2), ac));
1726
- return addUnsigned(rotateLeft(a2, s), b2);
1727
- }
1728
- __name(GG, "GG");
1729
- function HH(a2, b2, c2, d2, x2, s, ac) {
1730
- a2 = addUnsigned(a2, addUnsigned(addUnsigned(H(b2, c2, d2), x2), ac));
1731
- return addUnsigned(rotateLeft(a2, s), b2);
1732
- }
1733
- __name(HH, "HH");
1734
- function II(a2, b2, c2, d2, x2, s, ac) {
1735
- a2 = addUnsigned(a2, addUnsigned(addUnsigned(I(b2, c2, d2), x2), ac));
1736
- return addUnsigned(rotateLeft(a2, s), b2);
1737
- }
1738
- __name(II, "II");
1739
- function convertToWordArray(str2) {
1740
- const lWordCount = ((str2.length + 8 >> 6) + 1) * 16;
1741
- const lWordArray = Array(lWordCount - 1);
1742
- let lBytePosition = 0;
1743
- let lByteCount = 0;
1744
- while (lByteCount < str2.length) {
1745
- const lWordIndex2 = lByteCount >> 2;
1746
- if (lWordArray[lWordIndex2] === void 0) lWordArray[lWordIndex2] = 0;
1747
- lWordArray[lWordIndex2] |= (str2.charCodeAt(lByteCount) & 255) << lByteCount % 4 * 8;
1748
- lByteCount++;
1749
- }
1750
- const lWordIndex = lByteCount >> 2;
1751
- if (lWordArray[lWordIndex] === void 0) lWordArray[lWordIndex] = 0;
1752
- lWordArray[lWordIndex] |= 128 << lByteCount % 4 * 8;
1753
- lWordArray[lWordCount - 2] = str2.length << 3;
1754
- lWordArray[lWordCount - 1] = str2.length >>> 29;
1755
- return lWordArray;
1756
- }
1757
- __name(convertToWordArray, "convertToWordArray");
1758
- function wordToHex(lValue) {
1759
- let wordToHexValue = "", wordToHexValueTemp = "", lByte, lCount;
1760
- for (lCount = 0; lCount <= 3; lCount++) {
1761
- lByte = lValue >>> lCount * 8 & 255;
1762
- wordToHexValueTemp = "0" + lByte.toString(16);
1763
- wordToHexValue += wordToHexValueTemp.slice(-2);
1764
- }
1765
- return wordToHexValue;
1766
- }
1767
- __name(wordToHex, "wordToHex");
1768
- const x = convertToWordArray(str);
1769
- let a = 1732584193;
1770
- let b = 4023233417;
1771
- let c = 2562383102;
1772
- let d = 271733878;
1773
- for (let k = 0; k < x.length; k += 16) {
1774
- const [AA, BB, CC, DD] = [a, b, c, d];
1775
- a = FF(a, b, c, d, x[k + 0], 7, 3614090360);
1776
- d = FF(d, a, b, c, x[k + 1], 12, 3905402710);
1777
- c = FF(c, d, a, b, x[k + 2], 17, 606105819);
1778
- b = FF(b, c, d, a, x[k + 3], 22, 3250441966);
1779
- a = addUnsigned(a, AA);
1780
- b = addUnsigned(b, BB);
1781
- c = addUnsigned(c, CC);
1782
- d = addUnsigned(d, DD);
1783
- }
1784
- return (wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)).toLowerCase();
1785
- }
1786
- __name(md5, "md5");
1787
- function hashToSeed(md5str) {
1788
- let seed = 0;
1789
- for (let i = 0; i < md5str.length; i++) {
1790
- seed = seed * 31 + md5str.charCodeAt(i) >>> 0;
1791
- }
1792
- return seed;
1793
- }
1794
- __name(hashToSeed, "hashToSeed");
1795
- function makeSeededRandom(seed) {
1796
- return function() {
1797
- seed = seed * 1664525 + 1013904223 >>> 0;
1798
- return seed / 4294967295;
1799
- };
1800
- }
1801
- __name(makeSeededRandom, "makeSeededRandom");
1682
+ // src/DailySeededName.ts
1802
1683
  function DailySeededName(uid) {
1803
1684
  const now = /* @__PURE__ */ new Date();
1804
1685
  const utc = now.getTime() + now.getTimezoneOffset() * 6e4;
1805
1686
  const beijing = new Date(utc + 8 * 60 * 6e4);
1806
1687
  const dateStr = beijing.toISOString().slice(0, 10);
1807
- const fullStr = `${uid}_${dateStr}`;
1688
+ const year = beijing.getFullYear();
1689
+ const month = beijing.getMonth() + 1;
1690
+ const day = beijing.getDate();
1691
+ const pad = /* @__PURE__ */ __name((n) => n.toString().padStart(2, "0"), "pad");
1692
+ const fullStr = `${uid}_${year}-${pad(month)}-${pad(day)}`;
1808
1693
  return fullStr;
1809
1694
  }
1810
1695
  __name(DailySeededName, "DailySeededName");
1811
- function DailySeededRandom(uid) {
1812
- const now = /* @__PURE__ */ new Date();
1813
- const utc = now.getTime() + now.getTimezoneOffset() * 6e4;
1814
- const beijing = new Date(utc + 8 * 60 * 6e4);
1815
- const dateStr = beijing.toISOString().slice(0, 10);
1816
- const fullStr = `${uid}_${dateStr}`;
1817
- const md5str = md5(fullStr);
1818
- const seed = hashToSeed(md5str);
1819
- return makeSeededRandom(seed);
1696
+
1697
+ // src/PseudoRandom.ts
1698
+ function stringToSeed(str) {
1699
+ let hash = 2166136261;
1700
+ for (let i = 0; i < str.length; i++) {
1701
+ hash ^= str.charCodeAt(i);
1702
+ hash = Math.imul(hash, 16777619);
1703
+ }
1704
+ return hash >>> 0;
1705
+ }
1706
+ __name(stringToSeed, "stringToSeed");
1707
+ function mulberry32(a) {
1708
+ return function() {
1709
+ a |= 0;
1710
+ a = a + 1831565813 | 0;
1711
+ let t = Math.imul(a ^ a >>> 15, 1 | a);
1712
+ t ^= t + Math.imul(t ^ t >>> 7, 61 | t);
1713
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
1714
+ };
1820
1715
  }
1821
- __name(DailySeededRandom, "DailySeededRandom");
1716
+ __name(mulberry32, "mulberry32");
1717
+ var PseudoRandom = class {
1718
+ static {
1719
+ __name(this, "PseudoRandom");
1720
+ }
1721
+ seed;
1722
+ randFunc;
1723
+ constructor(seedStr) {
1724
+ this.seed = stringToSeed(seedStr);
1725
+ if (this.seed === 0) {
1726
+ this.seed = 123456789;
1727
+ }
1728
+ this.randFunc = mulberry32(this.seed);
1729
+ }
1730
+ next = /* @__PURE__ */ __name(() => {
1731
+ return this.randFunc();
1732
+ }, "next");
1733
+ nextInt = /* @__PURE__ */ __name((min, max) => {
1734
+ return Math.floor(this.next() * (max - min + 1)) + min;
1735
+ }, "nextInt");
1736
+ };
1737
+ var PseudoRandom_default = PseudoRandom;
1822
1738
 
1823
1739
  // src/templete/html.ts
1824
1740
  function HtmlCreator(cardList) {
@@ -2040,33 +1956,14 @@ function shuffleWithCustomRandom(array, rand) {
2040
1956
  return array;
2041
1957
  }
2042
1958
  __name(shuffleWithCustomRandom, "shuffleWithCustomRandom");
2043
- function getLerpValue(start, end, percent) {
2044
- var a = end - start;
2045
- var b = percent * a;
2046
- var c = start + b;
2047
- return c;
2048
- }
2049
- __name(getLerpValue, "getLerpValue");
2050
- function getLerpIntValue(start, end, percent) {
2051
- var a = getLerpValue(start, end, percent);
2052
- a = Math.floor(a + 0.5);
2053
- if (a > start && a > end) {
2054
- a = start > end ? start : end;
2055
- }
2056
- if (a < start && a < end) {
2057
- a = start < end ? start : end;
2058
- }
2059
- return a;
2060
- }
2061
- __name(getLerpIntValue, "getLerpIntValue");
2062
1959
  var star2Name = null;
2063
- function getCards(userId, config) {
1960
+ function getCards(seed, config) {
2064
1961
  let minCount = config.MinCount;
2065
1962
  let maxCount = config.MaxCount;
2066
- let randomer = DailySeededRandom(userId);
2067
- let drawTime = getLerpIntValue(minCount, maxCount, randomer());
1963
+ let randomer = new PseudoRandom_default(seed);
1964
+ let drawTime = randomer.nextInt(minCount, maxCount);
2068
1965
  let cardDataCache = [...cardData];
2069
- let cardDataRandom = shuffleWithCustomRandom(cardDataCache, randomer);
1966
+ let cardDataRandom = shuffleWithCustomRandom(cardDataCache, randomer.next);
2070
1967
  const allChanges = [
2071
1968
  config.Star5Chance,
2072
1969
  config.Star4Chance,
@@ -2093,7 +1990,7 @@ function getCards(userId, config) {
2093
1990
  });
2094
1991
  let result = [];
2095
1992
  for (let i = 0; i < drawTime; i++) {
2096
- let randomNumber = randomer();
1993
+ let randomNumber = randomer.next();
2097
1994
  let selectStarGroup = -999;
2098
1995
  for (let starGroup = 0; starGroup < allChanges.length; starGroup++) {
2099
1996
  if (randomNumber < allChanges[starGroup]) {
@@ -2103,8 +2000,7 @@ function getCards(userId, config) {
2103
2000
  }
2104
2001
  if (selectStarGroup > 0 && groupedByStar != null && groupedByStar[selectStarGroup]) {
2105
2002
  let nowGroupData = groupedByStar[selectStarGroup];
2106
- let pickUpNumber = randomer();
2107
- pickUpNumber = getLerpIntValue(0, nowGroupData.length - 1, pickUpNumber);
2003
+ let pickUpNumber = randomer.nextInt(0, nowGroupData.length - 1);
2108
2004
  let card = nowGroupData[pickUpNumber];
2109
2005
  card.starText = star2Name[card.star];
2110
2006
  if (!result.includes(card)) {
@@ -2125,9 +2021,11 @@ function apply(ctx, config) {
2125
2021
  argv.session?.send(`我是轨迹Cafe群聊机器人概念版~`);
2126
2022
  });
2127
2023
  ctx.command("轨迹抽卡/给我抽", "进行每日抽卡").action(async (argv, _) => {
2128
- let result = getCards(argv.session.userId, config);
2024
+ let seed = DailySeededName(argv.session.userId);
2025
+ let randomer = new PseudoRandom_default(seed);
2026
+ let result = getCards(seed, config);
2129
2027
  if (result.length <= 0) {
2130
- let msg2 = "你并不是本群限定非酋, 只不过抽到的卡牌被" + import_koishi2.Random.pick(animals) + "叼走了~\n";
2028
+ let msg2 = "你并不是本群限定非酋, 只不过抽到的卡牌被" + animals[randomer.nextInt(0, animals.length - 1)] + "叼走了~\n";
2131
2029
  argv.session?.send(`${At(argv)}${msg2}`);
2132
2030
  return;
2133
2031
  }
@@ -2153,28 +2051,41 @@ function apply(ctx, config) {
2153
2051
  argv.session?.send(msg);
2154
2052
  return;
2155
2053
  });
2054
+ const requestWebCache = {};
2156
2055
  ctx.command("轨迹抽卡/给我抽图", "进行抽卡并显示卡牌图片").action(async (argv, _) => {
2157
- let result = getCards(argv.session.userId, config);
2056
+ let seed = DailySeededName(argv.session.userId);
2057
+ let randomer = new PseudoRandom_default(seed);
2058
+ let result = getCards(seed, config);
2158
2059
  if (result.length <= 0) {
2159
- let msg = "你并不是本群限定非酋, 只不过抽到的卡牌被" + import_koishi2.Random.pick(animals) + "叼走了~\n";
2060
+ let msg = "你并不是本群限定非酋, 只不过抽到的卡牌被" + animals[randomer.nextInt(0, animals.length - 1)] + "叼走了~\n";
2160
2061
  argv.session?.send(`${At(argv)} ${msg}`);
2161
2062
  return;
2162
2063
  }
2064
+ if (requestWebCache[seed]) {
2065
+ argv.session?.send(`${At(argv)} 请等待上次抽取完成哦~`);
2066
+ return;
2067
+ }
2068
+ requestWebCache[seed] = "waiting";
2069
+ await argv.session?.sendQueued(`${At(argv)} 抽取中,请稍候`);
2163
2070
  var a = html_default(result);
2164
2071
  await ctx.http.post(
2165
2072
  config.ImageServer,
2166
2073
  {
2167
2074
  html: a,
2168
2075
  auth: config.ImageServerAuth,
2169
- filename: DailySeededName(argv.session.userId)
2076
+ filename: seed
2170
2077
  },
2171
2078
  {
2172
2079
  responseType: "json"
2173
2080
  }
2174
- ).then((res) => {
2175
- argv.session?.send(`${At(argv)}<img src="${res.data}"/>`);
2176
- }).catch((e) => {
2177
- argv.session?.send(`${At(argv)}图片生成失败`);
2081
+ ).then(async (res) => {
2082
+ delete requestWebCache[seed];
2083
+ await argv.session?.sendQueued(`${At(argv)}<img src="${res.data}"/>`);
2084
+ await argv.session?.cancelQueued();
2085
+ }).catch(async (e) => {
2086
+ delete requestWebCache[seed];
2087
+ await argv.session?.sendQueued(`${At(argv)}图片生成失败`);
2088
+ await argv.session?.cancelQueued();
2178
2089
  });
2179
2090
  return;
2180
2091
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@azimiao/koishi-plugin-cafe-bot-exp",
3
3
  "description": "cafe bot experiment",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -16,5 +16,19 @@
16
16
  ],
17
17
  "peerDependencies": {
18
18
  "koishi": "^4.18.7"
19
+ },
20
+ "koishi": {
21
+ "description": {
22
+ "zh": "轨迹Cafe概念版"
23
+ },
24
+ "service": {
25
+ "required": [
26
+ "http"
27
+ ]
28
+ },
29
+ "locales": [
30
+ "zh"
31
+ ],
32
+ "preview": true
19
33
  }
20
- }
34
+ }