public async Task <GetGameDefinitionResponse> GetGameDefinitionAsync(GetGameDefinitionRequest request) { var response = ServiceMessageFactory <GetGameDefinitionResponse> .CreateFrom(request); response.GameDefinition = cache.FirstOrDefault(i => i.Id == request.GameDefinitionId); return(await Task.FromResult(response)); }
public void SetRemoteAddressToHostServerNameIfNotAlreadySet() { var hostAddress = GetHostAddress(); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal(hostAddress, message.Headers.GetRemoteAddress()); }
public void DoNotSetOriginToHostServerNameIfAlreadySet() { var hostAddress = GetHostAddress(); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(new[] { new Header(Header.RemoteAddress, IPAddress.None.ToString(), checkReservedNames: false) }, new Object()); Assert.NotEqual(hostAddress, message.Headers.GetRemoteAddress()); }
public void SetRemoteAddressToHostServerNameIfNotAlreadySet() { var hostAddress = GetHostAddress(); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal(hostAddress, message.Headers.GetRemoteAddress()); }
public void DoNotSetOriginToHostServerNameIfAlreadySet() { var hostAddress = GetHostAddress(); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(new[] { new Header(Header.RemoteAddress, IPAddress.None.ToString(), checkReservedNames: false) }, new Object()); Assert.NotEqual(hostAddress, message.Headers.GetRemoteAddress()); }
public void DoNotSetUserNameIfThreadPrincipalIsNull() { Thread.CurrentPrincipal = null; var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal(String.Empty, message.Headers.GetUserName()); }
public void DoNotSetUserNameToThreadPrincipalIdentityNameIfAlreadySet() { Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("*****@*****.**"), new String[0]); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(new[] { new Header(Header.UserName, "*****@*****.**", checkReservedNames: false) }, new Object()); Assert.NotEqual("*****@*****.**", message.Headers.GetUserName()); }
public void SetUserNameToThreadPrincipalIdentityNameIfNotAlreadySet() { Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("*****@*****.**"), new String[0]); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal("*****@*****.**", message.Headers.GetUserName()); }
public void SetUserNameToThreadPrincipalIdentityNameIfNotAlreadySet() { Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("*****@*****.**"), new String[0]); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal("*****@*****.**", message.Headers.GetUserName()); }
public void DoNotSetUserNameToThreadPrincipalIdentityNameIfAlreadySet() { Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("*****@*****.**"), new String[0]); var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(new[] { new Header(Header.UserName, "*****@*****.**", checkReservedNames: false) }, new Object()); Assert.NotEqual("*****@*****.**", message.Headers.GetUserName()); }
public void DoNotSetUserNameIfThreadPrincipalIsNull() { Thread.CurrentPrincipal = null; var messageFactory = new ServiceMessageFactory(); var message = messageFactory.Create(HeaderCollection.Empty, new Object()); Assert.Equal(String.Empty, message.Headers.GetUserName()); }
public async Task <GetPlayerResponse> GetPlayerAsync(GetPlayerRequest request) { var response = ServiceMessageFactory <GetPlayerResponse> .CreateFrom(request); response.Player = cache.FirstOrDefault(i => i.Id == request.PlayerId); if (response.Player == null) { response.Errors = "Unable to find player."; } return(await Task.FromResult(response)); }
internal static T SerializeDeserializeTest <T>(T message) where T : ServiceMessage { var messageBytes = message.GetBytes(); var dm = ServiceMessageFactory.GetServiceMessage(messageBytes, 0); dm.Should().BeOfType <T>(); dm.Should().BeEquivalentTo(message); return((T)dm); }
public async Task <GetGamesResponse> GetGamesAsync(GetGamesRequest request) { var response = ServiceMessageFactory <Interface.GetGamesResponse> .CreateFrom(request); var gameDefinitionRequest = ServiceMessageFactory <Access.GameDefinition.Interface.GetGameDefinitionsRequest> .CreateFrom(request); var gameDefinitionResponse = await gameDefinitionAccess.GetGameDefinitionsAsync(gameDefinitionRequest); response.GameDefinitions = gameDefinitionResponse.GameDefinitions.Convert(); return(response); }
public async Task <IsTileOpenResponse> IsTileOpenAsync(IsTileOpenRequest request) { var response = ServiceMessageFactory <IsTileOpenResponse> .CreateFrom(request); var tileRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tileRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tileResponse = await tileAccess.FindTilesAsync(tileRequest); response.Value = tileResponse.Tiles.First(i => i.Address == request.Address).IsEmpty; return(response); }
public async Task <GetGameDefinitionsResponse> GetGameDefinitionsAsync(GetGameDefinitionsRequest request) { var response = ServiceMessageFactory <GetGameDefinitionsResponse> .CreateFrom(request); response.GameDefinitions = cache.ToArray(); if (!response.GameDefinitions.Any()) { response.Errors = "Unable to find any game definitions."; logger.LogError($"Initialization Error! Unable to find any game definitions."); } return(await Task.FromResult(response)); }
public void CanSerializeToJson() { var headers = new ServiceMessageFactory().Create(null, new Object()).Headers; var json = WriteJson(headers); Validate(json, @" { ""_t"": """ + headers.GetTimestamp().ToString(DateTimeFormat.RoundTrip) + @""", ""_o"": """ + headers.GetOrigin() + @""", ""_r"": """ + headers.GetRemoteAddress() + @""" }"); }
public void CanSerializeToJson() { var headers = new ServiceMessageFactory().Create(null, new Object()).Headers; var json = WriteJson(headers); Validate(json, @" { ""_t"": """ + headers.GetTimestamp().ToString(DateTimeFormat.RoundTrip) + @""", ""_o"": """ + headers.GetOrigin() + @""", ""_r"": """ + headers.GetRemoteAddress() + @""" }"); }
public async Task <FindWinnerResponse> FindWinnerAsync(Interface.FindWinnerRequest request) { var response = ServiceMessageFactory <FindWinnerResponse> .CreateFrom(request); var engineRequest = ServiceMessageFactory <FindWinnerRequest> .CreateFrom(request); engineRequest.GameSessionId = request.GameSessionId; var engineResponse = await gamePlayEngine.FindWinnerAsync(engineRequest); response.Player = engineResponse.Player.Convert(); return(response); }
public async Task <PlayTurnResponse> PlayTurnAsync(PlayTurnRequest request) { var response = ServiceMessageFactory <PlayTurnResponse> .CreateFrom(request); var playerRequest = ServiceMessageFactory <GetPlayerRequest> .CreateFrom(request); playerRequest.PlayerId = request.PlayerId; var playerResponse = await playerAccess.GetPlayerAsync(playerRequest); var player = playerResponse.Player.Convert(); var gameSessionRequest = ServiceMessageFactory <GetGameSessionRequest> .CreateFrom(request); gameSessionRequest.GameSessionId = request.GameSessionId; var gameSessionResponse = await gameSessionAccess.GetGameSessionAsync(gameSessionRequest); var gameSession = gameSessionResponse.GameSession.Convert(); var tilesRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tilesRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tilesResponse = await tileAccess.FindTilesAsync(tilesRequest); var tiles = tilesResponse.Tiles; Access.Tile.Interface.Tile tile; if (player.IsMachine) { tile = autoPlayer.PlayTurn(tiles); tile.PlayerId = player.Id; } else { var address = request.Address.ToUpperInvariant(); tile = tiles.First(i => i.Address == address); tile.PlayerId = player.Id; } var updateTileRequest = ServiceMessageFactory <UpdateTileRequest> .CreateFrom(request); updateTileRequest.Tile = tile; var updateTileResponse = await tileAccess.UpdateTileAsync(updateTileRequest); if (updateTileResponse.HasErrors) { response.Errors += "Tile update error."; } IncrementPlayer(gameSession); return(response); }
public async Task <ConfirmUsableAccessResponse> ConfirmUsableAddressAsync(ConfirmUsableAddressRequest request) { var response = ServiceMessageFactory <ConfirmUsableAccessResponse> .CreateFrom(request); var validationRequest = ServiceMessageFactory <ValidateUserInputRequest> .CreateFrom(request); validationRequest.GameSessionId = request.GameSessionId; validationRequest.Input = request.Address; var validationResponse = await validationEngine.ValidateAsync(validationRequest); response.ValidationResult = validationResponse.ValidationResult; return(response); }
public async Task <Interface.IsGamePlayableResponse> IsGamePlayableAsync(IsGamePlayableRequest request) { var response = ServiceMessageFactory <Interface.IsGamePlayableResponse> .CreateFrom(request); var engineRequest = ServiceMessageFactory <Engine.GamePlay.Interface.IsGamePlayableRequest> .CreateFrom(request); engineRequest.GameSessionId = request.GameSessionId; var engineResponse = await gamePlayEngine.IsGamePlayableAsync(engineRequest); response.IsPlayable = engineResponse.Value; return(response); }
public async Task <GetBoardResponse> GetBoardAsync(GetBoardRequest request) { var response = ServiceMessageFactory <GetBoardResponse> .CreateFrom(request); var engineRequest = ServiceMessageFactory <GetGameBoardRequest> .CreateFrom(request); engineRequest.GameSessionId = request.GameSessionId; var engineResponse = await gameBoardEngine.GetBoardAsync(engineRequest); response.GameBoard = engineResponse.GameBoard; return(response); }
public async Task <StartGameResponse> StartGameAsync(StartGameRequest request) { var response = ServiceMessageFactory <StartGameResponse> .CreateFrom(request); var initializeGameRequest = ServiceMessageFactory <InitializeGameRequest> .CreateFrom(request); initializeGameRequest.GameDefinitionId = request.GameDefinitionId; initializeGameRequest.NumberOfPlayers = request.PlayerCount; var initializeGameResponse = await gamePlayEngine.InitializeGameAsync(initializeGameRequest); response.GameSessionId = initializeGameResponse.GameSession.Id; return(response); }
public async Task <ApplyTurnResponse> AutoPlayTurn(ApplyTurnRequest request) { var response = ServiceMessageFactory <ApplyTurnResponse> .CreateFrom(request); var autoPlayRequest = ServiceMessageFactory <PlayTurnRequest> .CreateFrom(request); autoPlayRequest.IsAutoPlay = true; autoPlayRequest.Address = request.Address; autoPlayRequest.GameSessionId = request.GameSessionId; autoPlayRequest.PlayerId = request.PlayerId; var autoPlayResponse = await gamePlayEngine.PlayTurnAsync(autoPlayRequest); // ToDo: Add logging. return(response); }
public async Task <CreateGameSessionResponse> CreateGameSessionAsync(CreateGameSessionRequest request) { var response = ServiceMessageFactory <CreateGameSessionResponse> .CreateFrom(request); if (cache.FirstOrDefault(i => i.Id == request.GameSession.Id) == null) { cache.Add(request.GameSession); } else { response.Errors += "Game session already exists."; logger.LogError($"Game session ({request.GameSession.Id}) already exists."); } response.GameSession = request.GameSession; return(await Task.FromResult(response)); }
public async Task <GetCurrentPlayerResponse> GetCurrentPlayerAsync(GetCurrentPlayerRequest request) { var response = ServiceMessageFactory <GetCurrentPlayerResponse> .CreateFrom(request); var sessionRequest = ServiceMessageFactory <GetGameSessionRequest> .CreateFrom(request); sessionRequest.GameSessionId = request.GameSessionId; var sessionResponse = await gameSessionAccess.GetGameSessionAsync(sessionRequest); var playerRequest = ServiceMessageFactory <GetPlayerRequest> .CreateFrom(request); playerRequest.PlayerId = sessionResponse.GameSession.CurrentPlayerId; var playerResponse = await playerAccess.GetPlayerAsync(playerRequest); response.Player = playerResponse.Player.Convert(); return(response); }
public async Task <GetGameSessionResponse> GetGameSessionAsync(GetGameSessionRequest request) { var response = ServiceMessageFactory <GetGameSessionResponse> .CreateFrom(request); var gameSession = cache.FirstOrDefault(i => i.Id == request.GameSessionId); if (gameSession != null) { response.GameSession = gameSession; } else { response.Errors += "Unable to find game session."; logger.LogError($"Unable to find game session ({request.GameSessionId})."); } return(await Task.FromResult(response)); }
public async Task <UpdateTileResponse> UpdateTileAsync(UpdateTileRequest request) { var response = ServiceMessageFactory <UpdateTileResponse> .CreateFrom(request); var cachedTile = cache.FirstOrDefault(i => i.Id == request.Tile.Id); if (cachedTile != null) { cachedTile.PlayerId = request.Tile.PlayerId; response.Tile = cachedTile; } else { response.Errors = "Unable to find the tile to update."; } return(await Task.FromResult(response)); }
public async Task <UpdateGameSessionResponse> UpdateGameSessionAsync(UpdateGameSessionRequest request) { var response = ServiceMessageFactory <UpdateGameSessionResponse> .CreateFrom(request); var gameSession = cache.FirstOrDefault(i => i.Id == request.GameSession.Id); if (gameSession != null) { gameSession.CurrentPlayerId = request.GameSession.CurrentPlayerId; response.GameSession = gameSession; } else { response.Errors += "Unable to find game session to update."; logger.LogError($"Unable to find game session ({gameSession.Id}) to update."); } return(await Task.FromResult(response)); }
public async Task <GetTurnPromptResponse> GetTurnPromptAsync(GetTurnPromptRequest request) { var response = ServiceMessageFactory <GetTurnPromptResponse> .CreateFrom(request); var gameSessionRequest = ServiceMessageFactory <GetGameSessionRequest> .CreateFrom(request); gameSessionRequest.GameSessionId = request.GameSessionId; var gameSessionResponse = await gameSessionAccess.GetGameSessionAsync(gameSessionRequest); var gameDefinitionRequest = ServiceMessageFactory <GetGameDefinitionRequest> .CreateFrom(request); gameDefinitionRequest.GameDefinitionId = gameSessionResponse.GameSession.GameDefinitionId; var gameDefinitionResponse = await gameDefinitionAccess.GetGameDefinitionAsync(gameDefinitionRequest); response.Prompt = gameDefinitionResponse.GameDefinition.TurnPrompt; return(response); }
public async Task <CreatePlayersResponse> CreatePlayersAsync(CreatePlayersRequest request) { var response = ServiceMessageFactory <CreatePlayersResponse> .CreateFrom(request); var list = new List <Interface.Player>(); foreach (var player in request.Players) { cache.Add(player); list.Add(player); } response.Players = list.ToArray(); if (!cache.IsProperSupersetOf(request.Players)) { response.Errors = "Unable to create all of the players."; } return(response); }
public async Task <CreateTilesResponse> CreateTilesAsync(CreateTilesRequest request) { Contract.Assert(request.Tiles.Any(), "No tiles input to create."); var response = ServiceMessageFactory <CreateTilesResponse> .CreateFrom(request); try { var firstTile = request.Tiles.First(); foreach (var tile in request.Tiles) { if (cache.FirstOrDefault(i => i.Id == tile.Id) == null) { cache.Add(tile); } } response.Tiles = cache.Where(i => i.GameSessionId == firstTile.GameSessionId).ToArray(); } catch (Exception ex) { response.Errors = "Unable to add tiles."; } return(await Task.FromResult(response)); }
private async Task <Tuple <GameDefinition, IServiceMessage> > SelectGame() { var getGamesRequest = ServiceMessageFactory <GetGamesRequest> .Create(); var getGamesResponse = await gameManager.GetGamesAsync(getGamesRequest); while (true) { Console.WriteLine("Select a game to play."); var idx = 1; foreach (var gameDefinition in getGamesResponse.GameDefinitions) { Console.WriteLine($"[{idx++}] {gameDefinition.Name}"); } var input = ConsoleHelper.GetIntegerInput(); if (input > 0 && input <= getGamesResponse.GameDefinitions.Length) { return(new Tuple <GameDefinition, IServiceMessage>(getGamesResponse.GameDefinitions[input - 1], getGamesRequest)); } Console.WriteLine($"Invalid input ({input}){Environment.NewLine}"); } }
public async Task <ApplyTurnResponse> ApplyTurnAsync(ApplyTurnRequest request) { var response = ServiceMessageFactory <ApplyTurnResponse> .CreateFrom(request); var playerRequest = ServiceMessageFactory <GetPlayerRequest> .CreateFrom(request); playerRequest.PlayerId = request.PlayerId; var playerResponse = await playerAccess.GetPlayerAsync(playerRequest); var playTurnRequest = ServiceMessageFactory <PlayTurnRequest> .CreateFrom(request); playTurnRequest.GameSessionId = request.GameSessionId; playTurnRequest.PlayerId = request.PlayerId; playTurnRequest.Address = request.Address; playTurnRequest.IsAutoPlay = playerResponse.Player.IsMachine; var playTurnResponse = await gamePlayEngine.PlayTurnAsync(playTurnRequest); if (playTurnResponse.HasErrors) { logger.LogError($"{InstanceId}: {playTurnResponse.Errors}"); } return(response); }
public async Task <IsGamePlayableResponse> IsGamePlayableAsync(IsGamePlayableRequest request) { var response = ServiceMessageFactory <IsGamePlayableResponse> .CreateFrom(request); var tilesRequest = ServiceMessageFactory <FindTilesRequest> .CreateFrom(request); tilesRequest.Filter = tile => tile.GameSessionId == request.GameSessionId; var tileResponse = await tileAccess.FindTilesAsync(tilesRequest); var tiles = tileResponse.Tiles; if (tiles.All(i => i.IsEmpty)) // New Game { response.Value = true; } else if (tiles.All(i => !i.IsEmpty)) // No empty spaces. { response.Value = false; } // Check all possible vectors var dictionary = tiles.ToDictionary(tile => tile.Address, tile => tile.Convert()); // A Col if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { response.Value = false; } else // B Col if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { response.Value = false; } else // C Col if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { response.Value = false; } else // 1 Row if (IsWinningVector(dictionary["A1"], dictionary["A2"], dictionary["A3"])) { response.Value = false; } else // 2 Row if (IsWinningVector(dictionary["B1"], dictionary["B2"], dictionary["B3"])) { response.Value = false; } else // 3 Row if (IsWinningVector(dictionary["C1"], dictionary["C2"], dictionary["C3"])) { response.Value = false; } else // Right Diagonal if (IsWinningVector(dictionary["A1"], dictionary["B2"], dictionary["C3"])) { response.Value = false; } else // Left Diagonal if (IsWinningVector(dictionary["A3"], dictionary["B2"], dictionary["C1"])) { response.Value = false; } else { response.Value = true; } return(response); }