Skip to content

Commit

Permalink
Adjust Stats output to hide from overall lists if checked
Browse files Browse the repository at this point in the history
Person now has a default colour
  • Loading branch information
Hesketh committed Feb 22, 2024
1 parent f583f12 commit b122b97
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 31 deletions.
1 change: 1 addition & 0 deletions API.Data/Faction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public sealed class Faction : IEntity
{
public string Name { get; set; } = null!;
public string? Link { get; set; } = null;
public Guid Identifier { get; set; }
}
1 change: 1 addition & 0 deletions API.Data/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Hesketh.MecatolArchives.API.Data;
public sealed class Person : IEntity
{
public string Name { get; set; } = null!;
public Colour? DefaultColour { get; set; } = null;
public Guid Identifier { get; set; }

public string Initials
Expand Down
1 change: 1 addition & 0 deletions API.Data/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public sealed class Player : IEntity
public uint Points { get; set; } = 0;
public bool Winner { get; set; } = false;
public bool Eliminated { get; set; } = false;
public uint DraftOrder { get; set; } = 0;

public Person Person { get; set; } = null!;
public Faction Faction { get; set; } = null!;
Expand Down
1 change: 1 addition & 0 deletions API.Data/Post/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public sealed class Person
{
public string Name { get; set; } = null!;
public Guid? DefaultColourId { get; set; } = null;
}
8 changes: 8 additions & 0 deletions API.Data/Put/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Hesketh.MecatolArchives.API.Data.Put;

public sealed class Person
{
public Guid Identifier { get; set; }
public string Name { get; set; } = null!;
public Guid? DefaultColourId { get; set; } = null;
}
61 changes: 56 additions & 5 deletions API/Controllers/PersonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Hesketh.MecatolArchives.DB.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Hesketh.MecatolArchives.API.Controllers;

Expand All @@ -12,37 +13,87 @@ namespace Hesketh.MecatolArchives.API.Controllers;
[Authorize]
public sealed class PersonController : ControllerBase
{
private readonly MecatolArchivesDbContext _db;
private readonly IMapper _mapper;
private readonly CrudControllerHelper<Person, Data.Person, Data.Person, Data.Post.Person> _crud;

public PersonController(MecatolArchivesDbContext db, IMapper mapper)
{
_db = db;
_mapper = mapper;
_crud = new CrudControllerHelper<Person, Data.Person, Data.Person, Data.Post.Person>(db, mapper);
}

[HttpGet("{identifier}")]
[AllowAnonymous]
public async Task<ActionResult<Data.Person>> Get(Guid identifier)
{
return await _crud.GetAsync(identifier);
var person = await _db.People.Include(x => x.DefaultColour)
.FirstOrDefaultAsync(x => x.Identifier == identifier);
if (person == null)
return NotFound();

return _mapper.Map<DTM.Person>(person);
}

[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<ICollection<Data.Person>>> Get()
{
return await _crud.GetAsync();
var res = await _db.GetDbSet<Person>()
.Include(x => x.DefaultColour)
.OrderBy(x => x.Name)
.ToListAsync();

var mapped = _mapper.Map<ICollection<DTM.Person>>(res);
return Ok(mapped);
}

[HttpPost]
public async Task<ActionResult<Data.Person>> Post(Data.Post.Person model)
{
return await _crud.PostAsync(model);
var person = _mapper.Map<DBM.Person>(model);
if (model.DefaultColourId != null)
{
var colour = await _db.Colours.FirstOrDefaultAsync(x => x.Identifier == person.Identifier);
if (colour == null)
return NotFound("The colour could not be found");

person.DefaultColour = colour;
}

await _db.SaveChangesAsync();

var mapped = _mapper.Map<Data.Person>(person);
return Ok(mapped);
}

[HttpPut]
public async Task<ActionResult<Data.Person>> Put(Data.Person model)
public async Task<ActionResult<Data.Person>> Put(DTPut.Person model)
{
return await _crud.PutAsync(model);
var person = await _db.People.Include(x => x.DefaultColour)
.FirstOrDefaultAsync(x => x.Identifier == model.Identifier);
if (person == null)
return NotFound();

_mapper.Map(model, person);
if (model.DefaultColourId != null)
{
var colour = await _db.Colours.FirstOrDefaultAsync(x => x.Identifier == model.DefaultColourId);
if (colour == null)
return NotFound("The colour could not be found");

person.DefaultColour = colour;
}
else
{
person.DefaultColour = null;
}

await _db.SaveChangesAsync();

var mapped = _mapper.Map<Data.Person>(person);
return Ok(mapped);
}

[HttpDelete("{identifier}")]
Expand Down
46 changes: 20 additions & 26 deletions API/Controllers/StatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public StatsController(MecatolArchivesDbContext db)
public ActionResult<Statistics> GetPlayersFactions(Guid personIdentifier)
{
var overall = new Statistics();

var statsLookup = new Dictionary<Guid, Statistic>();
foreach (var faction in _db.Factions.Where(x => x.Name != MecatolArchivesDbContext.UnknownName))
foreach (var faction in _db.Factions)
{
statsLookup[faction.Identifier] = new Statistic(faction.Name, faction.Identifier);
}
Expand All @@ -34,21 +34,18 @@ public ActionResult<Statistics> GetPlayersFactions(Guid personIdentifier)
{
foreach (var player in play.Players.Where(x => x.Person.Identifier == personIdentifier))
{
if (!statsLookup.TryGetValue(player.Faction.Identifier, out var stats))
if (statsLookup.TryGetValue(player.Faction.Identifier, out var stats))
{
stats = new Statistic(player.Faction.Name, player.Faction.Identifier);
statsLookup[player.Faction.Identifier] = stats;
}
stats.Plays += 1;

stats.Plays += 1;
if (player.Winner)
{
stats.Wins += 1;
}

if (player.Winner)
{
stats.Wins += 1;
var pointPercentage = (double)player.Points / play.PointGoal;
stats.PointPercentSum += pointPercentage;
}

var pointPercentage = (double)player.Points / play.PointGoal;
stats.PointPercentSum += pointPercentage;
}
}

Expand All @@ -66,7 +63,7 @@ public ActionResult<Statistics> GetFactionStats()
var overall = new Statistics();

var statsLookup = new Dictionary<Guid, Statistic>();
foreach (var faction in _db.Factions.Where(x => x.Name != MecatolArchivesDbContext.UnknownName))
foreach (var faction in _db.Factions.Where(x => !x.HideFromStatistics))
{
statsLookup[faction.Identifier] = new Statistic(faction.Name, faction.Identifier);
}
Expand All @@ -77,21 +74,18 @@ public ActionResult<Statistics> GetFactionStats()
{
foreach (var player in play.Players)
{
if (!statsLookup.TryGetValue(player.Faction.Identifier, out var stats))
if (statsLookup.TryGetValue(player.Faction.Identifier, out var stats))
{
stats = new Statistic(player.Faction.Name, player.Faction.Identifier);
statsLookup[player.Faction.Identifier] = stats;
}
stats.Plays += 1;

stats.Plays += 1;
if (player.Winner)
{
stats.Wins += 1;
}

if (player.Winner)
{
stats.Wins += 1;
var pointPercentage = (double)player.Points / play.PointGoal;
stats.PointPercentSum += pointPercentage;
}

var pointPercentage = (double)player.Points / play.PointGoal;
stats.PointPercentSum += pointPercentage;
}
}

Expand All @@ -113,7 +107,7 @@ public ActionResult<Statistics> GetPeopleStats()
.ThenInclude(x => x.Person)
.Where(x => x.Players.Any(y => y.Winner)))
{
foreach (var player in play.Players)
foreach (var player in play.Players.Where(x => !x.Person.HideFromStatistics))
{
if (!statsLookup.TryGetValue(player.Person.Identifier, out var stats))
{
Expand Down
1 change: 1 addition & 0 deletions API/Mapping/Put.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class Put : Profile
public Put()
{
CreateMap<Play, DB.Models.Play>();
CreateMap<Person, DB.Models.Person>();
}
}

0 comments on commit b122b97

Please sign in to comment.