connectionToClient.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // This script function is called before a client connection
  24. // is accepted. Returning "" will accept the connection,
  25. // anything else will be sent back as an error to the client.
  26. // All the connect args are passed also to onConnectRequest
  27. //
  28. function GameConnection::onConnectRequest( %client, %netAddress, %name )
  29. {
  30. echo("Connect request from: " @ %netAddress);
  31. if($Server::PlayerCount >= $pref::Server::MaxPlayers)
  32. return "CR_SERVERFULL";
  33. return "";
  34. }
  35. //-----------------------------------------------------------------------------
  36. // This script function is the first called on a client accept
  37. function GameConnection::onConnect( %this, %clientData )
  38. {
  39. // Send down the connection error info, the client is responsible for
  40. // displaying this message if a connection error occurs.
  41. messageClient(%this, 'MsgConnectionError', "", $Pref::Server::ConnectionError);
  42. // Send mission information to the client
  43. sendLoadInfoToClient(%this);
  44. // Simulated client lag for testing...
  45. // %client.setSimulatedNetParams(0.1, 30);
  46. // Get the client's unique id:
  47. // %authInfo = %client.getAuthInfo();
  48. // %client.guid = getField(%authInfo, 3);
  49. %this.guid = 0;
  50. addToServerGuidList(%this.guid);
  51. // Set admin status
  52. if (%this.getAddress() $= "local")
  53. {
  54. %this.isAdmin = true;
  55. %this.isSuperAdmin = true;
  56. }
  57. else
  58. {
  59. %this.isAdmin = false;
  60. %this.isSuperAdmin = false;
  61. }
  62. echo("CADD: "@ %this @" "@ %this.getAddress());
  63. // If the mission is running, go ahead download it to the client
  64. if ($missionRunning)
  65. {
  66. %this.loadMission();
  67. }
  68. else if ($Server::LoadFailMsg !$= "")
  69. {
  70. messageClient(%this, 'MsgLoadFailed', $Server::LoadFailMsg);
  71. }
  72. %this.connectData = %clientData;
  73. $Server::PlayerCount++;
  74. }
  75. //-----------------------------------------------------------------------------
  76. // A player's name could be obtained from the auth server, but for
  77. // now we use the one passed from the client.
  78. // %realName = getField( %authInfo, 0 );
  79. //
  80. function GameConnection::setPlayerName(%client,%name)
  81. {
  82. %client.sendGuid = 0;
  83. // Minimum length requirements
  84. %name = trim( strToPlayerName( %name ) );
  85. if ( strlen( %name ) < 3 )
  86. %name = "Poser";
  87. // Make sure the alias is unique, we'll hit something eventually
  88. if (!isNameUnique(%name))
  89. {
  90. %isUnique = false;
  91. for (%suffix = 1; !%isUnique; %suffix++) {
  92. %nameTry = %name @ "." @ %suffix;
  93. %isUnique = isNameUnique(%nameTry);
  94. }
  95. %name = %nameTry;
  96. }
  97. // Tag the name with the "smurf" color:
  98. %client.nameBase = %name;
  99. %client.playerName = addTaggedString("\cp\c8" @ %name @ "\co");
  100. }
  101. function isNameUnique(%name)
  102. {
  103. %count = ClientGroup.getCount();
  104. for ( %i = 0; %i < %count; %i++ )
  105. {
  106. %test = ClientGroup.getObject( %i );
  107. %rawName = stripChars( detag( getTaggedString( %test.playerName ) ), "\cp\co\c6\c7\c8\c9" );
  108. if ( strcmp( %name, %rawName ) == 0 )
  109. return false;
  110. }
  111. return true;
  112. }
  113. //-----------------------------------------------------------------------------
  114. // This function is called when a client drops for any reason
  115. //
  116. function GameConnection::onDrop(%client, %reason)
  117. {
  118. %entityIds = parseMissionGroupForIds("Entity", "");
  119. %entityCount = getWordCount(%entityIds);
  120. for(%i=0; %i < %entityCount; %i++)
  121. {
  122. %entity = getWord(%entityIds, %i);
  123. for(%e=0; %e < %entity.getCount(); %e++)
  124. {
  125. %child = %entity.getObject(%e);
  126. if(%child.getClassName() $= "Entity")
  127. %entityIds = %entityIds SPC %child.getID();
  128. }
  129. %entity.notify("onClientDisconnect", %client);
  130. }
  131. if($missionRunning)
  132. theLevelInfo.onClientLeaveGame();
  133. removeFromServerGuidList( %client.guid );
  134. $Server::PlayerCount--;
  135. }
  136. //-----------------------------------------------------------------------------
  137. function GameConnection::startMission(%this)
  138. {
  139. // Inform the client the mission starting
  140. commandToClient(%this, 'MissionStart', $missionSequence);
  141. }
  142. function GameConnection::endMission(%this)
  143. {
  144. // Inform the client the mission is done. Note that if this is
  145. // called as part of the server destruction routine, the client will
  146. // actually never see this comment since the client connection will
  147. // be destroyed before another round of command processing occurs.
  148. // In this case, the client will only see the disconnect from the server
  149. // and should manually trigger a mission cleanup.
  150. commandToClient(%this, 'MissionEnd', $missionSequence);
  151. }