@ianlucas/cs2-lib 1.0.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.
- package/License.txt +21 -0
- package/README.md +26 -0
- package/assets/translations/items-brazilian.json +1 -0
- package/assets/translations/items-bulgarian.json +1 -0
- package/assets/translations/items-czech.json +1 -0
- package/assets/translations/items-danish.json +1 -0
- package/assets/translations/items-dutch.json +1 -0
- package/assets/translations/items-english.json +1 -0
- package/assets/translations/items-finnish.json +1 -0
- package/assets/translations/items-french.json +1 -0
- package/assets/translations/items-german.json +1 -0
- package/assets/translations/items-greek.json +1 -0
- package/assets/translations/items-hungarian.json +1 -0
- package/assets/translations/items-italian.json +1 -0
- package/assets/translations/items-japanese.json +1 -0
- package/assets/translations/items-koreana.json +1 -0
- package/assets/translations/items-latam.json +1 -0
- package/assets/translations/items-norwegian.json +1 -0
- package/assets/translations/items-polish.json +1 -0
- package/assets/translations/items-portuguese.json +1 -0
- package/assets/translations/items-romanian.json +1 -0
- package/assets/translations/items-russian.json +1 -0
- package/assets/translations/items-schinese.json +1 -0
- package/assets/translations/items-spanish.json +1 -0
- package/assets/translations/items-swedish.json +1 -0
- package/assets/translations/items-tchinese.json +1 -0
- package/assets/translations/items-thai.json +1 -0
- package/assets/translations/items-turkish.json +1 -0
- package/assets/translations/items-ukrainian.json +1 -0
- package/assets/translations/items-vietnamese.json +1 -0
- package/dist/economy-case.d.ts +17 -0
- package/dist/economy-case.js +51 -0
- package/dist/economy.d.ts +140 -0
- package/dist/economy.js +467 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +12 -0
- package/dist/inventory.d.ts +82 -0
- package/dist/inventory.js +379 -0
- package/dist/items.d.ts +2 -0
- package/dist/items.js +7 -0
- package/dist/keyvalues.d.ts +1 -0
- package/dist/keyvalues.js +93 -0
- package/dist/maps.d.ts +20 -0
- package/dist/maps.js +88 -0
- package/dist/teams.d.ts +6 -0
- package/dist/teams.js +13 -0
- package/dist/util.d.ts +5 -0
- package/dist/util.js +15 -0
- package/dist/veto.d.ts +26 -0
- package/dist/veto.js +111 -0
- package/package.json +38 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
export * from "./economy-case.js";
|
|
6
|
+
export * from "./economy.js";
|
|
7
|
+
export * from "./inventory.js";
|
|
8
|
+
export * from "./items.js";
|
|
9
|
+
export * from "./keyvalues.js";
|
|
10
|
+
export * from "./maps.js";
|
|
11
|
+
export * from "./teams.js";
|
|
12
|
+
export * from "./veto.js";
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { CS_EconomyInstance, CS_Item } from "./economy.js";
|
|
2
|
+
import { CS_Team } from "./teams.js";
|
|
3
|
+
export interface CS_BaseInventoryItem {
|
|
4
|
+
caseid?: number;
|
|
5
|
+
equipped?: boolean;
|
|
6
|
+
equippedCT?: boolean;
|
|
7
|
+
equippedT?: boolean;
|
|
8
|
+
id: number;
|
|
9
|
+
nametag?: string;
|
|
10
|
+
seed?: number;
|
|
11
|
+
stattrak?: number;
|
|
12
|
+
stickers?: number[];
|
|
13
|
+
stickerswear?: number[];
|
|
14
|
+
storage?: CS_BaseInventoryItem[];
|
|
15
|
+
uid: number;
|
|
16
|
+
updatedat?: number;
|
|
17
|
+
wear?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface CS_InventoryItem extends Omit<CS_BaseInventoryItem, "storage"> {
|
|
20
|
+
data: CS_Item;
|
|
21
|
+
storage?: Map<number, CS_InventoryItem>;
|
|
22
|
+
}
|
|
23
|
+
export interface CS_InventoryOptions {
|
|
24
|
+
maxItems: number;
|
|
25
|
+
storageUnitMaxItems: number;
|
|
26
|
+
}
|
|
27
|
+
export interface CS_InventorySpec extends CS_InventoryOptions {
|
|
28
|
+
economy: CS_EconomyInstance;
|
|
29
|
+
items: CS_BaseInventoryItem[] | Map<number, CS_InventoryItem>;
|
|
30
|
+
}
|
|
31
|
+
export declare const CS_INVENTORY_TIMESTAMP = 1707696138408;
|
|
32
|
+
export declare const CS_INVENTORY_STICKERS: readonly [0, 0, 0, 0];
|
|
33
|
+
export declare const CS_INVENTORY_STICKERS_WEAR: readonly [0, 0, 0, 0];
|
|
34
|
+
export declare const CS_INVENTORY_EQUIPPABLE_ITEMS: string[];
|
|
35
|
+
export declare function CS_getTimestamp(): number;
|
|
36
|
+
export declare function CS_getNextUid(map: Map<number, unknown>): number;
|
|
37
|
+
export declare class CS_Inventory {
|
|
38
|
+
private validateStorage;
|
|
39
|
+
private validateEquippable;
|
|
40
|
+
private validateBaseInventoryItem;
|
|
41
|
+
private asPartialInventoryItem;
|
|
42
|
+
private asInventoryItem;
|
|
43
|
+
private asPartialBaseInventoryItem;
|
|
44
|
+
private asBaseInventoryItem;
|
|
45
|
+
private asInventoryItemMap;
|
|
46
|
+
private economy;
|
|
47
|
+
private items;
|
|
48
|
+
readonly options: Readonly<CS_InventoryOptions>;
|
|
49
|
+
constructor({ economy, items, maxItems, storageUnitMaxItems }: Partial<CS_InventorySpec>);
|
|
50
|
+
isFull(): boolean;
|
|
51
|
+
add(item: Omit<CS_BaseInventoryItem, "uid"> & {
|
|
52
|
+
uid?: number;
|
|
53
|
+
}): this;
|
|
54
|
+
private addInventoryItem;
|
|
55
|
+
addWithNametag(toolUid: number, id: number, nametag: string): this;
|
|
56
|
+
addWithSticker(stickerUid: number, id: number, stickerIndex: number): this;
|
|
57
|
+
edit(itemUid: number, attributes: Partial<CS_BaseInventoryItem>): this;
|
|
58
|
+
equip(itemUid: number, team?: CS_Team): this;
|
|
59
|
+
unequip(uid: number, team?: CS_Team): this;
|
|
60
|
+
unlockCase(unlockedItem: ReturnType<typeof this.economy.unlockCase>, caseUid: number, keyUid?: number): this;
|
|
61
|
+
renameItem(toolUid: number, renameableUid: number, nametag?: string): this;
|
|
62
|
+
renameStorageUnit(storageUid: number, nametag: string): this;
|
|
63
|
+
isStorageUnitFull(storageUid: number): boolean;
|
|
64
|
+
isStorageUnitFilled(storageUid: number): boolean;
|
|
65
|
+
canDepositToStorageUnit(storageUid: number, size?: number): boolean;
|
|
66
|
+
canRetrieveFromStorageUnit(storageUid: number, size?: number): boolean;
|
|
67
|
+
getStorageUnitSize(storageUid: number): number;
|
|
68
|
+
getStorageUnitItems(storageUid: number): CS_InventoryItem[];
|
|
69
|
+
depositToStorageUnit(storageUid: number, depositUids: number[]): this;
|
|
70
|
+
retrieveFromStorageUnit(storageUid: number, retrieveUids: number[]): this;
|
|
71
|
+
applyItemSticker(targetUid: number, stickerUid: number, stickerIndex: number): this;
|
|
72
|
+
scrapeItemSticker(targetUid: number, stickerIndex: number): this;
|
|
73
|
+
incrementItemStatTrak(targetUid: number): this;
|
|
74
|
+
swapItemsStatTrak(toolUid: number, fromUid: number, toUid: number): this;
|
|
75
|
+
remove(uid: number): this;
|
|
76
|
+
removeAll(): this;
|
|
77
|
+
get(uid: number): CS_InventoryItem;
|
|
78
|
+
getAll(): CS_InventoryItem[];
|
|
79
|
+
size(): number;
|
|
80
|
+
move(): CS_Inventory;
|
|
81
|
+
export(): CS_BaseInventoryItem[];
|
|
82
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
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 { CS_Economy, CS_MAX_STATTRAK, CS_MAX_WEAR, CS_NONE, CS_STICKER_WEAR_FACTOR } from "./economy.js";
|
|
6
|
+
import { CS_TEAM_CT, CS_TEAM_T } from "./teams.js";
|
|
7
|
+
import { assert, float } from "./util.js";
|
|
8
|
+
export const CS_INVENTORY_TIMESTAMP = 1707696138408;
|
|
9
|
+
export const CS_INVENTORY_STICKERS = [CS_NONE, CS_NONE, CS_NONE, CS_NONE];
|
|
10
|
+
export const CS_INVENTORY_STICKERS_WEAR = [CS_NONE, CS_NONE, CS_NONE, CS_NONE];
|
|
11
|
+
export const CS_INVENTORY_EQUIPPABLE_ITEMS = [
|
|
12
|
+
"agent",
|
|
13
|
+
"glove",
|
|
14
|
+
"graffiti",
|
|
15
|
+
"melee",
|
|
16
|
+
"musickit",
|
|
17
|
+
"patch",
|
|
18
|
+
"pin",
|
|
19
|
+
"weapon"
|
|
20
|
+
];
|
|
21
|
+
export function CS_getTimestamp() {
|
|
22
|
+
return Math.ceil((Date.now() - CS_INVENTORY_TIMESTAMP) / 1000);
|
|
23
|
+
}
|
|
24
|
+
export function CS_getNextUid(map) {
|
|
25
|
+
let uid = 0;
|
|
26
|
+
while (true) {
|
|
27
|
+
if (!map.has(uid)) {
|
|
28
|
+
return uid;
|
|
29
|
+
}
|
|
30
|
+
uid++;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class CS_Inventory {
|
|
34
|
+
validateStorage(storage) {
|
|
35
|
+
if (storage !== undefined) {
|
|
36
|
+
let uids = new Set();
|
|
37
|
+
for (const item of storage) {
|
|
38
|
+
assert(!uids.has(item.uid), "Duplicate storage unit item uid.");
|
|
39
|
+
assert(!this.economy.isStorageUnitTool(item.id), "Storage unit cannot be stored in storage unit.");
|
|
40
|
+
this.validateBaseInventoryItem(item);
|
|
41
|
+
uids.add(item.uid);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
validateEquippable(item) {
|
|
46
|
+
if (this.economy.isGlove(item)) {
|
|
47
|
+
assert(!item.base, "Glove base cannot be equipped.");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
validateBaseInventoryItem({ id, nametag, seed, stattrak, stickers, stickerswear, storage, wear }) {
|
|
51
|
+
const item = this.economy.getById(id);
|
|
52
|
+
this.validateEquippable(item);
|
|
53
|
+
this.economy.validateWear(wear, item);
|
|
54
|
+
this.economy.validateSeed(seed, item);
|
|
55
|
+
this.economy.validateStickers(stickers, stickerswear, item);
|
|
56
|
+
this.economy.validateNametag(nametag, item);
|
|
57
|
+
this.economy.validateStatTrak(stattrak, item);
|
|
58
|
+
if (storage !== undefined) {
|
|
59
|
+
this.economy.expectStorageUnitTool(item);
|
|
60
|
+
this.validateStorage(storage);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
asPartialInventoryItem({ storage, ...base }) {
|
|
64
|
+
const item = { ...base };
|
|
65
|
+
if (base.id) {
|
|
66
|
+
item.data = this.economy.getById(base.id);
|
|
67
|
+
}
|
|
68
|
+
if (storage) {
|
|
69
|
+
item.storage = new Map(storage.map((item) => [item.uid, this.asInventoryItem(item)]));
|
|
70
|
+
}
|
|
71
|
+
return item;
|
|
72
|
+
}
|
|
73
|
+
asInventoryItem(base) {
|
|
74
|
+
return this.asPartialInventoryItem(base);
|
|
75
|
+
}
|
|
76
|
+
asPartialBaseInventoryItem({ data, storage, ...rest }) {
|
|
77
|
+
const base = { ...rest };
|
|
78
|
+
if (storage) {
|
|
79
|
+
base.storage = Array.from(storage.values()).map((item) => this.asBaseInventoryItem(item));
|
|
80
|
+
}
|
|
81
|
+
return base;
|
|
82
|
+
}
|
|
83
|
+
asBaseInventoryItem(item) {
|
|
84
|
+
return this.asPartialBaseInventoryItem(item);
|
|
85
|
+
}
|
|
86
|
+
asInventoryItemMap(items) {
|
|
87
|
+
const map = new Map();
|
|
88
|
+
for (const item of items) {
|
|
89
|
+
assert(!map.has(item.uid), "Duplicate inventory item uid.");
|
|
90
|
+
map.set(item.uid, this.asInventoryItem(item));
|
|
91
|
+
}
|
|
92
|
+
return map;
|
|
93
|
+
}
|
|
94
|
+
economy;
|
|
95
|
+
items;
|
|
96
|
+
options;
|
|
97
|
+
constructor({ economy, items, maxItems, storageUnitMaxItems }) {
|
|
98
|
+
this.economy = economy ?? CS_Economy;
|
|
99
|
+
this.items = items !== undefined ? (items instanceof Map ? items : this.asInventoryItemMap(items)) : new Map();
|
|
100
|
+
this.options = {
|
|
101
|
+
maxItems: maxItems ?? 256,
|
|
102
|
+
storageUnitMaxItems: storageUnitMaxItems ?? 32
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
isFull() {
|
|
106
|
+
return this.items.size >= this.options.maxItems;
|
|
107
|
+
}
|
|
108
|
+
add(item) {
|
|
109
|
+
assert(!this.isFull(), "Inventory is full.");
|
|
110
|
+
this.validateBaseInventoryItem(item);
|
|
111
|
+
const uid = CS_getNextUid(this.items);
|
|
112
|
+
this.items.set(uid, this.asInventoryItem(Object.assign(item, {
|
|
113
|
+
equipped: undefined,
|
|
114
|
+
equippedCT: undefined,
|
|
115
|
+
equippedT: undefined,
|
|
116
|
+
uid,
|
|
117
|
+
updatedat: CS_getTimestamp()
|
|
118
|
+
})));
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
addInventoryItem(item) {
|
|
122
|
+
assert(!this.isFull(), "Inventory is full.");
|
|
123
|
+
this.validateBaseInventoryItem(this.asBaseInventoryItem(item));
|
|
124
|
+
const uid = CS_getNextUid(this.items);
|
|
125
|
+
this.items.set(uid, Object.assign(item, {
|
|
126
|
+
equipped: undefined,
|
|
127
|
+
equippedCT: undefined,
|
|
128
|
+
equippedT: undefined,
|
|
129
|
+
uid,
|
|
130
|
+
updatedat: CS_getTimestamp()
|
|
131
|
+
}));
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
addWithNametag(toolUid, id, nametag) {
|
|
135
|
+
const tool = this.get(toolUid);
|
|
136
|
+
this.economy.expectNametagTool(tool.data);
|
|
137
|
+
this.economy.requireNametag(nametag);
|
|
138
|
+
this.items.delete(toolUid);
|
|
139
|
+
this.add({ id, nametag });
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
addWithSticker(stickerUid, id, stickerIndex) {
|
|
143
|
+
const sticker = this.get(stickerUid);
|
|
144
|
+
this.economy.expectSticker(sticker.data);
|
|
145
|
+
this.items.delete(stickerUid);
|
|
146
|
+
this.add({
|
|
147
|
+
id,
|
|
148
|
+
stickers: CS_INVENTORY_STICKERS.map((_, index) => (index === stickerIndex ? sticker.id : _))
|
|
149
|
+
});
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
edit(itemUid, attributes) {
|
|
153
|
+
const item = this.get(itemUid);
|
|
154
|
+
assert(!attributes.uid || item.uid === attributes.uid, "Item uid cannot be modified.");
|
|
155
|
+
assert(!attributes.id || item.id === attributes.id, "Item id cannot be modified.");
|
|
156
|
+
this.validateBaseInventoryItem({ ...attributes, id: item.id });
|
|
157
|
+
Object.assign(item, this.asPartialInventoryItem(attributes), {
|
|
158
|
+
updatedat: CS_getTimestamp()
|
|
159
|
+
});
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
equip(itemUid, team) {
|
|
163
|
+
const item = this.get(itemUid);
|
|
164
|
+
assert(item.equipped === undefined, "Item is already equipped.");
|
|
165
|
+
assert(team !== CS_TEAM_CT || item.equippedCT === undefined, "Item is already equipped to CT.");
|
|
166
|
+
assert(team !== CS_TEAM_T || item.equippedT === undefined, "Item is already equipped to T.");
|
|
167
|
+
assert(CS_INVENTORY_EQUIPPABLE_ITEMS.includes(item.data.type), "Item is not equippable.");
|
|
168
|
+
assert(team === undefined || item.data.teams?.includes(team), "Item cannot be equipped to this team.");
|
|
169
|
+
assert(team !== undefined || item.data.teams === undefined, "Item cannot be equipped to any team.");
|
|
170
|
+
for (const [otherUid, otherItem] of this.items) {
|
|
171
|
+
if (itemUid === otherUid) {
|
|
172
|
+
otherItem.equipped = team === undefined ? true : undefined;
|
|
173
|
+
otherItem.equippedCT = team === CS_TEAM_CT ? true : otherItem.equippedCT;
|
|
174
|
+
otherItem.equippedT = team === CS_TEAM_T ? true : otherItem.equippedT;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
if (otherItem.data.type === item.data.type &&
|
|
178
|
+
(item.data.type !== "weapon" || otherItem.data.model === item.data.model)) {
|
|
179
|
+
otherItem.equipped = team === undefined ? undefined : otherItem.equipped;
|
|
180
|
+
otherItem.equippedCT = team === CS_TEAM_CT ? undefined : otherItem.equippedCT;
|
|
181
|
+
otherItem.equippedT = team === CS_TEAM_T ? undefined : otherItem.equippedT;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
unequip(uid, team) {
|
|
188
|
+
const item = this.get(uid);
|
|
189
|
+
item.equipped = team === undefined ? undefined : item.equipped;
|
|
190
|
+
item.equippedCT = team === CS_TEAM_CT ? undefined : item.equippedCT;
|
|
191
|
+
item.equippedT = team === CS_TEAM_T ? undefined : item.equippedT;
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
unlockCase(unlockedItem, caseUid, keyUid) {
|
|
195
|
+
const caseItem = this.get(caseUid);
|
|
196
|
+
this.economy.validateUnlockedItem(caseItem.data, unlockedItem);
|
|
197
|
+
const keyItem = keyUid !== undefined ? this.get(keyUid) : undefined;
|
|
198
|
+
this.economy.validateCaseKey(caseItem.data, keyItem?.data);
|
|
199
|
+
this.items.delete(caseUid);
|
|
200
|
+
if (keyUid !== undefined) {
|
|
201
|
+
this.items.delete(keyUid);
|
|
202
|
+
}
|
|
203
|
+
this.add({
|
|
204
|
+
id: unlockedItem.id,
|
|
205
|
+
...unlockedItem.attributes,
|
|
206
|
+
updatedat: CS_getTimestamp()
|
|
207
|
+
});
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
renameItem(toolUid, renameableUid, nametag) {
|
|
211
|
+
nametag = this.economy.trimNametag(nametag);
|
|
212
|
+
const tool = this.get(toolUid);
|
|
213
|
+
this.economy.expectNametagTool(tool.data);
|
|
214
|
+
const renameable = this.get(renameableUid);
|
|
215
|
+
this.economy.validateNametag(nametag, renameable.data);
|
|
216
|
+
renameable.nametag = nametag;
|
|
217
|
+
renameable.updatedat = CS_getTimestamp();
|
|
218
|
+
this.items.delete(toolUid);
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
renameStorageUnit(storageUid, nametag) {
|
|
222
|
+
const trimmed = this.economy.trimNametag(nametag);
|
|
223
|
+
const storageUnit = this.get(storageUid);
|
|
224
|
+
this.economy.expectStorageUnitTool(storageUnit.data);
|
|
225
|
+
this.economy.requireNametag(trimmed);
|
|
226
|
+
storageUnit.nametag = trimmed;
|
|
227
|
+
storageUnit.updatedat = CS_getTimestamp();
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
isStorageUnitFull(storageUid) {
|
|
231
|
+
return this.get(storageUid).storage?.size === this.options.storageUnitMaxItems;
|
|
232
|
+
}
|
|
233
|
+
isStorageUnitFilled(storageUid) {
|
|
234
|
+
return this.getStorageUnitSize(storageUid) > 0;
|
|
235
|
+
}
|
|
236
|
+
canDepositToStorageUnit(storageUid, size = 1) {
|
|
237
|
+
return (this.get(storageUid).nametag !== undefined &&
|
|
238
|
+
this.getStorageUnitSize(storageUid) + size <= this.options.storageUnitMaxItems);
|
|
239
|
+
}
|
|
240
|
+
canRetrieveFromStorageUnit(storageUid, size = 1) {
|
|
241
|
+
return this.getStorageUnitSize(storageUid) - size >= 0 && this.size() + size <= this.options.maxItems;
|
|
242
|
+
}
|
|
243
|
+
getStorageUnitSize(storageUid) {
|
|
244
|
+
return this.get(storageUid).storage?.size ?? 0;
|
|
245
|
+
}
|
|
246
|
+
getStorageUnitItems(storageUid) {
|
|
247
|
+
return Array.from(this.get(storageUid).storage?.values() ?? []);
|
|
248
|
+
}
|
|
249
|
+
depositToStorageUnit(storageUid, depositUids) {
|
|
250
|
+
const item = this.get(storageUid);
|
|
251
|
+
this.economy.expectStorageUnitTool(item.data);
|
|
252
|
+
assert(depositUids.length > 0, "No items to deposit.");
|
|
253
|
+
assert(this.canDepositToStorageUnit(storageUid, depositUids.length), "Cannot deposit to storage unit.");
|
|
254
|
+
for (const sourceUid of depositUids) {
|
|
255
|
+
assert(!this.economy.isStorageUnitTool(this.get(sourceUid).data), "Cannot deposit storage unit.");
|
|
256
|
+
}
|
|
257
|
+
const storage = item.storage ?? new Map();
|
|
258
|
+
for (const sourceUid of depositUids) {
|
|
259
|
+
const uid = CS_getNextUid(storage);
|
|
260
|
+
storage.set(uid, Object.assign(this.get(sourceUid), {
|
|
261
|
+
equipped: undefined,
|
|
262
|
+
equippedCT: undefined,
|
|
263
|
+
equippedT: undefined,
|
|
264
|
+
uid
|
|
265
|
+
}));
|
|
266
|
+
this.items.delete(sourceUid);
|
|
267
|
+
}
|
|
268
|
+
item.storage = storage;
|
|
269
|
+
item.updatedat = CS_getTimestamp();
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
retrieveFromStorageUnit(storageUid, retrieveUids) {
|
|
273
|
+
const item = this.get(storageUid);
|
|
274
|
+
this.economy.expectStorageUnitTool(item.data);
|
|
275
|
+
const storage = item.storage;
|
|
276
|
+
assert(storage, "Storage unit is empty.");
|
|
277
|
+
assert(retrieveUids.length > 0, "No items to retrieve.");
|
|
278
|
+
assert(this.canRetrieveFromStorageUnit(storageUid, retrieveUids.length), "Cannot retrieve from storage unit.");
|
|
279
|
+
for (const uid of retrieveUids) {
|
|
280
|
+
assert(storage.has(uid), "Item not found.");
|
|
281
|
+
}
|
|
282
|
+
for (const uid of retrieveUids) {
|
|
283
|
+
const item = storage.get(uid);
|
|
284
|
+
this.addInventoryItem(item);
|
|
285
|
+
storage.delete(uid);
|
|
286
|
+
}
|
|
287
|
+
item.storage = storage.size > 0 ? storage : undefined;
|
|
288
|
+
item.updatedat = CS_getTimestamp();
|
|
289
|
+
return this;
|
|
290
|
+
}
|
|
291
|
+
applyItemSticker(targetUid, stickerUid, stickerIndex) {
|
|
292
|
+
const target = this.get(targetUid);
|
|
293
|
+
const sticker = this.get(stickerUid);
|
|
294
|
+
assert(this.economy.hasStickers(target.data), "Target item does not have stickers.");
|
|
295
|
+
this.economy.expectSticker(sticker.data);
|
|
296
|
+
const stickers = target.stickers ?? [...CS_INVENTORY_STICKERS];
|
|
297
|
+
assert(stickers[stickerIndex] === CS_NONE, "Sticker already applied.");
|
|
298
|
+
stickers[stickerIndex] = sticker.id;
|
|
299
|
+
target.stickers = stickers;
|
|
300
|
+
target.updatedat = CS_getTimestamp();
|
|
301
|
+
this.items.delete(stickerUid);
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
scrapeItemSticker(targetUid, stickerIndex) {
|
|
305
|
+
const target = this.get(targetUid);
|
|
306
|
+
assert(target.stickers !== undefined, "Target item does not have stickers.");
|
|
307
|
+
const { stickers } = target;
|
|
308
|
+
assert(typeof stickers[stickerIndex] === "number", "Invalid sticker index.");
|
|
309
|
+
const wears = target.stickerswear ?? [...CS_INVENTORY_STICKERS_WEAR];
|
|
310
|
+
const wear = wears[stickerIndex] ?? CS_NONE;
|
|
311
|
+
const nextWear = float(wear + CS_STICKER_WEAR_FACTOR);
|
|
312
|
+
if (nextWear > CS_MAX_WEAR) {
|
|
313
|
+
stickers[stickerIndex] = CS_NONE;
|
|
314
|
+
wears[stickerIndex] = CS_NONE;
|
|
315
|
+
target.stickers = stickers.filter((id) => id !== CS_NONE).length > 0 ? stickers : undefined;
|
|
316
|
+
target.stickerswear = wears.filter((wear) => wear !== CS_NONE).length > 0 ? wears : undefined;
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
wears[stickerIndex] = nextWear;
|
|
320
|
+
target.stickerswear = wears;
|
|
321
|
+
target.updatedat = CS_getTimestamp();
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
incrementItemStatTrak(targetUid) {
|
|
325
|
+
const target = this.get(targetUid);
|
|
326
|
+
assert(target.stattrak !== undefined, "Target item does not have stattrak.");
|
|
327
|
+
if (target.stattrak < CS_MAX_STATTRAK) {
|
|
328
|
+
target.stattrak++;
|
|
329
|
+
target.updatedat = CS_getTimestamp();
|
|
330
|
+
}
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
swapItemsStatTrak(toolUid, fromUid, toUid) {
|
|
334
|
+
assert(fromUid !== toUid, "Uids must be different.");
|
|
335
|
+
const tool = this.get(toolUid);
|
|
336
|
+
this.economy.expectStatTrakSwapTool(tool.data);
|
|
337
|
+
const fromItem = this.get(fromUid);
|
|
338
|
+
const toItem = this.get(toUid);
|
|
339
|
+
assert(fromItem.stattrak !== undefined && toItem.stattrak !== undefined, "Invalid inventory items.");
|
|
340
|
+
assert(fromItem.data.type === toItem.data.type, "Items must be of the same type.");
|
|
341
|
+
assert(fromItem.data.type === "musickit" || fromItem.data.def === toItem.data.def, "Items must be of the same type.");
|
|
342
|
+
const fromStattrak = fromItem.stattrak;
|
|
343
|
+
fromItem.stattrak = toItem.stattrak;
|
|
344
|
+
fromItem.updatedat = CS_getTimestamp();
|
|
345
|
+
toItem.stattrak = fromStattrak;
|
|
346
|
+
toItem.updatedat = CS_getTimestamp();
|
|
347
|
+
this.items.delete(toolUid);
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
remove(uid) {
|
|
351
|
+
this.items.delete(uid);
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
removeAll() {
|
|
355
|
+
this.items.clear();
|
|
356
|
+
return this;
|
|
357
|
+
}
|
|
358
|
+
get(uid) {
|
|
359
|
+
const item = this.items.get(uid);
|
|
360
|
+
assert(item !== undefined, "Item not found.");
|
|
361
|
+
return item;
|
|
362
|
+
}
|
|
363
|
+
getAll() {
|
|
364
|
+
return Array.from(this.items.values());
|
|
365
|
+
}
|
|
366
|
+
size() {
|
|
367
|
+
return this.items.size;
|
|
368
|
+
}
|
|
369
|
+
move() {
|
|
370
|
+
return new CS_Inventory({
|
|
371
|
+
...this.options,
|
|
372
|
+
economy: this.economy,
|
|
373
|
+
items: this.items
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
export() {
|
|
377
|
+
return Array.from(this.items.values()).map((value) => this.asBaseInventoryItem(value));
|
|
378
|
+
}
|
|
379
|
+
}
|
package/dist/items.d.ts
ADDED