kol.js 0.0.1 → 0.0.2

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/dist/Cache.js CHANGED
@@ -9,7 +9,7 @@ export class Cache {
9
9
  export class PlayerCache extends Cache {
10
10
  async fetch(identifier, full = false) {
11
11
  const player = await (async () => {
12
- const cached = this.cache.find(p => p.matchesIdentifier(identifier));
12
+ const cached = this.cache.find((p) => p.matchesIdentifier(identifier));
13
13
  if (cached) {
14
14
  return cached;
15
15
  }
package/dist/Client.d.ts CHANGED
@@ -24,6 +24,11 @@ type Events = {
24
24
  public: (message: KoLMessage) => void;
25
25
  rollover: () => void;
26
26
  };
27
+ type Familiar = {
28
+ id: number;
29
+ name: string;
30
+ image: string;
31
+ };
27
32
  declare const Client_base: new () => TypedEmitter<Events>;
28
33
  export declare class Client extends Client_base {
29
34
  #private;
@@ -73,6 +78,7 @@ export declare class Client extends Client_base {
73
78
  joinClan(id: number): Promise<boolean>;
74
79
  addToWhitelist(playerId: number, clanId: number): Promise<boolean>;
75
80
  getLeaderboard(leaderboardId: number): Promise<import("./utils/leaderboard.js").LeaderboardInfo>;
76
- useFamiliar(familiarId: number): Promise<void>;
81
+ useFamiliar(familiarId: number): Promise<boolean>;
82
+ getFamiliars(): Promise<Familiar[]>;
77
83
  }
78
84
  export {};
package/dist/Client.js CHANGED
@@ -30,19 +30,19 @@ export class Client extends EventEmitter {
30
30
  method: "POST",
31
31
  ...options,
32
32
  };
33
- const qs = formatQuerystring(params);
33
+ const qs = formatQuerystring({ ...params, pwd: this.#pwd });
34
34
  return await this.session(`https://www.kingdomofloathing.com/${path}${qs ? `?${qs}` : ""}`, {
35
35
  method,
36
36
  body: body ? createBody({ ...body, pwd: this.#pwd }) : undefined,
37
37
  });
38
38
  }
39
39
  async fetchText(path, options = {}) {
40
- if (!await this.login())
40
+ if (!(await this.login()))
41
41
  return options.fallback ?? "";
42
42
  return (await this.#fetch(path, options)).text();
43
43
  }
44
44
  async fetchJson(path, options = {}) {
45
- if (!await this.login())
45
+ if (!(await this.login()))
46
46
  return options.fallback ?? null;
47
47
  return (await this.#fetch(path, options)).json();
48
48
  }
@@ -315,11 +315,29 @@ export class Client extends EventEmitter {
315
315
  return parseLeaderboard(page);
316
316
  }
317
317
  async useFamiliar(familiarId) {
318
- await this.fetchText("familiar.php", {
318
+ const result = await this.fetchText("familiar.php", {
319
319
  params: {
320
320
  action: "newfam",
321
321
  newfam: familiarId.toFixed(0),
322
322
  },
323
323
  });
324
+ return result.includes(`var currentfam = ${familiarId};`);
325
+ }
326
+ async getFamiliars() {
327
+ const terrarium = await this.fetchText("familiar.php");
328
+ const matches = terrarium.matchAll(/onClick='fam\((\d+)\)'(?:><img)? src=".*?\/(\w+\/\w+.(?:gif|png))".*?\d+-pound (.*?) \(/g);
329
+ const familiars = [...matches].map((m) => ({
330
+ id: Number(m[1]),
331
+ image: m[2],
332
+ name: m[3],
333
+ }));
334
+ if (terrarium.includes("fam(278)")) {
335
+ familiars.push({
336
+ id: 278,
337
+ image: "otherimages/righthandbody.png",
338
+ name: "Left-Hand Man",
339
+ });
340
+ }
341
+ return familiars;
324
342
  }
325
343
  }
@@ -2,9 +2,14 @@ import { describe, expect, test, vi } from "vitest";
2
2
  import { Client } from "./Client.js";
3
3
  import { loadFixture } from "./testUtils.js";
4
4
  const { json, text } = vi.hoisted(() => ({ json: vi.fn(), text: vi.fn() }));
5
- vi.mock("./utils/visit.js", () => ({
6
- createSession: vi.fn().mockReturnValue({ json, text }),
7
- }));
5
+ vi.mock("./Client.js", async (importOriginal) => {
6
+ const client = await importOriginal();
7
+ client.Client.prototype.login = async () => true;
8
+ client.Client.prototype.checkLoggedIn = async () => true;
9
+ client.Client.prototype.fetchText = text;
10
+ client.Client.prototype.fetchJson = json;
11
+ return client;
12
+ });
8
13
  const client = new Client("", "");
9
14
  describe("LoathingChat", () => {
10
15
  test("Can parse a regular message", async () => {
@@ -130,3 +135,28 @@ describe("Skill descriptions", () => {
130
135
  });
131
136
  });
132
137
  });
138
+ describe("Familiars", () => {
139
+ test("can fetch all familiars", async () => {
140
+ text.mockResolvedValueOnce(await loadFixture(__dirname, "familiar_in_standard_run.html"));
141
+ const familiars = await client.getFamiliars();
142
+ // Current
143
+ expect(familiars).toContainEqual({
144
+ id: 294,
145
+ name: "Jill-of-All-Trades",
146
+ image: "itemimages/darkjill2f.gif",
147
+ });
148
+ // Problematic
149
+ expect(familiars).toContainEqual({
150
+ id: 278,
151
+ name: "Left-Hand Man",
152
+ image: "otherimages/righthandbody.png",
153
+ });
154
+ expect(familiars).toContainEqual({
155
+ id: 279,
156
+ name: "Melodramedary",
157
+ image: "otherimages/camelfam_left.gif",
158
+ });
159
+ // Just to be sure
160
+ expect(familiars).toHaveLength(206);
161
+ });
162
+ });
package/dist/Player.js CHANGED
@@ -131,6 +131,6 @@ export class Player {
131
131
  }
132
132
  async isOnline() {
133
133
  const response = await this.#client.useChatMacro(`/whois ${this.name}`);
134
- return response?.output.includes("This player is currently online") ?? false;
134
+ return (response?.output.includes("This player is currently online") ?? false);
135
135
  }
136
136
  }
@@ -4,9 +4,14 @@ import { Player } from "./Player.js";
4
4
  import { expectNotNull, loadFixture } from "./testUtils.js";
5
5
  import { Client } from "./Client.js";
6
6
  const { json, text } = vi.hoisted(() => ({ json: vi.fn(), text: vi.fn() }));
7
- vi.mock("./utils/visit.js", () => ({
8
- createSession: vi.fn().mockReturnValue({ json, text }),
9
- }));
7
+ vi.mock("./Client.js", async (importOriginal) => {
8
+ const client = await importOriginal();
9
+ client.Client.prototype.login = async () => true;
10
+ client.Client.prototype.checkLoggedIn = async () => true;
11
+ client.Client.prototype.fetchText = text;
12
+ client.Client.prototype.fetchJson = json;
13
+ return client;
14
+ });
10
15
  const client = new Client("", "");
11
16
  test("Can search for a player by name", async () => {
12
17
  vi.mocked(text).mockResolvedValueOnce(await loadFixture(__dirname, "searchplayer_mad_carew.html"));
package/dist/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export { Player } from "./Player.js";
2
2
  export { Client } from "./Client.js";
3
- export { createSession } from "./utils/visit.js";
4
3
  export { resolveKoLImage } from "./utils/utils.js";
5
4
  export type { KoLMessage } from "./utils/kmail.js";
6
5
  export type { SubboardInfo } from "./utils/leaderboard.js";
package/dist/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export { Player } from "./Player.js";
2
2
  export { Client } from "./Client.js";
3
- export { createSession } from "./utils/visit.js";
4
3
  export { resolveKoLImage } from "./utils/utils.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kol.js",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "files": [
@@ -10,7 +10,8 @@
10
10
  "scripts": {
11
11
  "build": "tsc",
12
12
  "test": "vitest",
13
- "format": "prettier --write ."
13
+ "format": "prettier --write .",
14
+ "prepack": "yarn run build"
14
15
  },
15
16
  "devDependencies": {
16
17
  "prettier": "^3.2.5",
package/dist/start.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/start.js DELETED
@@ -1,4 +0,0 @@
1
- import { Client } from "./Client.js";
2
- const client = new Client("onweb", "beer146beef31");
3
- const gausie = await client.players.fetch("gausie", true);
4
- console.log(gausie);