//method that call a proper method from client contract private ReturnData SendDataToClient(DataSenderType dataType, IMakaoGameClientService proxy, object dataToTransfer) { ReturnData output = null; var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info($"Sending info to specific player in the room: {dataType.ToString()}."); try { switch (dataType) { case DataSenderType.EngineInstanceCreatedData: output = EngineInstanceCreatedDataHandler(proxy, dataToTransfer); break; case DataSenderType.EngineInstanceUpdateData: output = EngineDataUpdateHandler(proxy, dataToTransfer); break; case DataSenderType.GameFinished: output = EndGameHandler(proxy, dataToTransfer); break; } } catch (Exception ex) { var logger2 = NLog.LogManager.GetCurrentClassLogger(); logger2.Error($"Sending info to specific player in the room: {dataType.ToString()} failed: {ex.Message}"); } return(output); }
//method for sending proper info private void SendInfoMessageToClient(ClientInfoType messageType, IMakaoGameClientService proxy, object dataToSend) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Sending info to specific player in the room: " + messageType.ToString()); switch (messageType) { case ClientInfoType.PlayersDataChanged: proxy.UpdateTheCurrentListOfPlayers(); break; case ClientInfoType.ClosedByHost: proxy.RoomWasDeleted(DeletionReason.ClosedByHost); break; case ClientInfoType.PlayersReadinessChanged: proxy.UpdatePlayersGameReadinessData((ActualizedPlayersReadinessDataRequest)dataToSend); break; case ClientInfoType.JoiningTimeout: proxy.RoomWasDeleted(DeletionReason.JoiningTimeout); break; case ClientInfoType.ReadinessTimeout: proxy.RoomWasDeleted(DeletionReason.ReadinessTimeout); break; case ClientInfoType.CheckAliveness: CheckAlivenessOfPlayer(proxy); break; case ClientInfoType.LostConnectionToClient: proxy.RoomWasDeleted(DeletionReason.LostConnection); break; case ClientInfoType.PlayerLeftGame: proxy.RoomWasDeleted(DeletionReason.PlayerLeftGame); break; } }
//create new channel factory private void ChannelFactoryCreation(Uri endpoint, ClientInfoType messageType, object dataToSend) { try { ChannelFactory <IMakaoGameClientService> factory = new ChannelFactory <IMakaoGameClientService> (new BasicHttpBinding(), new EndpointAddress(endpoint)); IMakaoGameClientService proxy = factory.CreateChannel(); SendInfoMessageToClient(messageType, proxy, dataToSend); } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("Couldn't connect to the client endpoint while sending info" + " to every current player from list: " + messageType + "; " + ex.Message); } }
//send info about game end private ReturnData EndGameHandler(IMakaoGameClientService proxy, object dataToTransfer) { try { proxy.ShowGameResultsWindow((GameFinishedDataRequest)dataToTransfer); } catch (Exception ex) { CatchConnectionException(ex, (dataToTransfer as GameFinishedDataRequest).WinnerPlayerNumber); } return(new ReturnData() { Response = true, PlayerID = "", PlayerNumber = 0 }); }
//calling the method that returns true if the service is running public static bool CheckIfServiceIsAlive() { bool response = false; try { IMakaoGameClientService proxy = factory.CreateChannel(); bool proxyResponse = proxy.CheckIfServiceIsWorking(); response = proxyResponse & !clientServiceNotStarted; } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("MakaoGameClientService is not running: " + ex.Message); } return(response); }
//update the game private ReturnData EngineDataUpdateHandler(IMakaoGameClientService proxy, object dataToTransfer) { UpdatingGameStatusResponse response = null; ReturnData output = null; try { response = proxy.UpdateGameStateAndData((PersonalizedPlayerDataRequest)dataToTransfer); } catch (Exception ex) { CatchConnectionException(ex, (dataToTransfer as PersonalizedPlayerDataRequest).DataOfThisPlayer.ThisPlayerNumber); } if (response != null) { output = GenerateReturnData(response); } return(output); }
//start new game private ReturnData EngineInstanceCreatedDataHandler(IMakaoGameClientService proxy, object dataToTransfer) { UpdatingGameStatusResponse response = null; ReturnData output = null; try { response = proxy.StartNewGameWindow((PersonalizedForSpecificPlayerStartGameDataRequest)dataToTransfer); } catch (Exception ex) { CatchConnectionException(ex, (dataToTransfer as PersonalizedForSpecificPlayerStartGameDataRequest).DataOfThisPlayer.ThisPlayerNumber); } if (response != null) { output = GenerateReturnData(response); } return(output); }
//method for creating channel factory private ReturnData ChannelFactoryCreation(Uri endpoint, DataSenderType dataType, object dataToSend) { ReturnData output = null; try { //creata channel factory based on tje uri ChannelFactory <IMakaoGameClientService> factory = new ChannelFactory <IMakaoGameClientService> (new BasicHttpBinding(), new EndpointAddress(endpoint)); //now create proxy IMakaoGameClientService proxy = factory.CreateChannel(); //and call some method output = SendDataToClient(dataType, proxy, dataToSend); } catch (Exception ex) { var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error($"Couldn't connect to the client endpoint game state data. DataType: {dataType.ToString()}. Exception: {ex.Message}."); } return(output); }
//method for checking aliveness of one player private void CheckAlivenessOfPlayer(IMakaoGameClientService proxy) { bool alive = proxy.CheckIfServiceIsWorking(); AlivenessList.Add(alive); }