PageRenderTime 80ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/CatalogueRaisonne/CRServer/Source/BackendAsyncOperations.cs

https://github.com/bretambrose/CatalogueRaisonnePublic
C# | 202 lines | 144 code | 29 blank | 29 comment | 6 complexity | 782cd5593666ad9ea7e160e39c3fc637 MD5 | raw file
  1. /*
  2. BackendAsyncOperations.cs
  3. (c) Copyright 2010-2011, Bret Ambrose (mailto:bretambrose@gmail.com).
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using CRShared;
  17. using CRShared.Chat;
  18. using CRServer.Chat;
  19. namespace CRServer
  20. {
  21. public static class CAsyncBackendOperations
  22. {
  23. // Public interface
  24. // Operations
  25. // Channel Creation
  26. public static void Create_Lobby_Chat_Channel( CChatChannelConfig channel_config, ELobbyID lobby_id, EPersistenceID source_player )
  27. {
  28. CCreateChatChannelRequestServerMessage create_lobby_channel_message = new CCreateChatChannelRequestServerMessage( channel_config );
  29. create_lobby_channel_message.Handler =
  30. delegate( CResponseMessage response )
  31. {
  32. CCreateChatChannelResponseServerMessage response_msg = response as CCreateChatChannelResponseServerMessage;
  33. On_Lobby_Chat_Channel_Creation_Response( lobby_id, source_player, response_msg );
  34. };
  35. CServerMessageRouter.Send_Message_To_Chat_Server( create_lobby_channel_message );
  36. }
  37. public static void Create_Match_Chat_Channel( CChatChannelConfig match_channel_config, EMatchInstanceID match_id )
  38. {
  39. CCreateChatChannelRequestServerMessage create_match_channel_message = new CCreateChatChannelRequestServerMessage( match_channel_config );
  40. create_match_channel_message.Handler =
  41. delegate( CResponseMessage response )
  42. {
  43. CCreateChatChannelResponseServerMessage response_msg = response as CCreateChatChannelResponseServerMessage;
  44. On_Match_Chat_Channel_Creation_Response( match_id, response_msg );
  45. };
  46. CServerMessageRouter.Send_Message_To_Chat_Server( create_match_channel_message );
  47. }
  48. public static void Create_Observer_Chat_Channel( CChatChannelConfig observer_channel_config, EMatchInstanceID match_id )
  49. {
  50. CCreateChatChannelRequestServerMessage create_observer_channel_message = new CCreateChatChannelRequestServerMessage( observer_channel_config );
  51. create_observer_channel_message.Handler =
  52. delegate( CResponseMessage response )
  53. {
  54. CCreateChatChannelResponseServerMessage response_msg = response as CCreateChatChannelResponseServerMessage;
  55. On_Observer_Chat_Channel_Creation_Response( match_id, response_msg );
  56. };
  57. CServerMessageRouter.Send_Message_To_Chat_Server( create_observer_channel_message );
  58. }
  59. public static void New_Player_Announce_To_Chat( CConnectedPlayer player, CPersistentPlayerData player_data, EMessageRequestID client_request_id )
  60. {
  61. ESessionID session_id = player.SessionID;
  62. CAnnouncePlayerToChatServerRequest announce_request = new CAnnouncePlayerToChatServerRequest( session_id, player.PersistenceID, player.Name, player_data.IgnoreList );
  63. announce_request.Handler = delegate( CResponseMessage response ) { On_Chat_Server_Announce_Response( response, session_id, client_request_id ); };
  64. CServerMessageRouter.Send_Message_To_Chat_Server( announce_request );
  65. }
  66. // Player ops
  67. public static void Join_General_Channel( EPersistenceID player_id, EMessageRequestID client_request_id )
  68. {
  69. ESessionID session_id = CConnectedPlayerManager.Instance.Get_Active_Player_Session_ID( player_id );
  70. Change_System_Channel( player_id,
  71. CConnectedPlayerManager.Instance.GeneralChatChannel,
  72. EChannelGameProperties.OrdinarySingletonMask,
  73. delegate( CResponseMessage response ) { On_Join_General_Channel_Response( response, session_id, client_request_id ); } );
  74. }
  75. public static void Player_Join_Lobby_Channel( EPersistenceID player_id, ELobbyID lobby_id, EChannelID channel_id )
  76. {
  77. Change_System_Channel( player_id,
  78. channel_id,
  79. EChannelGameProperties.OrdinarySingletonMask,
  80. delegate( CResponseMessage response ) {
  81. On_Lobby_Chat_Channel_Join_Response( player_id, lobby_id, response as CJoinChatChannelResponseServerMessage );
  82. } );
  83. }
  84. public static void Join_Match_Channel( EMatchInstanceID match_id, EPersistenceID player_id, EChannelID match_channel_id )
  85. {
  86. Change_System_Channel( player_id,
  87. match_channel_id,
  88. EChannelGameProperties.OrdinarySingletonMask,
  89. delegate( CResponseMessage response ) {
  90. On_Match_Chat_Channel_Join_Response( match_id, player_id, response as CJoinChatChannelResponseServerMessage );
  91. } );
  92. }
  93. public static void Join_Observer_Channel( EMatchInstanceID match_id, EPersistenceID player_id, EChannelID observer_channel_id )
  94. {
  95. Change_System_Channel( player_id,
  96. observer_channel_id,
  97. EChannelGameProperties.OrdinarySingletonMask,
  98. delegate( CResponseMessage response ) {
  99. On_Observer_Chat_Channel_Join_Response( match_id, player_id, response as CJoinChatChannelResponseServerMessage );
  100. } );
  101. }
  102. // Private interface
  103. // Callbacks
  104. // Channel Creation
  105. private static void On_Lobby_Chat_Channel_Creation_Response( ELobbyID lobby_id, EPersistenceID player_id, CCreateChatChannelResponseServerMessage response_msg )
  106. {
  107. CServerLobbyManager.Instance.On_Lobby_Chat_Channel_Creation_Response( lobby_id, player_id, response_msg );
  108. }
  109. private static void On_Match_Chat_Channel_Creation_Response( EMatchInstanceID match_id, CCreateChatChannelResponseServerMessage response_msg )
  110. {
  111. CServerMatchInstanceManager.Instance.On_Match_Chat_Channel_Creation_Response( match_id, response_msg );
  112. }
  113. private static void On_Observer_Chat_Channel_Creation_Response( EMatchInstanceID match_id, CCreateChatChannelResponseServerMessage response_msg )
  114. {
  115. CServerMatchInstanceManager.Instance.On_Observer_Chat_Channel_Creation_Response( match_id, response_msg );
  116. }
  117. // Player Ops
  118. private static void On_Chat_Server_Announce_Response( CResponseMessage response, ESessionID session_id, EMessageRequestID client_request_id )
  119. {
  120. if ( !CConnectedPlayerManager.Instance.Is_Connection_Valid( session_id ) )
  121. {
  122. return;
  123. }
  124. CAnnouncePlayerToChatServerResponse message = response as CAnnouncePlayerToChatServerResponse;
  125. CAnnouncePlayerToChatServerRequest request = message.Request as CAnnouncePlayerToChatServerRequest;
  126. CConnectedPlayer player = CConnectedPlayerManager.Instance.Get_Active_Player_By_Persistence_ID( request.PersistenceID );
  127. if ( player != null )
  128. {
  129. CConnectedPlayerManager.Instance.On_Chat_Server_Announce_Response( player, message.Success, client_request_id );
  130. }
  131. }
  132. private static void On_Join_General_Channel_Response( CResponseMessage response, ESessionID session_id, EMessageRequestID client_request_id )
  133. {
  134. if ( !CConnectedPlayerManager.Instance.Is_Connection_Valid( session_id ) )
  135. {
  136. return;
  137. }
  138. CJoinChatChannelResponseServerMessage message = response as CJoinChatChannelResponseServerMessage;
  139. CJoinChatChannelRequestServerMessage request = message.Request as CJoinChatChannelRequestServerMessage;
  140. EPersistenceID player_id = request.PlayerID;
  141. CConnectedPlayer player = CConnectedPlayerManager.Instance.Get_Active_Player_By_Persistence_ID( player_id );
  142. if ( player != null )
  143. {
  144. player.On_Join_General_Channel_Response( message.Error, client_request_id );
  145. }
  146. }
  147. private static void On_Lobby_Chat_Channel_Join_Response( EPersistenceID player_id, ELobbyID lobby_id, CJoinChatChannelResponseServerMessage response )
  148. {
  149. CServerLobbyManager.Instance.On_Lobby_Chat_Channel_Join_Response( lobby_id, player_id, response );
  150. }
  151. private static void On_Match_Chat_Channel_Join_Response( EMatchInstanceID match_id, EPersistenceID player_id, CJoinChatChannelResponseServerMessage response )
  152. {
  153. CServerMatchInstanceManager.Instance.On_Match_Chat_Channel_Join_Response( match_id, player_id, response );
  154. }
  155. private static void On_Observer_Chat_Channel_Join_Response( EMatchInstanceID match_id, EPersistenceID player_id, CJoinChatChannelResponseServerMessage response )
  156. {
  157. CServerMatchInstanceManager.Instance.On_Observer_Chat_Channel_Join_Response( match_id, player_id, response );
  158. }
  159. // Helper
  160. private static void Change_System_Channel( EPersistenceID player_id, EChannelID new_channel_id, EChannelGameProperties remove_mask, DRequestResponseHandler response_handler )
  161. {
  162. CJoinChatChannelRequestServerMessage change_request = new CJoinChatChannelRequestServerMessage( player_id, new_channel_id, remove_mask );
  163. change_request.Handler = response_handler;
  164. CServerMessageRouter.Send_Message_To_Chat_Server( change_request );
  165. }
  166. }
  167. }