This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
forked from joshuaferrara/node-csgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (114 loc) · 4.76 KB
/
index.js
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
var EventEmitter = require('events').EventEmitter,
fs = require("fs"),
util = require("util"),
Schema = require('protobuf').Schema,
base_gcmessages = new Schema(fs.readFileSync(__dirname + "/generated/base_gcmessages.desc")),
gcsdk_gcmessages = new Schema(fs.readFileSync(__dirname + "/generated/gcsdk_gcmessages.desc")),
csgo_gcmessages = new Schema(fs.readFileSync(__dirname + "/generated/cstrike15_gcmessages.desc")),
protoMask = 0x80000000,
bignumber = require("bignumber.js"),
CSGO = exports;
var CSGOClient = function CSGOClient(steamClient, debug) {
EventEmitter.call(this);
this.debug = debug || false;
this._client = steamClient;
this._appid = 730;
this.chatChannels = []; // Map channel names to channel data.
this._gcReady = false;
this._gcClientHelloIntervalId = null;
var self = this;
this._client.on("fromGC", function fromGC(app, type, message, callback) {
callback = callback || null;
var kMsg = type & ~protoMask;
if (self.debug) util.log("CS:GO fromGC: " + [app, kMsg].join(", ")); // TODO: Turn type-protoMask into key name.
if (kMsg in self._handlers) {
if (callback) {
self._handlers[kMsg].call(self, message, callback);
}
else {
self._handlers[kMsg].call(self, message);
}
}
else {
self.emit("unhandled", kMsg);
}
});
this._sendClientHello = function() {
if (self.debug) util.log("Sending ClientHello");
if (!self._client) {
util.log("Client went missing");
}
else {
self._client.toGC(self._appid, (CSGO.EGCBaseClientMsg.k_EMsgGCClientHello | protoMask), gcsdk_gcmessages.CMsgClientHello.serialize({}));
}
};
};
util.inherits(CSGOClient, EventEmitter);
require("./generated/messages");
CSGOClient.prototype.ServerRegion = CSGO.ServerRegion;
CSGOClient.prototype.GameMode = CSGO.GameMode;
CSGOClient.prototype.ToAccountID = function(accid){
return new bignumber(accid).minus('76561197960265728')-0;
};
CSGOClient.prototype.ToSteamID = function(accid){
return new bignumber(accid).plus('76561197960265728')+"";
};
// Methods
CSGOClient.prototype.launch = function() {
/* Reports to Steam that we are running Counter-Strike: Global Offensive. Initiates communication with GC with EMsgGCClientHello */
if (this.debug) util.log("Launching CS:GO");
this._client.gamesPlayed([this._appid]);
// Keep knocking on the GCs door until it accepts us.
this._gcClientHelloIntervalId = setInterval(this._sendClientHello, 2500);
};
CSGOClient.prototype.exit = function() {
/* Reports to Steam we are not running any apps. */
if (this.debug) util.log("Exiting CS:GO");
/* stop knocking if exit comes before ready event */
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
}
this._gcReady = false;
this._client.gamesPlayed([]);
};
// Handlers
var handlers = CSGOClient.prototype._handlers = {};
handlers[CSGO.EGCBaseClientMsg.k_EMsgGCClientWelcome] = function clientWelcomeHandler(message) {
/* Response to our k_EMsgGCClientHello, now we can execute other GC commands. */
// Only execute if _gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'ready');
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
if (this.debug) util.log("Received client welcome.");
this._gcReady = true;
this.emit("ready");
}
};
handlers[CSGO.EGCBaseClientMsg.k_EMsgGCClientConnectionStatus] = function gcClientConnectionStatus(message) {
/* Catch and handle changes in connection status, cuz reasons u know. */
var status = gcsdk_gcmessages.CMsgConnectionStatus.parse(message).status;
switch (status) {
case CSGO.GCConnectionStatus.GCConnectionStatus_HAVE_SESSION:
if (this.debug) util.log("GC Connection Status regained.");
// Only execute if _gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'ready');
if (this._gcClientHelloIntervalId) {
clearInterval(this._gcClientHelloIntervalId);
this._gcClientHelloIntervalId = null;
this._gcReady = true;
this.emit("ready");
}
break;
default:
if (this.debug) util.log("GC Connection Status unreliable - " + status);
// Only execute if !_gcClientHelloIntervalID, otherwise it's already been handled (and we don't want to emit multiple 'unready');
if (!this._gcClientHelloIntervalId) {
this._gcClientHelloIntervalId = setInterval(this._sendClientHello, 2500); // Continually try regain GC session
this._gcReady = false;
this.emit("unready");
}
break;
}
};
CSGO.CSGOClient = CSGOClient;
require("./handlers/match");