-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParaAPI.js
More file actions
500 lines (462 loc) · 19.4 KB
/
ParaAPI.js
File metadata and controls
500 lines (462 loc) · 19.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
function API(title, author, version) {
this.Title = title;
this.Author = author;
this.Version = version;
}
API.prototype = {
Init: function() {
global = importNamespace("");
this.LoadDefaultConfig();
this.getData();
this.registerPermissions();
this.moduleHandler();
},
/*-----------------------------------------------------------------
grabPlayerData
-- Can be called to certain player data
- @steamID - Users Steam ID
- @key - Object key to search for and value to return
------------------------------------------------------------------*/
grabPlayerData: function(steamID, key) {
return APIData.PlayerData[steamID][key];
},
OnServerInitialized: function() {
clansOn = plugins.Find('Clans');
ch = plugins.Find('chathandler');
},
/*-----------------------------------------------------------------
moduleHandler
-- Locates Modules and Builds new modules as needed
-- builds the files based on the data/config file of
-- this API's file system.
------------------------------------------------------------------*/
moduleHandler: function() {
var modules = {};
modules = {
Ranks: new RanksSystem(this.Title, this.Author, V(1, 0, 0), [APIData, this.Config]) || null
// PrefixHandler: new PrefixHandler(this.Title, this.Author, V(1, 0, 0), [APIData, this.Config]) || null,
// PunishmentSystem: new PunishmentSystem(this.Title, this.Author, V(1, 0, 0), [APIData, this.Config]) || null,
// Achievements: new Achievements(this.Title, this.Author, V(1, 0, 0), [APIData, this.Config]) || null
};
print("Modules");
print("----------------------------------");
print(modules.Ranks);
if (modules.length) {
if (modules.PrefixHandler) {
print("[Modules]: Located Prefix Handler Module.");
print("[PrefixHandler]: Loaded Successfully");
}
if (modules.PunishmentSystem) {
print("[Modules]: Located Punishment Module.");
print("[Punishment]: Loaded Successfully");
}
if (modules.Achievements) {
print("[Modules]: Located Achievements Module.");
print("[Achievements]: Loaded Successfully");
}
if (modules.Ranks) {
print("[Modules]: Located Ranks Module.");
print("[Ranks]: Loaded Successfully");
}
} else {
print("[Modules]: No Modules Located");
}
print("----------------------------------");
this.SaveConfig();
},
LoadDefaultConfig: function() {
this.Config.Info = "Welcome to ParaAPI, this config is the hub of all Module Plugins designed by KillParadise. This setup is the same with the Data file, Think of this as a central 'Hub' for the data";
this.Config.Settings = {
AntiAbuseOn: true,
ChatColor: "#FFFFFF",
ChatNameColor: "#6699FF",
StaffChatNameColor: "#009900",
AllowColor: true
};
this.Config.Modules = this.Config.Modules || {};
this.Config.Permissions = this.Config.Permissions || {
"staff": "isStaff"
};
},
/*-----------------------------------------------------------------
OnPlayerInit
-- Handles player login and initializes data
- @player - Base Player Object
------------------------------------------------------------------*/
OnPlayerInit: function(player) {
var steamID = rust.UserIDFromPlayer(player);
this.buildPlayerData(player, steamID);
},
/*-----------------------------------------------------------------
getData
-- Handles building initial data system
------------------------------------------------------------------*/
getData: function() {
APIData = data.GetData("ParaAPI");
APIData = APIData || {};
APIData.PlayerData = APIData.PlayerData || {};
},
/*-----------------------------------------------------------------
buildPlayerData
-- Handles building initial player data
-- and setting proper values on login
- @player - Base Player Object
- @steamID - User steam ID
------------------------------------------------------------------*/
buildPlayerData: function(player, steamID) {
APIData.PlayerData[steamID] = APIData.PlayerData[steamID] || {};
APIData.PlayerData[steamID].Name = APIData.PlayerData[steamID].Name || player.displayName;
APIData.PlayerData[steamID].IsStaff = APIData.PlayerData[steamID].IsStaff || this.hasPermission(player, "isStaff");
},
/*-----------------------------------------------------------------
saveData
-- Handles building initial player data
-- and setting proper values on login
------------------------------------------------------------------*/
saveData: function() {
data.SaveData('ParaAPI');
},
/*-----------------------------------------------------------------
clearData
-- Wipes The entire data file and then rebuilds it
-- WARNING: CANNOT be undone
- @data - object - data object
------------------------------------------------------------------*/
clearData: function(data) {
delete data;
},
/*-----------------------------------------------------------------
refreshData
-- Handles refreshing player data
-- sends them through ranking process again
- @steamID - String - SteamID of the triggering player
- @data -
------------------------------------------------------------------*/
refreshData: function(steamID, data, value) {
if (data.PlayerData[steamID] === undefined) {
print("No Data found, Attempting to build Data");
}
data[steamID][value] = "";
//this.checkPlayerData(player, steamID);
//Will need to write a function to handle reloading a players data
},
/*-----------------------------------------------------------------
registerPermissions
-- Handles registering and building permissions
------------------------------------------------------------------*/
registerPermissions: function() {
//single permissions
for (var perm in this.Config.Permissions) {
if (!permission.PermissionExists(this.Config.Permissions[perm])) {
permission.RegisterPermission(this.Config.Permissions[perm], this.Plugin);
}
}
},
/*-----------------------------------------------------------------
hasPermission
-- Handles checking permissions of a user
- @player - Base Player Object
- @perm - given permission to look up
------------------------------------------------------------------*/
hasPermission: function(player, perm) {
var steamID = rust.UserIDFromPlayer(player);
if (player.net.connection.authLevel === 2) {
return true;
}
if (permission.UserHasPermission(steamID, perm)) {
return true;
}
rust.SendChatMessage(player, prefix, msgs.noPerms, "0");
return false;
},
/*-----------------------------------------------------------------
getDataSet
-- Builds an array of karma values to compare
-- against the players current karma
- @data - object - object to build the data set from
- @value - String/Number - value to build the data set off of
------------------------------------------------------------------*/
getDataSet: function(data, value) {
var temp = [];
for (var i = 0, len = data.length; i < len; i++) {
temp.push(data[i][value]);
}
return temp;
},
/*-----------------------------------------------------------------
getValue
-- Grabs the victims title and locates the karmaModifier
- @array - Array - array to find value from
- @param - String - parameter to search for in an object
- @data - String/Number - Data to compare value too
------------------------------------------------------------------*/
getValue: function(array, param, data) {
var i = 0,
value = 0,
j = array.length;
for (i; i < j; i++) {
if (array[i][param] === data) {
value = array[i][param];
break;
} else {
value = 1;
}
}
return value;
},
/*-----------------------------------------------------------------
getColor
-- Handles finding, and grabbing the
-- current color of the player
- @steamID - Current User id to grab the color for.
------------------------------------------------------------------*/
getColor: function(data) {
var color = "",
colorArr = [];
if (data.array) {
for (var i = 0, len = data.array.length; i < len; i++) {
if (APIData.PlayerData[data.steamID][data.value] === data.array[i][data.value]) {
color = data.array[i][data.value];
}
}
} else {
for (var key in data.obj) {
if (key === data.compare) {
color = data[key].color;
}
}
}
return color;
},
/*-----------------------------------------------------------------
getClosest
-- Locates closes rank to current players karma
- @closestTo - the current karma of the player
- @data - object - an object to search
- @value - Number/String - value to search through the object for
------------------------------------------------------------------*/
getClosest: function(closestTo, data, value) {
var arr = this.getDataSet(data, value);
if (arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
if (closestTo >= 0) {
if (arr[i] <= closestTo && arr[i] >= 0) closest = arr[i];
} else if (closestTo <= 0) {
if (arr[i] >= closestTo && arr[i] <= 0) closest = arr[i];
}
}
}
return closest;
},
/*-----------------------------------------------------------------
antiAbuse
-- Handles clan mate killings
-- karma system
- @victimID - Victims Steam ID
- @attackerID - Attacker Steam ID
------------------------------------------------------------------*/
antiAbuse: function(user, user2) {
if (clansOn) {
Clan1 = clansOn.GetClanOf(user);
Clan2 = clansOn.GetClanOf(user2);
if (Clan1 === Clan2) {
return true;
} else {
return false;
}
}
return false;
},
/*-----------------------------------------------------------------
String Builder
-- Builds and returns a string based off array of values
- @string - Sent string (Or array)
- @values - array of values in order they appear in string
------------------------------------------------------------------*/
buildString: function(string, values) {
var temp = [],
tempColor = [],
i = 0,
sb = "",
regObj = {};
var re = "";
if (values.length === 0) {
return string;
}
if (string.constructor === Array) {
for (var ii = 0, len = string.length; ii < len; ii++) {
temp.push(string[ii].match(/\{([^{]+)\}/g));
newString = string[ii].replace(temp[ii], values[ii]);
sb += newString + "\n";
}
} else {
temp.push(string.match(/\{([^{]+)\}/g));
temp = temp.toString().split(",");
for (i; i < temp.length; i++) {
regObj[temp[i]] = values[i];
re += temp[i] + "|";
}
re = re.substring("|", re.length - 1);
var expression = new RegExp(re, "g");
sb = string.replace(expression, function(match) {
return regObj[match];
});
}
return sb;
},
/*-----------------------------------------------------------------
findPlayerByName
-- Locates Base Player object using the users name
- @playerName - String - of base player name
------------------------------------------------------------------*/
findPlayerByName: function(playerName) {
var found = [],
foundID;
playerName = playerName.toLowerCase();
var itPlayerList = global.BasePlayer.activePlayerList.GetEnumerator();
while (itPlayerList.MoveNext()) {
var displayName = itPlayerList.Current.displayName.toLowerCase();
if (displayName.search(playerName) > -1) {
print("found match " + displayName);
found.push(itPlayerList.Current);
}
if (playerName.length === 17) {
if (rust.UserIDFromPlayer(displayName).search(playerName)) {
found.push(itPlayerList.Current);
}
}
}
if (found.length) {
foundID = rust.UserIDFromPlayer(found[0]);
found.push(foundID);
return found;
} else {
return false;
}
},
/*-----------------------------------------------------------------
deathHandler
-- Triggers a new death instance and builds a new object with given data
- @data - Object - A data object containing chat data
------------------------------------------------------------------*/
deathHandler: function(data) {
if (data.killer === null || data.victim === null || !data.killer.ToPlayer() || !data.victim.ToPlayer()) return false;
if (this.Config.Settings.AntiAbuseOn && this.antiAbuse(data.killer, data.victim)) return false;
var cbObj = {
killer: data.killer,
victim: data.victim,
killerID: rust.UserIDFromPlayer(data.killer),
victimID: rust.UserIDFromPlayer(data.victim)
};
return cbObj;
}
};
var ParaAPI = new API("ParaAPI", "KillParadise", V(1, 0, 0));
/**
* Handles Chat calls from outside modules to build and return messages
* @method chatHandler
* @memberOf ParaAPI
* @param {Object} data An Object of values sent to the instance upon creation
* @return {String} Returns a concatinated and formatted String
*/
function chatHandler(data) {
if (ch) return null;
var newMsg = "";
newMsg = this.init(data);
return newMsg;
}
chatHandler.prototype = {
/*-----------------------------------------------------------------
init
-- chatHandlers Initializer grabs color from the data
- @data - Object - A data object containing chat data
------------------------------------------------------------------*/
init: function(data) {
if (data.msg.substring(1, 1) === "/" || data.msg === "") return null;
data.color = API.getColor({
obj: data.config,
compare: APIData.PlayerData[data.steamID][data.useTitle]
});
var formattedMsg = this.buildMsg(data);
return formattedMsg;
},
/*-----------------------------------------------------------------
buildMsg
-- chatHandlers actual message builder, grabs module data
-- if not present to build a full string
- @data - Object - A data object containing chat data
------------------------------------------------------------------*/
buildMsg: function(data) {
var formattedMsg = "";
var msg = data.msg;
var useColor = "";
if (this.Config.Settings.AllowColor && permission.UserHasPermission(data.steamID, "isStaff")) {
useColor = "<color=" + this.Config.Settings.StaffChatNameColor + ">";
} else {
useColor = "<color=" + this.Config.Settings.ChatNameColor + ">";
}
if (modules.Ranks) var ranks = this.buildRanksPortion(data, data.steamID);
if (modules.PrefixHandler) var prefixes = this.buildPrefixPortion(data, data.steamID);
formattedMsg = prefixes + useColor + player.displayName + "</color> " + ranks + msg + "</color>";
return formattedMsg;
},
/*-----------------------------------------------------------------
buildRanksPortion
-- Builds the Ranks Portion of our string
- @data - Object - A data object containing chat data
- @steamID - String - Contains a user Steam ID
------------------------------------------------------------------*/
buildRanksPortion: function(data, steamID) {
var ranksSetup = "",
ranksColor = "",
chatColor = "";
if (this.Config.Prefixes === data.config) {
if (this.Config.Settings.AllowColor) {
data.titleColor = "<color=" + data.color + ">[";
data.chatColor = this.Config.Settings.ChatColor;
}
ranksSetup = data.titleColor + data.useTitle + data.chatColor;
} else if (this.Config.Ranks) {
for (var key in this.Config.Ranks.Main) {
if (this.Config.Prefixes.Main[key] === APIData.PlayerData[steamID].Title) {
if (this.Config.Settings.AllowColor) {
ranksColor = this.Config.Ranks.Main[key].color,
chatColor = this.Config.Settings.ChatColor;
}
ranksSetup = ranksColor + this.Config.Ranks.Main[key].title + chatColor;
}
}
} else {
ranksSetup = "";
}
return ranksSetup;
},
/*-----------------------------------------------------------------
buildPrefixPortion
-- Builds the Prefix Portion of our string
- @data - Object - A data object containing chat data
- @steamID - String - Contains a user Steam ID
------------------------------------------------------------------*/
buildPrefixPortion: function(data, steamID) {
var prefixSetup = "",
prefixColor = "";
if (this.Config.Prefixes === data.config) {
if (data.colorOn) {
data.prefixColor = "<color=" + data.color + ">[";
}
prefixSetup = data.prefixColor + data.usePrefix;
} else if (this.Config.Prefixes) {
for (var key in this.Config.Prefixes.Main) {
if (this.Config.Prefixes.Main[key] === APIData.PlayerData[steamID].Prefix) {
if (this.Config.Settings.AllowColor) {
prefixColor = this.Config.Ranks.Main[key].color;
}
prefixSetup = prefixColor + this.Config.Prefixes.Main[key].Prefix;
}
}
} else {
prefixSetup = "";
}
return prefixSetup;
}
};