Skip to content

Commit

Permalink
ya querys
Browse files Browse the repository at this point in the history
  • Loading branch information
amandanoris committed Dec 3, 2023
1 parent 1c13a27 commit 804cd37
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 448 deletions.
49 changes: 49 additions & 0 deletions src/Application/Statistics/ClanNamesQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Copyright (c) 2023-2025
* This file is part of sep3cs.
*
* sep3cs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sep3cs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>.
*/

using DataClash.Application.Common.Interfaces;
using MediatR;
using FluentValidation;

namespace DataClash.Application.Statistics.AllClanNames
{
public record GetAllClanNamesQuery : IRequest<List<string>>;

public class GetAllClanNamesQueryHandler : IRequestHandler<GetAllClanNamesQuery, List<string>>
{
private readonly IApplicationDbContext _context;

public GetAllClanNamesQueryHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<List<string>> Handle(GetAllClanNamesQuery request, CancellationToken cancellationToken)

Check warning on line 35 in src/Application/Statistics/ClanNamesQuery.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var allClanNames = _context.Clans.Select(c => c.Name).ToList();
return allClanNames;

Check warning on line 38 in src/Application/Statistics/ClanNamesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in value of type 'List<string?>' doesn't match target type 'List<string>'.
}
}

public class GetAllClanNamesQueryValidator : AbstractValidator<GetAllClanNamesQuery>
{
public GetAllClanNamesQueryValidator()
{
// Add validation rules here
}
}
}
8 changes: 4 additions & 4 deletions src/Application/Statistics/CompletedChallengesQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public GetCompletedChallengesQueryHandler(IApplicationDbContext context)
public async Task<List<string[]>> Handle(GetCompletedChallengesQuery request, CancellationToken cancellationToken)

Check warning on line 35 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var result = _context.PlayerChallenges
.Where(pc => pc.WonThrophies == pc.Challenge.Bounty)
.Select(pc => new string[] {
pc.Player.Nickname,
pc.Challenge.Name
.Where(pc => pc.WonThrophies == pc.Challenge.Bounty)

Check warning on line 38 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
.Select(pc => new string[] {
pc.Player.Nickname,

Check warning on line 40 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 40 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
pc.Challenge.Name

Check warning on line 41 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 41 in src/Application/Statistics/CompletedChallengesQuery.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
})
.ToList();

Expand Down
40 changes: 16 additions & 24 deletions src/Application/Statistics/MostGiftedCardsByRegionQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using DataClash.Application.Common.Interfaces;
using MediatR;
using FluentValidation;
using Microsoft.EntityFrameworkCore;

namespace DataClash.Application.Statistics.MostGiftedCardsByRegion
{
Expand All @@ -34,39 +35,30 @@ public GetMostGiftedCardsQueryHandler(IApplicationDbContext context)

public async Task<List<string[]>> Handle(GetMostGiftedCardsQuery request, CancellationToken cancellationToken)
{

//var lastMonth = DateTime.Now.AddMonths(-1);

var cardGifts = _context.CardGifts
// .Where(g => g.Date >= lastMonth)
var joinedEntities = _context.CardGifts
.Include(cg => cg.Clan)
.ToList();

var mostDonatedCards = cardGifts
.GroupBy(g => new { g.Clan.Region.Code, g.CardId })
.Select(g => new
var groupedEntities = joinedEntities
.GroupBy(e => new { e.CardId, e.Clan.Region }) // Agrupa por CardId y Region
.Select(g => new string[]
{
CardId = g.First().CardId,
Region = g.First().Clan.Region,
Count = g.Count()
_context.Cards.Find(g.Key.CardId).Name,
g.Key.Region.ToString(),
g.Count().ToString(),
})
.OrderByDescending(g => g.Count)
.ToList();

var result = new List<string[]>();
foreach (var cardGift in mostDonatedCards)
{
var card = await _context.Cards.FindAsync(cardGift.CardId);
result.Add(new string[]
{
card.Name,
cardGift.Region.ToString(),
cardGift.Count.ToString()
});
}
// Ordena los grupos por el recuento de tarjetas en orden descendente y toma los primeros 3 de cada grupo
var limitedEntities = groupedEntities
.GroupBy(g => g[1]) // Agrupa por Region
.SelectMany(g => g.OrderByDescending(x => int.Parse(x[2])).Take(3)) // Ordena por el recuento de tarjetas y toma los primeros 3
.ToList();

return result;
return limitedEntities;
}


}

}
Expand Down
96 changes: 55 additions & 41 deletions src/Application/Statistics/MostPopularCardByClanQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,105 @@
* You should have received a copy of the GNU General Public License
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>.
*/


using DataClash.Application.Common.Interfaces;
using MediatR;
using FluentValidation;
using DataClash.Domain.Entities;

namespace DataClash.Application.Statistics.MostPopularCards
namespace DataClash.Application.Statistics.PopularCards
{
public class MostPopularCards
{
public IEnumerable<Tuple<Card, string, Clan>> GetMostPopularCards(IApplicationDbContext context, string clanName)
public record GetPopularCardsQuery(string clanName) : IRequest<List<string[]>>;

public class GetPopularCardsQueryHandler : IRequestHandler<GetPopularCardsQuery, List<string[]>>
{
private readonly IApplicationDbContext _context;

public GetPopularCardsQueryHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<List<string[]>> Handle(GetPopularCardsQuery request, CancellationToken cancellationToken)
{
// Obtén el clan dado
var clan = context.Clans.FirstOrDefault(c => c.Name == clanName);
var clan = _context.Clans.FirstOrDefault(c => c.Name == request.clanName);
if (clan == null)
{
return Enumerable.Empty<Tuple<Card, string, Clan>>();
return Enumerable.Empty<string[]>().ToList();
}

// Obtén todos los jugadores en el clan
var players = context.PlayerClans
.Where(pc => pc.ClanId == clan.Id)
.Select(pc => pc.Player)
.ToList();
var players = _context.PlayerClans
.Where(pc => pc.ClanId == clan.Id)
.Select(pc => pc.Player)
.ToList();

// Agrupa los jugadores por su tarjeta favorita y cuenta cuántos jugadores tienen cada tarjeta como favorita
var favoriteCards = players
.Where(p => p.FavoriteCardId.HasValue)
.GroupBy(p => p.FavoriteCardId.Value)
.Select(g => new { CardId = g.Key, Count = g.Count() })
.ToList();
.Where(p => p.FavoriteCardId.HasValue)
.GroupBy(p => p.FavoriteCardId.Value)
.Select(g => new { CardId = g.Key, Count = g.Count() })
.ToList();

// Ordena las tarjetas por el número de jugadores que las tienen como favorita
favoriteCards.Sort((a, b) => b.Count.CompareTo(a.Count));

// Obtén la tarjeta más popular para cada tipo de tarjeta
// Obtén la tarjeta más popular para cada tipo de tarjeta
var mostPopularCards = new List<Tuple<Card, string, Clan>>();
var mostPopularCards = new List<string[]>();

// Para tarjetas mágicas
var magicCards = favoriteCards
.Where(f => context.Cards.OfType<MagicCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();
.Where(f => _context.Cards.OfType<MagicCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();

if (magicCards != null)
{
var card = context.Cards.OfType<MagicCard>().First(c => c.Id == magicCards.CardId);
var cardTuple = Tuple.Create((Card)card, "MagicCard", clan);
mostPopularCards.Add(cardTuple);
var card = _context.Cards.OfType<MagicCard>().First(c => c.Id == magicCards.CardId);
var cardArray = new string[] { card.Name, "MagicCard", clan.Name };
mostPopularCards.Add(cardArray);
}

// Para tarjetas estructurales
var structCards = favoriteCards
.Where(f => context.Cards.OfType<StructCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();
.Where(f => _context.Cards.OfType<StructCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();

if (structCards != null)
{
var card = context.Cards.OfType<StructCard>().First(c => c.Id == structCards.CardId);
var cardTuple = Tuple.Create((Card)card, "StructCard", clan);
mostPopularCards.Add(cardTuple);
var card = _context.Cards.OfType<StructCard>().First(c => c.Id == structCards.CardId);
var cardArray = new string[] { card.Name, "StructCard", clan.Name };
mostPopularCards.Add(cardArray);
}

// Para tarjetas de tropas
var troopCards = favoriteCards
.Where(f => context.Cards.OfType<TroopCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();
.Where(f => _context.Cards.OfType<TroopCard>().Any(c => c.Id == f.CardId))
.OrderByDescending(f => f.Count)
.FirstOrDefault();

if (troopCards != null)
{
var card = context.Cards.OfType<TroopCard>().First(c => c.Id == troopCards.CardId);
var cardTuple = Tuple.Create((Card)card, "TroopCard", clan);
mostPopularCards.Add(cardTuple);
var card = _context.Cards.OfType<TroopCard>().First(c => c.Id == troopCards.CardId);
var cardArray = new string[] { card.Name, "TroopCard", clan.Name };
mostPopularCards.Add(cardArray);
}

return mostPopularCards;

}

public IEnumerable<string> GetAllClansNames(IApplicationDbContext context)
}

public class GetPopularCardsQueryValidator : AbstractValidator<GetPopularCardsQuery>
{
public GetPopularCardsQueryValidator()
{
var allClanNames = context.Clans.Select(c => c.Name).ToList();
return allClanNames;
// Add validation rules here
}

}
}
}



60 changes: 34 additions & 26 deletions src/Application/Statistics/TopPlayersInWarsQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,51 @@
* along with sep3cs. If not, see <http://www.gnu.org/licenses/>.
*/


using DataClash.Application.Common.Interfaces;
using DataClash.Domain.Entities;
using MediatR;
using FluentValidation;

namespace DataClash.Application.Statistics.TopPlayersInWars
namespace DataClash.Application.Statistics.BestPlayer
{
public class BestPlayers
{
public IEnumerable<Tuple<Player, Clan, long>> GetBestPlayer(IApplicationDbContext context, int warId)
{
public record GetBestPlayerQuery(int WarId) : IRequest<List<string[]>>;

public class GetBestPlayerQueryHandler : IRequestHandler<GetBestPlayerQuery, List<string[]>>
{
private readonly IApplicationDbContext _context;

public GetBestPlayerQueryHandler(IApplicationDbContext context)
{
_context = context;
}

var bestPlayers = (from pc in context.PlayerClans
join wc in context.WarClans on pc.ClanId equals wc.ClanId
join w in context.Wars on wc.WarId equals w.Id
join p in context.Players on pc.PlayerId equals p.Id
where w.Id == warId
public async Task<List<string[]>> Handle(GetBestPlayerQuery request, CancellationToken cancellationToken)
{
var bestPlayers = (from pc in _context.PlayerClans
join wc in _context.WarClans on pc.ClanId equals wc.ClanId
where wc.WarId == request.WarId
join p in _context.Players on pc.PlayerId equals p.Id
select new { Player = p, Clan = pc.ClanId, Trophies = p.TotalThrophies })
.ToList();
.ToList();

var groupedPlayers = bestPlayers
.GroupBy(pc => pc.Clan)
.Select(g => new { Clan = g.Key, BestPlayer = g.OrderByDescending(x => x.Trophies).First() })
.ToList();
.GroupBy(pc => pc.Clan)
.Select(g => new { Clan = g.Key, BestPlayer = g.OrderByDescending(x => x.Trophies).First() })
.ToList();

var result = groupedPlayers
.Select(g => new Tuple<Player, Clan, long>(g.BestPlayer.Player, context.Clans.Find(g.BestPlayer.Clan), g.BestPlayer.Trophies))
.ToList();
.Select(g => new string[] { g.BestPlayer.Player.Nickname, _context.Clans.Find(g.Clan).Name, g.BestPlayer.Trophies.ToString() })
.ToList();

return result;
}

public IEnumerable<long> GetAllWarIds(IApplicationDbContext context)
{
var allWarIds = context.Wars.Select(w => w.Id).ToList();
return allWarIds;
}


}
}

public class GetBestPlayerQueryValidator : AbstractValidator<GetBestPlayerQuery>
{
public GetBestPlayerQueryValidator()
{
// Add validation rules here
}
}
}
Loading

0 comments on commit 804cd37

Please sign in to comment.