-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathschema.ts
More file actions
384 lines (300 loc) · 9.91 KB
/
schema.ts
File metadata and controls
384 lines (300 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { requireStatic, SchemaEnum, DefindexToName } from 'tf2-static-schema';
import isNumber from '../util/isNumber';
import { ISchema, ItemsGame, NameToDefindex, SchemaItem } from '../types/schema';
const DEFINDEXES: { [name: string]: number } = {
// Local naming
'Strangifier Chemistry Set': 20000,
'Specialized Killstreak Kit Fabricator': 20002,
'Professional Killstreak Kit Fabricator': 20003,
'Chemistry Set': 20005,
'Mann Co. Supply Crate Key': 5021,
Lugermorph: 160,
/**
* Old keys, list might be incomplete.
*/
'Festive Winter Crate Key': 5049,
'Refreshing Summer Cooler Key': 5067,
'Naughty Winter Crate Key': 5072,
'Nice Winter Crate Key': 5073,
'Scorched Key': 5079,
'Fall Key': 5081,
'Eerie Key': 5628,
'Naughty Winter Crate Key 2012': 5631,
'Nice Winter Crate Key 2012': 5632,
'Spooky Key': 5713,
'Naughty Winter Crate Key 2013': 5716,
'Nice Winter Crate Key 2013': 5717,
'Limited Late Summer Crate Key': 5762,
'Naughty Winter Crate Key 2014': 5791,
'Nice Winter Crate Key 2014': 5792,
// "'Decorated War Hero' War Paint Civilian Grade Keyless Case": 18000,
// "'Decorated War Hero' War Paint Freelance Grade Keyless Case": 18001,
// "'Decorated War Hero' War Paint Mercenary Grade Keyless Case": 18002,
// "'Contract Campaigner' War Paint\nCivilian Grade Keyless Case": 18003,
// "'Contract Campaigner' War Paint\nFreelance Grade Keyless Case": 18004,
// "'Contract Campaigner' War Paint\nMercenary Grade Keyless Case": 18005,
};
const NAMES: { [defindex: number]: string } = {
160: 'Lugermorph',
5021: 'Mann Co. Supply Crate Key',
5049: 'Festive Winter Crate Key',
5067: 'Refreshing Summer Cooler Key',
5072: 'Naughty Winter Crate Key',
5073: 'Nice Winter Crate Key',
5079: 'Scorched Key',
5081: 'Fall Key',
5628: 'Eerie Key',
5631: 'Naughty Winter Crate Key 2012',
5632: 'Nice Winter Crate Key 2012',
5713: 'Spooky Key',
5716: 'Naughty Winter Crate Key 2013',
5717: 'Nice Winter Crate Key 2013',
5762: 'Limited Late Summer Crate Key',
5791: 'Naughty Winter Crate Key 2014',
5792: 'Nice Winter Crate Key 2014',
20000: 'Strangifier Chemistry Set',
20005: 'Chemistry Set',
// 18000: "'Decorated War Hero' War Paint\nCivilian Grade Keyless Case",
// 18001: "'Decorated War Hero' War Paint\nFreelance Grade Keyless Case",
// 18002: "'Decorated War Hero' War Paint\nMercenary Grade Keyless Case",
// 18003: "'Contract Campaigner' War Paint\nCivilian Grade Keyless Case",
// 18004: "'Contract Campaigner' War Paint\nFreelance Grade Keyless Case",
// 18005: "'Contract Campaigner' War Paint\nMercenary Grade Keyless Case",
};
/* TODO: Set boundaries between these.
"20000":"Chemistry Set",
"20001":"Chemistry Set",
"20005":"Chemistry Set",
"20006":"Chemistry Set",
"20007":"Chemistry Set",
"20008":"Chemistry Set",
"20009":"Chemistry Set"
*/
export class Schema implements ISchema {
public effects!: SchemaEnum;
protected effectNames!: NameToDefindex;
public wears!: SchemaEnum;
public killstreaks!: SchemaEnum;
public textures!: SchemaEnum;
protected textureNames!: NameToDefindex;
public itemNames!: DefindexToName;
public items!: SchemaItem[];
public qualities!: SchemaEnum;
public itemsGame!: ItemsGame;
constructor() {}
getVersion(): number {
return 1; // Never changes during runtime
}
getTextures() {
if (!this.textures) this.loadTextures();
return this.textures;
}
getEffects() {
if (!this.effects) this.loadEffects();
return this.effects;
}
getItems(): SchemaItem[] {
if (!this.items) this.loadDefindexes();
return this.items;
}
loadEffects(): void {
this.effects = requireStatic('effects') as SchemaEnum;
this.effectNames = {}
for (const effect of Object.keys(this.effects)) {
if (isNumber(effect)) {
continue;
}
this.effectNames[effect] = this.effects[effect];
}
}
loadWears(): void {
this.wears = requireStatic('wears') as SchemaEnum;
}
loadKillstreaks(): void {
this.killstreaks = requireStatic('killstreaks') as SchemaEnum;
}
loadTextures(): void {
this.textures = requireStatic('paint-kits') as SchemaEnum;
this.textureNames = {}
for (const effect of Object.keys(this.textures)) {
if (isNumber(effect)) {
continue;
}
this.textureNames[effect] = this.textures[effect];
}
}
loadItemNames(): void {
this.itemNames = requireStatic('item-names') as DefindexToName;
}
loadDefindexes(): void {
this.items = requireStatic('items') as SchemaItem[];
}
loadQualities(): void {
this.qualities = requireStatic('qualities') as SchemaEnum;
}
loadItemsGame(): void {
this.itemsGame = requireStatic('items-game') as ItemsGame;
}
getEffect(search: string | number): number | string {
if (!this.effects) this.loadEffects();
return this.effects[search];
}
getWear(search: string | number): number | string {
if (!this.wears) this.loadWears();
return this.wears[search];
}
getKillstreak(search: string | number): number | string {
if (!this.killstreaks) this.loadKillstreaks();
return this.killstreaks[search];
}
getTexture(search: string | number): number | string {
if (!this.textures) this.loadTextures();
return this.textures[search];
}
/**
* @todo https://github.com/Nicklason/tf2-automatic/blob/master/src/lib/items.ts
* @param {string} search
* @return {number}
*/
getDefindex(search: number | string): number | null {
if (!this.items) this.loadDefindexes();
if (typeof search === 'number') return search;
// Exceptions
if (DEFINDEXES[search]) return DEFINDEXES[search];
let upgradeableDfx: number | null = null;
for (let i = 0; i < this.items.length; i++) {
const item: SchemaItem = this.items[i];
const name: string = selectName(item);
if (name === search) {
if (!hasUpgradeable(item) || isUpgradeable(item.name)) {
return item.defindex;
}
upgradeableDfx = item.defindex;
}
}
return upgradeableDfx;
}
getName(search: number | string): string {
if (!this.itemNames) this.loadItemNames();
if (!isNumber(search)) return search as string;
const name = NAMES[search];
if (name) return name;
return this.itemNames[search as number];
}
getQuality(search: number | string): number | string {
if (!this.qualities) this.loadQualities();
return this.qualities[search];
}
getEffectName(effect: number | string): string {
if (!isNumber(effect)) return effect as string;
return this.getEffect(effect as number) as string;
}
getWearName(wear: number | string): string {
if (!isNumber(wear)) return wear as string;
return this.getWear(wear as number) as string;
}
getKillstreakName(killstreak: number | string): string {
if (!isNumber(killstreak)) return killstreak as string;
return this.getKillstreak(killstreak as number) as string;
}
getTextureName(texture: number | string): string {
if (!isNumber(texture)) return texture as string;
return this.getTexture(texture as number) as string;
}
getQualityName(quality: number | string): string {
if (!isNumber(quality)) return quality as string;
return this.getQuality(quality as number) as string;
}
getEffectEnum(effect: number | string): number {
if (isNumber(effect)) return effect as number;
return this.getEffect(effect as string) as number;
}
getWearEnum(wear: number | string): number {
if (isNumber(wear)) return wear as number;
return this.getWear(wear as string) as number;
}
getKillstreakEnum(killstreak: number | string): number {
if (isNumber(killstreak)) return killstreak as number;
return this.getKillstreak(killstreak as string) as number;
}
getTextureEnum(texture: number | string): number {
if (isNumber(texture)) return texture as number;
return parseInt(this.getTexture(texture as string) as string);
}
getQualityEnum(quality: number | string): number {
if (isNumber(quality)) return quality as number;
return this.getQuality(quality as string) as number;
}
isUniqueHat(defindexOrName: string | number): boolean {
if (isNumber(defindexOrName)) {
defindexOrName = this.getName(defindexOrName);
}
const item = this.getSchemaItemFromName(defindexOrName);
return !!item?.proper_name;
}
getCrateNumber(defindexOrName: string | number): number {
if (!this.itemsGame) this.loadItemsGame();
if (!isNumber(defindexOrName)) {
const defindex = this.getDefindex(defindexOrName);
if (!defindex) return 0;
defindexOrName = defindex;
}
const item = this.itemsGame.items[defindexOrName + ''];
if (!item) return 0;
const crateSeries = parseInt(
(item.static_attrs &&
item.static_attrs['set supply crate series']) as string
);
return isNaN(crateSeries) ? 0 : crateSeries;
}
private getSchemaItemFromName(search: string) {
if (!this.items) this.loadDefindexes();
let byDefindex: number = 0;
if (DEFINDEXES[search]) {
byDefindex = DEFINDEXES[search];
}
let correctItem: SchemaItem | null = null;
for (let i = 0; i < this.items.length; i++) {
const item: SchemaItem = this.items[i];
const name: string = selectName(item);
if (byDefindex ? byDefindex === item.defindex : name === search) {
if (!hasUpgradeable(item) || isUpgradeable(item.name)) {
return item;
}
correctItem = item;
}
}
return correctItem;
}
public getTextureNames(): NameToDefindex {
if (!this.textures) {
this.loadTextures();
}
return this.textureNames;
}
public getEffectNames(): NameToDefindex {
if (!this.effects) {
this.loadEffects();
}
return this.effectNames;
}
}
/**
* @todo get from schema
* @param {object} item
* @return {string}
*/
function selectName(item: SchemaItem): string {
if (item.item_name === 'Kit') return item.item_type_name;
// Due to BackpackTF naming colisions.
if (item.defindex === 20003) return 'Professional Killstreak Fabricator';
if (item.defindex === 20002) return 'Specialized Killstreak Fabricator';
return item.item_name;
}
function isUpgradeable(name: string): boolean {
return name.startsWith('Upgradeable ');
}
function hasUpgradeable(item: SchemaItem): boolean {
return item.name.includes(item.item_class.toUpperCase());
}
export default new Schema();