Advertisement
irishstorm

ServerCore.js

Jan 21st, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1.  
  2. /* core.js
  3. Description : This script will hold the core elements for a dedicated server.
  4. Date : 21/01/2014 */
  5.  
  6. /* static function StartServer(value, value, value, value, value)
  7. This function will initialize a dedicated server and register it on the master server with the parameters given,
  8. this function is static so parameters can be introduced from other scripts and it will also print to the server
  9. log-file. */
  10. static function StartServer(gameName : String, hostName: String, hostDescription : String, slots : int, port : int)
  11. {
  12. // Add unity's built in security layer.
  13. Network.InitializeSecurity();
  14.  
  15. // Start the server. (max players, port, ipaddress)
  16. Network.InitializeServer(slots, port, !Network.HavePublicAddress);
  17.  
  18. // Register the server with the master server. (Game Name, Server Name, Server Description)
  19. MasterServer.RegisterHost(gameName, hostName, hostDescription);
  20.  
  21. // Set the server to be dedicated.
  22. MasterServer.dedicatedServer = true;
  23.  
  24. // Write/print to the logfile,screen and console.
  25. file.WriteToFile("Initalizing Server.", "Server");
  26. Error.message = "Initalizing Server.";
  27. Debug.Log("Initalizing Server.");
  28. }
  29.  
  30.  
  31. /* function OnServerInitialized()
  32. This function will print to the server log-file, to the screen and the console when the server has started. */
  33. function OnServerInitialized()
  34. {
  35. file.WriteToFile("Server Initialized", "Server");
  36. Error.message = "Server Initialized";
  37. Debug.Log("Server Initialized");
  38. }
  39.  
  40.  
  41. /* function OnMasterServerEvent(Value)
  42. This function will check for master server events such as, server registered or registration failed and it
  43. will print to the screen and to the server log-file. */
  44. function OnMasterServerEvent(mse : MasterServerEvent)
  45. {
  46. // checks for events.
  47. if(mse == MasterServerEvent.RegistrationSucceeded)
  48. {
  49. file.WriteToFile("Server is Registered", "Server");
  50. Error.message = "Server is Registered";
  51. }
  52.  
  53. if(mse == MasterServerEvent.RegistrationFailedNoServer)
  54. {
  55. file.WriteToFile("Server is not running", "Server");
  56. Error.message = "Server is not running";
  57. }
  58. }
  59.  
  60.  
  61. /* function OnDisconnectedFromServer(Value)
  62. This function is responsible for checking the server and players disconnection. */
  63. function OnDisconnectedFromServer(nw : NetworkDisconnection)
  64. {
  65. // if the server has disconnected then print to the console and server logfile.
  66. if(Network.isServer)
  67. {
  68. Debug.Log("Server Shutdown.");
  69. file.WriteToFile("Server Shutdown.", "Server");
  70. }
  71.  
  72.  
  73. // Client
  74. if(Network.isClient)
  75. {
  76. // If the player has disconnected, print to the console and server logfile.
  77. if(nw == NetworkDisconnection.Disconnected)
  78. {
  79. Debug.Log(login.username + " Disconnected.");
  80. file.WriteToFile(login.username + "Disconnected.", "Server");
  81. }
  82.  
  83. // if the player has lost connection, print to the console and server logfile.
  84. if(nw == NetworkDisconnection.LostConnection)
  85. {
  86. Debug.Log(login.username + " has lost connection.");
  87. file.WriteToFile(login.username + " has lost connection.", "Server");
  88. }
  89. }
  90.  
  91. Application.LoadLevel(Application.loadedLevel);
  92. }
  93.  
  94.  
  95. /* function OnPlayerConnected(player: NetworkPlayer)
  96. When the player is connected to the server, print to the console and server log-file the users username,
  97. ip and port. */
  98. function OnPlayerConnected(player : NetworkPlayer)
  99. {
  100. Debug.Log("Player " + player + " connected from " + player.ipAddress + ":" + player.port);
  101. file.WriteToFile("Player " + player + " connected from " + player.ipAddress + ":" + player.port, "Server");
  102. }
  103.  
  104.  
  105.  
  106. function OnPlayerDisconnected(player : NetworkPlayer)
  107. {
  108. Network.RemoveRPCs(player);
  109. Network.DestroyPlayerObjects(player);
  110.  
  111. Debug.Log("Player " + player + " disconnected");
  112. file.WriteToFile("Player " + player + " disconnected", "Server");
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement