Skip to content

Commit

Permalink
Merge pull request #1 from mikedegeofroy/lab-1
Browse files Browse the repository at this point in the history
Lab 1
  • Loading branch information
mikedegeofroy authored Mar 15, 2024
2 parents 38f70e9 + 5e49c44 commit 69a79f8
Show file tree
Hide file tree
Showing 56 changed files with 994 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ csharp_style_namespace_declarations = file_scoped:warning
dotnet_diagnostic.CS1591.severity = none

dotnet_diagnostic.CA1062.severity = none
dotnet_diagnostic.CA1707.severity = none
dotnet_diagnostic.CA1034.severity = none


dotnet_diagnostic.CA2012.severity = none

[/src/Lab5/ATM.Infrastructure.DataAccess/Migrations/*.cs]
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="SourceKit.Analyzers.Collections" Version="1.0.4" />
<PackageVersion Include="SourceKit.Analyzers.Enumerable" Version="1.0.4" />
<PackageVersion Include="SourceKit.Analyzers.MemberAccessibility" Version="1.0.4" />
Expand Down
42 changes: 42 additions & 0 deletions src/Lab1/Environments/Cosmos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Router;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;

public class Cosmos : IEnvironment
{
private readonly List<ICosmosObstacle> _obstacles;

public Cosmos(IEnumerable<ICosmosObstacle> obstacles)
{
_obstacles = obstacles.ToList();
}

public Cosmos()
: this(Enumerable.Empty<ICosmosObstacle>())
{
}

public TraversalResult TraverseEnvironment(IShip ship, int length)
{
foreach (ICosmosObstacle highDensityObstacle in _obstacles)
{
highDensityObstacle.GiveDamage(ship);
}

EngineConsumption consumption = ship.DriveEngine.GetConsumption(length);
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
}

public void AddObstacle(IObstacle obstacle)
{
if (obstacle is not ICosmosObstacle cosmosObstacle)
throw new ArgumentException("You can't add this obstacle to this environment.");
_obstacles.Add(cosmosObstacle);
}
}
48 changes: 48 additions & 0 deletions src/Lab1/Environments/HighDensity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Router;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;

public class HighDensity : IEnvironment
{
private readonly List<IHighDensityObstacle> _obstacles;

public HighDensity(IEnumerable<IHighDensityObstacle> obstacles)
{
_obstacles = obstacles.ToList();
}

public HighDensity()
: this(Enumerable.Empty<IHighDensityObstacle>())
{
}

public TraversalResult TraverseEnvironment(IShip ship, int length)
{
if (ship.JumpEngine == null || ship.JumpEngine.GetRange() < length)
return new TraversalResult.LostShip("Ship lost in a channel");

foreach (IHighDensityObstacle highDensityObstacle in _obstacles)
{
if (highDensityObstacle is AntimatterFlare && !ship.PhotonDeflector)
return new TraversalResult.DeathOfCrew();
highDensityObstacle.GiveDamage(ship);
}

EngineConsumption consumption = ship.JumpEngine.GetConsumption(length);
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
}

public void AddObstacle(IObstacle obstacle)
{
if (obstacle is not IHighDensityObstacle highDensityObstacle)
throw new ArgumentException("You can't add this obstacle to this environment.");
_obstacles.Add(highDensityObstacle);
}
}
12 changes: 12 additions & 0 deletions src/Lab1/Environments/IEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Router;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;

public interface IEnvironment
{
void AddObstacle(IObstacle obstacle);

TraversalResult TraverseEnvironment(IShip ship, int length);
}
46 changes: 46 additions & 0 deletions src/Lab1/Environments/NitrineNebula.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Router;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines.DriveEngines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;

public class NitrineNebula : IEnvironment
{
private readonly List<INitrineParticleObstacle> _obstacles;

public NitrineNebula(IEnumerable<INitrineParticleObstacle> obstacles)
{
_obstacles = obstacles.ToList();
}

public NitrineNebula()
: this(Enumerable.Empty<INitrineParticleObstacle>())
{
}

public TraversalResult TraverseEnvironment(IShip ship, int length)
{
if (ship.DriveEngine is not ExponentialDriveEngine)
return new TraversalResult.LostShip("The engines weren't powerful enough.");

foreach (INitrineParticleObstacle x in _obstacles)
{
x.GiveDamage(ship);
}

EngineConsumption consumption = ship.DriveEngine.GetConsumption(length);
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
}

public void AddObstacle(IObstacle obstacle)
{
if (obstacle is not INitrineParticleObstacle nitrineParticleObstacle)
throw new ArgumentException("You can't add this obstacle to this environment.");
_obstacles.Add(nitrineParticleObstacle);
}
}
1 change: 1 addition & 0 deletions src/Lab1/Lab1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<RootNamespace>Itmo.ObjectOrientedProgramming.Lab1</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions src/Lab1/Obstacles/AntimatterFlare.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;

public class AntimatterFlare : IHighDensityObstacle
{
private const double Damage = 10;

public void GiveDamage(IShip ship)
{
ship.TakeDamage(Damage);
}
}
14 changes: 14 additions & 0 deletions src/Lab1/Obstacles/Asteroid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;

public class Asteroid : ICosmosObstacle
{
private const double Damage = 10;

public void GiveDamage(IShip ship)
{
ship.TakeDamage(Damage);
}
}
21 changes: 21 additions & 0 deletions src/Lab1/Obstacles/CosmoWhale.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;

public class CosmoWhale : INitrineParticleObstacle
{
private const double Damage = 200;
private readonly int _population;

public CosmoWhale(int population)
{
_population = population;
}

public void GiveDamage(IShip ship)
{
if (!ship.AntiNitrineEmitter)
ship.TakeDamage(Damage * _population);
}
}
5 changes: 5 additions & 0 deletions src/Lab1/Obstacles/Interfaces/ICosmosObstacle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;

public interface ICosmosObstacle : IObstacle
{
}
5 changes: 5 additions & 0 deletions src/Lab1/Obstacles/Interfaces/IHighDensityObstacle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;

public interface IHighDensityObstacle : IObstacle
{
}
5 changes: 5 additions & 0 deletions src/Lab1/Obstacles/Interfaces/INitrineParticleObstacle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;

public interface INitrineParticleObstacle : IObstacle
{
}
8 changes: 8 additions & 0 deletions src/Lab1/Obstacles/Interfaces/IObstacle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;

public interface IObstacle
{
void GiveDamage(IShip ship);
}
14 changes: 14 additions & 0 deletions src/Lab1/Obstacles/Meteorite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;

public class Meteorite : ICosmosObstacle
{
private const double Damage = 10;

public void GiveDamage(IShip ship)
{
ship.TakeDamage(Damage);
}
}
21 changes: 21 additions & 0 deletions src/Lab1/Router/Path.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Itmo.ObjectOrientedProgramming.Lab1.Environments;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;

namespace Itmo.ObjectOrientedProgramming.Lab1.Router;

public class Path
{
private readonly IEnvironment _environment;
private readonly int _length;

public Path(IEnvironment environment, int length)
{
_environment = environment;
_length = length;
}

public TraversalResult TraversePath(IShip ship)
{
return _environment.TraverseEnvironment(ship, _length);
}
}
32 changes: 32 additions & 0 deletions src/Lab1/Router/Route.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Router;

public class Route
{
private readonly List<Path> _paths;

public Route(IEnumerable<Path> paths)
{
_paths = paths.ToList();
}

public TraversalResult Traverse(IShip ship)
{
int time = 0;
List<Fuel> fuels = new();

foreach (TraversalResult result in _paths.Select(path => path.TraversePath(ship)))
{
if (result is not TraversalResult.Success success)
return result;
time += success.Time;
fuels.AddRange(success.Fuel);
}

return new TraversalResult.Success(time, fuels);
}
}
15 changes: 15 additions & 0 deletions src/Lab1/Router/TraversalResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Router;

public abstract record TraversalResult
{
public record Success(int Time, IEnumerable<Fuel> Fuel) : TraversalResult;

public record LostShip(string Reason) : TraversalResult;

public record DeathOfCrew : TraversalResult;

public record DestroyedShip : TraversalResult;
}
11 changes: 11 additions & 0 deletions src/Lab1/Services/FuelExchange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;

namespace Itmo.ObjectOrientedProgramming.Lab1.Services;

public static class FuelExchange
{
public static double GetFuelPrice(Fuel fuel)
{
return fuel.Amount * fuel.Rarity;
}
}
Loading

0 comments on commit 69a79f8

Please sign in to comment.