示例#1
0
        public async Task <IActionResult> PutTile(int id, TilePuttedDTO tile)
        {
            // TODO unauthorized
            // validate indices

            // validate tile.BoardId = id;

            var tileFromRepo = await _boardService.GetTile(id, tile.X, tile.Y);

            if (tileFromRepo != null)
            {
                return(BadRequest("Tile at these coordinates already exists."));
            }

            if (!await _boardService.ValidateTerrain(id, tile))
            {
                return(BadRequest("Cannot put here. Terrain not valid."));
            }

            Tile record = _mapper.Map <TilePuttedDTO, Tile>(tile);

            if (await _putTileService.PutTile(record))
            {
                return(Ok()); // CreatedAt
            }

            // check components -> connect components



            // TODO global exception handler
            throw new Exception("Putting tile failed on save.");
        }
示例#2
0
        public async Task <bool> ValidateTerrain(int boardId, TilePuttedDTO tile)
        {
            var x = tile.X;
            var y = tile.Y;

            var tileTop = await _context.Tiles.Include(t => t.TileType).Include(t => t.TileType.Terrains)
                          .Where(t => t.BoardId == boardId && t.X == x && t.Y == y - 1)
                          .FirstOrDefaultAsync();

            var tileRight = await _context.Tiles.Include(t => t.TileType).Include(t => t.TileType.Terrains)
                            .Where(t => t.BoardId == boardId && t.X == x + 1 && t.Y == y)
                            .FirstOrDefaultAsync();

            var tileBottom = await _context.Tiles.Include(t => t.TileType).Include(t => t.TileType.Terrains)
                             .Where(t => t.BoardId == boardId && t.X == x && t.Y == y + 1)
                             .FirstOrDefaultAsync();

            var tileLeft = await _context.Tiles.Include(t => t.TileType).Include(t => t.TileType.Terrains)
                           .Where(t => t.BoardId == boardId && t.X == x - 1 && t.Y == y)
                           .FirstOrDefaultAsync();

            var terrainOfTop    = tileTop?.GetTerrainAt(TilePosition.Bottom) ?? TerrainType.Void;
            var terrainOfRight  = tileRight?.GetTerrainAt(TilePosition.Left) ?? TerrainType.Void;
            var terrainOfBottom = tileBottom?.GetTerrainAt(TilePosition.Top) ?? TerrainType.Void;
            var terrainOfLeft   = tileLeft?.GetTerrainAt(TilePosition.Right) ?? TerrainType.Void;

            if (terrainOfTop != tile.Top && terrainOfTop != TerrainType.Void)
            {
                return(false);
            }
            if (terrainOfRight != tile.Right && terrainOfRight != TerrainType.Void)
            {
                return(false);
            }
            if (terrainOfBottom != tile.Bottom && terrainOfBottom != TerrainType.Void)
            {
                return(false);
            }
            if (terrainOfLeft != tile.Left && terrainOfLeft != TerrainType.Void)
            {
                return(false);
            }

            return(true);
        }