Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ The `premium` property of node-tf2 would now be true and the `backpackSlots` pro
Emitted when we receive a new item. `item` is the item that we just received, and `tf2.backpack` is updated before the event is emitted.

### itemChanged
- `oldItem` - The old item data
- `oldItem` - The old item data (may be same as `newItem` if backpack was not loaded yet)
- `newItem` - The new item data

Emitted when an item in our backpack changes (e.g. style update, position changed, etc.).
Expand Down
53 changes: 27 additions & 26 deletions handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,12 @@ handlers[Language.SO_Create] = function(body) {
return; // Not an item
}

if (!this.backpack) {
return; // We don't have our backpack yet!
}

let item = decodeProto(Schema.CSOEconItem, proto.object_data);
item.position = item.inventory & 0x0000FFFF;
this.backpack.push(item);

let isNew = (item.inventory >>> 30) & 1;
item.position = (isNew ? 0 : item.inventory & 0xFFFF);

if (this.backpack) this.backpack.push(item);
this.emit('itemAcquired', item);
};

Expand All @@ -199,22 +198,24 @@ handlers[Language.SO_UpdateMultiple] = function(body) {
TeamFortress2.prototype._handleSOUpdate = function(so) {
switch (so.type_id) {
case 1:
if (!this.backpack) {
return; // We don't have our backpack yet!
}
let newItem = decodeProto(Schema.CSOEconItem, so.object_data);

let isNew = (newItem.inventory >>> 30) & 1;
newItem.position = (isNew ? 0 : newItem.inventory & 0xFFFF);

let item = decodeProto(Schema.CSOEconItem, so.object_data);
item.position = item.inventory & 0x0000FFFF;
for (let i = 0; i < this.backpack.length; i++) {
if (this.backpack[i].id == item.id) {
let oldItem = this.backpack[i];
this.backpack[i] = item;
let oldItem = null;

this.emit('itemChanged', oldItem, item);
if (this.backpack) {
for (let i = 0; i < this.backpack.length; i++) {
if (this.backpack[i].id != newItem.id) continue;

oldItem = this.backpack[i];
this.backpack[i] = newItem;
break;
}
}

this.emit('itemChanged', oldItem || newItem, newItem);
break;
case 7:
let data = decodeProto(Schema.CSOEconGameAccountClient, so.object_data);
Expand Down Expand Up @@ -255,21 +256,21 @@ handlers[Language.SO_Destroy] = function(body) {
return; // Not an item
}

if (!this.backpack) {
return; // We don't have our backpack yet
}

let item = decodeProto(Schema.CSOEconItem, proto.object_data);
let itemData = null;
for (let i = 0; i < this.backpack.length; i++) {
if (this.backpack[i].id == item.id) {
itemData = this.backpack[i];

let isNew = (item.inventory >>> 30) & 1;
item.position = (isNew ? 0 : item.inventory & 0xFFFF);

if (this.backpack) {
for (let i = 0; i < this.backpack.length; i++) {
if (this.backpack[i].id != item.id) continue;

this.backpack.splice(i, 1);
break;
}
}

this.emit('itemRemoved', itemData);
this.emit('itemRemoved', item);
};

// Item manipulation
Expand Down Expand Up @@ -376,4 +377,4 @@ function decodeProto(proto, encoded) {
// Anything falsy is true
return !val;
}
}
}