Skip to content

Commit ded939a

Browse files
authored
Merge pull request #486 from danocmx/feat/autogenerate-exceptions
2 parents 181e413 + eaa5a55 commit ded939a

File tree

18 files changed

+1141
-233
lines changed

18 files changed

+1141
-233
lines changed

README.md

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# TF2 Item Format
2+
23
Formatting for TF2 items.
34

4-
Use `v5` branch for current version.
55
Install it via `npm install tf2-item-format`
66

77
## Features
8+
89
- Parses item name into attribute object
910
- Stringifies attribute object into item name same as backpack.tf
1011
- Parses ECON item into attributes
@@ -13,38 +14,34 @@ Install it via `npm install tf2-item-format`
1314
- Creates backpack.tf compatible format for listing creation
1415

1516
## Support the project
16-
tf2-item-format is now used in number of production grade projects for handling item data,
17-
help support the project by donating items [here](https://steamcommunity.com/tradeoffer/new/?partner=162338347&token=Od7J3LIh).
18-
19-
## Migrating from v4 to v5
20-
To keep your application working like before with version 5 you have to:
21-
```ts
22-
import { parseString } from 'tf2-item-format/static' // -> from 'tf2-item-format'
2317

24-
const { parseString } = require('tf2-item-format/static') // -> from 'tf2-item-format'
25-
```
18+
`tf2-item-format` is now used in number of production grade projects for handling item data,
19+
help support the project by donating items [here](https://steamcommunity.com/tradeoffer/new/?partner=162338347&token=Od7J3LIh).
2620

27-
### New
21+
### API
2822

2923
To inject your own schema use this interface:
24+
3025
```ts
3126
export type ISchema = {
32-
getDefindex(search: number | string): number|null;
33-
getName(search: number | string): string;
34-
getEffectName(effect: number | string): string;
35-
getWearName(wear: number | string): string;
36-
getKillstreakName(killstreak: number | string): string;
37-
getTextureName(texture: number | string): string;
38-
getQualityName(quality: number | string): string;
39-
getEffectEnum(effect: number | string): number;
40-
getWearEnum(wear: number | string): number;
41-
getKillstreakEnum(killstreak: number | string): number;
42-
getTextureEnum(texture: number | string): number;
43-
getQualityEnum(quality: number | string): number;
44-
getTextures(): SchemaEnum;
45-
getEffects(): SchemaEnum;
46-
isUniqueHat(defindexOrName: string | number): boolean;
47-
}
27+
getDefindex(search: number | string): number | null;
28+
getName(search: number | string): string;
29+
getEffectName(effect: number | string): string;
30+
getWearName(wear: number | string): string;
31+
getKillstreakName(killstreak: number | string): string;
32+
getTextureName(texture: number | string): string;
33+
getQualityName(quality: number | string): string;
34+
getEffectEnum(effect: number | string): number;
35+
getWearEnum(wear: number | string): number;
36+
getKillstreakEnum(killstreak: number | string): number;
37+
getTextureEnum(texture: number | string): number;
38+
getQualityEnum(quality: number | string): number;
39+
getTextures(): SchemaEnum;
40+
getEffects(): SchemaEnum;
41+
isUniqueHat(defindexOrName: string | number): boolean;
42+
getItems(): SchemaItem[]; // Return GetSchemaItems API response
43+
getVersion(): number;
44+
};
4845

4946
// Extra types you might need:
5047
export type NameToDefindex = { [name: string]: number };
@@ -54,29 +51,41 @@ export type SchemaEnum = NameToDefindex & DefindexToName;
5451
// Import these like:
5552
// import { ISchema, NameToDefindex, DefindexToName, SchemaEnum } from 'tf2-item-format'
5653
```
54+
5755
Then you just have to:
56+
5857
```ts
5958
import { createFormat } from 'tf2-item-format';
6059

6160
const format = createFormat(schema);
6261
```
62+
6363
Every method is then export same as before.
6464

6565
You can use `parseSKU` and `toSKU` without schema:
66+
6667
```ts
6768
import { parseSKU, toSKU } from 'tf2-item-format';
6869
```
6970

7071
Please note that static schema has implemented a lot of overwrites for defindexes that simply cannot be obtained by the limited data we have, so when you are implementing it yourself you should use the current `Schema` as your guide.
7172

73+
`getVersion` function should return a versioning number for your schema, we recommend incrementing
74+
this whenever you update your schema, so that cached items, like exceptions for item combinations
75+
that cannot co-exist, can be recalculated. Only do this whenever the schema actually changes,
76+
easiest way to do that, is by checking the item count between updates.
77+
7278
## Instalation
79+
7380
Via NPM: `npm install tf2-item-format`
7481
Or Yarn: `yarn add tf2-item-format`
7582

7683
## Documentation
84+
7785
Currently there is no full documentation.
7886

7987
We export these methods:
88+
8089
- `parseString`
8190
- `stringify`
8291
- `parseEconItem`
@@ -86,12 +95,14 @@ We export these methods:
8695
- `fixName`
8796

8897
And static schema:
98+
8999
- under property `schema`
90100

91101
Everything is fully typed so you should be able to get this working on your own, until I make a documentation.
92102
Some of the types are a bit confusing which will get fixed in next major version.
93103

94104
### parseEconItem
105+
95106
```ts
96107
parseEconItem(econ: EconItem, inNumbers: boolean, useDefindexes: boolean, options?: { useTrueDefindex: boolean }): ParsedEconItem;
97108
```
@@ -101,9 +112,12 @@ parseEconItem(econ: EconItem, inNumbers: boolean, useDefindexes: boolean, option
101112
- `useDefindexes` - appends item defindex to the object, adds target & output aswell
102113
- `options`
103114
- `useTrueDefindex` - returns true defindex item has on this econ, doesn't ask schema.
115+
- `itemNumberFromFraudWarning` - retrieve item number (crate series or craft number) from `fraudwarning` property
116+
- `retrieveCrateNumber` - if item is a case, retrieve it's case number
104117
- Returns `ParsedEconItem` which is an object of all attributes the econ gives us
105118

106119
### createBPListing
120+
107121
```ts
108122
createBPListing(attributes: ItemAttributes | StringifySKUAttributes, options?: CreateBPListingOptions): BackpackTFListing
109123
```
@@ -114,6 +128,7 @@ createBPListing(attributes: ItemAttributes | StringifySKUAttributes, options?: C
114128
- Returns `BackpackTFListing` object compatible with `item` object in backpack.tf listing creation api
115129

116130
### stringify
131+
117132
```ts
118133
stringify(attributes: StrigifySKUAttributes | ItemAttributes, options?: StringifyOptions): string
119134
```
@@ -123,10 +138,25 @@ stringify(attributes: StrigifySKUAttributes | ItemAttributes, options?: Stringif
123138
- `determineUniqueHat` - if set it checks in schema if given item should use `The` in name, defaults to `false`
124139
- Returns the item name
125140

126-
#### Note for stringify
127-
If you are using sku format to store your items and want to use `The` in your names, you have to turn `determineUniqueHat` to `true`.
141+
> [!INFO]
142+
> If you are using sku format to store your items and want to use `The` in your names, you have to turn `determineUniqueHat` to `true`.
143+
144+
### parseSKU
145+
```ts
146+
parseSKU(sku: string): SKUAttributes
147+
```
148+
149+
Parse Marketplace.tf's SKU format into `SKUAttributes` object, which is compatible with other methods.
150+
151+
### toSKU
152+
```ts
153+
toSKU(attribute: SKUAttributes): string
154+
```
155+
156+
Get an SKU from `SKUAttributes` object, you can also supply `EconItem` or attributes from `parseString` if you have `inNumbers` and `useDefindexes` set to true.
128157

129158
## Compability usage
159+
130160
```ts
131161
// Have a name, want a sku
132162
const attributes = parseString(name, true, true); // To get defindexes and enums
@@ -140,4 +170,5 @@ const name = stringify(attributes);
140170
const attributes = parseSKU(sku);
141171
const listing = createBPListing(attributes);
142172
```
173+
143174
Every method should be interchangable in similar ways.

package-lock.json

Lines changed: 27 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parseEconItem/ParsedEcon/getNameAttributes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function (econ: ParsedEcon): NameAttributes {
2222
*/
2323
const attributes: NameAttributes = {
2424
australium: isAustralium(name),
25-
isUniqueHat: isUniqueHat(name),
25+
isUniqueHat: isUniqueHat(econ.schema, name),
2626
};
2727

2828
if (!texture) {
@@ -40,6 +40,7 @@ export default function (econ: ParsedEcon): NameAttributes {
4040
}
4141

4242
const usableItem = getUsableItem(
43+
econ.schema,
4344
attributes.itemNumber
4445
? removeItemNumber(name, attributes.itemNumber)
4546
: name

0 commit comments

Comments
 (0)