public void SaveGame() { var playerController = PlayerGameObject.GetComponentInChildren <PlayerController>(); var gemsController = GemSpawnerGameObject.GetComponentInChildren <GemSpawnerController>(); var frogsController = FrogSpawnerGameObject.GetComponentInChildren <FrogAreaController>(); var gemsPositions = gemsController.GemsList.Select(x => new PointDTO { X = x.transform.position.x, Y = x.transform.position.y }); var frogsPositions = frogsController.FrogsList.Select(x => new PointDTO { X = x.transform.position.x, Y = x.transform.position.y }); var gameState = new GameStateDTO { HPState = playerController.Health, Position = new PointDTO { X = playerController.transform.position.x, Y = playerController.transform.position.y, }, Gems = gemsPositions.ToList(), Frogs = frogsPositions.ToList() }; var gameStateAsJson = JsonConvert.SerializeObject(gameState); }
public IHttpActionResult GetGameState(int id) { GameState gameState = db.GameStates.Find(id); if (gameState == null) { return(NotFound()); } var userName = User.Identity.GetUserName(); GameStateDTO gameStateDTO = new GameStateDTO { Id = gameState.Id, Food = gameState.Food, Wood = gameState.Wood, Stone = gameState.Stone, Gold = gameState.Gold, FarmsLevel = gameState.FarmsLevel, LumberjackLevel = gameState.LumberjackLevel, CastleLevel = gameState.CastleLevel, HousingLevel = gameState.HousingLevel, MinesLevel = gameState.MinesLevel, UserName = userName, }; return(Ok(gameStateDTO)); }
// Manage the changes in the scene. void ProcessUpdate() { startTime = Time.time; if (!gameStateBuffer.HasNext()) { return; } GameStateDTO gameState = gameStateBuffer.Pop(); // TODO: era might have to be passed to each of the managers as a second // parameter, as some require it for the prefab name and each mapfeature // doesn't reach the scope of that JSON. // Player updates. PlayerDTO[] players = gameState.players; playerManager.UpdatePlayersState(players); // Pickup updates. PickupDTO[] pickups = gameState.pickups; pickupManager.UpdateFeatures(pickups); // Score updates. ScoreLocationDTO[] scores = gameState.scoreLocations; scorePointManager.UpdateFeatures(scores); }
public void TestPopsWithEnqueues() { var a = new GameStateDTO() { northEastCorner = new Location(0, 0) }; var b = new GameStateDTO() { northEastCorner = new Location(1, 0) }; var c = new GameStateDTO() { northEastCorner = new Location(2, 0) }; var d = new GameStateDTO() { northEastCorner = new Location(3, 0) }; var e = new GameStateDTO() { northEastCorner = new Location(4, 0) }; buffer.Enqueue(a); Assert.AreEqual(buffer.Pop(), a); buffer.Enqueue(b); buffer.Enqueue(c); buffer.Enqueue(d); Assert.AreEqual(buffer.Pop(), c); Assert.AreEqual(buffer.Pop(), d); buffer.Enqueue(e); Assert.AreEqual(buffer.Pop(), e); Assert.Throws <InvalidOperationException>(() => buffer.Pop()); }
public void NewGameState(string gameStateString) { // Convert the string to our DTO format. GameStateDTO gameState = ConvertJSONtoDTO(gameStateString); // Check if this is the first game-state event received. If so, set // up the terrain and don't do it ever again. if (gameStateEventCount == 1) { TerrainGenerator terrainGenerator = new TerrainGenerator(); int width = terrainGenerator.CalculateWidthFromGameState(gameState); int height = terrainGenerator.CalculateHeightFromGameState(gameState); TerrainDTO terrainDTO = new TerrainDTO(width, height); terrainGenerator.GenerateTerrainMMO(terrainDTO); OverlayGenerator overlayGenerator = new OverlayGenerator(terrainFolder: terrainGenerator.terrainFolder); overlayGenerator.GenerateGridForTerrain(terrainDTO); // Generate the obstacles only on initial game creation. Don't duplicate. ObstacleDTO[] obstacles = gameState.obstacles; obstacleManager.UpdateFeatures(obstacles); } RenderGameState(gameState); gameStateEventCount++; }
public async Task <GameStateDTO> UpdateGameRoundState(NewGuessLetterDTO newGuessLetterDTO) { var guessWord = await GetGuessedWord(newGuessLetterDTO.GuessWordId); var newGuessLetterString = newGuessLetterDTO.GuessLetter; _logger.LogInformation("Persisting new player's move..."); await CreateGuessLetter(guessWord !, newGuessLetterString); // previously validated guess word var gameRound = guessWord !.Round; var allGuessLetters = guessWord.GuessLetters.Select(letter => letter.Letter); // with new guess letter already var updatedGameState = new GameStateDTO() { GuessWord = null, IsOver = false, PlayerHealth = guessWord.Round.Health, GuessWordSoFar = _gameLogic.GetGuessWordSoFar(allGuessLetters, guessWord.Word), GuessedLetters = allGuessLetters, }; if (_gameLogic.IsGuessedLetterInGuessWord(newGuessLetterString, guessWord.Word)) { _logger.LogInformation("Player guessed a right letter. Checking if turn is over..."); if (_gameLogic.HasPlayerHasDiscoveredGuessWord(allGuessLetters, guessWord.Word)) { _logger.LogInformation("Guess word {:l} has been found! Turn is over...", guessWord.Word); gameRound = _gameLogic.FinishGameRound(gameRound); await _repositoryGameRound.Update(gameRound); _logger.LogInformation("Setting final game state for return..."); updatedGameState.IsOver = true; updatedGameState.GuessWord = guessWord.Word; } } else { _logger.LogInformation("Player has guessed a wrong letter. Punishing him with a health hit..."); gameRound = _gameLogic.ReducePlayerHealth(gameRound); if (_gameLogic.HasPlayerBeenHung(gameRound)) { _logger.LogInformation("Player has been hung and is dead! Turn is over..."); gameRound = _gameLogic.FinishGameRound(gameRound); _logger.LogInformation("Setting final game state for return..."); updatedGameState.IsOver = true; updatedGameState.GuessWord = guessWord.Word; } await _repositoryGameRound.Update(gameRound); } updatedGameState.PlayerHealth = gameRound.Health; _logger.LogInformation("Returning updated game state data..."); return(updatedGameState); }
public void TestPop() { var a = new GameStateDTO(); Assert.IsFalse(buffer.HasNext()); buffer.Enqueue(a); Assert.IsTrue(buffer.HasNext()); Assert.AreEqual(buffer.Pop(), a); }
public void TestCalculateWidthFromGameState() { int southWestX = -20; int northEastX = 10; GameStateDTO gameState = new GameStateDTO(); gameState.southWestCorner = new Location(southWestX, 0); gameState.northEastCorner = new Location(northEastX, 0); // Row with x=0 is expected too. Assert.AreEqual(31, generator.CalculateWidthFromGameState(gameState)); }
public void TestCalculateHeightFromGameState() { int southWestY = -20; int northEastY = 10; GameStateDTO gameState = new GameStateDTO(); gameState.southWestCorner = new Location(0, southWestY); gameState.northEastCorner = new Location(0, northEastY); // Row with y=0 is expected too. Assert.AreEqual(31, generator.CalculateHeightFromGameState(gameState)); }
public void TestPopWithTwoEnqueues() { var a = new GameStateDTO() { northEastCorner = new Location(0, 0) }; var b = new GameStateDTO() { northEastCorner = new Location(1, 0) }; buffer.Enqueue(a); buffer.Enqueue(b); Assert.AreEqual(buffer.Pop(), a); Assert.AreEqual(buffer.Pop(), b); }
// Manage the changes in the scene. void ProcessUpdate() { startTime = Time.time; if (!gameStateBuffer.HasNext()) { return; } GameStateDTO gameState = gameStateBuffer.Pop(); // TODO: era might have to be passed to each of the managers as a second // parameter, as some require it for the prefab name and each mapfeature // doesn't reach the scope of that JSON. // Player updates. PlayerDTO[] players = gameState.players; playerManager.UpdatePlayersState(players); // Interactable updates. InteractableDTO[] interactables = gameState.interactables; interactableManager.UpdateFeatures(interactables); }
public void TestGameUpdateDTODeserialisation() { string gameUpdateJSON = @" { ""era"": ""less_flat"", ""southWestCorner"": { ""x"": -2, ""y"": -2 }, ""northEastCorner"": { ""x"": 2, ""y"": 2 }, ""players"": [ { ""id"": 1, ""score"": 0, ""health"": 5, ""location"": { ""x"": 2, ""y"": 2 }, ""orientation"": ""east"" } ], ""interactables"": [ { ""type"": ""boost"", ""location"": { ""x"": 1, ""y"": 3 } }, { ""type"": ""score"", ""location"": { ""x"": 1, ""y"": 3 } } ], ""obstacles"": [ { ""location"": { ""x"": 0, ""y"": 1 }, ""width"": 2, ""height"": 1, ""type"": ""van"", ""orientation"": ""east"" } ] }"; GameStateDTO gameState = JsonUtility.FromJson <GameStateDTO>(gameUpdateJSON); Assert.AreEqual(Era.Future, gameState.EraType); Assert.AreEqual(new Location(-2, -2), gameState.southWestCorner); Assert.AreEqual(new Location(2, 2), gameState.northEastCorner); Assert.AreEqual(1, gameState.players.Length); Assert.AreEqual(2, gameState.interactables.Length); Assert.AreEqual(1, gameState.obstacles.Length); }
public int CalculateWidthFromGameState(GameStateDTO gameState) { int width = Math.Abs(gameState.northEastCorner.x - gameState.southWestCorner.x) + 1; return(width); }
public int CalculateHeightFromGameState(GameStateDTO gameState) { int height = Math.Abs(gameState.northEastCorner.y - gameState.southWestCorner.y) + 1; return(height); }
// Receive updates from the backend, parse them and delegate to the // classes in charge of creating, deleting and updating game objects. void RenderGameState(GameStateDTO gameStateDTO) { gameStateBuffer.Enqueue(gameStateDTO); }
// public IActionResult ClickedTile(string clickedTile, string gameId, string selectedTile, List<string> canMove, List<string> canTake) public IActionResult ClickedTile([FromBody] TileClick data) { var gameId = data.GameID; var clickedTile = data.ClickedTile; var selectedTile = data.SelectedTile; var canMove = data.CanMove; var canTake = data.CanTake; var didMove = false; List <string> changedTiles = new List <string>(); changedTiles.AddRange(canMove); changedTiles.AddRange(canTake); string userId = _signInManager.UserManager.GetUserId(User); RefreshUser(User); GameStateDTO gamestate = _GameRepo.GetGamestate(gameId); char userColor = _GameRepo.GetPlayerColor(gameId, userId); if (!_GameRepo.IsActivePlayer(gameId, userId)) { // Not players turn return(Json("")); } else if (selectedTile == clickedTile) { // Clicked already selected tile. Deselect it. changedTiles.Add(selectedTile); selectedTile = null; canMove = new List <string>(); canTake = new List <string>(); } else if (_chessLogicManager.PositionIsColor(gamestate.Board, clickedTile, userColor)) { // Clicked own piece. Get possible moves. SelectedPieceDTO selectedPiece = new SelectedPieceDTO { Board = gamestate.Board, PlayerColor = userColor, Selected = clickedTile }; var moves = _chessLogicManager.GetPossibleMoves(selectedPiece); canMove = moves.PositionsPieceCanMoveTo; canTake = moves.PositionsPieceCanKillAt; changedTiles.AddRange(canMove); changedTiles.AddRange(canTake); if (selectedTile != null) { changedTiles.Add(selectedTile); } changedTiles.Add(clickedTile); selectedTile = clickedTile; } else if (selectedTile != null) { //a tile is already selected, and clicked an enemy or empty tile //Can the selected piece move there? MovePlanDTO movePlan = new MovePlanDTO { Board = gamestate.Board, From = selectedTile, To = clickedTile, PlayerColor = userColor }; changedTiles.Add(clickedTile); changedTiles.Add(selectedTile); if (_chessLogicManager.IsValidMove(movePlan)) { //yes, the selected piece can move there //Apply the game logic, save the new board state, and log the move. string[,] newBoard = _chessLogicManager.DoMove(movePlan); NewMoveDTO newMove = new NewMoveDTO { NewBoard = newBoard, From = selectedTile, To = clickedTile, GameID = gameId }; _GameRepo.AddNewMove(newMove); _GameRepo.SetChangedTiles(gameId, new List <string> { clickedTile, selectedTile }); didMove = true; if (_chessLogicManager.IsBlackCheckMate(newBoard) || _chessLogicManager.IsWhiteCheckMate(newBoard)) { _GameRepo.SetGameToFinished(gameId); _GameRepo.SetGameToFinished(gameId); } } // Either a move was made, or the tile was deselected by clicking // a tile where a move wasn't possible. Remove highlights. canMove = new List <string>(); canTake = new List <string>(); selectedTile = null; } gamestate = _GameRepo.GetGamestate(gameId); ChessboardPartialViewModel board = new ChessboardPartialViewModel { GameState = gamestate, SelectedTile = selectedTile, CanMoveToAndTakeTiles = canTake, CanMoveToTiles = canMove, GameId = gameId, PlayerColor = userColor, ChangedTiles = changedTiles, DidMove = didMove }; return(Json(board)); }
public GameStateDTO StartGame() { var gameData = new GameStateDTO(); return(gameData); }
private GameStateDTO ConvertJSONtoDTO(string gameStateJSON) { GameStateDTO gameState = JsonUtility.FromJson <GameStateDTO>(gameStateJSON); return(gameState); }
// Receive updates from the backend, parse them and delegate to the // classes in charge of creating, deleting and updating game objects. void RenderGameState(GameStateDTO gameStateDTO) { dataQueue.Enqueue(gameStateDTO); }