@ianlucas/cs2-lib 5.0.0-rc.1 → 5.0.0-rc.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.
@@ -22,7 +22,6 @@ export declare const CS2_MIN_STICKER_WEAR = 0;
22
22
  export declare const CS2_MAX_STICKER_WEAR = 0.9;
23
23
  export declare const CS2_MAX_STICKERS = 5;
24
24
  export declare const CS2_MAX_PATCHES = 5;
25
- export declare const CS2_NONE = 0;
26
25
  export declare const CS2_STICKER_WEAR_FACTOR = 0.1;
27
26
  export declare const CS2_WEAR_FACTOR = 0.000001;
28
27
  export declare const CS2_NAMETAG_RE: RegExp;
@@ -26,7 +26,6 @@ export const CS2_MIN_STICKER_WEAR = 0;
26
26
  export const CS2_MAX_STICKER_WEAR = 0.9;
27
27
  export const CS2_MAX_STICKERS = 5;
28
28
  export const CS2_MAX_PATCHES = 5;
29
- export const CS2_NONE = 0;
30
29
  export const CS2_STICKER_WEAR_FACTOR = 0.1;
31
30
  export const CS2_WEAR_FACTOR = 0.000001;
32
31
  export const CS2_NAMETAG_RE = /^[A-Za-z0-9`!@#$%^&*-+=(){}\[\]\/\|\\,.?:;'_\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\s]{0,20}$/u;
@@ -1,5 +1,5 @@
1
- import { CS2RarityColorValues } from "./economy-container.js";
2
- import { EnumValues } from "./utils.js";
1
+ import type { CS2RarityColorValues, CS2RaritySoundNameValues } from "./economy-container.js";
2
+ import type { EnumValues } from "./utils.js";
3
3
  export declare const CS2ItemTeam: {
4
4
  readonly T: 0;
5
5
  readonly CT: 1;
@@ -23,11 +23,11 @@ export declare const CS2ItemType: {
23
23
  };
24
24
  export type CS2ItemTypeValues = EnumValues<typeof CS2ItemType>;
25
25
  export declare const CS2ItemWear: {
26
- readonly FactoryNew: 0;
27
- readonly MinimalWear: 1;
28
- readonly FieldTested: 2;
29
- readonly WellWorn: 3;
30
- readonly BattleScarred: 4;
26
+ readonly FactoryNew: "FN";
27
+ readonly MinimalWear: "MW";
28
+ readonly FieldTested: "FT";
29
+ readonly WellWorn: "WW";
30
+ readonly BattleScarred: "BS";
31
31
  };
32
32
  export type CS2ItemWearValues = EnumValues<typeof CS2ItemWear>;
33
33
  export declare const CS2ContainerType: {
@@ -77,3 +77,14 @@ export interface CS2ItemLocalization {
77
77
  }
78
78
  export type CS2ItemLocalizationMap = Record<number, CS2ItemLocalization | undefined>;
79
79
  export type CS2ItemLocalizationByLanguage = Record<string, CS2ItemLocalizationMap>;
80
+ export interface CS2UnlockedItem {
81
+ attributes: {
82
+ containerId: number;
83
+ seed: number | undefined;
84
+ statTrak: number | undefined;
85
+ wear: number | undefined;
86
+ };
87
+ id: number;
88
+ rarity: CS2RaritySoundNameValues;
89
+ special: boolean;
90
+ }
@@ -23,11 +23,11 @@ export const CS2ItemType = {
23
23
  Weapon: "weapon"
24
24
  };
25
25
  export const CS2ItemWear = {
26
- FactoryNew: 0,
27
- MinimalWear: 1,
28
- FieldTested: 2,
29
- WellWorn: 3,
30
- BattleScarred: 4
26
+ FactoryNew: "FN",
27
+ MinimalWear: "MW",
28
+ FieldTested: "FT",
29
+ WellWorn: "WW",
30
+ BattleScarred: "BS"
31
31
  };
32
32
  export const CS2ContainerType = {
33
33
  WeaponCase: 0,
package/dist/economy.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { CS2RarityColorValues, CS2RaritySoundNameValues } from "./economy-container.js";
2
- import { CS2ContainerTypeValues, CS2Item, CS2ItemLocalization, CS2ItemLocalizationMap, CS2ItemTeamValues, CS2ItemTypeValues, CS2ItemWearValues } from "./economy-types.js";
1
+ import { CS2RarityColorValues } from "./economy-container.js";
2
+ import { CS2ContainerTypeValues, CS2Item, CS2ItemLocalization, CS2ItemLocalizationMap, CS2ItemTeamValues, CS2ItemTypeValues, CS2ItemWearValues, CS2UnlockedItem } from "./economy-types.js";
3
3
  import { CS2TeamValues } from "./teams.js";
4
4
  import { Interface } from "./utils.js";
5
5
  type CS2EconomyItemPredicate = Partial<CS2EconomyItem> & {
@@ -37,12 +37,15 @@ export declare class CS2EconomyInstance {
37
37
  resolveContainerSpecialsImage(baseUrl: string, item: number | CS2EconomyItem): string;
38
38
  validateContainerAndKey(containerItem: number | CS2EconomyItem, keyItem?: number | CS2EconomyItem): boolean;
39
39
  safeValidateContainerAndKey(containerItem: number | CS2EconomyItem, keyItem?: number | CS2EconomyItem): boolean;
40
- validateUnlockedItem(item: number | CS2EconomyItem, { id }: ReturnType<InstanceType<typeof CS2EconomyItem>["unlock"]>): void;
40
+ validateUnlockedItem(item: number | CS2EconomyItem, { id }: ReturnType<InstanceType<typeof CS2EconomyItem>["unlockContainer"]>): void;
41
41
  }
42
42
  export declare class CS2EconomyItem implements Interface<Omit<CS2Item, "contents" | "specials" | "teams"> & CS2ItemLocalization & {
43
43
  contents: CS2EconomyItem[] | undefined;
44
44
  teams: CS2TeamValues[] | undefined;
45
45
  }> {
46
+ economy: CS2EconomyInstance;
47
+ item: CS2Item;
48
+ language: CS2ItemLocalization;
46
49
  altName: string | undefined;
47
50
  base: boolean | undefined;
48
51
  baseId: number | undefined;
@@ -74,10 +77,9 @@ export declare class CS2EconomyItem implements Interface<Omit<CS2Item, "contents
74
77
  wearMax: number | undefined;
75
78
  wearMin: number | undefined;
76
79
  private _contents;
77
- private _economy;
78
80
  private _specials;
79
81
  private _teams;
80
- constructor(economyInstance: CS2EconomyInstance, item: CS2Item, language: CS2ItemLocalization);
82
+ constructor(economy: CS2EconomyInstance, item: CS2Item, language: CS2ItemLocalization);
81
83
  set contents(value: number[] | undefined);
82
84
  get contents(): CS2EconomyItem[];
83
85
  get parent(): CS2EconomyItem | undefined;
@@ -118,17 +120,7 @@ export declare class CS2EconomyItem implements Interface<Omit<CS2Item, "contents
118
120
  isSouvenirCase(): boolean;
119
121
  groupContents(): Record<string, CS2EconomyItem[]>;
120
122
  listContents(hideSpecials?: boolean): CS2EconomyItem[];
121
- unlock(): {
122
- attributes: {
123
- containerId: number;
124
- seed: number | undefined;
125
- statTrak: number | undefined;
126
- wear: number | undefined;
127
- };
128
- id: number;
129
- rarity: CS2RaritySoundNameValues;
130
- special: boolean;
131
- };
123
+ unlockContainer(): CS2UnlockedItem;
132
124
  }
133
125
  export declare const CS2Economy: CS2EconomyInstance;
134
126
  export {};
package/dist/economy.js CHANGED
@@ -190,6 +190,9 @@ export class CS2EconomyInstance {
190
190
  }
191
191
  }
192
192
  export class CS2EconomyItem {
193
+ economy;
194
+ item;
195
+ language;
193
196
  altName;
194
197
  base;
195
198
  baseId;
@@ -221,11 +224,12 @@ export class CS2EconomyItem {
221
224
  wearMax;
222
225
  wearMin;
223
226
  _contents;
224
- _economy;
225
227
  _specials;
226
228
  _teams;
227
- constructor(economyInstance, item, language) {
228
- this._economy = economyInstance;
229
+ constructor(economy, item, language) {
230
+ this.economy = economy;
231
+ this.item = item;
232
+ this.language = language;
229
233
  Object.assign(this, item);
230
234
  Object.assign(this, language);
231
235
  assert(typeof this.id === "number");
@@ -238,10 +242,10 @@ export class CS2EconomyItem {
238
242
  }
239
243
  get contents() {
240
244
  this.expectContainer();
241
- return ensure(this._contents).map((id) => this._economy.get(id));
245
+ return ensure(this._contents).map((id) => this.economy.get(id));
242
246
  }
243
247
  get parent() {
244
- return this.baseId !== undefined ? this._economy.get(this.baseId) : undefined;
248
+ return this.baseId !== undefined ? this.economy.get(this.baseId) : undefined;
245
249
  }
246
250
  get rawContents() {
247
251
  return this._contents;
@@ -254,7 +258,7 @@ export class CS2EconomyItem {
254
258
  }
255
259
  get specials() {
256
260
  this.expectContainer();
257
- return this._specials?.map((id) => this._economy.get(id));
261
+ return this._specials?.map((id) => this.economy.get(id));
258
262
  }
259
263
  set teams(value) {
260
264
  this._teams = value;
@@ -395,7 +399,7 @@ export class CS2EconomyItem {
395
399
  (CS2RarityColorOrder[b.rarity] ?? CS2_RARITY_COLOR_DEFAULT));
396
400
  });
397
401
  }
398
- unlock() {
402
+ unlockContainer() {
399
403
  // @see https://www.csgo.com.cn/news/gamebroad/20170911/206155.shtml
400
404
  const contents = this.groupContents();
401
405
  const keys = Object.keys(contents);
package/dist/index.d.ts CHANGED
@@ -2,7 +2,6 @@ export * from "./economy-constants.js";
2
2
  export * from "./economy-container.js";
3
3
  export * from "./economy-types.js";
4
4
  export * from "./economy.js";
5
- export * from "./inventory-instance.js";
6
5
  export * from "./inventory.js";
7
6
  export * from "./items.js";
8
7
  export * from "./keyvalues.js";
package/dist/index.js CHANGED
@@ -6,7 +6,6 @@ export * from "./economy-constants.js";
6
6
  export * from "./economy-container.js";
7
7
  export * from "./economy-types.js";
8
8
  export * from "./economy.js";
9
- export * from "./inventory-instance.js";
10
9
  export * from "./inventory.js";
11
10
  export * from "./items.js";
12
11
  export * from "./keyvalues.js";
@@ -0,0 +1,2 @@
1
+ import { CS2InventoryData } from "./inventory.js";
2
+ export declare function resolveInventoryData(stringValue: string | null | undefined): CS2InventoryData | undefined;
@@ -0,0 +1,74 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Ian Lucas. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { CS2_INVENTORY_VERSION } from "./inventory.js";
6
+ const upgrades = {
7
+ 1: (data) => {
8
+ function walkV0(v0) {
9
+ const v1 = {};
10
+ for (let [key, value] of Object.entries(v0)) {
11
+ switch (key) {
12
+ case "caseid":
13
+ key = "containerId";
14
+ break;
15
+ case "nametag":
16
+ key = "nameTag";
17
+ break;
18
+ case "stattrak":
19
+ key = "statTrak";
20
+ break;
21
+ case "stickers":
22
+ value =
23
+ value !== undefined
24
+ ? Object.fromEntries(value.map((stickerId, slot) => [
25
+ slot,
26
+ {
27
+ id: stickerId,
28
+ wear: v0.stickerswear?.[slot]
29
+ }
30
+ ]))
31
+ : undefined;
32
+ break;
33
+ case "stickerswear":
34
+ continue;
35
+ case "storage":
36
+ value =
37
+ value !== undefined
38
+ ? Object.fromEntries(value.map((v0) => [v0.uid, walkV0(v0)]))
39
+ : undefined;
40
+ break;
41
+ case "uid":
42
+ continue;
43
+ case "updatedat":
44
+ key = "updatedAt";
45
+ break;
46
+ }
47
+ v1[key] = value;
48
+ }
49
+ return v1;
50
+ }
51
+ return {
52
+ items: Object.fromEntries(data.map((v0) => [v0.uid, walkV0(v0)])),
53
+ version: 1
54
+ };
55
+ }
56
+ };
57
+ export function resolveInventoryData(stringValue) {
58
+ try {
59
+ if (!stringValue) {
60
+ return undefined;
61
+ }
62
+ let value = JSON.parse(stringValue);
63
+ const currentVersion = value.version ?? 0;
64
+ for (let i = currentVersion + 1; i <= CS2_INVENTORY_VERSION; i++) {
65
+ if (upgrades[i]) {
66
+ value = upgrades[i](value);
67
+ }
68
+ }
69
+ return value;
70
+ }
71
+ catch {
72
+ return undefined;
73
+ }
74
+ }
@@ -1,4 +1,114 @@
1
- import { CS2InventoryData } from "./inventory-instance.js";
1
+ import { CS2ItemTypeValues, CS2UnlockedItem } from "./economy-types.js";
2
+ import { CS2EconomyInstance, CS2EconomyItem } from "./economy.js";
3
+ import { CS2TeamValues } from "./teams.js";
4
+ import { Interface, MapValue } from "./utils.js";
5
+ export interface CS2BaseInventoryItem {
6
+ containerId?: number;
7
+ equipped?: boolean;
8
+ equippedCT?: boolean;
9
+ equippedT?: boolean;
10
+ id: number;
11
+ nameTag?: string;
12
+ patches?: Record<number, number>;
13
+ seed?: number;
14
+ statTrak?: number;
15
+ stickers?: Record<number, {
16
+ id: number;
17
+ wear?: number;
18
+ x?: number;
19
+ y?: number;
20
+ }>;
21
+ storage?: Record<number, CS2BaseInventoryItem>;
22
+ updatedAt?: number;
23
+ wear?: number;
24
+ }
25
+ export interface CS2InventoryData {
26
+ items: Record<number, CS2BaseInventoryItem>;
27
+ version: number;
28
+ }
29
+ export interface CS2InventoryOptions {
30
+ maxItems: number;
31
+ storageUnitMaxItems: number;
32
+ }
33
+ export interface CS2InventorySpec extends CS2InventoryOptions {
34
+ economy: CS2EconomyInstance;
35
+ data: CS2InventoryData;
36
+ }
37
+ export declare const CS2_INVENTORY_VERSION = 1;
38
+ export declare const CS2_INVENTORY_TIMESTAMP = 1707696138408;
39
+ export declare const CS2_INVENTORY_EQUIPPABLE_ITEMS: CS2ItemTypeValues[];
40
+ export declare function getTimestamp(): number;
41
+ export declare function getNextUid(map: Map<number, unknown>): number;
2
42
  export declare class CS2Inventory {
3
- static parse(stringValue: string | null | undefined): CS2InventoryData | undefined;
43
+ private economy;
44
+ private items;
45
+ readonly options: Readonly<CS2InventoryOptions>;
46
+ static parse(stringValue: string | undefined | null): CS2InventoryData | undefined;
47
+ constructor({ economy, data, maxItems, storageUnitMaxItems }: Partial<CS2InventorySpec>);
48
+ private validateEquippable;
49
+ private validateStickers;
50
+ private validatePatches;
51
+ validateBaseInventoryItem({ id, nameTag, patches, seed, statTrak, stickers, wear }: CS2BaseInventoryItem): void;
52
+ private toInventoryItems;
53
+ private toBaseInventoryItem;
54
+ private toBaseInventoryItems;
55
+ stringify(): string;
56
+ isFull(): boolean;
57
+ add(item: CS2BaseInventoryItem): this;
58
+ private addInventoryItem;
59
+ addWithNametag(nameTagUid: number, id: number, nameTag: string): this;
60
+ addWithSticker(stickerUid: number, id: number, stickerIndex: number): this;
61
+ edit(itemUid: number, properties: Partial<CS2BaseInventoryItem>): this;
62
+ equip(itemUid: number, team?: CS2TeamValues): this;
63
+ unequip(uid: number, team?: CS2TeamValues): this;
64
+ unlockContainer(unlockedItem: CS2UnlockedItem, containerUid: number, keyUid?: number): this;
65
+ renameItem(nameTagUid: number, renameableUid: number, nameTag?: string): this;
66
+ renameStorageUnit(storageUid: number, nameTag: string): this;
67
+ isStorageUnitFull(storageUid: number): boolean;
68
+ getStorageUnitSize(storageUid: number): number;
69
+ isStorageUnitFilled(storageUid: number): boolean;
70
+ canDepositToStorageUnit(storageUid: number, size?: number): boolean;
71
+ canRetrieveFromStorageUnit(storageUid: number, size?: number): boolean;
72
+ getStorageUnitItems(storageUid: number): CS2InventoryItem[];
73
+ depositToStorageUnit(storageUid: number, depositUids: number[]): this;
74
+ retrieveFromStorageUnit(storageUid: number, retrieveUids: number[]): this;
75
+ applyItemSticker(targetUid: number, stickerUid: number, stickerIndex: number): this;
76
+ scrapeItemSticker(targetUid: number, stickerIndex: number): this;
77
+ applyItemPatch(targetUid: number, patchUid: number, patchIndex: number): this;
78
+ removeItemPatch(targetUid: number, patchIndex: number): this;
79
+ incrementItemStatTrak(targetUid: number): this;
80
+ swapItemsStatTrak(statTrakSwapToolUid: number, fromUid: number, toUid: number): this;
81
+ remove(uid: number): this;
82
+ removeAll(): this;
83
+ get(uid: number): CS2InventoryItem;
84
+ getAll(): CS2InventoryItem[];
85
+ setAll(items: Map<number, CS2InventoryItem>): this;
86
+ size(): number;
87
+ move(): CS2Inventory;
88
+ }
89
+ export declare class CS2InventoryItem extends CS2EconomyItem implements Interface<Omit<CS2BaseInventoryItem, "patches" | "stickers" | "storage">> {
90
+ private inventory;
91
+ uid: number;
92
+ containerId: number | undefined;
93
+ equipped: boolean | undefined;
94
+ equippedCT: boolean | undefined;
95
+ equippedT: boolean | undefined;
96
+ nameTag: string | undefined;
97
+ patches: Map<number, number> | undefined;
98
+ seed: number | undefined;
99
+ statTrak: number | undefined;
100
+ stickers: Map<number, {
101
+ id: number;
102
+ wear?: number;
103
+ x?: number;
104
+ y?: number;
105
+ }> | undefined;
106
+ storage: Map<number, CS2InventoryItem> | undefined;
107
+ updatedAt: number | undefined;
108
+ wear: number | undefined;
109
+ private assign;
110
+ constructor(inventory: CS2Inventory, uid: number, baseInventoryItem: CS2BaseInventoryItem, { economy, item, language }: CS2EconomyItem);
111
+ edit(...sources: Partial<CS2BaseInventoryItem>[]): void;
4
112
  }
113
+ export declare function mapStickers(stickers: CS2InventoryItem["stickers"]): [number, MapValue<typeof stickers>][];
114
+ export declare function mapPatches(patches: CS2InventoryItem["patches"]): [number, number][];