SHARE
TWEET

Untitled

a guest Nov 22nd, 2018 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // evaluating libs with vars in current context instead of 'require'
  2. var util = require('util')
  3. var vm = require("vm");
  4. var fs = require("fs");
  5. var load = function(path, context) {
  6.   var data = fs.readFileSync(path);
  7.   vm.runInThisContext(data, path);
  8. }
  9.  
  10. var WebSocketClient = require("websocket").client
  11.  
  12. // adapter for browser websocket
  13. WebSocket = function (uri) {
  14.     console.info("============", uri);
  15.     var self = this;
  16.     this.connection = null;
  17.     this.socket = new WebSocketClient();
  18.     this.socket.on('connectFailed', function (errorDescription) {
  19.         self.onerror(errorDescription);
  20.     })
  21.     this.socket.on('connect', function (connection) {
  22.         console.info("============");
  23.         self.connection = connection;
  24.  
  25.         connection.on('error', function (error) {
  26.             console.info("WS error:", error);
  27.             self.onerror(error);
  28.         });
  29.  
  30.         connection.on('close', function (ev) {
  31.             console.info("WS close:", ev);
  32.             self.onclose(ev);
  33.         });
  34.  
  35.         connection.on('message', function (message) {
  36. //          console.info("WS message:", message);
  37.             if (message.type === 'utf8') {
  38.                 self.onmessage({data:message.utf8Data});
  39.             }
  40.             return true;
  41.         });
  42.  
  43.         self.onopen();
  44.     });
  45.     this.socket.connect(uri);
  46. }
  47.  
  48. WebSocket.prototype.send = function (data) {
  49.     if(this.connection)
  50.         this.connection.sendUTF(data);
  51. }
  52.  
  53. WebSocket.prototype.close = function () {
  54.     if(this.connection)
  55.         this.connection.close();
  56. }
  57. ///////
  58.  
  59. load("./photon/Photon-Javascript_SDK.js")
  60.  
  61. var LBC = Photon.LoadBalancing.LoadBalancingClient;
  62. var lbc = new LBC(Photon.ConnectionProtocol.Ws, process.env.PHOTONAPPID, "0.3.1a");
  63.  
  64. lbc.regions = null;
  65. lbc.connectToNextRegion = false;
  66. lbc.setLogLevel(Exitgames.Common.Logger.Level.WARN);
  67.  
  68. lbc.onStateChange = function (state)
  69. {
  70.     console.info("State:", LBC.StateToName(state));
  71.     switch (state) {
  72.         case LBC.State.ConnectedToNameServer:    
  73.             if (lbc.regions == null)  {
  74.                 lbc.getRegions();
  75.             }
  76.             break;        
  77.         case LBC.State.ConnectedToMaster:                
  78.             break;                    
  79.         case LBC.State.Disconnected:
  80.             if (lbc.connectToNextRegion)
  81.             {
  82.                 lbc.connectToNextRegion = false;
  83.                 lbc.connect();
  84.             }
  85.             break;
  86.         default:
  87.             break;
  88.     }
  89. };
  90.  
  91. lbc.onEvent = function (code, data) {
  92.     console.info("Event:", code, data);
  93.     lbc.raiseEvent(code, data);
  94. }
  95.  
  96. lbc.onOperationResponse = function (errorCode, errorMsg, code, content) {
  97.     //console.info("op resp:", errorCode, errorMsg, code, content);
  98.     if (errorCode) {
  99.         switch (code) {
  100.             default:
  101.                 console.error("Operation Response error:", errorCode, errorMsg, code, content);
  102.                 break;
  103.         }
  104.     }
  105. };
  106.  
  107. lbc.onGetRegionsResult = function (errorCode, errorMsg, regions)
  108. {    
  109.     if (errorCode != 0) {
  110.         console.error("onGetRegionsResult:", errorCode, errorMsg);
  111.         return;
  112.     }
  113.  
  114.     lbc.regions = [];
  115.     lbc.regionIndex = 0;
  116.  
  117.     console.info("Photon regions: ");
  118.  
  119.     // create array of IP addresses
  120.     for (var key in regions) {
  121.         if (regions.hasOwnProperty(key))
  122.         {
  123.             var region = {
  124.                 code: key,
  125.                 address: regions[key],
  126.                 masterPeerCount: 0,
  127.                 peerCount: 0,
  128.                 gameCount: 0
  129.             }
  130.  
  131.             lbc.regions.push(region);
  132.             console.info(key + " : " + regions[key]);
  133.         }
  134.     }  
  135.  
  136.     if (lbc.regions.length > 0)
  137.     {
  138.         lbc.setMasterServerAddress(lbc.regions[0].address);
  139.         lbc.connect();
  140.     }    
  141. };
  142.  
  143. lbc.onLobbyStats = function (errorCode, errorMsg, lobbies)
  144. {
  145.     if (errorCode != 0) {
  146.         console.error("onLobbyStats:", errorCode, errorMsg);
  147.         return;
  148.     }    
  149.     console.info("onLobbyStats: " + lobbies.length);
  150.     for (var i=0; i < lobbies.length; i++)
  151.     {
  152.         var lobby = lobbies[i];
  153.         console.info("lobby: " + lobby.lobbyName + " lobbyType: " + lobby.lobbyType + " peerCount: " + lobby.peerCount + " gameCount: " + lobby.gameCount);
  154.     }
  155.  
  156.     lbc.disconnect();    
  157. };
  158.  
  159. lbc.onAppStats = function (errorCode, errorMsg, stats)
  160. {
  161.     if (errorCode != 0) {
  162.         console.error("onAppStats:", errorCode, errorMsg);
  163.         return;
  164.     }    
  165.    
  166.     console.info("appStats: " + stats.masterPeerCount + " " + stats.peerCount + " " + stats.gameCount);
  167.     lbc.requestLobbyStats();
  168.  
  169.     if (lbc.regionIndex < lbc.regions.length)
  170.     {
  171.         var region = lbc.regions[lbc.regionIndex];
  172.  
  173.         console.info(region.code + " (" + (lbc.regionIndex+1) + "/" + lbc.regions.length + ") : games " + stats.gameCount + " master peers: " + stats.masterPeerCount + " peers " + stats.peerCount );
  174.  
  175.         region.masterPeerCount = Math.max(0, stats.masterPeerCount - 1); // - this bot
  176.         region.peerCount = stats.peerCount;
  177.         region.gameCount = stats.gameCount;
  178.  
  179.         lbc.regions[lbc.regionIndex] = region;
  180.  
  181.         // connect to next region
  182.         lbc.regionIndex++;
  183.         if (lbc.regionIndex < lbc.regions.length)
  184.         {
  185.             console.info("Connecting to: " + lbc.regions[lbc.regionIndex].code);
  186.             lbc.setMasterServerAddress(lbc.regions[lbc.regionIndex].address);
  187.             lbc.connectToNextRegion = true;
  188.         }      
  189.         else
  190.         {        
  191.             // regions done save to file
  192.  
  193.             fs.writeFile("./regions.json", JSON.stringify(lbc.regions, null, 2) , 'utf-8', function(err) {
  194.                 if(err) {
  195.                     return console.log(err);
  196.                 }
  197.                 console.log("Saved regions to file.");
  198.             });            
  199.         }
  200.     }
  201. };
  202.  
  203. module.exports.lbc = lbc;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top