diff --git a/.gitignore b/.gitignore index 8432eb7..dc90c3d 100644 --- a/.gitignore +++ b/.gitignore @@ -448,4 +448,4 @@ $RECYCLE.BIN/ ## Visual Studio Code ## .vscode/* - +.editorconfig diff --git a/Calculator.cs b/Calculator.cs new file mode 100644 index 0000000..1341ed3 --- /dev/null +++ b/Calculator.cs @@ -0,0 +1,43 @@ + +namespace DaresGacha; + +public class Calculator +{ + public static double[] GetProbability(int done) + { + //chance for difficulty level jump, tie, slap, kiss, sex + //every 10 dares increase difficulty + var list = new double[,] + { + {80, 15, 2, 0, 0}, //start + {70, 23.2, 3.5, 0.3, 0}, //-10 +8.2 +1.5 +0.3 + {57, 30, 8, 1, 0.0}, //-13 +7.8 +4.5 +0.7 + {29.7, 45, 18, 3, 0.3}, //-27.3 +15 +10 +2 +0.3 + {0, 52, 40.5, 10, 1.5}, //-29.7 +7 +22.5 +7 +1.2 + {0, 6, 50, 38, 6}, //-0 -46 +9.5 +28 +4.5 + {0, 0, 20, 60, 20}, //-0 -6 -30 +22 +14 + {0, 0, 0, 20, 80}, //-0 -0 -20 -40 +60 + }; + + int i = Math.Min(list.Length, (int)(done / 10)); + return Enumerable.Range(0, list.GetLength(1)) + .Select(x => list[i, x]) + .ToArray(); + } + + public static int GetLvl(double[] probability) + { + //sum probabilities + var chance = new double[probability.Length]; + probability.CopyTo(chance, 0); + for (int i = 1; i < chance.Length; i++) + chance[i] += chance[i - 1]; + + int random = new Random().Next(0, 100); + + for (int j = 0; j < chance.Length; j++) + if (random <= chance[j]) return j; + + return chance.Length - 1; + } +} diff --git a/Controllers/DareController.cs b/Controllers/DareController.cs index 3d1b38b..a6bb034 100644 --- a/Controllers/DareController.cs +++ b/Controllers/DareController.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; +using DaresGacha.Dtos; +using DaresGacha.Services; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; namespace DaresGacha.Controllers { @@ -12,17 +8,59 @@ namespace DaresGacha.Controllers [Route("[controller]")] public class DareController : ControllerBase { - private readonly ILogger _logger; + private readonly IDareService _dareService; - public DareController(ILogger logger) + public DareController(IDareService dareService) { - _logger = logger; + _dareService = dareService; + } + + + [HttpPatch] + public async Task Add(DareAddDto newDare) + { + var response = await _dareService.Add(newDare); + if (response.Success == true) + return Ok(response.Data); + return (BadRequest(response.Exception)); + } + + [HttpDelete("{id}")] + public async Task Delete(int id) + { + var response = await _dareService.Delete(id); + if (response.Success == true) + return Ok(); + return (BadRequest(response.Exception)); } [HttpGet] - public IActionResult Get() + public async Task GetAll(int? lvl = null, bool? done = null, bool? isDeleted = null) { - return Ok("xd lalala"); + var response = await _dareService.GetAll(lvl, done, isDeleted); + if (response.Success == true) + return Ok(response.Data); + return (BadRequest(response.Exception)); + } + + [HttpGet("random")] + public async Task GetRandom() + { + var response = await _dareService.GetRandom(); + if (response.Success == true) + return Ok(response.Data); + return (BadRequest(response.Exception)); + } + + [HttpPut("{id}")] + public async Task UpdateLvl(int id, DareUpdateDto newDare) + { + newDare.Id = id; + var response = await _dareService.Update(newDare); + + if (response.Success == true) + return Ok(); + return (BadRequest(response.Exception)); } } } \ No newline at end of file diff --git a/DaresGacha.csproj b/DaresGacha.csproj index 60bf9ea..a76c501 100644 --- a/DaresGacha.csproj +++ b/DaresGacha.csproj @@ -7,6 +7,12 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/Data/DataContext.cs b/Data/DataContext.cs new file mode 100644 index 0000000..fbba871 --- /dev/null +++ b/Data/DataContext.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace DaresGacha.Data +{ + public class DataContext : DbContext + { + public DataContext(DbContextOptions options) : base(options) + { + + } + + public DbSet Dares { get; set; } = null!; + } +} \ No newline at end of file diff --git a/Dtos/DareAddDto.cs b/Dtos/DareAddDto.cs new file mode 100644 index 0000000..b85b97c --- /dev/null +++ b/Dtos/DareAddDto.cs @@ -0,0 +1,8 @@ +namespace DaresGacha.Dtos; + +public class DareAddDto +{ + public string Text { get; set; } = string.Empty; + public int Level { get; set; } + public bool Done { get; set; } = false; +} diff --git a/Dtos/DareGetDto.cs b/Dtos/DareGetDto.cs new file mode 100644 index 0000000..3daf2bb --- /dev/null +++ b/Dtos/DareGetDto.cs @@ -0,0 +1,10 @@ +namespace DaresGacha.Dtos; + +public class DareGetDto +{ + public int Id { get; set; } + public string Text { get; set; } = string.Empty; + public int Level { get; set; } + public bool Done { get; set; } = false; + public bool IsDeleted { get; set; } +} diff --git a/Dtos/DareGetRandomDto.cs b/Dtos/DareGetRandomDto.cs new file mode 100644 index 0000000..575e590 --- /dev/null +++ b/Dtos/DareGetRandomDto.cs @@ -0,0 +1,9 @@ +namespace DaresGacha.Dtos; + +public class DareGetRandomDto +{ + public int Id { get; set; } + public string Text { get; set; } = string.Empty; + public int Level { get; set; } + public double[] Difficulty { get; set; } = { }; +} diff --git a/Dtos/DareUpdateDto.cs b/Dtos/DareUpdateDto.cs new file mode 100644 index 0000000..bfdceb0 --- /dev/null +++ b/Dtos/DareUpdateDto.cs @@ -0,0 +1,9 @@ +namespace DaresGacha.Dtos; + +public class DareUpdateDto +{ + public int Id { get; set; } + public string Text { get; set; } = string.Empty; + public int Level { get; set; } + public bool Done { get; set; } = false; +} diff --git a/MapperProfile.cs b/MapperProfile.cs new file mode 100644 index 0000000..1ac0f41 --- /dev/null +++ b/MapperProfile.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DaresGacha.Dtos; + +namespace DaresGacha +{ + public class MapperProfile : Profile + { + public MapperProfile() + { + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + + } + } +} \ No newline at end of file diff --git a/Migrations/20221224191319_InitialCreate.Designer.cs b/Migrations/20221224191319_InitialCreate.Designer.cs new file mode 100644 index 0000000..ffb61e6 --- /dev/null +++ b/Migrations/20221224191319_InitialCreate.Designer.cs @@ -0,0 +1,49 @@ +// +using DaresGacha.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DaresGacha.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20221224191319_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DaresGacha.Model.Dare", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Level") + .HasColumnType("int"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Dares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221224191319_InitialCreate.cs b/Migrations/20221224191319_InitialCreate.cs new file mode 100644 index 0000000..87d94b9 --- /dev/null +++ b/Migrations/20221224191319_InitialCreate.cs @@ -0,0 +1,35 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DaresGacha.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Dares", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Text = table.Column(type: "nvarchar(max)", nullable: false), + Level = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Dares", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Dares"); + } + } +} diff --git a/Migrations/20221225003835_UpdateDare.Designer.cs b/Migrations/20221225003835_UpdateDare.Designer.cs new file mode 100644 index 0000000..89ff0aa --- /dev/null +++ b/Migrations/20221225003835_UpdateDare.Designer.cs @@ -0,0 +1,55 @@ +// +using DaresGacha.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DaresGacha.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20221225003835_UpdateDare")] + partial class UpdateDare + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DaresGacha.Model.Dare", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Done") + .HasColumnType("bit"); + + b.Property("Level") + .HasColumnType("int"); + + b.Property("Skipped") + .HasColumnType("int"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Dares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221225003835_UpdateDare.cs b/Migrations/20221225003835_UpdateDare.cs new file mode 100644 index 0000000..7971963 --- /dev/null +++ b/Migrations/20221225003835_UpdateDare.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DaresGacha.Migrations +{ + /// + public partial class UpdateDare : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Done", + table: "Dares", + type: "bit", + nullable: false, + defaultValue: false); + + migrationBuilder.AddColumn( + name: "Skipped", + table: "Dares", + type: "int", + nullable: false, + defaultValue: 0); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Done", + table: "Dares"); + + migrationBuilder.DropColumn( + name: "Skipped", + table: "Dares"); + } + } +} diff --git a/Migrations/20230701142423_DaresUpdate.Designer.cs b/Migrations/20230701142423_DaresUpdate.Designer.cs new file mode 100644 index 0000000..6bf1c74 --- /dev/null +++ b/Migrations/20230701142423_DaresUpdate.Designer.cs @@ -0,0 +1,58 @@ +// +using DaresGacha.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DaresGacha.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20230701142423_DaresUpdate")] + partial class DaresUpdate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DaresGacha.Model.Dare", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Done") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Level") + .HasColumnType("int"); + + b.Property("Skipped") + .HasColumnType("int"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Dares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20230701142423_DaresUpdate.cs b/Migrations/20230701142423_DaresUpdate.cs new file mode 100644 index 0000000..e6fffc2 --- /dev/null +++ b/Migrations/20230701142423_DaresUpdate.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DaresGacha.Migrations +{ + /// + public partial class DaresUpdate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "IsDeleted", + table: "Dares", + type: "bit", + nullable: false, + defaultValue: false); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "IsDeleted", + table: "Dares"); + } + } +} diff --git a/Migrations/DataContextModelSnapshot.cs b/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..9d0e8a9 --- /dev/null +++ b/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,55 @@ +// +using DaresGacha.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DaresGacha.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.1") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DaresGacha.Model.Dare", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Done") + .HasColumnType("bit"); + + b.Property("IsDeleted") + .HasColumnType("bit"); + + b.Property("Level") + .HasColumnType("int"); + + b.Property("Skipped") + .HasColumnType("int"); + + b.Property("Text") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Dares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Model/Base.cs b/Model/Base.cs new file mode 100644 index 0000000..ca783f4 --- /dev/null +++ b/Model/Base.cs @@ -0,0 +1,6 @@ +namespace DaresGacha.Model; + +public abstract class Base +{ + public int Id { get; set; } +} diff --git a/Model/Dare.cs b/Model/Dare.cs index 5e6ddcf..b065e77 100644 --- a/Model/Dare.cs +++ b/Model/Dare.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace DaresGacha.Model; -namespace DaresGacha.Model +public class Dare : Base { - public class Dare - { - public int Id { get; set; } - public string Text { get; set; } = string.Empty; - public int Level { get; set; } - } -} \ No newline at end of file + public string Text { get; set; } = string.Empty; + public int Level { get; set; } = 1; //max 5 + public bool Done { get; set; } = false; + public int Skipped { get; set; } = 0; + public bool IsDeleted { get; set; } +} diff --git a/Model/Player.cs b/Model/Player.cs index eb98c20..649cb5d 100644 --- a/Model/Player.cs +++ b/Model/Player.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +namespace DaresGacha.Model; -namespace DaresGacha.Model +public class Player : Base { - public class Player - { - public int Id { get; set; } - public string Name { get; set; } = string.Empty; - } -} \ No newline at end of file + public string Name { get; set; } = string.Empty; + public bool IsMale { get; set; } + public int PartnerId { get; set; } +} diff --git a/Model/ServiceResponse.cs b/Model/ServiceResponse.cs new file mode 100644 index 0000000..fa30f9a --- /dev/null +++ b/Model/ServiceResponse.cs @@ -0,0 +1,9 @@ + +namespace DaresGacha.Model; + +public class ServiceResponse +{ + public T? Data { get; set; } + public bool Success { get; set; } = true; + public string Exception { get; set; } = string.Empty; +} diff --git a/Program.cs b/Program.cs index 4332f92..8c3e6f5 100644 --- a/Program.cs +++ b/Program.cs @@ -1,17 +1,28 @@ global using DaresGacha.Model; +using DaresGacha.Data; +using DaresGacha.Services; +using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); + +builder.Services.AddAutoMapper(typeof(Program).Assembly); +builder.Services.AddScoped, Repository>(); +// builder.Services.AddScoped(); +builder.Services.AddScoped(); + builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); + var app = builder.Build(); -// Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/Repositories/IRepository.cs b/Repositories/IRepository.cs new file mode 100644 index 0000000..f8b9c36 --- /dev/null +++ b/Repositories/IRepository.cs @@ -0,0 +1,14 @@ +namespace DaresGacha.Services; + +public interface IRepository where T : Base +{ + Task Add(Base entity); + + Task Delete(int id); + + Task Get(int id); + + Task> GetAll(); + + Task UpdateAsync(Base entity); +} diff --git a/Repositories/Repository.cs b/Repositories/Repository.cs new file mode 100644 index 0000000..0a61091 --- /dev/null +++ b/Repositories/Repository.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DaresGacha.Data; +using Microsoft.EntityFrameworkCore; + +namespace DaresGacha.Services +{ + public class Repository : IRepository where T : Base + { + protected readonly DataContext _context; + + public Repository(DataContext context, IMapper mapper) + { + _context = context; + } + + + public async Task Add(Base entity) + { + _context.Set().Add((T)entity); + await _context.SaveChangesAsync(); + + return entity.Id; + } + + public async Task Delete(int id) + { + var entity = await Get(id); + if (entity == null) return; + + _context.Set().Remove((T)entity); + await _context.SaveChangesAsync(); + } + + public async Task Get(int id) + { + return await _context.Set().FirstOrDefaultAsync(e => e.Id == id); + } + + public async Task> GetAll() + { + return await _context.Set().ToListAsync(); + } + + public async Task UpdateAsync(Base entity) + { + _context.Set().Update((T)entity); + await _context.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/Services/BaseService.cs b/Services/BaseService.cs new file mode 100644 index 0000000..84509cc --- /dev/null +++ b/Services/BaseService.cs @@ -0,0 +1,102 @@ +using AutoMapper; + +namespace DaresGacha.Services +{ + public abstract class BaseService where T : Dare + { + protected readonly IRepository _repository; + protected readonly IMapper _mapper; + + public BaseService(IRepository repository, IMapper mapper) + { + _repository = repository; + _mapper = mapper; + } + + + public async Task> Add(Base entity) + { + try + { + var id = await _repository.Add(entity); + return new ServiceResponse() { Data = id }; + } + catch (Exception e) + { + return new ServiceResponse() + { + Success = false, + Exception = e.Message + }; + } + } + + public async Task> Delete(int id) + { + try + { + await _repository.Delete(id); + return new ServiceResponse(); + } + catch (Exception e) + { + return new ServiceResponse() + { + Success = false, + Exception = e.Message + }; + } + } + + public async Task> Get(int id) + { + try + { + var entity = await _repository.Get(id); + return new ServiceResponse() { Data = entity }; + } + catch (Exception e) + { + return new ServiceResponse() + { + Success = false, + Exception = e.Message + }; + } + } + + public async Task>> GetAll() + { + try + { + var list = await _repository.GetAll(); + return new ServiceResponse>() { Data = list }; + } + catch (Exception e) + { + return new ServiceResponse>() + { + Success = false, + Exception = e.Message + }; + } + } + + public async Task> Update(T entity) + { + try + { + var id = await _repository.Add(entity); + return new ServiceResponse(); + } + catch (Exception e) + { + return new ServiceResponse() + { + Success = false, + Exception = e.Message + }; + } + } + } +} \ No newline at end of file diff --git a/Services/DareService.cs b/Services/DareService.cs new file mode 100644 index 0000000..f642688 --- /dev/null +++ b/Services/DareService.cs @@ -0,0 +1,78 @@ +using AutoMapper; +using DaresGacha.Data; +using DaresGacha.Dtos; +using System.Linq; +using Microsoft.EntityFrameworkCore; + +namespace DaresGacha.Services; + +public class DareService : BaseService, IDareService +{ + public DareService(IRepository repository, IMapper mapper) : base(repository, mapper) { } + + public async Task> Add(DareAddDto newDare) + { + var dare = _mapper.Map(newDare); + return await base.Add(dare); + } + + public async Task>> GetAll(int? lvl, bool? done, bool? isDeleted) + { + var dares = await _repository.GetAll(); + + if (lvl != null) + dares = dares.Where(d => d.Level == lvl); + if (done != null) + dares = dares.Where(d => d.Done == done); + if (isDeleted != null) + dares = dares.Where(d => d.IsDeleted == isDeleted); + + return new ServiceResponse>() { Data = _mapper.Map>(dares) }; + } + + public async Task> GetRandom() + { + var done = (await _repository.GetAll()).Where(e => e.Done == true).Count(); + var difficulty = Calculator.GetProbability(done); + var lvl = Calculator.GetLvl(difficulty); + + var dares = await _repository.GetAll(); + dares = FilterByLvl(dares, lvl, 0, difficulty.Length); + + if (dares.Count() == 0) return new ServiceResponse() { Success = false, Exception = "No element found" }; + + int i = new Random().Next(0, dares.Count()); + var chosenOne = dares.ToArray()[i]; + return new ServiceResponse() { Data = _mapper.Map(chosenOne) }; + } + + public async Task> Update(DareUpdateDto newDare) + { + var dare = _mapper.Map(newDare); + return await base.Update(dare); + } + + private IEnumerable FilterByLvl(IEnumerable dares, int lvl, int minLvl, int maxLvl) + { + if (dares.Where(e => e.Level == lvl).Count() > 0) + return dares.Where(e => e.Level == lvl); + + int originalLvl = lvl; + //get dares of smaller level + while (lvl >= minLvl) + { + if (dares.Where(e => e.Level == lvl).Count() > 0) + return dares.Where(e => e.Level == lvl); + lvl--; + } + //get dares of greater level + lvl = originalLvl; + while (lvl <= maxLvl) + { + if (dares.Where(e => e.Level == lvl).Count() > 0) + return dares.Where(e => e.Level == lvl); + lvl++; + } + return dares; //no elements + } +} diff --git a/Services/IDareService.cs b/Services/IDareService.cs new file mode 100644 index 0000000..a4f067b --- /dev/null +++ b/Services/IDareService.cs @@ -0,0 +1,12 @@ +using DaresGacha.Dtos; + +namespace DaresGacha.Services; + +public interface IDareService +{ + Task> Add(DareAddDto newDare); + Task> Delete(int id); + Task>> GetAll(int? lvl, bool? done, bool? isDeleted); + Task> GetRandom(); + Task> Update(DareUpdateDto newDare); +} diff --git a/appsettings.json b/appsettings.json index 10f68b8..392cb7b 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=DaresGacha;Trusted_Connection=True;TrustServerCertificate=True" + }, "Logging": { "LogLevel": { "Default": "Information", diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..4859e3a --- /dev/null +++ b/notes.txt @@ -0,0 +1,9 @@ +needed: +t-shirt to tear +strings +rope +handcuffs +tap water +draw drink +if there is a challenge (ex. guess sth) and you fail to do it, then you drink +pocky game \ No newline at end of file