Samlar ihop och visar poäng från databasen

This commit is contained in:
2025-09-20 09:57:47 +02:00
parent 066503da74
commit 1bf42ef7e6
6 changed files with 109 additions and 16 deletions

View File

@ -1,5 +1,6 @@
using GreadyPoang.DataLayer.Database; using GreadyPoang.DataLayer.Database;
using GreadyPoang.EntityLayer; using GreadyPoang.EntityLayer;
using Microsoft.EntityFrameworkCore;
namespace GreadyPoang.DataLayer; namespace GreadyPoang.DataLayer;
@ -43,6 +44,34 @@ public class CombinedRepository : ICombinedRepository
} }
public IEnumerable<RoundBuilderElement> roundBuilderElementsDb()
{
var result = _context.RoundBuilderElements
.FromSqlRaw(@"
SELECT
gp.GamePointId,
gp.GameRoundId,
gp.GameRoundRegNr,
gp.GameRegPoints,
gr.GameRoundStartDate,
gr.GameStatus AS Status,
p.ParticipantId,
(p.LastName || ' ' || p.FirstName) AS ParticipantName
FROM GamePoints gp
INNER JOIN (
SELECT ParticipantId, GameRoundId, MAX(GamePointId) AS MaxGamePointId
FROM GamePoints
GROUP BY ParticipantId, GameRoundId
) latest ON gp.GamePointId = latest.MaxGamePointId
INNER JOIN GameRounds gr ON gp.GameRoundId = gr.GameRoundId
INNER JOIN Participants p ON gp.ParticipantId = p.ParticipantId
ORDER BY gr.GameRoundStartDate DESC, p.LastName, p.FirstName, gp.GameRoundRegNr
")
.ToList();
return result;
}
public IEnumerable<RoundBuilderElement> roundBuilderElementsTotal() public IEnumerable<RoundBuilderElement> roundBuilderElementsTotal()
{ {
@ -65,4 +94,28 @@ public class CombinedRepository : ICombinedRepository
return result; return result;
} }
public IEnumerable<RoundBuilderElement> roundBuilderElementsTotalById(int roundId)
{
var result = (from gameRound in _context.GameRounds
join gamePoint in _context.GamePoints on gameRound.GameRoundId equals gamePoint.GameRoundId
join participant in _context.Participants on gamePoint.ParticipantId equals participant.ParticipantId
where gameRound.GameRoundId == roundId
orderby gameRound.GameRoundStartDate descending, participant.LastName, participant.FirstName, gamePoint.GameRoundRegNr
select new RoundBuilderElement
{
ParticipantId = participant.ParticipantId,
ParticipantName = participant.LastNameFirstName,
GamePointId = gamePoint.GamePointId,
GameRoundRegNr = gamePoint.GameRoundRegNr,
GameRegPoints = gamePoint.GameRegPoints,
GameRoundId = gameRound.GameRoundId,
GameRoundStartDate = gameRound.GameRoundStartDate,
Status = gameRound.GameStatus
}).ToList();
return result;
}
} }

View File

@ -14,6 +14,8 @@ public class DataContext : DbContext
public DbSet<GamePoint> GamePoints { get; set; } public DbSet<GamePoint> GamePoints { get; set; }
public DbSet<GameRound> GameRounds { get; set; } public DbSet<GameRound> GameRounds { get; set; }
public DbSet<RoundBuilderElement> RoundBuilderElements { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
@ -33,7 +35,7 @@ public class DataContext : DbContext
new GamePoint { GamePointId = 3, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 12, 15), GameRoundRegNr = 2, GameRegPoints = 1000 }, new GamePoint { GamePointId = 3, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 12, 15), GameRoundRegNr = 2, GameRegPoints = 1000 },
new GamePoint { GamePointId = 4, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 20, 15), GameRoundRegNr = 4, GameRegPoints = 400 } new GamePoint { GamePointId = 4, ParticipantId = 3, GameRoundId = 2, GameDate = new DateTime(2025, 10, 15, 20, 20, 15), GameRoundRegNr = 4, GameRegPoints = 400 }
); );
modelBuilder.Entity<RoundBuilderElement>().HasNoKey();
} }
} }

View File

@ -6,5 +6,7 @@ namespace GreadyPoang.DataLayer;
public interface ICombinedRepository public interface ICombinedRepository
{ {
IEnumerable<RoundBuilderElement> roundBuilderElements(); IEnumerable<RoundBuilderElement> roundBuilderElements();
IEnumerable<RoundBuilderElement> roundBuilderElementsDb();
IEnumerable<RoundBuilderElement> roundBuilderElementsTotal(); IEnumerable<RoundBuilderElement> roundBuilderElementsTotal();
IEnumerable<RoundBuilderElement> roundBuilderElementsTotalById(int roundId);
} }

View File

@ -80,7 +80,8 @@ public class RoundRunningViewModel : ViewModelBase
public ObservableCollection<RoundBuilderElement> Get() public ObservableCollection<RoundBuilderElement> Get()
{ {
BuilderObject.ParticipantName = "Kalle Default"; BuilderObject.ParticipantName = _objectMessage.CurrentGroup.Elements[0].ParticipantName;
BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId;
if (_objectMessage.CurrentGroup != null) if (_objectMessage.CurrentGroup != null)
{ {
@ -93,14 +94,14 @@ public class RoundRunningViewModel : ViewModelBase
{ {
_roundElements.Add(item); _roundElements.Add(item);
} }
FillupResultTable(_objectMessage.CurrentGroup.Elements); var localElements = _combined.roundBuilderElementsTotalById(_objectMessage.CurrentGroup.GameRoundId);
FillupResultTable(localElements);
} }
return RoundElements; return RoundElements;
} }
private void FillupResultTable(List<RoundBuilderElement> elements) private void FillupResultTable(IEnumerable<RoundBuilderElement> elements)
{ {
Random slumper = new Random();
if (_playerColumns != null) if (_playerColumns != null)
{ {
_playerColumns.Clear(); _playerColumns.Clear();
@ -109,19 +110,51 @@ public class RoundRunningViewModel : ViewModelBase
{ {
_playerColumns = new Collection<PlayerColumn>(); _playerColumns = new Collection<PlayerColumn>();
} }
foreach (var element in elements)
if (elements.Any(g => g.GameRegPoints > 0))
{ {
var player = new PlayerColumn var oldPart = -1;
{ PlayerColumn player = new PlayerColumn();
PlayerName = element.ParticipantName
};
for (int i = 0; i < slumper.Next(6); i++) foreach (var element in elements)
{ {
player.Values.Add(slumper.Next(1, 10).ToString()); if (element.ParticipantId != oldPart)
{
player = new PlayerColumn
{
PlayerName = element.ParticipantName
};
}
if (element.GameRegPoints > 0)
player.Values.Add(element.GameRegPoints.ToString());
oldPart = element.ParticipantId;
if (!_playerColumns.Contains(player))
{
_playerColumns.Add(player);
}
} }
}
else
{
Random slumper = new Random();
_playerColumns.Add(player); foreach (var element in elements)
{
var player = new PlayerColumn
{
PlayerName = element.ParticipantName
};
for (int i = 0; i < slumper.Next(6); i++)
{
player.Values.Add(slumper.Next(1, 10).ToString());
}
_playerColumns.Add(player);
}
} }
} }

View File

@ -151,7 +151,8 @@ public class RoundStartingViewModel : ViewModelBase
{ {
if (_combined != null) if (_combined != null)
{ {
var GameRoundSummary = _combined.roundBuilderElements(); // var GameRoundSummary = _combined.roundBuilderElements();
var GameRoundSummary = _combined.roundBuilderElementsDb();
var groupedRounds = GameRoundSummary var groupedRounds = GameRoundSummary
.GroupBy(r => r.GameRoundId) .GroupBy(r => r.GameRoundId)

View File

@ -84,9 +84,11 @@
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Label Grid.Row="0" Text="{Binding BuilderObject.ParticipantName}" FontAttributes="Bold" FontSize="Subtitle" /> <Label Grid.Row="0" Text="{Binding BuilderObject.GameRoundId, StringFormat='Runda: {0}'}"/>
<Entry Grid.Row="1" x:Name="Points" Placeholder="Latest Points" Text="{Binding BuilderObject.GameRegPoints}" FontAttributes="Bold" FontSize="20" > <Label Grid.Row="1" Text="{Binding BuilderObject.ParticipantName}" FontAttributes="Bold" FontSize="Subtitle" />
<Entry Grid.Row="2" x:Name="Points" Placeholder="Latest Points" Text="{Binding BuilderObject.GameRegPoints}" FontAttributes="Bold" FontSize="20" >
<Entry.Behaviors> <Entry.Behaviors>
<behaviors:DigitsOnlyBehavior/> <behaviors:DigitsOnlyBehavior/>
</Entry.Behaviors> </Entry.Behaviors>