From ff3fdab4e726e54d970d8f3e397b4f9ffed884e5 Mon Sep 17 00:00:00 2001 From: Surya-AI <70279050+Surya-AI@users.noreply.github.com> Date: Thu, 21 Nov 2024 05:49:31 +0530 Subject: [PATCH] added assault troops and removed DB dependence --- BHServer.csproj | 6 ++ Controllers/APIController.cs | 56 -------------- Controllers/AssaultTroopController.cs | 60 +++++++++++++++ Controllers/ProvinceController.cs | 65 +++++++++++++--- Data/ApplicationDbContext.cs | 18 ----- ...0241111135918_AddProvincesToDb.Designer.cs | 59 -------------- Migrations/20241111135918_AddProvincesToDb.cs | 38 --------- ...1116161946_UpdateProvinceTable.Designer.cs | 62 --------------- .../20241116161946_UpdateProvinceTable.cs | 29 ------- .../ApplicationDbContextModelSnapshot.cs | 59 -------------- Models/AssaultTroop.cs | 23 ++++++ Program.cs | 6 -- Views/Province/Index.cshtml | 77 +------------------ appsettings.json | 8 +- 14 files changed, 146 insertions(+), 420 deletions(-) delete mode 100644 Controllers/APIController.cs create mode 100644 Controllers/AssaultTroopController.cs delete mode 100644 Data/ApplicationDbContext.cs delete mode 100644 Migrations/20241111135918_AddProvincesToDb.Designer.cs delete mode 100644 Migrations/20241111135918_AddProvincesToDb.cs delete mode 100644 Migrations/20241116161946_UpdateProvinceTable.Designer.cs delete mode 100644 Migrations/20241116161946_UpdateProvinceTable.cs delete mode 100644 Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 Models/AssaultTroop.cs diff --git a/BHServer.csproj b/BHServer.csproj index a4d4e22..f387d47 100644 --- a/BHServer.csproj +++ b/BHServer.csproj @@ -18,6 +18,12 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs deleted file mode 100644 index 22f91ed..0000000 --- a/Controllers/APIController.cs +++ /dev/null @@ -1,56 +0,0 @@ -using BHServer.Data; -using Microsoft.AspNetCore.Mvc; - -// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 - -namespace BHServer.Controllers -{ - [Route("[controller]")] - [ApiController] - public class APIController : ControllerBase - { - private readonly ApplicationDbContext _db; - public APIController(ApplicationDbContext db) - { - _db = db; - } - - // GET: / - [HttpGet] - public IEnumerable Get() - { - List arr = []; - foreach (var entry in _db.Provinces.ToList()) - { - String str = @"{""id"": " + entry.ProvinceId + @", ""name"": """ + entry.Name + @""", ""faction"": " + entry.Faction + @", ""mul"": " + entry.Multiplier + @", ""ats"": " + entry.ATs + @", ""state"": " + entry.State + "}"; - arr.Add(str); - } - return arr; - } - - // GET //5 - [HttpGet("{id}")] - public string Get(int id) - { - return "value"; - } - - // POST / - [HttpPost] - public void Post([FromBody] string value) - { - } - - // PUT //5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) - { - } - - // DELETE //5 - [HttpDelete("{id}")] - public void Delete(int id) - { - } - } -} diff --git a/Controllers/AssaultTroopController.cs b/Controllers/AssaultTroopController.cs new file mode 100644 index 0000000..8b90658 --- /dev/null +++ b/Controllers/AssaultTroopController.cs @@ -0,0 +1,60 @@ +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace BHServer.Controllers +{ + public class AssaultTroopController : ControllerBase + { + List MockData = [ + "{\"id\": 1, \"name\": \"First Infantry Division\", \"faction\": 2, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 1}", + "{\"id\": 2, \"name\": \"First Infantry Division\", \"faction\": 1, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 2}", + "{\"id\": 3, \"name\": \"Second Infantry Division\", \"faction\": 1, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 2}", + "{\"id\": 4, \"name\": \"Third Infantry Division\", \"faction\": 1, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 2}", + "{\"id\": 5, \"name\": \"Cheeki Breeki Boys\", \"faction\": 2, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 3}", + "{\"id\": 6, \"name\": \"Hitler\\'s Men\", \"faction\": 2, \"type\": 1, \"province\": 1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 4}", + "{\"id\": 7, \"name\": \"Hitler\\'s Men\", \"faction\": 2, \"type\": 1, \"province\": 1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 4}", + "{\"id\": 8, \"name\": \"Hitler\\'s Men\", \"faction\": 2, \"type\": 1, \"province\": 1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 4}", + "{\"id\": 9, \"name\": \"Cheeki Breeki Boys\", \"faction\": 2, \"type\": 1, \"province\": 1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 3}", + "{\"id\": 10, \"name\": \"Hitler\\'s Men\", \"faction\": 2, \"type\": 1, \"province\": 0, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 4}", + "{\"id\": 11, \"name\": \"Fifth Infantry Division\", \"faction\": 1, \"type\": 1, \"province\": -1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 5}", + "{\"id\": 12, \"name\": \"Fifth Infantry Division\", \"faction\": 1, \"type\": 1, \"province\": -1, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 5}", + "{\"id\": 13, \"name\": \"First Armor Division\", \"faction\": 1, \"type\": 4, \"province\": 2, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 2}", + "{\"id\": 14, \"name\": \"Second Armor Division\", \"faction\": 1, \"type\": 4, \"province\": 2, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 2}", + "{\"id\": 15, \"name\": \"Cheeki Breeki Tonks\", \"faction\": 2, \"type\": 4, \"province\": 2, \"orders\": {\"Dep\": [], \"Mov\": []}, \"owner\": 3}" + ]; + + // GET: / + [HttpGet] + public IEnumerable Get() + { + return MockData; + } + + // GET //5 + [HttpGet("{id}")] + public string Get(int id) + { + var entry = MockData.ElementAt(id); + return entry; + } + + // POST / + [HttpPost] + public void Post([FromBody] string value) + { + } + + // PUT //5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE //5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Controllers/ProvinceController.cs b/Controllers/ProvinceController.cs index 8713900..f245948 100644 --- a/Controllers/ProvinceController.cs +++ b/Controllers/ProvinceController.cs @@ -1,22 +1,65 @@ -using BHServer.Data; -using BHServer.Models; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using System; namespace BHServer.Controllers { public class ProvinceController : Controller { - private readonly ApplicationDbContext _db; - public ProvinceController(ApplicationDbContext db) - { - _db = db; - } + List MockData = [ + "{\"id\": 0, \"name\": \"Hessen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [2, 3, 4], \"Axis\": [1, 5, 9, 10]}, \"state\": 1}", + "{\"id\": 1, \"name\": \"Thuringen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [6, 7, 8]}, \"state\": 0}", + "{\"id\": 2, \"name\": \"Rheinland-Pfalz\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [13, 14], \"Axis\": [15]}, \"state\": 1}", + "{\"id\": 3, \"name\": \"Saarland\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [16, 17, 18], \"Axis\": []}, \"state\": 0}", + "{\"id\": 4, \"name\": \"Baden-Wurttemberg\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [17, 19], \"Axis\": []}, \"state\": 0}", + "{\"id\": 5, \"name\": \"Bayern\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [20, 21], \"Axis\": []}, \"state\": 0}", + "{\"id\": 6, \"name\": \"Sachsen\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [23], \"Axis\": []}, \"state\": 0}", + "{\"id\": 7, \"name\": \"Brandenburg\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [25, 26, 27, 28]}, \"state\": 0}", + "{\"id\": 8, \"name\": \"Berlin\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [29, 30, 31]}, \"state\": 0}", + "{\"id\": 9, \"name\": \"Schleswig-Holstein\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [33]}, \"state\": 0}", + "{\"id\": 10, \"name\": \"Hamburg\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [34]}, \"state\": 0}", + "{\"id\": 11, \"name\": \"Niedersachsen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [35]}, \"state\": 0}", + "{\"id\": 12, \"name\": \"Nordreihn-Westfalen\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [36, 37], \"Axis\": []}, \"state\": 0}" + ]; + public IActionResult Index() { - List ObjectProvinceList = _db.Provinces.ToList(); - // If the above line faulters then ensure SQL Server is running - return View(ObjectProvinceList); + return View(); + } + + [HttpGet] + public IEnumerable Get() + { + return MockData; + } + + // GET //5 + [HttpGet("{id}")] + public string Get(int id) + { + var entry = MockData.ElementAt(id); + return entry; + } + + // POST / + [HttpPost] + public void Post([FromBody] string value) + { + // TODO: Implement + } + + // PUT //5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + // TODO: Implement + } + + // DELETE //5 + [HttpDelete("{id}")] + public void Delete(int id) + { + // TODO: Implement } } } + diff --git a/Data/ApplicationDbContext.cs b/Data/ApplicationDbContext.cs deleted file mode 100644 index cb4f444..0000000 --- a/Data/ApplicationDbContext.cs +++ /dev/null @@ -1,18 +0,0 @@ -using BHServer.Models; -using Microsoft.EntityFrameworkCore; - -namespace BHServer.Data -{ - public class ApplicationDbContext : DbContext - { - public ApplicationDbContext(DbContextOptions options) : base(options) - // this will pass along the options from the implementation to the definition - { - } - - public DbSet Provinces { get; set; } // specify the table and it's class - // to run migrations, run `add-migration ` in the NuGet console - // then make the migrations from the console by using `update-database` - // to rollback migrations run the command `remove-migration ` - } -} diff --git a/Migrations/20241111135918_AddProvincesToDb.Designer.cs b/Migrations/20241111135918_AddProvincesToDb.Designer.cs deleted file mode 100644 index 9af57b4..0000000 --- a/Migrations/20241111135918_AddProvincesToDb.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -// -using BHServer.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace BHServer.Migrations -{ - [DbContext(typeof(ApplicationDbContext))] - [Migration("20241111135918_AddProvincesToDb")] - partial class AddProvincesToDb - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.10") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("BHServer.Models.Province", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ATs") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Faction") - .HasColumnType("int"); - - b.Property("Multiplier") - .HasColumnType("int"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("State") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Provinces"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Migrations/20241111135918_AddProvincesToDb.cs b/Migrations/20241111135918_AddProvincesToDb.cs deleted file mode 100644 index 1a8e1c0..0000000 --- a/Migrations/20241111135918_AddProvincesToDb.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace BHServer.Migrations -{ - /// - public partial class AddProvincesToDb : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Provinces", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Name = table.Column(type: "nvarchar(max)", nullable: false), - Faction = table.Column(type: "int", nullable: false), - Multiplier = table.Column(type: "int", nullable: false), - ATs = table.Column(type: "nvarchar(max)", nullable: false), - State = table.Column(type: "int", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Provinces", x => x.Id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Provinces"); - } - } -} diff --git a/Migrations/20241116161946_UpdateProvinceTable.Designer.cs b/Migrations/20241116161946_UpdateProvinceTable.Designer.cs deleted file mode 100644 index ac6764c..0000000 --- a/Migrations/20241116161946_UpdateProvinceTable.Designer.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -using BHServer.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace BHServer.Migrations -{ - [DbContext(typeof(ApplicationDbContext))] - [Migration("20241116161946_UpdateProvinceTable")] - partial class UpdateProvinceTable - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.10") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("BHServer.Models.Province", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ATs") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Faction") - .HasColumnType("int"); - - b.Property("Multiplier") - .HasColumnType("int"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("ProvinceId") - .HasColumnType("int"); - - b.Property("State") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Provinces"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Migrations/20241116161946_UpdateProvinceTable.cs b/Migrations/20241116161946_UpdateProvinceTable.cs deleted file mode 100644 index 292b2dc..0000000 --- a/Migrations/20241116161946_UpdateProvinceTable.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace BHServer.Migrations -{ - /// - public partial class UpdateProvinceTable : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AddColumn( - name: "ProvinceId", - table: "Provinces", - type: "int", - nullable: false, - defaultValue: 0); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropColumn( - name: "ProvinceId", - table: "Provinces"); - } - } -} diff --git a/Migrations/ApplicationDbContextModelSnapshot.cs b/Migrations/ApplicationDbContextModelSnapshot.cs deleted file mode 100644 index 65163ca..0000000 --- a/Migrations/ApplicationDbContextModelSnapshot.cs +++ /dev/null @@ -1,59 +0,0 @@ -// -using BHServer.Data; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace BHServer.Migrations -{ - [DbContext(typeof(ApplicationDbContext))] - partial class ApplicationDbContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "8.0.10") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("BHServer.Models.Province", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ATs") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Faction") - .HasColumnType("int"); - - b.Property("Multiplier") - .HasColumnType("int"); - - b.Property("Name") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("ProvinceId") - .HasColumnType("int"); - - b.Property("State") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("Provinces"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Models/AssaultTroop.cs b/Models/AssaultTroop.cs new file mode 100644 index 0000000..6be54cc --- /dev/null +++ b/Models/AssaultTroop.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; + +namespace BHServer.Models +{ + public class AssaultTroop + { + [Key] + public int Id { get; set; } + public int ATId { get; set; } + [Required] + public string Name { get; set; } + [Required] + public int Faction { get; set; } // 0 - Neutral, 1 - Allies, 2 - Axis + [Required] + public int Type { get; set; } // 1 - infantry, 4 - Armor + [Required] + public int Province { get; set; } // -1 - not deployed, provinces id + [Required] + public string Orders { get; set; } // Json String + [Required] + public int Owner { get; set; } // owner id + } +} diff --git a/Program.cs b/Program.cs index 708b52e..4846a02 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,3 @@ -using BHServer.Data; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); @@ -6,11 +5,6 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); -builder.Services.AddDbContext( - options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")) - // `ConnectionString` is defined in the appsettings.json file -); - var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Views/Province/Index.cshtml b/Views/Province/Index.cshtml index 063bd44..3d13b8a 100644 --- a/Views/Province/Index.cshtml +++ b/Views/Province/Index.cshtml @@ -4,79 +4,4 @@ @{ } -@model List - -

Provinces

- - - - - - - - - - - - - @foreach (var obj in Model) { - - - - @switch (obj.Faction) - { - case 0: - - break; - case 1: - - break; - case 2: - - break; - } - - - @switch (obj.State) - { - case 0: - - break; - case 1: - - break; - } - - } - -
- Id - - Name - - Faction - - Multiplier - - ATs - - State -
- @obj.ProvinceId - - @obj.Name - - Neutral - - Allies - - Axis - - @obj.Multiplier - - @obj.ATs - - At Peace - - At War -
\ No newline at end of file +

Provinces

\ No newline at end of file diff --git a/appsettings.json b/appsettings.json index a7b4476..9cd9ace 100644 --- a/appsettings.json +++ b/appsettings.json @@ -5,10 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "ConnectionStrings": { - "DefaultConnection": "Server=CREATORPC;Database=BHDB;Trusted_Connection=True;TrustServerCertificate=True" - // the server name in `Server=` needs to point to an actual live server - // We use the command `update-database` in the NuGet Console to create the database - } + "AllowedHosts": "*" + // This also needs to be changed }