Skip to content

Commit 69a79f8

Browse files
Merge pull request #1 from mikedegeofroy/lab-1
Lab 1
2 parents 38f70e9 + 5e49c44 commit 69a79f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+994
-17
lines changed

.editorconfig

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ csharp_style_namespace_declarations = file_scoped:warning
9494
dotnet_diagnostic.CS1591.severity = none
9595

9696
dotnet_diagnostic.CA1062.severity = none
97+
dotnet_diagnostic.CA1707.severity = none
9798
dotnet_diagnostic.CA1034.severity = none
98-
99-
10099
dotnet_diagnostic.CA2012.severity = none
101100

102101
[/src/Lab5/ATM.Infrastructure.DataAccess/Migrations/*.cs]

Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
1010
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
1111
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
12+
<PackageVersion Include="NSubstitute" Version="5.1.0" />
1213
<PackageVersion Include="SourceKit.Analyzers.Collections" Version="1.0.4" />
1314
<PackageVersion Include="SourceKit.Analyzers.Enumerable" Version="1.0.4" />
1415
<PackageVersion Include="SourceKit.Analyzers.MemberAccessibility" Version="1.0.4" />

src/Lab1/Environments/Cosmos.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
5+
using Itmo.ObjectOrientedProgramming.Lab1.Router;
6+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
7+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
8+
9+
namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;
10+
11+
public class Cosmos : IEnvironment
12+
{
13+
private readonly List<ICosmosObstacle> _obstacles;
14+
15+
public Cosmos(IEnumerable<ICosmosObstacle> obstacles)
16+
{
17+
_obstacles = obstacles.ToList();
18+
}
19+
20+
public Cosmos()
21+
: this(Enumerable.Empty<ICosmosObstacle>())
22+
{
23+
}
24+
25+
public TraversalResult TraverseEnvironment(IShip ship, int length)
26+
{
27+
foreach (ICosmosObstacle highDensityObstacle in _obstacles)
28+
{
29+
highDensityObstacle.GiveDamage(ship);
30+
}
31+
32+
EngineConsumption consumption = ship.DriveEngine.GetConsumption(length);
33+
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
34+
}
35+
36+
public void AddObstacle(IObstacle obstacle)
37+
{
38+
if (obstacle is not ICosmosObstacle cosmosObstacle)
39+
throw new ArgumentException("You can't add this obstacle to this environment.");
40+
_obstacles.Add(cosmosObstacle);
41+
}
42+
}

src/Lab1/Environments/HighDensity.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
5+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
6+
using Itmo.ObjectOrientedProgramming.Lab1.Router;
7+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
8+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
9+
10+
namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;
11+
12+
public class HighDensity : IEnvironment
13+
{
14+
private readonly List<IHighDensityObstacle> _obstacles;
15+
16+
public HighDensity(IEnumerable<IHighDensityObstacle> obstacles)
17+
{
18+
_obstacles = obstacles.ToList();
19+
}
20+
21+
public HighDensity()
22+
: this(Enumerable.Empty<IHighDensityObstacle>())
23+
{
24+
}
25+
26+
public TraversalResult TraverseEnvironment(IShip ship, int length)
27+
{
28+
if (ship.JumpEngine == null || ship.JumpEngine.GetRange() < length)
29+
return new TraversalResult.LostShip("Ship lost in a channel");
30+
31+
foreach (IHighDensityObstacle highDensityObstacle in _obstacles)
32+
{
33+
if (highDensityObstacle is AntimatterFlare && !ship.PhotonDeflector)
34+
return new TraversalResult.DeathOfCrew();
35+
highDensityObstacle.GiveDamage(ship);
36+
}
37+
38+
EngineConsumption consumption = ship.JumpEngine.GetConsumption(length);
39+
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
40+
}
41+
42+
public void AddObstacle(IObstacle obstacle)
43+
{
44+
if (obstacle is not IHighDensityObstacle highDensityObstacle)
45+
throw new ArgumentException("You can't add this obstacle to this environment.");
46+
_obstacles.Add(highDensityObstacle);
47+
}
48+
}

src/Lab1/Environments/IEnvironment.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Router;
3+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
4+
5+
namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;
6+
7+
public interface IEnvironment
8+
{
9+
void AddObstacle(IObstacle obstacle);
10+
11+
TraversalResult TraverseEnvironment(IShip ship, int length);
12+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
5+
using Itmo.ObjectOrientedProgramming.Lab1.Router;
6+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
7+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
8+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines.DriveEngines;
9+
10+
namespace Itmo.ObjectOrientedProgramming.Lab1.Environments;
11+
12+
public class NitrineNebula : IEnvironment
13+
{
14+
private readonly List<INitrineParticleObstacle> _obstacles;
15+
16+
public NitrineNebula(IEnumerable<INitrineParticleObstacle> obstacles)
17+
{
18+
_obstacles = obstacles.ToList();
19+
}
20+
21+
public NitrineNebula()
22+
: this(Enumerable.Empty<INitrineParticleObstacle>())
23+
{
24+
}
25+
26+
public TraversalResult TraverseEnvironment(IShip ship, int length)
27+
{
28+
if (ship.DriveEngine is not ExponentialDriveEngine)
29+
return new TraversalResult.LostShip("The engines weren't powerful enough.");
30+
31+
foreach (INitrineParticleObstacle x in _obstacles)
32+
{
33+
x.GiveDamage(ship);
34+
}
35+
36+
EngineConsumption consumption = ship.DriveEngine.GetConsumption(length);
37+
return new TraversalResult.Success(consumption.Time, new[] { consumption.Consumption });
38+
}
39+
40+
public void AddObstacle(IObstacle obstacle)
41+
{
42+
if (obstacle is not INitrineParticleObstacle nitrineParticleObstacle)
43+
throw new ArgumentException("You can't add this obstacle to this environment.");
44+
_obstacles.Add(nitrineParticleObstacle);
45+
}
46+
}

src/Lab1/Lab1.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<RootNamespace>Itmo.ObjectOrientedProgramming.Lab1</RootNamespace>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
56
</PropertyGroup>
67

78
</Project>

src/Lab1/Obstacles/AntimatterFlare.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
5+
6+
public class AntimatterFlare : IHighDensityObstacle
7+
{
8+
private const double Damage = 10;
9+
10+
public void GiveDamage(IShip ship)
11+
{
12+
ship.TakeDamage(Damage);
13+
}
14+
}

src/Lab1/Obstacles/Asteroid.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
5+
6+
public class Asteroid : ICosmosObstacle
7+
{
8+
private const double Damage = 10;
9+
10+
public void GiveDamage(IShip ship)
11+
{
12+
ship.TakeDamage(Damage);
13+
}
14+
}

src/Lab1/Obstacles/CosmoWhale.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
5+
6+
public class CosmoWhale : INitrineParticleObstacle
7+
{
8+
private const double Damage = 200;
9+
private readonly int _population;
10+
11+
public CosmoWhale(int population)
12+
{
13+
_population = population;
14+
}
15+
16+
public void GiveDamage(IShip ship)
17+
{
18+
if (!ship.AntiNitrineEmitter)
19+
ship.TakeDamage(Damage * _population);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
3+
public interface ICosmosObstacle : IObstacle
4+
{
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
3+
public interface IHighDensityObstacle : IObstacle
4+
{
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
3+
public interface INitrineParticleObstacle : IObstacle
4+
{
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
4+
5+
public interface IObstacle
6+
{
7+
void GiveDamage(IShip ship);
8+
}

src/Lab1/Obstacles/Meteorite.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Obstacles.Interfaces;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Obstacles;
5+
6+
public class Meteorite : ICosmosObstacle
7+
{
8+
private const double Damage = 10;
9+
10+
public void GiveDamage(IShip ship)
11+
{
12+
ship.TakeDamage(Damage);
13+
}
14+
}

src/Lab1/Router/Path.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Environments;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Router;
5+
6+
public class Path
7+
{
8+
private readonly IEnvironment _environment;
9+
private readonly int _length;
10+
11+
public Path(IEnvironment environment, int length)
12+
{
13+
_environment = environment;
14+
_length = length;
15+
}
16+
17+
public TraversalResult TraversePath(IShip ship)
18+
{
19+
return _environment.TraverseEnvironment(ship, _length);
20+
}
21+
}

src/Lab1/Router/Route.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Itmo.ObjectOrientedProgramming.Lab1.Ships;
4+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
5+
6+
namespace Itmo.ObjectOrientedProgramming.Lab1.Router;
7+
8+
public class Route
9+
{
10+
private readonly List<Path> _paths;
11+
12+
public Route(IEnumerable<Path> paths)
13+
{
14+
_paths = paths.ToList();
15+
}
16+
17+
public TraversalResult Traverse(IShip ship)
18+
{
19+
int time = 0;
20+
List<Fuel> fuels = new();
21+
22+
foreach (TraversalResult result in _paths.Select(path => path.TraversePath(ship)))
23+
{
24+
if (result is not TraversalResult.Success success)
25+
return result;
26+
time += success.Time;
27+
fuels.AddRange(success.Fuel);
28+
}
29+
30+
return new TraversalResult.Success(time, fuels);
31+
}
32+
}

src/Lab1/Router/TraversalResult.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
3+
4+
namespace Itmo.ObjectOrientedProgramming.Lab1.Router;
5+
6+
public abstract record TraversalResult
7+
{
8+
public record Success(int Time, IEnumerable<Fuel> Fuel) : TraversalResult;
9+
10+
public record LostShip(string Reason) : TraversalResult;
11+
12+
public record DeathOfCrew : TraversalResult;
13+
14+
public record DestroyedShip : TraversalResult;
15+
}

src/Lab1/Services/FuelExchange.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Itmo.ObjectOrientedProgramming.Lab1.Ships.Engines;
2+
3+
namespace Itmo.ObjectOrientedProgramming.Lab1.Services;
4+
5+
public static class FuelExchange
6+
{
7+
public static double GetFuelPrice(Fuel fuel)
8+
{
9+
return fuel.Amount * fuel.Rarity;
10+
}
11+
}

0 commit comments

Comments
 (0)