@ianlucas/cs2-lib 5.0.0-rc.1 → 5.0.0-rc.11

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: {
@@ -75,5 +75,16 @@ export interface CS2ItemLocalization {
75
75
  name: string;
76
76
  tournamentDesc?: string | undefined;
77
77
  }
78
- export type CS2ItemLocalizationMap = Record<number, CS2ItemLocalization | undefined>;
78
+ export type CS2ItemLocalizationMap = Record<string, 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;
@@ -87,6 +89,7 @@ export declare class CS2EconomyItem implements Interface<Omit<CS2Item, "contents
87
89
  get specials(): CS2EconomyItem[] | undefined;
88
90
  set teams(value: CS2ItemTeamValues);
89
91
  get teams(): CS2TeamValues[] | undefined;
92
+ isStub(): boolean;
90
93
  isC4(): boolean;
91
94
  isAgent(): boolean;
92
95
  isSticker(): boolean;
@@ -118,17 +121,7 @@ export declare class CS2EconomyItem implements Interface<Omit<CS2Item, "contents
118
121
  isSouvenirCase(): boolean;
119
122
  groupContents(): Record<string, CS2EconomyItem[]>;
120
123
  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
- };
124
+ unlockContainer(): CS2UnlockedItem;
132
125
  }
133
126
  export declare const CS2Economy: CS2EconomyInstance;
134
127
  export {};
package/dist/economy.js CHANGED
@@ -27,9 +27,6 @@ export class CS2EconomyInstance {
27
27
  this.stickers.clear();
28
28
  this.itemsAsArray = [];
29
29
  for (const item of items) {
30
- if (item.type === CS2ItemType.Stub) {
31
- continue;
32
- }
33
30
  const economyItem = new CS2EconomyItem(this, item, ensure(language[item.id]));
34
31
  this.items.set(item.id, economyItem);
35
32
  if (economyItem.isSticker()) {
@@ -190,6 +187,9 @@ export class CS2EconomyInstance {
190
187
  }
191
188
  }
192
189
  export class CS2EconomyItem {
190
+ economy;
191
+ item;
192
+ language;
193
193
  altName;
194
194
  base;
195
195
  baseId;
@@ -221,27 +221,32 @@ export class CS2EconomyItem {
221
221
  wearMax;
222
222
  wearMin;
223
223
  _contents;
224
- _economy;
225
224
  _specials;
226
225
  _teams;
227
- constructor(economyInstance, item, language) {
228
- this._economy = economyInstance;
226
+ constructor(economy, item, language) {
227
+ this.economy = economy;
228
+ this.item = item;
229
+ this.language = language;
229
230
  Object.assign(this, item);
230
231
  Object.assign(this, language);
231
232
  assert(typeof this.id === "number");
233
+ assert(typeof this.id === "number");
232
234
  assert(this.name);
233
- assert(this.rarity);
234
235
  assert(this.type);
236
+ if (item.type !== CS2ItemType.Stub) {
237
+ // @todo: add a rarity to stubs.
238
+ assert(this.rarity);
239
+ }
235
240
  }
236
241
  set contents(value) {
237
242
  this._contents = value;
238
243
  }
239
244
  get contents() {
240
245
  this.expectContainer();
241
- return ensure(this._contents).map((id) => this._economy.get(id));
246
+ return ensure(this._contents).map((id) => this.economy.get(id));
242
247
  }
243
248
  get parent() {
244
- return this.baseId !== undefined ? this._economy.get(this.baseId) : undefined;
249
+ return this.baseId !== undefined ? this.economy.get(this.baseId) : undefined;
245
250
  }
246
251
  get rawContents() {
247
252
  return this._contents;
@@ -254,7 +259,7 @@ export class CS2EconomyItem {
254
259
  }
255
260
  get specials() {
256
261
  this.expectContainer();
257
- return this._specials?.map((id) => this._economy.get(id));
262
+ return this._specials?.map((id) => this.economy.get(id));
258
263
  }
259
264
  set teams(value) {
260
265
  this._teams = value;
@@ -271,6 +276,9 @@ export class CS2EconomyItem {
271
276
  return undefined;
272
277
  }
273
278
  }
279
+ isStub() {
280
+ return this.type === CS2ItemType.Stub;
281
+ }
274
282
  isC4() {
275
283
  return this.category === "c4";
276
284
  }
@@ -395,7 +403,7 @@ export class CS2EconomyItem {
395
403
  (CS2RarityColorOrder[b.rarity] ?? CS2_RARITY_COLOR_DEFAULT));
396
404
  });
397
405
  }
398
- unlock() {
406
+ unlockContainer() {
399
407
  // @see https://www.csgo.com.cn/news/gamebroad/20170911/206155.shtml
400
408
  const contents = this.groupContents();
401
409
  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,76 @@
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
25
+ .filter((stickerId) => stickerId !== 0)
26
+ .map((stickerId, slot) => [
27
+ slot,
28
+ {
29
+ id: stickerId,
30
+ wear: v0.stickerswear?.[slot] || undefined
31
+ }
32
+ ]))
33
+ : undefined;
34
+ break;
35
+ case "stickerswear":
36
+ continue;
37
+ case "storage":
38
+ value =
39
+ value !== undefined
40
+ ? Object.fromEntries(value.map((v0) => [v0.uid, walkV0(v0)]))
41
+ : undefined;
42
+ break;
43
+ case "uid":
44
+ continue;
45
+ case "updatedat":
46
+ key = "updatedAt";
47
+ break;
48
+ }
49
+ v1[key] = value;
50
+ }
51
+ return v1;
52
+ }
53
+ return {
54
+ items: Object.fromEntries(data.map((v0) => [v0.uid, walkV0(v0)])),
55
+ version: 1
56
+ };
57
+ }
58
+ };
59
+ export function resolveInventoryData(stringValue) {
60
+ try {
61
+ if (!stringValue) {
62
+ return undefined;
63
+ }
64
+ let value = JSON.parse(stringValue);
65
+ const currentVersion = value.version ?? 0;
66
+ for (let i = currentVersion + 1; i <= CS2_INVENTORY_VERSION; i++) {
67
+ if (upgrades[i]) {
68
+ value = upgrades[i](value);
69
+ }
70
+ }
71
+ return value;
72
+ }
73
+ catch {
74
+ return undefined;
75
+ }
76
+ }
@@ -1,4 +1,117 @@
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 toBaseInventoryItems;
54
+ stringify(): string;
55
+ isFull(): boolean;
56
+ add(item: CS2BaseInventoryItem): this;
57
+ private addInventoryItem;
58
+ addWithNametag(nameTagUid: number, id: number, nameTag: string): this;
59
+ addWithSticker(stickerUid: number, id: number, stickerIndex: number): this;
60
+ edit(itemUid: number, properties: Partial<CS2BaseInventoryItem>): this;
61
+ equip(itemUid: number, team?: CS2TeamValues): this;
62
+ unequip(uid: number, team?: CS2TeamValues): this;
63
+ unlockContainer(unlockedItem: CS2UnlockedItem, containerUid: number, keyUid?: number): this;
64
+ renameItem(nameTagUid: number, renameableUid: number, nameTag?: string): this;
65
+ renameStorageUnit(storageUid: number, nameTag: string): this;
66
+ isStorageUnitFull(storageUid: number): boolean;
67
+ getStorageUnitSize(storageUid: number): number;
68
+ isStorageUnitFilled(storageUid: number): boolean;
69
+ canDepositToStorageUnit(storageUid: number, size?: number): boolean;
70
+ canRetrieveFromStorageUnit(storageUid: number, size?: number): boolean;
71
+ getStorageUnitItems(storageUid: number): CS2InventoryItem[];
72
+ depositToStorageUnit(storageUid: number, depositUids: number[]): this;
73
+ retrieveFromStorageUnit(storageUid: number, retrieveUids: number[]): this;
74
+ applyItemSticker(targetUid: number, stickerUid: number, stickerIndex: number): this;
75
+ scrapeItemSticker(targetUid: number, stickerIndex: number): this;
76
+ applyItemPatch(targetUid: number, patchUid: number, patchIndex: number): this;
77
+ removeItemPatch(targetUid: number, patchIndex: number): this;
78
+ incrementItemStatTrak(targetUid: number): this;
79
+ swapItemsStatTrak(statTrakSwapToolUid: number, fromUid: number, toUid: number): this;
80
+ remove(uid: number): this;
81
+ removeAll(): this;
82
+ get(uid: number): CS2InventoryItem;
83
+ getAll(): CS2InventoryItem[];
84
+ getAllAsBase(): CS2BaseInventoryItem[];
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;
112
+ allStickers(): [number, MapValue<CS2InventoryItem["stickers"]> | undefined][];
113
+ someStickers(): [number, MapValue<CS2InventoryItem["stickers"]>][];
114
+ allPatches(): [number, number | undefined][];
115
+ somePatches(): [number, number][];
116
+ asBase(): CS2BaseInventoryItem;
4
117
  }