kol.js 0.0.2 → 0.1.1

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 (58) hide show
  1. package/package.json +7 -6
  2. package/src/Cache.ts +33 -0
  3. package/src/Client.test.ts +206 -0
  4. package/src/Client.ts +506 -0
  5. package/src/Player.test.ts +101 -0
  6. package/src/Player.ts +209 -0
  7. package/src/__fixtures__/backoffice_prices_alien_meat.html +46 -0
  8. package/src/__fixtures__/backoffice_prices_lov_elephant.html +30 -0
  9. package/src/__fixtures__/backoffice_prices_magical_mystery_juice.html +30 -0
  10. package/src/__fixtures__/backoffice_prices_tofurkey_leg.html +48 -0
  11. package/src/__fixtures__/backoffice_prices_turtle_wax_greaves.html +42 -0
  12. package/src/__fixtures__/backoffice_prices_turtle_wax_helmet.html +42 -0
  13. package/src/__fixtures__/backoffice_prices_turtle_wax_shield.html +47 -0
  14. package/src/__fixtures__/desc_effect_pasta_oneness.html +31 -0
  15. package/src/__fixtures__/desc_effect_the_visible_adventurer.html +31 -0
  16. package/src/__fixtures__/desc_item_alien_meat.html +38 -0
  17. package/src/__fixtures__/desc_item_hypodermic_needle.html +39 -0
  18. package/src/__fixtures__/desc_item_lov_elephant.html +39 -0
  19. package/src/__fixtures__/desc_item_magical_mystery_juice.html +39 -0
  20. package/src/__fixtures__/desc_item_mosquito_larva.html +39 -0
  21. package/src/__fixtures__/desc_item_tofurkey_leg.html +38 -0
  22. package/src/__fixtures__/desc_item_turtle_wax_shield.html +79 -0
  23. package/src/__fixtures__/desc_skill_impetuous_sauciness.html +32 -0
  24. package/src/__fixtures__/desc_skill_overload_discarded_refridgerator.html +32 -0
  25. package/src/__fixtures__/familiar_in_standard_run.html +184 -0
  26. package/src/__fixtures__/searchplayer_mad_carew.html +53 -0
  27. package/src/__fixtures__/showplayer_dependence_day.html +47 -0
  28. package/src/__fixtures__/showplayer_golden_gun.html +28 -0
  29. package/src/__fixtures__/showplayer_regular.html +56 -0
  30. package/{dist/index.d.ts → src/index.ts} +1 -0
  31. package/src/testUtils.ts +13 -0
  32. package/src/utils/avatar.ts +90 -0
  33. package/src/utils/kmail.ts +48 -0
  34. package/src/utils/leaderboard.ts +84 -0
  35. package/src/utils/utils.ts +33 -0
  36. package/dist/Cache.d.ts +0 -11
  37. package/dist/Cache.js +0 -26
  38. package/dist/Client.d.ts +0 -84
  39. package/dist/Client.js +0 -343
  40. package/dist/Client.test.d.ts +0 -1
  41. package/dist/Client.test.js +0 -162
  42. package/dist/Player.d.ts +0 -28
  43. package/dist/Player.js +0 -136
  44. package/dist/Player.test.d.ts +0 -1
  45. package/dist/Player.test.js +0 -48
  46. package/dist/index.js +0 -3
  47. package/dist/testUtils.d.ts +0 -2
  48. package/dist/testUtils.js +0 -10
  49. package/dist/utils/avatar.d.ts +0 -1
  50. package/dist/utils/avatar.js +0 -70
  51. package/dist/utils/kmail.d.ts +0 -32
  52. package/dist/utils/kmail.js +0 -1
  53. package/dist/utils/leaderboard.d.ts +0 -16
  54. package/dist/utils/leaderboard.js +0 -56
  55. package/dist/utils/utils.d.ts +0 -4
  56. package/dist/utils/utils.js +0 -27
  57. package/dist/utils/visit.d.ts +0 -3
  58. package/dist/utils/visit.js +0 -10
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "kol.js",
3
- "version": "0.0.2",
4
- "main": "dist/index.js",
3
+ "version": "0.1.1",
4
+ "main": "src/index.ts",
5
5
  "type": "module",
6
6
  "files": [
7
- "/dist"
7
+ "/src"
8
8
  ],
9
9
  "license": "MIT",
10
10
  "scripts": {
11
11
  "build": "tsc",
12
12
  "test": "vitest",
13
- "format": "prettier --write .",
14
- "prepack": "yarn run build"
13
+ "format": "prettier --write ."
15
14
  },
16
15
  "devDependencies": {
16
+ "@types/tough-cookie": "^4",
17
17
  "prettier": "^3.2.5",
18
18
  "typescript": "^5.4.5",
19
19
  "vitest": "^1.6.0"
@@ -22,9 +22,10 @@
22
22
  "@xmldom/xmldom": "^0.8.10",
23
23
  "async-mutex": "^0.5.0",
24
24
  "date-fns": "^3.6.0",
25
- "fetch-cookie": "^3.0.1",
25
+ "got": "^14.3.0",
26
26
  "node-html-parser": "^6.1.13",
27
27
  "querystring": "^0.2.1",
28
+ "tough-cookie": "^4.1.4",
28
29
  "typed-emitter": "^2.1.0",
29
30
  "xpath": "^0.0.34"
30
31
  }
package/src/Cache.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { Client } from "./Client.js";
2
+ import { Player } from "./Player.js";
3
+
4
+ export class Cache<T> {
5
+ client: Client;
6
+
7
+ constructor(client: Client) {
8
+ this.client = client;
9
+ }
10
+
11
+ cache: T[] = [];
12
+ }
13
+
14
+ export class PlayerCache extends Cache<Player<boolean>> {
15
+ async fetch(identifier: string | number, full: true): Promise<Player<true>>;
16
+ async fetch(identifier: string | number): Promise<Player<boolean>>;
17
+ async fetch(identifier: string | number, full = false) {
18
+ const player = await (async () => {
19
+ const cached = this.cache.find((p) => p.matchesIdentifier(identifier));
20
+ if (cached) {
21
+ return cached;
22
+ }
23
+ const fetched = await Player.from(this.client, identifier);
24
+ if (!fetched) return null;
25
+ this.cache.push(fetched);
26
+ return fetched;
27
+ })();
28
+
29
+ if (!player) return player;
30
+
31
+ return full ? await player.full() : player;
32
+ }
33
+ }
@@ -0,0 +1,206 @@
1
+ import { describe, expect, it, test, vi } from "vitest";
2
+
3
+ import { Client } from "./Client.js";
4
+ import { loadFixture } from "./testUtils.js";
5
+
6
+ const { json, text } = vi.hoisted(() => ({ json: vi.fn(), text: vi.fn() }));
7
+
8
+ vi.mock("./Client.js", async (importOriginal) => {
9
+ const client = await importOriginal<typeof import("./Client.js")>();
10
+ client.Client.prototype.login = async () => true;
11
+ client.Client.prototype.checkLoggedIn = async () => true;
12
+ client.Client.prototype.fetchText = text;
13
+ client.Client.prototype.fetchJson = json;
14
+ return client;
15
+ });
16
+
17
+ const client = new Client("", "");
18
+
19
+ describe("LoathingChat", () => {
20
+ test("Can parse a regular message", async () => {
21
+ json.mockResolvedValue({});
22
+ json.mockResolvedValueOnce({
23
+ msgs: [
24
+ {
25
+ msg: "testing",
26
+ type: "public",
27
+ mid: "1538072797",
28
+ who: { name: "gAUSIE", id: "1197090", color: "black" },
29
+ format: "0",
30
+ channel: "talkie",
31
+ channelcolor: "green",
32
+ time: "1698787642",
33
+ },
34
+ ],
35
+ });
36
+
37
+ const messageSpy = vi.fn();
38
+
39
+ client.on("public", messageSpy);
40
+
41
+ await client.checkMessages();
42
+
43
+ expect(messageSpy).toHaveBeenCalledOnce();
44
+ const [message] = messageSpy.mock.calls[0];
45
+ expect(message).toMatchObject({
46
+ type: "public",
47
+ msg: "testing",
48
+ time: new Date(1698787642000),
49
+ who: { id: 1197090, name: "gAUSIE" },
50
+ });
51
+ });
52
+
53
+ test("Can parse a system message for rollover in 5 minutes", async () => {
54
+ json.mockResolvedValueOnce({
55
+ msgs: [
56
+ {
57
+ msg: "The system will go down for nightly maintenance in 5 minutes.",
58
+ type: "system",
59
+ mid: "1538084998",
60
+ who: { name: "System Message", id: "-1", color: "" },
61
+ format: "2",
62
+ channelcolor: "green",
63
+ time: "1698809101",
64
+ },
65
+ ],
66
+ });
67
+
68
+ const messageSpy = vi.fn();
69
+
70
+ client.on("system", messageSpy);
71
+
72
+ await client.checkMessages();
73
+
74
+ expect(messageSpy).toHaveBeenCalledOnce();
75
+ const [message] = messageSpy.mock.calls[0];
76
+ expect(message).toMatchObject({
77
+ type: "system",
78
+ who: { id: -1, name: "System Message" },
79
+ msg: "The system will go down for nightly maintenance in 5 minutes.",
80
+ time: new Date(1698809101000),
81
+ });
82
+ });
83
+
84
+ test("Can parse a system message for rollover in one minute", async () => {
85
+ json.mockResolvedValueOnce({
86
+ msgs: [
87
+ {
88
+ msg: "The system will go down for nightly maintenance in 1 minute.",
89
+ type: "system",
90
+ mid: "1538084998",
91
+ who: { name: "System Message", id: "-1", color: "" },
92
+ format: "2",
93
+ channelcolor: "green",
94
+ time: "1698809101",
95
+ },
96
+ ],
97
+ });
98
+
99
+ const messageSpy = vi.fn();
100
+
101
+ client.on("system", messageSpy);
102
+
103
+ await client.checkMessages();
104
+
105
+ expect(messageSpy).toHaveBeenCalledOnce();
106
+ const [message] = messageSpy.mock.calls[0];
107
+ expect(message).toMatchObject({
108
+ type: "system",
109
+ who: { id: -1, name: "System Message" },
110
+ msg: "The system will go down for nightly maintenance in 1 minute.",
111
+ time: new Date(1698809101000),
112
+ });
113
+ });
114
+
115
+ test("Can parse a system message for rollover complete", async () => {
116
+ json.mockResolvedValueOnce({
117
+ msgs: [
118
+ {
119
+ msg: "Rollover is over.",
120
+ type: "system",
121
+ mid: "1538085619",
122
+ who: { name: "System Message", id: "-1", color: "" },
123
+ format: "2",
124
+ channelcolor: "green",
125
+ time: "1698809633",
126
+ },
127
+ ],
128
+ });
129
+
130
+ const messageSpy = vi.fn();
131
+
132
+ client.on("system", messageSpy);
133
+
134
+ await client.checkMessages();
135
+
136
+ expect(messageSpy).toHaveBeenCalledOnce();
137
+ const [message] = messageSpy.mock.calls[0];
138
+ expect(message).toMatchObject({
139
+ type: "system",
140
+ who: { id: -1, name: "System Message" },
141
+ msg: "Rollover is over.",
142
+ time: new Date(1698809633000),
143
+ });
144
+ });
145
+ });
146
+
147
+ describe("Skill descriptions", () => {
148
+ test("can describe a Skill with no bluetext", async () => {
149
+ text.mockResolvedValueOnce(
150
+ await loadFixture(
151
+ __dirname,
152
+ "desc_skill_overload_discarded_refridgerator.html",
153
+ ),
154
+ );
155
+
156
+ const description = await client.getSkillDescription(7017);
157
+
158
+ expect(description).toStrictEqual({
159
+ blueText: "",
160
+ });
161
+ });
162
+
163
+ test("can describe a Skill with bluetext", async () => {
164
+ text.mockResolvedValueOnce(
165
+ await loadFixture(__dirname, "desc_skill_impetuous_sauciness.html"),
166
+ );
167
+
168
+ const description = await client.getSkillDescription(4015);
169
+
170
+ expect(description).toStrictEqual({
171
+ blueText: "Makes Sauce Potions last longer",
172
+ });
173
+ });
174
+ });
175
+
176
+ describe("Familiars", () => {
177
+ test("can fetch all familiars", async () => {
178
+ text.mockResolvedValueOnce(
179
+ await loadFixture(__dirname, "familiar_in_standard_run.html"),
180
+ );
181
+
182
+ const familiars = await client.getFamiliars();
183
+
184
+ // Current
185
+ expect(familiars).toContainEqual({
186
+ id: 294,
187
+ name: "Jill-of-All-Trades",
188
+ image: "itemimages/darkjill2f.gif",
189
+ });
190
+
191
+ // Problematic
192
+ expect(familiars).toContainEqual({
193
+ id: 278,
194
+ name: "Left-Hand Man",
195
+ image: "otherimages/righthandbody.png",
196
+ });
197
+ expect(familiars).toContainEqual({
198
+ id: 279,
199
+ name: "Melodramedary",
200
+ image: "otherimages/camelfam_left.gif",
201
+ });
202
+
203
+ // Just to be sure
204
+ expect(familiars).toHaveLength(206);
205
+ });
206
+ });