-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi.js
More file actions
154 lines (139 loc) · 3.78 KB
/
api.js
File metadata and controls
154 lines (139 loc) · 3.78 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
var wait = require('wait.for');
var jsonrpc = require('multitransport-jsonrpc');
var config = require('config');
var rangeCheck = require('range_check');
var requireDir = require('require-dir');
global.utils = require('./utils');
var methods = requireDir('./methods', {recurse: true});
var rpc = new jsonrpc.client(new jsonrpc.transports.client.tcp(config.rpcHost,
config.rpcPort));
rpc.register(['obj', 'api', 'admin', 'gs']);
var rpcDataProxy = function rpcDataProxy(type) {
return {
get: function (target, property) {
if (!target[property]) {
// Not in cache. Get (and set) info from RPC.
try {
var rpcData = rpcCall('api', 'apiGetJSFileObject',
[type + '/' + property + '.js']);
gsData[type][property] = rpcData;
log.info('Cached ' + type + '/' + property);
}
catch (e) {
// Failed to get info via RPC.
}
}
return target[property];
}
};
};
var gsData = {
achievements: new Proxy({}, rpcDataProxy('achievements')),
items: new Proxy({}, rpcDataProxy('items')),
quests: new Proxy({}, rpcDataProxy('quests')),
skills: {},
locations: {},
upgrades: {}
};
global.gsData = gsData;
exports.init = function init() {
// Fetch GSJS config (skills, locations and upgrades).
log.info('Fetching GSJS config...');
var gsjsConfig = rpcCall('gs', 'getGsjsConfig');
gsData.skills = gsjsConfig.data_skills;
gsData.locations = gsjsConfig.data_maps;
gsData.upgrades = gsjsConfig.data_imagination_upgrades;
log.info('Received GSJS config');
// Fetch extra Items (not found in GSJS).
log.info('Fetching extra items...');
var extraItems = require('./extras').extra_items;
Object.keys(extraItems).forEach(function store(classTSID) {
log.info('Loading extra item ' + classTSID + ' from extras.js');
extraItems[classTSID].missing_item = 1;
gsData.items[classTSID] = extraItems[classTSID];
});
// TODO: Fetch Wardrobe/Vanity (fetch from a database after namerizer)
};
exports.handle = function handle(name, req, callback) {
var handler;
try {
var parts = name.split('.');
if (!parts.length) {
callback(new Error('invalid_request'));
return;
}
if (parts[0] === 'god' && config.internalIpRange) {
var srcIp = req.connection.remoteAddress;
if (!rangeCheck.inRange(srcIp, config.internalIpRange)) {
return callback(new Error('unauthorized_internal_request'));
}
}
parts.reverse();
handler = methods;
while (parts.length > 0) {
handler = handler[parts.pop()];
}
}
catch (e) {
log.warn('Could not parse HTTP API function name "%s" (err: %s)', name, e);
}
if (typeof handler !== 'function') {
callback(new Error('invalid_request'));
}
else {
// TODO: Authentication.
// pc = 'PA9HIFJTI1E2TJS';
var pc = req.body.ctoken ? req.body.ctoken : undefined;
try {
var ret = handler(req, pc);
ret = getRes(ret);
callback(null, ret);
}
catch (e) {
log.error(e);
callback(new Error(e.message));
}
}
};
function getRes(body, status) {
if (body.error) {
return {
status: 200,
body: {ok: 0, error: body.error}
};
}
else {
body.ok = 1;
return {
status: 200,
body: body
};
}
}
/*
* RPC examples
* obj: rpcObjCall('PA9HIFJTI1E2TJS', 'skills_train', ['teleportation_1']);
* api: rpcCall('api', 'apiIsPlayerOnline', ['PA9HIFJTI1E2TJS']);
* admin: rpcCall('admin', 'adminGetItemDescExtras', { class_id: 'tomato' });
* gs: rpcCall('gs', 'getConnectData', ['PA9HIFJTI1E2TJS']);
*/
global.rpcObjCall = rpcObjCall;
function rpcObjCall(obj, fname, args) {
try {
var res = wait.forMethod(rpc, 'obj', 'HTTPAPI', obj, fname, args);
return res;
}
catch (e) {
throw new Error('RPC error');
}
}
global.rpcCall = rpcCall;
function rpcCall(type, fname, args) {
try {
var res = wait.forMethod(rpc, type, 'HTTPAPI', fname, args);
return res;
}
catch (e) {
throw new Error('RPC error');
}
}