yolkbot 1.4.7 → 1.5.0

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 (129) hide show
  1. package/README.md +1 -1
  2. package/browser/build/global.js +1 -1
  3. package/browser/build/module.js +1 -1
  4. package/dist/api.d.ts +34 -16
  5. package/dist/api.js +164 -121
  6. package/dist/bot/GamePlayer.d.ts +2 -2
  7. package/dist/bot/GamePlayer.js +15 -5
  8. package/dist/bot.d.ts +121 -83
  9. package/dist/bot.js +624 -1077
  10. package/dist/comm/CommIn.js +14 -17
  11. package/dist/comm/CommOut.js +15 -15
  12. package/dist/constants/CommCode.js +1 -1
  13. package/dist/constants/findItemById.js +2 -1
  14. package/dist/constants/guns.d.ts +16 -41
  15. package/dist/constants/guns.js +137 -259
  16. package/dist/constants/index.d.ts +7 -9
  17. package/dist/constants/index.js +15 -13
  18. package/dist/dispatches/BanPlayerDispatch.d.ts +3 -1
  19. package/dist/dispatches/BootPlayerDispatch.d.ts +3 -1
  20. package/dist/dispatches/ChatDispatch.d.ts +3 -1
  21. package/dist/dispatches/ChatDispatch.js +3 -3
  22. package/dist/dispatches/FireDispatch.d.ts +3 -1
  23. package/dist/dispatches/GameOptionsDispatch.d.ts +3 -1
  24. package/dist/dispatches/GoToAmmoDispatch.d.ts +2 -0
  25. package/dist/dispatches/GoToAmmoDispatch.js +15 -16
  26. package/dist/dispatches/GoToCoopDispatch.d.ts +2 -0
  27. package/dist/dispatches/GoToCoopDispatch.js +19 -20
  28. package/dist/dispatches/GoToGrenadeDispatch.d.ts +2 -0
  29. package/dist/dispatches/GoToGrenadeDispatch.js +15 -16
  30. package/dist/dispatches/GoToPlayerDispatch.d.ts +3 -1
  31. package/dist/dispatches/GoToPlayerDispatch.js +16 -21
  32. package/dist/dispatches/GoToSpatulaDispatch.d.ts +2 -0
  33. package/dist/dispatches/GoToSpatulaDispatch.js +11 -14
  34. package/dist/dispatches/LookAtDispatch.d.ts +3 -1
  35. package/dist/dispatches/LookAtDispatch.js +8 -6
  36. package/dist/dispatches/LookAtPosDispatch.d.ts +3 -1
  37. package/dist/dispatches/LookAtPosDispatch.js +1 -1
  38. package/dist/dispatches/MeleeDispatch.d.ts +2 -0
  39. package/dist/dispatches/MeleeDispatch.js +4 -4
  40. package/dist/dispatches/MovementDispatch.d.ts +3 -1
  41. package/dist/dispatches/MovementDispatch.js +1 -1
  42. package/dist/dispatches/PauseDispatch.d.ts +2 -0
  43. package/dist/dispatches/ReloadDispatch.d.ts +2 -0
  44. package/dist/dispatches/ReloadDispatch.js +17 -10
  45. package/dist/dispatches/ReportPlayerDispatch.d.ts +4 -2
  46. package/dist/dispatches/ReportPlayerDispatch.js +8 -6
  47. package/dist/dispatches/ResetGameDispatch.d.ts +2 -0
  48. package/dist/dispatches/SaveLoadoutDispatch.d.ts +4 -2
  49. package/dist/dispatches/SaveLoadoutDispatch.js +9 -8
  50. package/dist/dispatches/SpawnDispatch.d.ts +2 -0
  51. package/dist/dispatches/SpawnDispatch.js +5 -1
  52. package/dist/dispatches/SwapWeaponDispatch.d.ts +3 -1
  53. package/dist/dispatches/SwapWeaponDispatch.js +1 -1
  54. package/dist/dispatches/SwitchTeamDispatch.d.ts +2 -0
  55. package/dist/dispatches/SwitchTeamDispatch.js +2 -1
  56. package/dist/dispatches/ThrowGrenadeDispatch.d.ts +3 -1
  57. package/dist/dispatches/index.d.ts +105 -182
  58. package/dist/dispatches/index.js +24 -25
  59. package/dist/enums.d.ts +154 -0
  60. package/dist/enums.js +114 -0
  61. package/dist/env/fetch.d.ts +15 -0
  62. package/dist/env/fetch.js +113 -79
  63. package/dist/env/globals.d.ts +9 -0
  64. package/dist/env/globals.js +11 -9
  65. package/dist/index.d.ts +31 -0
  66. package/dist/index.js +24 -14
  67. package/dist/packets/addPlayer.js +63 -0
  68. package/dist/packets/beginShellStreak.js +44 -0
  69. package/dist/packets/challengeCompleted.js +16 -0
  70. package/dist/packets/changeCharacter.js +46 -0
  71. package/dist/packets/chat.js +10 -0
  72. package/dist/packets/collectItem.js +26 -0
  73. package/dist/packets/die.js +40 -0
  74. package/dist/packets/endShellStreak.js +21 -0
  75. package/dist/packets/eventModifier.js +8 -0
  76. package/dist/packets/explode.js +19 -0
  77. package/dist/packets/fire.js +21 -0
  78. package/dist/packets/gameAction.js +33 -0
  79. package/dist/packets/gameJoined.js +64 -0
  80. package/dist/packets/gameOptions.js +27 -0
  81. package/dist/packets/hitMe.js +12 -0
  82. package/dist/packets/hitMeHardBoiled.js +18 -0
  83. package/dist/packets/hitThem.js +12 -0
  84. package/dist/packets/melee.js +8 -0
  85. package/dist/packets/metaGameState.js +62 -0
  86. package/dist/packets/pause.js +17 -0
  87. package/dist/packets/ping.js +19 -0
  88. package/dist/packets/playerInfo.js +15 -0
  89. package/dist/packets/reload.js +17 -0
  90. package/dist/packets/removePlayer.js +10 -0
  91. package/dist/packets/respawn.js +31 -0
  92. package/dist/packets/socketReady.js +16 -0
  93. package/dist/packets/spawnItem.js +11 -0
  94. package/dist/packets/swapWeapon.js +11 -0
  95. package/dist/packets/switchTeam.js +13 -0
  96. package/dist/packets/syncMe.js +25 -0
  97. package/dist/packets/syncThem.js +63 -0
  98. package/dist/packets/throwGrenade.js +17 -0
  99. package/dist/packets/updateBalance.js +8 -0
  100. package/dist/pathing/astar.js +33 -20
  101. package/dist/pathing/mapnode.d.ts +3 -1
  102. package/dist/pathing/mapnode.js +170 -65
  103. package/dist/socket.d.ts +21 -6
  104. package/dist/socket.js +48 -38
  105. package/dist/util.d.ts +4 -1
  106. package/dist/util.js +105 -45
  107. package/dist/wasm/bytes.d.ts +1 -1
  108. package/dist/wasm/bytes.js +1 -1
  109. package/dist/wasm/direct.d.ts +1 -1
  110. package/dist/wasm/direct.js +13 -13
  111. package/dist/wasm/legacy.d.ts +7 -6
  112. package/dist/wasm/legacy.js +14 -8
  113. package/package.json +43 -30
  114. package/dist/comm/index.d.ts +0 -12
  115. package/dist/comm/index.js +0 -11
  116. package/dist/constants/changelog.d.ts +0 -7
  117. package/dist/constants/housePromo.d.ts +0 -40
  118. package/dist/constants/language.d.ts +0 -3
  119. package/dist/constants/notices.d.ts +0 -4
  120. package/dist/constants/shellNews.d.ts +0 -12
  121. package/dist/constants/shellYoutube.d.ts +0 -12
  122. package/dist/constants/shopItems.d.ts +0 -15
  123. package/dist/constants/sounds.d.ts +0 -10
  124. package/dist/matchmaker.d.ts +0 -50
  125. package/dist/matchmaker.js +0 -141
  126. package/dist/pathing/binaryheap.d.ts +0 -18
  127. package/dist/pathing/binaryheap.js +0 -79
  128. package/dist/wasm/util.d.ts +0 -9
  129. package/dist/wasm/util.js +0 -19
package/dist/socket.js CHANGED
@@ -1,17 +1,27 @@
1
1
  import globals from "./env/globals.js";
2
2
  import { UserAgent } from "./constants/index.js";
3
3
 
4
- class yolkws {
4
+ export class yolkws {
5
+ connected = false;
6
+ autoReconnect = false;
5
7
  url = "";
6
8
  proxy = "";
7
9
  binaryType = "";
8
- maxRetries = 5;
9
10
  connectionTimeout = 5000;
10
- constructor(url, proxy) {
11
- if (globals.isBrowser && proxy)
12
- throw new Error("You cannot pass a proxy to a WebSocket in a browser.");
11
+ errorLogger = (...args) => console.error(...args);
12
+ onopen = () => {};
13
+ onmessage = () => {};
14
+ onclose = () => {};
15
+ onerror = () => {};
16
+ constructor(url, params = {}) {
17
+ if (typeof params !== "object")
18
+ params = {};
19
+ if (typeof process === "undefined" && params.proxy)
20
+ throw new Error("You cannot pass a proxy to a WebSocket in this environment.");
13
21
  this.url = url;
14
- this.proxy = proxy;
22
+ this.proxy = params.proxy;
23
+ if (typeof params.errorLogger === "function")
24
+ this.errorLogger = params.errorLogger;
15
25
  }
16
26
  async tryConnect(tries = 1) {
17
27
  const url = new URL(this.url);
@@ -21,7 +31,7 @@ class yolkws {
21
31
  return await this.tryConnect(tries + 1);
22
32
  };
23
33
  try {
24
- this.socket = globals.isBrowser ? new globals.WebSocket(this.url) : new globals.WebSocket(this.url, {
34
+ this.socket = typeof process === "undefined" ? new globals.WebSocket(this.url) : new globals.WebSocket(this.url, {
25
35
  proxy: this.proxy || null,
26
36
  headers: {
27
37
  "accept-encoding": "gzip, deflate, br, zstd",
@@ -30,14 +40,13 @@ class yolkws {
30
40
  connection: "Upgrade",
31
41
  origin: url.origin.replace("ws", "http"),
32
42
  pragma: "no-cache",
33
- "sec-websocket-extensions": "permessage-deflate; client_max_window_bits",
34
43
  "user-agent": UserAgent
35
44
  }
36
45
  });
37
46
  if (this.binaryType)
38
47
  this.socket.binaryType = this.binaryType;
39
48
  } catch (e) {
40
- console.error(`Failed to connect on try ${tries}, trying again...`, e);
49
+ this.errorLogger(`Failed to connect on try ${tries}, trying again...`, e);
41
50
  return await retryOrQuit();
42
51
  }
43
52
  if (this.onBeforeConnect)
@@ -49,49 +58,50 @@ class yolkws {
49
58
  }, this.connectionTimeout);
50
59
  const errorListener = async (e) => {
51
60
  clearTimeout(timeout);
52
- console.error("WebSocket error", e);
61
+ this.errorLogger("WebSocket error", e);
53
62
  resolve(await retryOrQuit());
54
63
  };
55
64
  this.socket.addEventListener("open", async () => {
56
65
  clearTimeout(timeout);
66
+ this.connected = true;
67
+ this.onopen();
57
68
  this.socket.removeEventListener("error", errorListener);
69
+ this.socket.addEventListener("message", (messageEvent) => this.onmessage(messageEvent));
70
+ this.socket.addEventListener("close", (closeEvent) => !this.autoReconnect && this.onclose(closeEvent));
71
+ this.socket.addEventListener("error", (event) => this.onerror(event));
58
72
  resolve(true);
59
73
  });
60
74
  this.socket.addEventListener("error", errorListener);
75
+ this.socket.addEventListener("close", () => {
76
+ if (this.connected) {
77
+ this.connected = false;
78
+ if (this.autoReconnect)
79
+ setTimeout(async () => {
80
+ const didConnect = await this.tryConnect();
81
+ if (!didConnect) {
82
+ if (this.onclose)
83
+ this.onclose();
84
+ this.errorLogger("tryConnect: failed to reconnect to", this.url, "after 5 attempts.");
85
+ this.errorLogger("tryConnect: please check your internet connection & ensure your IP isn't blocked.");
86
+ }
87
+ }, 250);
88
+ }
89
+ });
61
90
  });
62
91
  }
63
- get onmessage() {
64
- return this.socket?.onmessage;
65
- }
66
- set onmessage(handler) {
67
- if (this.socket)
68
- this.socket.onmessage = handler;
69
- else
70
- console.error("set onmessage before socket existed");
71
- }
72
- get onclose() {
73
- return this.socket?.onclose;
74
- }
75
- set onclose(handler) {
76
- if (this.socket)
77
- this.socket.onclose = handler;
78
- else
79
- console.error("set onclose before socket existed");
80
- }
81
- get onerror() {
82
- return this.socket?.onerror;
83
- }
84
- set onerror(handler) {
85
- if (this.socket)
86
- this.socket.onerror = handler;
87
- else
88
- console.error("set onclose before socket existed");
89
- }
90
92
  send(data) {
91
93
  return this.socket?.send(data);
92
94
  }
93
95
  close(data) {
94
- return this.socket?.close(data);
96
+ if (!this.socket)
97
+ return;
98
+ this.autoReconnect = false;
99
+ if (this.socket.terminate) {
100
+ try {
101
+ this.socket.terminate();
102
+ } catch {}
103
+ } else
104
+ this.socket.close(data);
95
105
  }
96
106
  }
97
107
  export default yolkws;
package/dist/util.d.ts CHANGED
@@ -2,5 +2,8 @@ import { Zone } from './bot';
2
2
  import { Position } from './bot/GamePlayer';
3
3
  import { MapJSON } from './constants/maps';
4
4
 
5
+ export declare function createGun<T extends { ammo: { capacity: number; store: number } }>(baseGun: T): T;
6
+ export declare function createError<T = string>(message: T): { ok: false; error: T };
5
7
  export declare function fetchMap(name: string, hash: string): Promise<MapJSON>;
6
- export declare function initKotcZones(meshData: Array<Position>): Array<Array<Zone>>;
8
+ export declare function initKotcZones(meshData: Array<Position>): Array<Array<Zone>>;
9
+ export declare function isDoubleEggWeeknd(): boolean;
package/dist/util.js CHANGED
@@ -1,6 +1,26 @@
1
- import globals from "./env/globals.js";
1
+ export const createGun = (baseGun) => {
2
+ const gun = structuredClone(baseGun);
3
+ gun.ammo.rounds = gun.ammo.capacity;
4
+ gun.ammo.storeMax = gun.ammo.store;
5
+ return gun;
6
+ };
7
+ export const createError = (errorEnum) => ({ ok: false, error: errorEnum });
8
+ const attemptFetch = async (url, options = {}, retries = 3, backoff = 300) => {
9
+ try {
10
+ const response = await fetch(url, options);
11
+ if (!response.ok)
12
+ throw new Error(`failed to fetch map; status: ${response.status}`);
13
+ return response;
14
+ } catch (error) {
15
+ if (retries > 0) {
16
+ await new Promise((res) => setTimeout(res, backoff));
17
+ return attemptFetch(url, options, retries - 1, backoff * 2);
18
+ } else
19
+ throw error;
20
+ }
21
+ };
2
22
  export const fetchMap = async (name, hash) => {
3
- if (!globals.isIsolated && typeof process !== "undefined") {
23
+ if (typeof process !== "undefined") {
4
24
  const { existsSync, mkdirSync, readFileSync, writeFileSync } = process.getBuiltinModule("node:fs");
5
25
  const { join } = process.getBuiltinModule("node:path");
6
26
  const { homedir } = process.getBuiltinModule("node:os");
@@ -10,59 +30,99 @@ export const fetchMap = async (name, hash) => {
10
30
  const mapCache = join(yolkbotCache, "maps");
11
31
  if (!existsSync(mapCache))
12
32
  mkdirSync(mapCache);
13
- const mapFile = join(mapCache, `${name}-${hash}.json`);
33
+ const safeName = String(name).replace(/[^a-z0-9_-]/gi, "_");
34
+ const safeHash = String(hash).replace(/[^a-z0-9_-]/gi, "_");
35
+ const mapFile = join(mapCache, `${safeName}-${safeHash}.json`);
14
36
  if (existsSync(mapFile))
15
37
  return JSON.parse(readFileSync(mapFile, "utf-8"));
16
- const data = await (await fetch(`https://x.yolkbot.xyz/data/maps/full/${name}.json?${hash}`)).json();
38
+ const data = await (await attemptFetch(`https://x.yolkbot.xyz/data/maps/full/${name}.json?${hash}`)).json();
17
39
  writeFileSync(mapFile, JSON.stringify(data, null, 4), { flag: "w+" });
18
40
  return data;
19
41
  }
20
- const data = await (await fetch(`https://esm.sh/gh/yolkorg/maps/maps/${name}.json?${hash}`)).json();
42
+ const data = await (await attemptFetch(`https://x.yolkbot.xyz/data/maps/full/${name}.json?${hash}`)).json();
21
43
  return data;
22
44
  };
23
45
  export const initKotcZones = (meshData) => {
24
- let numCaptureZones = 0;
25
- const mapData = {};
26
- const zones = [];
27
- for (const cell of meshData) {
28
- if (!mapData[cell.x])
29
- mapData[cell.x] = {};
30
- if (!mapData[cell.x][cell.y])
31
- mapData[cell.x][cell.y] = {};
32
- mapData[cell.x][cell.y][cell.z] = { zone: null };
46
+ const len = meshData.length;
47
+ if (!len)
48
+ return [];
49
+ let minX = 1 / 0, minY = 1 / 0, minZ = 1 / 0;
50
+ let maxX = -1 / 0, maxY = -1 / 0, maxZ = -1 / 0;
51
+ for (let i = 0;i < len; i++) {
52
+ const { x, y, z } = meshData[i];
53
+ if (x < minX)
54
+ minX = x;
55
+ if (x > maxX)
56
+ maxX = x;
57
+ if (y < minY)
58
+ minY = y;
59
+ if (y > maxY)
60
+ maxY = y;
61
+ if (z < minZ)
62
+ minZ = z;
63
+ if (z > maxZ)
64
+ maxZ = z;
33
65
  }
34
- const offsets = [
35
- { x: -1, z: 0 },
36
- { x: 1, z: 0 },
37
- { x: 0, z: -1 },
38
- { x: 0, z: 1 }
39
- ];
40
- const getMapCellAt = (x, y, z) => mapData[x] && mapData[x][y] && mapData[x][y][z] ? mapData[x][y][z] : null;
41
- for (const cellA of meshData) {
42
- if (!mapData[cellA.x][cellA.y][cellA.z].zone) {
43
- cellA.zone = ++numCaptureZones;
44
- mapData[cellA.x][cellA.y][cellA.z].zone = cellA.zone;
45
- const currentZone = [cellA];
46
- let hits;
47
- do {
48
- hits = 0;
49
- for (const cellB of meshData) {
50
- if (!mapData[cellB.x][cellB.y][cellB.z].zone) {
51
- for (const o of offsets) {
52
- const cell = getMapCellAt(cellB.x + o.x, cellB.y, cellB.z + o.z);
53
- if (cell && cell.zone === cellA.zone) {
54
- hits++;
55
- cellB.zone = cellA.zone;
56
- mapData[cellB.x][cellB.y][cellB.z].zone = cellA.zone;
57
- currentZone.push(cellB);
58
- break;
59
- }
60
- }
61
- }
62
- }
63
- } while (hits > 0);
64
- zones.push(currentZone);
66
+ const sizeX = maxX - minX + 1 | 0;
67
+ const sizeY = maxY - minY + 1 | 0;
68
+ const sizeZ = maxZ - minZ + 1 | 0;
69
+ const strideY = sizeX;
70
+ const strideZ = sizeX * sizeY;
71
+ const cellIndex = new Int32Array(sizeX * sizeY * sizeZ).fill(-1);
72
+ const zoneIds = new Uint16Array(len);
73
+ const queue = new Uint32Array(len);
74
+ for (let i = 0;i < len; i++) {
75
+ const { x, y, z } = meshData[i];
76
+ cellIndex[(x - minX | 0) + (y - minY | 0) * strideY + (z - minZ | 0) * strideZ] = i;
77
+ }
78
+ const zones = [];
79
+ let zoneId = 0;
80
+ for (let i = 0;i < len; i++) {
81
+ if (zoneIds[i])
82
+ continue;
83
+ const zone = ++zoneId;
84
+ const activeZone = [];
85
+ let head = 0, tail = 0;
86
+ queue[tail++] = i;
87
+ zoneIds[i] = zone;
88
+ while (head < tail) {
89
+ const idx = queue[head++];
90
+ const cell = meshData[idx];
91
+ cell.zone = zone;
92
+ activeZone[activeZone.length] = cell;
93
+ const { x, y, z } = cell;
94
+ const flatIdx = (x - minX | 0) + (y - minY | 0) * strideY + (z - minZ | 0) * strideZ;
95
+ let nIdx;
96
+ if (x > minX && (nIdx = cellIndex[flatIdx - 1]) !== -1 && !zoneIds[nIdx]) {
97
+ zoneIds[nIdx] = zone;
98
+ queue[tail++] = nIdx;
99
+ }
100
+ if (x < maxX && (nIdx = cellIndex[flatIdx + 1]) !== -1 && !zoneIds[nIdx]) {
101
+ zoneIds[nIdx] = zone;
102
+ queue[tail++] = nIdx;
103
+ }
104
+ if (y > minY && (nIdx = cellIndex[flatIdx - strideY]) !== -1 && !zoneIds[nIdx]) {
105
+ zoneIds[nIdx] = zone;
106
+ queue[tail++] = nIdx;
107
+ }
108
+ if (y < maxY && (nIdx = cellIndex[flatIdx + strideY]) !== -1 && !zoneIds[nIdx]) {
109
+ zoneIds[nIdx] = zone;
110
+ queue[tail++] = nIdx;
111
+ }
112
+ if (z > minZ && (nIdx = cellIndex[flatIdx - strideZ]) !== -1 && !zoneIds[nIdx]) {
113
+ zoneIds[nIdx] = zone;
114
+ queue[tail++] = nIdx;
115
+ }
116
+ if (z < maxZ && (nIdx = cellIndex[flatIdx + strideZ]) !== -1 && !zoneIds[nIdx]) {
117
+ zoneIds[nIdx] = zone;
118
+ queue[tail++] = nIdx;
119
+ }
65
120
  }
121
+ zones[zones.length] = activeZone;
66
122
  }
67
123
  return zones;
68
124
  };
125
+ export const isDoubleEggWeeknd = () => {
126
+ const day = new Date().getUTCDay();
127
+ return day >= 5 && new Date().getUTCHours() >= 20 || day === 6 || day === 0;
128
+ };
@@ -1 +1 @@
1
- export declare const wasmBytes: string;
1
+ export declare const wasmBytes: Uint8Array;