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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ Opens a crate with `crateID` using a key with `keyID`. If successful, you'll get

Requests global stats for the Spy vs. Engi War. `spyVsEngiWarStats` will be emitted when the request is fulfilled.

### fulfillDynamicRecipe(components, recipe)

Given a `recipe` item id and an array of `components`, put them towards completing the recipe. Each item in the `components` array should be an array of the format `[itemID, defIndex]` where `itemID` is the ID of the item to be used and `defIndex` is the defIndex from the recipe's schema that the item should contribute to. `FulfillDynamicRecipeComponentResponse` will be emitted when complete.

# Events

### connectedToGC
Expand Down Expand Up @@ -326,4 +330,4 @@ Emitted when the GC sends us back the response of a `resetServerIdentity(id)` ca
- `spyScore` - The score for the Spy team
- `engiScore` - The score for the Engineer team

Emitted when the GC sends us back the response of a `requestSpyVsEngiWarStats()` call.
Emitted when the GC sends us back the response of a `requestSpyVsEngiWarStats()` call.
50 changes: 31 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function TeamFortress2(steam) {
this.haveGCSession = false;
this._hadGCSession = false;
this._isInTF2 = false;

var self = this;

// "extend" the default steam.gamesPlayed function so we can catch when TF2 starts up
var gamesPlayed = steam.gamesPlayed;
steam.gamesPlayed = function(appids) {
Expand All @@ -33,24 +33,24 @@ function TeamFortress2(steam) {
clearInterval(self._helloInterval);
self._helloInterval = null;
}

self._isInTF2 = false;
self.haveGCSession = false;
self._hadGCSession = false;
}

gamesPlayed.call(steam, appids);
};

steam.on('fromGC', function(appID, type, body) {
if(appID != 440) {
// Not from the TF2 GC
return;
}

var protobuf = !!(type & protomask);
type &= ~protomask;

if(self._handlers[type]) {
self._handlers[type].call(self, protobuf ? body : ByteBuffer.wrap(body, ByteBuffer.LITTLE_ENDIAN));
} else {
Expand All @@ -61,11 +61,11 @@ function TeamFortress2(steam) {
break;
}
}

self.emit('debug', "Got unhandled GC message " + msgName + (protobuf ? " (protobuf)" : ""));
}
});

steam.on('loggedOff', function() {
self._isInTF2 = false;
self._hadGCSession = self.haveGCSession;
Expand All @@ -74,7 +74,7 @@ function TeamFortress2(steam) {
self.haveGCSession = false;
}
});

steam.on('error', function(e) {
self._isInTF2 = false;
self._hadGCSession = false;
Expand All @@ -83,7 +83,7 @@ function TeamFortress2(steam) {
self.haveGCSession = false;
}
});

steam.on('loggedOn', function() {
if(self._hadGCSession) {
self._connect();
Expand All @@ -96,15 +96,15 @@ TeamFortress2.prototype._connect = function() {
if(!this._isInTF2 || this._helloInterval) {
return; // We're not in TF2 or we're already trying to connect
}

var self = this;
this._helloInterval = setInterval(function() {
if(self.haveGCSession) {
clearInterval(self._helloInterval);
self._helloInterval = null;
return;
}

self._send(Language.ClientHello, Protos.CMsgClientHello, {});
}, 5000);
};
Expand All @@ -113,24 +113,24 @@ TeamFortress2.prototype._send = function(type, protobuf, body) {
if(!this._steam.loggedOn) {
return false;
}

var msgName = type;
for(var i in Language) {
if(Language[i] == type) {
msgName = i;
break;
}
}

this.emit('debug', "Sending GC message " + msgName);

if(protobuf) {
this._steam.toGC(440, type | protomask, (new protobuf(body)).toBuffer());
} else {
// This is a ByteBuffer
this._steam.toGC(440, type, body.flip().toBuffer());
}

return true;
};

Expand All @@ -149,7 +149,7 @@ TeamFortress2.prototype.craft = function(items, recipe) {
for(var i = 0; i < items.length; i++) {
buffer.writeUint64(coerceToLong(items[i]));
}

this._send(Language.Craft, null, buffer);
};

Expand Down Expand Up @@ -255,11 +255,23 @@ TeamFortress2.prototype.requestSpyVsEngiWarStats = function() {
this._send(Language.SpyVsEngyWar_RequestGlobalStats, Protos.CGCMsgGC_SpyVsEngyWar_RequestGlobalStats, {});
};

TeamFortress2.prototype.fulfillRecipe = function(components, recipe) {
this._send(Language.FulfillDynamicRecipeComponent, Protos.CMsgFulfillDynamicRecipeComponent, {
'toolItemId':recipe,
'consumptionComponents': components.map(function(component) {
return {
subjectItemId: component[0],
attributeIndex: component[1]
}
})
});
};

TeamFortress2.prototype._handlers = {};

function coerceToLong(num, signed) {
return typeof num === 'string' ? new ByteBuffer.Long.fromString(num, !signed, 10) : num;
}

require('./enums.js');
require('./handlers.js');
require('./handlers.js');