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