diff --git a/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs b/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs index 3cbd867..4d01f3c 100644 --- a/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs +++ b/GreadyPoang.DataLayer/DataClasses/CombinedRepository.cs @@ -71,6 +71,33 @@ public class CombinedRepository : ICombinedRepository return result; } + public IEnumerable roundBuilderElementsDbById(int GameId) + { + 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 + WHERE gp.GameRoundId = {GameId.ToString()} + ORDER BY gr.GameRoundStartDate DESC, p.LastName, p.FirstName, gp.GameRoundRegNr + ") + .ToList(); + return result; + } public IEnumerable roundBuilderElementsTotal() { diff --git a/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs b/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs index 0d85903..fe7d614 100644 --- a/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs +++ b/GreadyPoang.DataLayer/LocalInterfaces/ICombinedRepository.cs @@ -7,6 +7,7 @@ public interface ICombinedRepository { IEnumerable roundBuilderElements(); IEnumerable roundBuilderElementsDb(); + IEnumerable roundBuilderElementsDbById(int GameId); IEnumerable roundBuilderElementsTotal(); IEnumerable roundBuilderElementsTotalById(int roundId); } \ No newline at end of file diff --git a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs index 240f3f1..201289a 100644 --- a/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs +++ b/GreadyPoang.ViewModelLayer/ViewModelClasses/RoundRunningViewModel.cs @@ -80,11 +80,11 @@ public class RoundRunningViewModel : ViewModelBase public ObservableCollection Get() { - BuilderObject.ParticipantName = _objectMessage.CurrentGroup.Elements[0].ParticipantName; - BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId; + if (_objectMessage.CurrentGroup != null) { + //CurrentGroup är satt från RoundStarting ViewModel Debug.WriteLine($"Chosen round: {_objectMessage.CurrentGroup.GameRoundId}"); if (RoundElements.Count > 0) { @@ -94,12 +94,65 @@ public class RoundRunningViewModel : ViewModelBase { _roundElements.Add(item); } + + var nxt = nextPlayerElement(); + BuilderObject.ParticipantName = _objectMessage.CurrentGroup.Elements[nxt].ParticipantName; + BuilderObject.GameRoundId = _objectMessage.CurrentGroup.GameRoundId; + BuilderObject.ParticipantId = _objectMessage.CurrentGroup.Elements[nxt].ParticipantId; + var localElements = _combined.roundBuilderElementsTotalById(_objectMessage.CurrentGroup.GameRoundId); FillupResultTable(localElements); } return RoundElements; } + + public void StoreAndHandlePoints() + { + var regNr = RoundElements.Count > 0 ? RoundElements.Max(e => e.GameRoundRegNr) + 1 : 1; + var points = BuilderObject.GameRegPoints; + var newPoint = new GamePoint + { + ParticipantId = BuilderObject.ParticipantId, + GameRoundId = BuilderObject.GameRoundId, + GameDate = DateTime.Now, + GameRoundRegNr = regNr, + GameRegPoints = points + }; + var pointId = _pointsRepo?.Save(newPoint).GetAwaiter().GetResult(); + + // Uppdatera listan med element + var tmpElements = _combined.roundBuilderElementsDbById(BuilderObject.GameRoundId); + RoundElements.Clear(); + foreach (var item in tmpElements) + { + RoundElements.Add(item); + } + // Uppdatera spelaren som skall spela nästa + var nxt = nextPlayerElement(); + BuilderObject.ParticipantName = RoundElements[nxt].ParticipantName; + BuilderObject.ParticipantId = RoundElements[nxt].ParticipantId; + BuilderObject.GameRoundId = RoundElements[0].GameRoundId; + + var localElements = _combined.roundBuilderElementsTotalById(BuilderObject.GameRoundId); + FillupResultTable(localElements); + + } + + private int nextPlayerElement() + { + for (int i = 0; i < RoundElements.Count; i++) + { + if (RoundElements[i].GameRoundRegNr == -1 || + (i > 0 && (RoundElements[i - 1].GameRoundRegNr > RoundElements[i].GameRoundRegNr)) || + (i == 0 && RoundElements[i].GameRoundRegNr < RoundElements[RoundElements.Count - 1].GameRoundRegNr)) + return i; + } + return -1; + } + + + private void FillupResultTable(IEnumerable elements) { if (_playerColumns != null) diff --git a/GreadyPoang/CommandClasses/RoundRunningViewModelCommands.cs b/GreadyPoang/CommandClasses/RoundRunningViewModelCommands.cs index 1dee87b..c4407c8 100644 --- a/GreadyPoang/CommandClasses/RoundRunningViewModelCommands.cs +++ b/GreadyPoang/CommandClasses/RoundRunningViewModelCommands.cs @@ -33,14 +33,14 @@ public class RoundRunningViewModelCommands : RoundRunningViewModel public ICommand RensaCommand { get; private set; } public ICommand ElementTappedCommand { get; private set; } public ICommand ParticipantTappedCommand { get; private set; } - public ICommand StoreAndHandlePontsCommand { get; private set; } + public ICommand StoreAndHandlePointsCommand { get; private set; } #endregion public override void Init() { base.Init(); - StoreAndHandlePontsCommand = new Command(async () => StoreAndHandle()); + StoreAndHandlePointsCommand = new Command(async () => StoreAndHandleAsync()); //EditCommand = new Command(async (id) => await EditAsync(id), (id) => id > 0); //RensaCommand = new Command(async () => RensaAsync()); //ParticipantTappedCommand = new Command(async (selectedParticipant) => SelectNewlyAddedParticipant(selectedParticipant)); @@ -48,8 +48,9 @@ public class RoundRunningViewModelCommands : RoundRunningViewModel } - private void StoreAndHandle() + private async Task StoreAndHandleAsync() { - throw new NotImplementedException(); + base.StoreAndHandlePoints(); + await Shell.Current.GoToAsync(".."); } } diff --git a/GreadyPoang/Views/RoundRunningView.xaml b/GreadyPoang/Views/RoundRunningView.xaml index e70b031..b00c32a 100644 --- a/GreadyPoang/Views/RoundRunningView.xaml +++ b/GreadyPoang/Views/RoundRunningView.xaml @@ -94,7 +94,9 @@ -