//sending the request to the host private AddNewPlayerResponse ObtainJoiningResult(Uri clientEndpoint, string password) { AddNewPlayerRequest request = new AddNewPlayerRequest() { PlayerName = MainWindow.SavedData.CurrentPlayerName, PlayedGames = MainWindow.SavedData.SavedAmountOfPlayedGames, PlayedAndWonGames = MainWindow.SavedData.SavedAmountOfPlayedAndWonGames, PlayerEndpoint = clientEndpoint, Password = password, }; IMakaoGameHostService proxy = factory.CreateChannel(); try { AddNewPlayerResponse response = proxy.AddNotHostPlayerToTheRoom(request); return(response); } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error($"Error while trying to send password to host: {ex.Message}"); return(null); } }
//method for sending an avatar image to the host private void SendAvatarImageToHost(int playerNumber) { //sending data with image MemoryStream avatarMemoryStream = new MemoryStream(); byte[] bytes = File.ReadAllBytes(MainWindow.SavedData.CurrentAvatarPicture); avatarMemoryStream.Write(bytes, 0, bytes.Length); avatarMemoryStream.Position = 0; IMakaoGameHostService proxy = factory.CreateChannel(); //actual sending bool success = EngineHostHandler.SendAvatarToHost(avatarMemoryStream, playerNumber, proxy); if (!success) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("Player's avatar not assigned in the host implementation"); MessageBox.Show("Twojego avatara nie udało się przesłać do hosta.", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information); } else { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Player's avatar successfully assigned in the host implementation"); //if successfully joined the game, fire the event - it sholud be //served in the main window. SynchCont.Post(_ => OnSuccessfullyJoinedToTheRoom(), null); } }
public static bool SetGameSetup() { //create request AssignGameSetupDataRequest request = new AssignGameSetupDataRequest() { AmountOfPlayers = MainWindow.SavedData.SavedAmountOfPlayers, AmountOfDecks = MainWindow.SavedData.SavedAmountOfDecks, AmountOfJokers = MainWindow.SavedData.SavedAmountOfJokers, AmountOfStartCards = MainWindow.SavedData.SavedAmountOfStartCards, RoomPassword = password, HostID = MainWindow.PlayerID, HostName = MainWindow.SavedData.CurrentPlayerName, JoiningTimeoutEnabled = MainWindow.SavedData.JoiningTimeoutEnabled, JoiningTimeoutInMinutes = MainWindow.SavedData.JoiningTimeoutMinutesAmount, ReadinessTimeoutEnabled = MainWindow.SavedData.ReadinessTimeoutEnabled, ReadinessTimeoutInMinutes = MainWindow.SavedData.ReadinessTimeoutMinutesAmount, }; //create new proxy IMakaoGameHostService proxy = factory.CreateChannel(); bool ssetupAssigned = proxy.AssignGameSetupData(request); return(ssetupAssigned); }
//retrieving data from host private GetRoomDetailsWhenJoiningRoomResponse ObtainHostDetailedData() { IMakaoGameHostService proxy = factory.CreateChannel(); GetRoomDetailsWhenJoiningRoomResponse response = proxy.GetHostRoomDetails(); return(response); }
//method that gets data from host private CurrentPlayersListDataResponse GetCurrentPlayersDataFromHost() { IMakaoGameHostService proxy = factory.CreateChannel(); CurrentPlayersListDataResponse response = proxy.GetCurrentPlayersInTheRoomData(); return(response); }
//method thad downloads all images, for all players and saves as files private void DownloadPLayersAvatars(List <int> playersNumbers) { IMakaoGameHostService proxy = factory.CreateChannel(); foreach (int item in playersNumbers) { Stream responseStream = proxy.DownloadAvatarImage(item); EngineHostHandler.SaveAnImage(responseStream, item); } }
//deletion from room algorthm private void DeleteUserFromRoomAlgorithmMethod(LeavingTheRoomWindowType windowType) { LeaveTheRoomRequest request = null; IMakaoGameHostService proxy = null; try //try to connect to service host - may be problem when didin't connect before and endpoint is not assigned { if (makaoGameHostEndpoint == null) { AssignHostEndpoint(MakaoGameHostServiceEndpointObtainer.GetHostEndpointWhileBeeingHost()); } ChannelFactory <IMakaoGameHostService> factory = new ChannelFactory <IMakaoGameHostService>(new BasicHttpBinding(), new EndpointAddress(MakaoGameHostEndpoint)); //creating request request = new LeaveTheRoomRequest() { IsHostPlayer = IsHostPlayer, PlayerID = PlayerID, PlayerName = SavedDataPlaceholder.CurrentPlayerName, PlayerNumber = PlayerNumber, ClosedWindowType = windowType, }; proxy = factory.CreateChannel(); } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("Error while making attemp to connect to host to delete player " + ex.Message + ex.StackTrace); } bool response = false; try { response = proxy.DeletePlayerFromRoom(request); } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("Error while obtaning response from host about deletion of player: " + ex.Message + ex.StackTrace); } if (response) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Current player was deleted from room"); } if (IsHostPlayer == true) { Task.Run(() => StopTheHostServiceWithDelay(3)); } }
private void SendInfoAboutMovementToHost(MakeAMoveRequest request, SynchronizationContext synchCont) { try { IMakaoGameHostService proxy = factory.CreateChannel(); bool response = proxy.PerformPlayerMove(request); if (!response) { synchCont.Post(_ => ShowInfoAboutMovePerformingError(), null); } } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error($"Error while performing a move - sending data to host: {ex.Message}."); } }
//method for adding the host player to the room public static AddNewPlayerResponse AddHostPlayerToRoom() { //create the request AddNewPlayerRequest addPlayerRequest = new AddNewPlayerRequest() { PlayerName = MainWindow.SavedData.CurrentPlayerName, PlayedGames = MainWindow.SavedData.SavedAmountOfPlayedGames, PlayerEndpoint = MainWindow.ThisClientEndpoint, Password = password, PlayedAndWonGames = MainWindow.SavedData.SavedAmountOfPlayedAndWonGames }; //creating new proxy IMakaoGameHostService proxy = factory.CreateChannel(); //sending data without an image AddNewPlayerResponse response = proxy.CreateNewRoomAndHostPlayer(addPlayerRequest); //if user was added to room if (response.AddedToGame) { //assigning an ID for player MainWindow.PlayerID = response.PlayerID; MainWindow.PlayerNumber = response.PlayerNumber; MainWindow.IsHostPlayer = response.IsHostPlayer; MainWindow.TotalAMountOfPlayerObtainedFromHost = response.TotalAMountOfPlayers; //assign data to player info storage in client service CurrentPlayerDataStorage.PlayerNumber = response.PlayerNumber; CurrentPlayerDataStorage.PlayerID = response.PlayerID; CurrentPlayerDataStorage.PlayerName = response.PlayerName; //change app status MainWindow.ChangeGameStatus(response.IsHostPlayer); //sending data with image MemoryStream avatarMemoryStream = new MemoryStream(); byte[] bytes = File.ReadAllBytes(MainWindow.SavedData.CurrentAvatarPicture); avatarMemoryStream.Write(bytes, 0, bytes.Length); avatarMemoryStream.Position = 0; //calling method for sending to and downloding avatar from host SendAnImage(avatarMemoryStream, response.PlayerNumber, proxy); } return(response); }
private void ChangeCardBackToJoker(ChangeJokerBackRequest request, MainUser control, SynchronizationContext synchCont) { try { IMakaoGameHostService proxy = factory.CreateChannel(); ChangeJokerCardResponse response = proxy.ChangeCardIntoJokerBack(request); if (response.PlayerID == MainWindow.PlayerID) { synchCont.Post(_ => AssignMainControCards(control, response.CurrentPlayerCards), null); } } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error($"Error while changing joker back: {ex.Message}."); } }
//algorithm method private void StartGameReadinessHandlingMethod(Uri endpoint) { //create factory channel StartNewFactoryChannel(endpoint); //creating the request PlayerIsReadyToPlayGameRequest request = new PlayerIsReadyToPlayGameRequest() { IsReadyToPlay = true, PlayerID = MainWindow.PlayerID, PlayerNumber = MainWindow.PlayerNumber, }; //building proxy and obtaining the response IMakaoGameHostService proxy = factory.CreateChannel(); bool response = proxy.ChangePlayerReadinessToPlay(request); //if response is true, deactivate the button if (response) { SynchCont.Post(_ => ReadyToPlayButton.IsEnabled = false, null); } }
//method for sending avatar to the host (host will store it) public static bool SendAvatarToHost(MemoryStream avatarMemoryStream, int playerNumber, IMakaoGameHostService proxy) { //building game host service request object for sending an avatar SaveAvatarRequest addAvatarRequest = new SaveAvatarRequest() { AvatarStream = avatarMemoryStream, PlayerNumber = playerNumber, }; //and serializing it MemoryStream requestSerialized = EngineHost.DataPlaceholders.AvatarSerializerDeserializer.SerializeToStream(addAvatarRequest); requestSerialized.Position = 0; //uploading an avatar image bool streamSuccess = proxy.UploadAvatarImagePlayer(requestSerialized); return(streamSuccess); }
//internal method for sending user's current avatar to the host private static void SendAnImage(MemoryStream avatarMemoryStream, int playerNumber, IMakaoGameHostService proxy) { //uploading an avatar image bool streamSuccess = SendAvatarToHost(avatarMemoryStream, playerNumber, proxy); //if image succesfully saved in host, download it back and save if (streamSuccess) { Stream downloadedImage = proxy.DownloadAvatarImage(playerNumber); SaveAnImage(downloadedImage, playerNumber); } }