Skip to content

Commit

Permalink
Merge pull request #86 from benpollarduk/sonar-fixes
Browse files Browse the repository at this point in the history
Sonar fixes
  • Loading branch information
benpollarduk authored Oct 28, 2024
2 parents 4fa0158 + 8bb3ed4 commit 8dd5e8a
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 348 deletions.
63 changes: 36 additions & 27 deletions NetAF.Tests/Assets/Locations/Matrix_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NetAF.Assets.Locations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace NetAF.Tests.Assets.Locations
{
Expand All @@ -9,7 +10,7 @@ public class Matrix_Tests
[TestMethod]
public void GivenNoRooms_WhenGetWidth_Then0()
{
var matrix = new Matrix(new Room[0,0,0]);
var matrix = new Matrix([]);

var result = matrix.Width;

Expand All @@ -19,7 +20,7 @@ public void GivenNoRooms_WhenGetWidth_Then0()
[TestMethod]
public void GivenNoRooms_WhenGetHeight_Then0()
{
var matrix = new Matrix(new Room[0, 0, 0]);
var matrix = new Matrix([]);

var result = matrix.Height;

Expand All @@ -29,7 +30,7 @@ public void GivenNoRooms_WhenGetHeight_Then0()
[TestMethod]
public void GivenNoRooms_WhenGetDepth_Then0()
{
var matrix = new Matrix(new Room[0, 0, 0]);
var matrix = new Matrix([]);

var result = matrix.Depth;

Expand All @@ -39,12 +40,14 @@ public void GivenNoRooms_WhenGetDepth_Then0()
[TestMethod]
public void Given1RoomWide_WhenGetWidth_Then1()
{
var rooms = new Room[1, 2, 3];
rooms[0, 0, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 1] = new(string.Empty, string.Empty);
rooms[0, 1, 2] = new(string.Empty, string.Empty);
var matrix = new Matrix(rooms);
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), 0, 0, 0),
new(new(string.Empty, string.Empty), 0, 1, 0),
new(new(string.Empty, string.Empty), 0, 1, 1),
new(new(string.Empty, string.Empty), 0, 1, 2)
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.Width;

Expand All @@ -54,12 +57,14 @@ public void Given1RoomWide_WhenGetWidth_Then1()
[TestMethod]
public void Given2RoomsHigh_WhenGetHeight_Then2()
{
var rooms = new Room[1, 2, 3];
rooms[0, 0, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 1] = new(string.Empty, string.Empty);
rooms[0, 1, 2] = new(string.Empty, string.Empty);
var matrix = new Matrix(rooms);
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), 0, 0, 0),
new(new(string.Empty, string.Empty), 0, 1, 0),
new(new(string.Empty, string.Empty), 0, 1, 1),
new(new(string.Empty, string.Empty), 0, 1, 2)
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.Height;

Expand All @@ -69,12 +74,14 @@ public void Given2RoomsHigh_WhenGetHeight_Then2()
[TestMethod]
public void Given3RoomsDeep_WhenGetDepth_Then3()
{
var rooms = new Room[1, 2, 3];
rooms[0, 0, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 1] = new(string.Empty, string.Empty);
rooms[0, 1, 2] = new(string.Empty, string.Empty);
var matrix = new Matrix(rooms);
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), 0, 0, 0),
new(new(string.Empty, string.Empty), 0, 1, 0),
new(new(string.Empty, string.Empty), 0, 1, 1),
new(new(string.Empty, string.Empty), 0, 1, 2)
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.Depth;

Expand All @@ -84,12 +91,14 @@ public void Given3RoomsDeep_WhenGetDepth_Then3()
[TestMethod]
public void Given4Rooms_WhenToRooms_Then4Rooms()
{
var rooms = new Room[1, 2, 3];
rooms[0, 0, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 0] = new(string.Empty, string.Empty);
rooms[0, 1, 1] = new(string.Empty, string.Empty);
rooms[0, 1, 2] = new(string.Empty, string.Empty);
var matrix = new Matrix(rooms);
List<RoomPosition> roomPositions =
[
new(new(string.Empty, string.Empty), 0, 0, 0),
new(new(string.Empty, string.Empty), 0, 1, 0),
new(new(string.Empty, string.Empty), 0, 1, 1),
new(new(string.Empty, string.Empty), 0, 1, 2)
];
var matrix = new Matrix([.. roomPositions]);

var result = matrix.ToRooms();

Expand Down
33 changes: 9 additions & 24 deletions NetAF/Assets/Attributes/Attribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,33 @@
/// <summary>
/// Provides a description of an attribute.
/// </summary>
public class Attribute
/// <param name="name">Specify the name of the attribute.</param>
/// <param name="description">Specify the description of the attribute.</param>
/// <param name="minimum">Specify the minimum limit of the attribute.</param>
/// <param name="maximum">Specify the maximum limit of the attribute.</param>
public class Attribute(string name, string description, int minimum, int maximum)
{
#region Properties

/// <summary>
/// Get the name of the attribute.
/// </summary>
public string Name { get; }
public string Name { get; } = name;

/// <summary>
/// Get the description of the attribute.
/// </summary>
public string Description { get; }
public string Description { get; } = description;

/// <summary>
/// Get the minimum limit of the attribute.
/// </summary>
public int Minimum { get; }
public int Minimum { get; } = minimum;

/// <summary>
/// Get the maximum limit of the attribute.
/// </summary>
public int Maximum { get; }

#endregion

#region Constructors

/// <summary>
/// Initailizes a new instance of the Attribute class.
/// </summary>
/// <param name="name">Specify the name of the attibute.</param>
/// <param name="description">Specify the description of the attibute.</param>
/// <param name="minimum">Specify the minimum limit of the attibute.</param>
/// <param name="maximum">Specify the maximum limit of the attibute.</param>
public Attribute(string name, string description, int minimum, int maximum)
{
Name = name;
Description = description;
Minimum = minimum;
Maximum = maximum;
}
public int Maximum { get; } = maximum;

#endregion
}
Expand Down
23 changes: 5 additions & 18 deletions NetAF/Assets/Interaction/Reaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,21 @@
/// <summary>
/// Represents a reaction.
/// </summary>
public sealed class Reaction
/// <param name="result">The result.</param>
/// <param name="description">A description of the result.</param>
public sealed class Reaction(ReactionResult result, string description)
{
#region Properties

/// <summary>
/// Get the result.
/// </summary>
public ReactionResult Result { get; }
public ReactionResult Result { get; } = result;

/// <summary>
/// Get a description of the result.
/// </summary>
public string Description { get; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the Reaction class.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="description">A description of the result.</param>
public Reaction(ReactionResult result, string description)
{
Result = result;
Description = description;
}
public string Description { get; } = description;

#endregion
}
Expand Down
33 changes: 10 additions & 23 deletions NetAF/Assets/Locations/Matrix.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Collections.Generic;
using System;
using System.Linq;

namespace NetAF.Assets.Locations
{
/// <summary>
/// Provides a 3D matrix of rooms.
/// </summary>
/// <param name="rooms">The rooms to be represented.</param>
public sealed class Matrix(Room[,,] rooms)
/// <param name="roomPositions">The rooms to be represented.</param>
public sealed class Matrix(RoomPosition[] roomPositions)
{
#region Fields

private readonly Room[,,] rooms = rooms;
private readonly RoomPosition[] roomPositions = roomPositions;

#endregion

Expand All @@ -23,22 +24,22 @@ public sealed class Matrix(Room[,,] rooms)
/// <param name="y">The y position.</param>
/// <param name="z">The z position.</param>
/// <returns>The room.</returns>
public Room this[int x, int y, int z] => rooms[x, y, z];
public Room this[int x, int y, int z] => Array.Find(roomPositions, r => r.IsAtPosition(x, y, z))?.Room;

/// <summary>
/// Get the width of the matrix.
/// </summary>
public int Width => rooms.GetLength(0);
public int Width => roomPositions.Length > 0 ? roomPositions.Max(r => r.X) - roomPositions.Min(r => r.X) + 1 : 0;

/// <summary>
/// Get the height of the matrix.
/// </summary>
public int Height => rooms.GetLength(1);
public int Height => roomPositions.Length > 0 ? roomPositions.Max(r => r.Y) - roomPositions.Min(r => r.Y) + 1 : 0;

/// <summary>
/// Get the depth of the matrix.
/// </summary>
public int Depth => rooms.GetLength(2);
public int Depth => roomPositions.Length > 0 ? roomPositions.Max(r => r.Z) - roomPositions.Min(r => r.Z) + 1 : 0;

#endregion

Expand All @@ -50,21 +51,7 @@ public sealed class Matrix(Room[,,] rooms)
/// <returns>The rooms, as a one dimensional array.</returns>
public Room[] ToRooms()
{
List<Room> roomList = [];

for (var z = 0; z < Depth; z++)
{
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
if (this[x, y, z] != null)
roomList.Add(this[x, y, z]);
}
}
}

return [.. roomList];
return roomPositions.Select(x => x.Room).ToArray();
}

#endregion
Expand Down
33 changes: 9 additions & 24 deletions NetAF/Assets/Locations/RoomPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,33 @@
/// <summary>
/// Represents a room position.
/// </summary>
public class RoomPosition
/// <param name="room">The room/</param>
/// <param name="x">The x position of the room.</param>
/// <param name="y">The y position of the room.</param>
/// <param name="z">The z position of the room.</param>
public class RoomPosition(Room room, int x, int y, int z)
{
#region Properties

/// <summary>
/// Get the room.
/// </summary>
public Room Room { get; }
public Room Room { get; } = room;

/// <summary>
/// Get the X position of the room.
/// </summary>
public int X { get; }
public int X { get; } = x;

/// <summary>
/// Get the Y position of the room.
/// </summary>
public int Y { get; }
public int Y { get; } = y;

/// <summary>
/// Get the Z position of the room.
/// </summary>
public int Z { get; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the RoomPosition class.
/// </summary>
/// <param name="room">The room/</param>
/// <param name="x">The x position of the room.</param>
/// <param name="y">The y position of the room.</param>
/// <param name="z">The z position of the room.</param>
public RoomPosition(Room room, int x, int y, int z)
{
Room = room;
X = x;
Y = y;
Z = z;
}
public int Z { get; } = z;

#endregion

Expand Down
23 changes: 5 additions & 18 deletions NetAF/Assets/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,21 @@
/// <summary>
/// Represents a size.
/// </summary>
public struct Size
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public readonly struct Size(int width, int height)
{
#region Properties

/// <summary>
/// Get the width.
/// </summary>
public int Width { get; }
public int Width { get; } = width;

/// <summary>
/// Get the height.
/// </summary>
public int Height { get; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the Size struct.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public Size(int width, int height)
{
Width = width;
Height = height;
}
public int Height { get; } = height;

#endregion
}
Expand Down
Loading

0 comments on commit 8dd5e8a

Please sign in to comment.