Advertisement
Guest User

Untitled

a guest
May 17th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. const md5 = require('md5');
  2. const SteamID = require('steamid');
  3.  
  4.  
  5. const Broadcaster = function(redisClient, broadcastChannel, APP_KEY) {
  6. const that = this;
  7.  
  8. this.redisClient = redisClient;
  9. this.APP_KEY = APP_KEY || "";
  10. this.broadcastChannel = broadcastChannel || 'private-broadcast-channel';
  11.  
  12. this.lastUser = false;
  13.  
  14. };
  15.  
  16. module.exports = Broadcaster;
  17.  
  18. var prototype = Broadcaster.prototype;
  19.  
  20. /**
  21. * Converts Steamid, accountid, etc to Broadcast channel.
  22. * @param user e.g STEAM_0:0:NNNNNN or ACCOUNT_ID or Secure:EncryptedChannel
  23. * @returns {*}
  24. */
  25. prototype.GetUserBroadcastChannel = function GetUserBroadcastChannel(user) {
  26. var channel = false;
  27.  
  28. if (typeof user == "number")
  29. user = user.toString();
  30.  
  31.  
  32. // check if ths is a secure channel
  33. if (typeof user == "string" && user.indexOf('Secure:') != -1)
  34. channel = user;
  35.  
  36. else
  37. {
  38. var _steam = new SteamID(user);
  39.  
  40. if (_steam.isValid())
  41. {
  42. var steam64 = _steam.getSteamID64();
  43.  
  44. var encrypted = md5( md5(steam64) +''+ this.APP_KEY );
  45. channel = 'Secure:'+encrypted;
  46. }
  47. else
  48. {
  49.  
  50. var sid = new SteamID();
  51. sid.universe = SteamID.Universe.PUBLIC;
  52. sid.type = SteamID.Type.INDIVIDUAL;
  53. sid.instance = SteamID.Instance.DESKTOP;
  54. sid.accountid = user;
  55.  
  56.  
  57. if (sid.isValid()) {
  58. var steam64 = sid.getSteam2RenderedID();
  59. var _steam = new SteamID(steam64);
  60. steam64 = _steam.getSteamID64();
  61.  
  62. var encrypted = md5( md5(steam64) + '' + this.APP_KEY);
  63. channel = 'Secure:' + encrypted;
  64. }
  65. }
  66. }
  67.  
  68. return channel;
  69. };
  70.  
  71.  
  72. /**
  73. * Plays a sound effect on users browser.
  74. * @param user SteamID || AccountID || Secure Channel
  75. * @param effect Effect Name, or file.
  76. * @param Settings Object { Volume: 0.1, Repeat: 0, Length: N }
  77. * @constructor
  78. */
  79. prototype.soundfx = function soundfx(user, effect, Settings ) {
  80. var event = 'PlayAudioEffect';
  81.  
  82. //if (typeof Settings == undefined)
  83. // var Settings = {};
  84. //
  85. var extraSettings = Settings || {};
  86.  
  87.  
  88. if (typeof payload != "object")
  89. payload = {Sound: effect, Settings: extraSettings };
  90. else
  91. {
  92. payload.Sound = effect;
  93. payload.Settings = extraSettings;
  94. }
  95.  
  96. return this.send(user, event, payload);
  97.  
  98. };
  99.  
  100. /**
  101. * Reason we are all here, utility to broadcast.
  102. * @param user
  103. * @param event
  104. * @param payload
  105. * @returns {boolean}
  106. * @constructor
  107. */
  108. prototype.send = function send(user, event, payload) {
  109. this.lastUser = user;
  110.  
  111. var BroadcastTo = this.GetUserBroadcastChannel(user);
  112.  
  113. if (!BroadcastTo)
  114. {
  115. console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
  116. return false;
  117. }
  118.  
  119.  
  120. payload.BroadcastTo = BroadcastTo;
  121.  
  122. if (event.indexOf('App\\Events\\') == -1)
  123. event = 'App\\Events\\'+event;
  124.  
  125.  
  126.  
  127. var payloadC = {'event': event,
  128. 'data': payload };
  129.  
  130.  
  131. var payload_json = JSON.stringify(payloadC);
  132.  
  133. this.redisClient.publish(this.broadcastChannel, payload_json );
  134. 205
  135. return this;
  136. };
  137.  
  138.  
  139. /**
  140. * Sends a sound to the last user, useful with method chaining.
  141. * @param effect
  142. * @param options
  143. */
  144. prototype.withSound = function withSound(effect, options)
  145. {
  146. return this.soundfx(this.lastUser, effect, options);
  147. };
  148.  
  149. /**
  150. * Sends status message event
  151. * @param user - Steam ID or any identifable user.
  152. * @param message Message to send
  153. * @param className info|warning|error|default|success
  154. * @returns {boolean}
  155. */
  156. prototype.statusMsg = function status(user, message, className)
  157. {
  158. var payload = { Status: message, Type: className || 'default' };
  159. return this.send(user, 'Steam_User_Items_Status', payload);
  160. };
  161.  
  162. prototype.bot = function(username, event, payload)
  163. {
  164. var BroadcastTo = "bot-"+username;
  165.  
  166. if (!BroadcastTo)
  167. {
  168. console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
  169. return false;
  170. }
  171.  
  172. if (event.indexOf('App\\Events\\') == -1)
  173. event = 'App\\Events\\'+event;
  174.  
  175. var payloadC = {'event': event,
  176. 'data': payload };
  177.  
  178. var payload_json = JSON.stringify(payloadC);
  179. return this.redisClient.publish(BroadcastTo, payload_json );
  180. };
  181.  
  182. prototype.irc = function (event, payload)
  183. {
  184. var BroadcastTo = "bot-irc";
  185. if (!BroadcastTo)
  186. {
  187. console.log("[BROADCAST ERROR] Error trying to broadcast to "+user+" - Cannot obtain Secure channel from input.");
  188. return false;
  189. }
  190. console.log("IRC> "+event+" with payload: "+payload);
  191.  
  192. if (event.indexOf('App\\Events\\') == -1)
  193. event = 'App\\Events\\'+event;
  194.  
  195. console.log("IRC> "+event+" with payload: "+payload);
  196.  
  197.  
  198. var payloadC = {'event': event,
  199. 'data': payload };
  200.  
  201. var payload_json = JSON.stringify(payloadC);
  202. this.redisClient.publish(BroadcastTo, payload_json );
  203. return this;
  204. };
  205.  
  206.  
  207. prototype.irc_steambotsay = function(bot, message)
  208. {
  209. if (typeof bot == "object" && typeof bot.username != "undefined") bot = bot.username;
  210. else if (typeof bot != "string") bot = "["+typeof bot+"] undefined";
  211.  
  212.  
  213. return this.irc("SteamBotSay", {nick:bot,
  214. response:message}
  215. );
  216. };
  217.  
  218. prototype.alert = function (user, message, type, options)
  219. {
  220.  
  221. if (typeof options == 'undefined')
  222. options = {timeout: 15000, layout: "topCenter", 'theme': 'bootstrapTheme', animation: {open: 'shake', close: 'hinge'}};
  223.  
  224.  
  225. // Alert, Success, Error, Warning, Information, Confirm
  226. if (type.indexOf('alert') == -1 && type.indexOf('error') == -1 && type.indexOf('warning') == -1 && type.indexOf('information') == -1) {
  227. type = 'information';
  228. }
  229.  
  230. var payload = {
  231. Message: message,
  232. Type: type || "information",
  233.  
  234. Options: {
  235. timeout: options.timeout || 15000,
  236. modal: false, // not implimented
  237. layout: options.layout || "topCenter",
  238. theme: options.theme || "bootstrapTheme",
  239.  
  240. animation: {
  241. open: options.animation.open || "shake",
  242. close: options.animation.close || "hinge"
  243. }
  244. }
  245. };
  246.  
  247.  
  248. return this.send(user, "Alert", payload );
  249.  
  250. };
  251.  
  252. //BroadcastToUser('STEAM_0:0:6771510', 'Steam_User_Items_Status', {Status: 'Broadcast by SteamID [STEAM_0:0:6771510] - Testing!', Type: 'error'});
  253. //
  254. //setTimeout(() => {
  255. // BroadcastToUser('Secure:5a831e69aaa1ed381d4f545d0a7e4689', 'Steam_User_Items_Status', {Status: 'Broadcast via Channel [Secure:5a831e69aaa1ed381d4f545d0a7e4689]', Type: 'warning'});
  256. //}, 2000);
  257. //
  258. //setTimeout(() => {
  259. // BroadcastToUser('13543020', 'Steam_User_Items_Status', {Status: 'Broadcast by account ID [13543020]', Type: 'warning'});
  260. //}, 3000);
  261. //
  262. //setTimeout(() => {
  263. // BroadcastToUser('[U:1:13543020]', 'Steam_User_Items_Status', {Status: 'Broadcast by account ID [U:1:267645796]', Type: 'error'});
  264. //}, 7000);
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. //var channel = 'private-broadcast-channel';
  274. //
  275. //var payload = {'event': 'App\\Events\\Steam_User_Items_Status',
  276. // 'data': {
  277. // BroadcastTo: 'Secure:5a831e69aaa1ed381d4f545d0a7e4689',
  278. // type: 'success', Status: 'Testing Broadcast System 1. 2. 3.' } };
  279. //
  280. //var payload = JSON.stringify(payload);
  281. //redisclient.publish(channel, payload);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement