Skip to content

Commit

Permalink
Dares basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
WildCup committed Jul 1, 2023
1 parent cc5adc7 commit f250c29
Show file tree
Hide file tree
Showing 29 changed files with 809 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,4 @@ $RECYCLE.BIN/
## Visual Studio Code
##
.vscode/*

.editorconfig
43 changes: 43 additions & 0 deletions Calculator.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
60 changes: 49 additions & 11 deletions Controllers/DareController.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
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
{
[ApiController]
[Route("[controller]")]
public class DareController : ControllerBase
{
private readonly ILogger<DareController> _logger;
private readonly IDareService _dareService;

public DareController(ILogger<DareController> logger)
public DareController(IDareService dareService)
{
_logger = logger;
_dareService = dareService;
}


[HttpPatch]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> 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<IActionResult> GetRandom()
{
var response = await _dareService.GetRandom();
if (response.Success == true)
return Ok(response.Data);
return (BadRequest(response.Exception));
}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateLvl(int id, DareUpdateDto newDare)
{
newDare.Id = id;
var response = await _dareService.Update(newDare);

if (response.Success == true)
return Ok();
return (BadRequest(response.Exception));
}
}
}
6 changes: 6 additions & 0 deletions DaresGacha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

Expand Down
18 changes: 18 additions & 0 deletions Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -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<DataContext> options) : base(options)
{

}

public DbSet<Dare> Dares { get; set; } = null!;
}
}
8 changes: 8 additions & 0 deletions Dtos/DareAddDto.cs
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions Dtos/DareGetDto.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
9 changes: 9 additions & 0 deletions Dtos/DareGetRandomDto.cs
Original file line number Diff line number Diff line change
@@ -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; } = { };
}
9 changes: 9 additions & 0 deletions Dtos/DareUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions MapperProfile.cs
Original file line number Diff line number Diff line change
@@ -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<Dare, DareGetDto>().ReverseMap();
CreateMap<Dare, DareAddDto>().ReverseMap();
CreateMap<Dare, DareUpdateDto>().ReverseMap();
CreateMap<Dare, DareGetRandomDto>().ReverseMap();

}
}
}
49 changes: 49 additions & 0 deletions Migrations/20221224191319_InitialCreate.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Migrations/20221224191319_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace DaresGacha.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Dares",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Text = table.Column<string>(type: "nvarchar(max)", nullable: false),
Level = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Dares", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Dares");
}
}
}
55 changes: 55 additions & 0 deletions Migrations/20221225003835_UpdateDare.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f250c29

Please sign in to comment.