TestMatchTheElementViewModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Windows;
  3. using System.Collections.Generic;
  4. using EntranseTesting.Models;
  5. using System.Collections.ObjectModel;
  6. using System.Runtime.InteropServices;
  7. using ReactiveUI;
  8. using System.Linq;
  9. using Avalonia.Input;
  10. using System.Diagnostics;
  11. using Avalonia.Interactivity;
  12. using Avalonia.Controls;
  13. using System.Reactive;
  14. using CommunityToolkit.Mvvm.Input;
  15. using Avalonia;
  16. using Microsoft.EntityFrameworkCore;
  17. using System.Text.RegularExpressions;
  18. using DynamicData;
  19. using CommunityToolkit.Mvvm.ComponentModel;
  20. using System.ComponentModel;
  21. using System.Xml.Linq;
  22. namespace EntranseTesting.ViewModels
  23. {
  24. public partial class TestMatchTheElementViewModel : ObservableObject, INotifyPropertyChanged
  25. {
  26. public event PropertyChangedEventHandler PropertyChanged;
  27. protected virtual void OnPropertyChanged(string propertyName)
  28. {
  29. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  30. }
  31. EntranceTestingContext baseConnection = new EntranceTestingContext();
  32. [ObservableProperty]int numberTask;
  33. [ObservableProperty] string question;
  34. [ObservableProperty] string nameGroup1;
  35. [ObservableProperty] string nameGroup2;
  36. ObservableCollection<ElementOfGroup> elementMatchGroup1 = new ObservableCollection<ElementOfGroup>();
  37. ObservableCollection<ElementOfGroup> elementMatchGroup2 = new ObservableCollection<ElementOfGroup>();
  38. [ObservableProperty] List<ItemMatch> matches = new List<ItemMatch>();
  39. [ObservableProperty] List<QuestionImage> qImage = new List<QuestionImage>();
  40. public TestMatchTheElementViewModel(int numberTask)
  41. {
  42. this.numberTask = numberTask;
  43. Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
  44. QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
  45. List<Models.Group> _group = baseConnection.Groups.Where(tb => tb.IdQuestion == numberTask).ToList();
  46. NameGroup1 = _group[0].Name;
  47. NameGroup2 = _group[1].Name;
  48. List<ElementOfGroup> _list1 = baseConnection.ElementOfGroups.Include(tb => tb.IdGroupNavigation).Where(tb => tb.IdGroup == _group[0].Id).ToList();
  49. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list1));
  50. foreach (ElementOfGroup elem in _list1)
  51. ElementMatchGroup1.Add(elem);
  52. List<ElementOfGroup> _list2 = baseConnection.ElementOfGroups.Include(tb => tb.IdGroupNavigation).Where(tb => tb.IdGroup == _group[1].Id).ToList();
  53. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list2));
  54. foreach (ElementOfGroup elem in _list2)
  55. ElementMatchGroup2.Add(elem);
  56. //çàïîëíÿåì äàííûå
  57. int responseIndex = Response.IndexResponse(numberTask);
  58. if (Response.responseUsers[responseIndex].UserResponseMatchTheElements.Count > 0)//åñëè ïîëüçîâàòåëü íå îòâå÷àë
  59. {
  60. Matches.Clear();
  61. List<UserResponseMatchTheElement> _response = Response.responseUsers[responseIndex].UserResponseMatchTheElements.ToList();
  62. int i = 1;
  63. foreach (UserResponseMatchTheElement item in _response)
  64. {
  65. int index1 = _list1.IndexOf(_list1.FirstOrDefault(tb => tb.Id == item.IdElement1));
  66. int index2 = _list2.IndexOf(_list2.FirstOrDefault(tb => tb.Id == item.IdElement2));
  67. _list1[index1].IsActive = true;
  68. _list2[index2].IsActive = true;
  69. _list1[index1].NumGroup = i;
  70. _list2[index2].NumGroup = i;
  71. ItemMatch elem = new ItemMatch()
  72. {
  73. Elem1 = _list1[index1],
  74. Elem2 = _list2[index2],
  75. NumGroup = i++
  76. };
  77. Matches.Add(elem);
  78. }
  79. }
  80. }
  81. public ObservableCollection<ElementOfGroup> ElementMatchGroup1 { get => elementMatchGroup1; set { elementMatchGroup1 = value; OnPropertyChanged(); } }
  82. public ObservableCollection<ElementOfGroup> ElementMatchGroup2 { get => elementMatchGroup2; set { elementMatchGroup2 = value; OnPropertyChanged(); } }
  83. public void MatchLine(ElementOfGroup elem, ref Border border)
  84. {
  85. ItemMatch item = Matches.FirstOrDefault(tb => tb.Elem1 == null || tb.Elem2 == null);
  86. if (item == null)
  87. item = new ItemMatch();
  88. else
  89. Matches.Remove(item);
  90. ElementOfGroup elem1 = ElementMatchGroup1.FirstOrDefault(tb => tb.Id == elem.Id);
  91. ElementOfGroup elem2 = ElementMatchGroup2.FirstOrDefault(tb => tb.Id == elem.Id);
  92. Matches.RemoveMany(matches.Where(tb => tb.Elem1 == elem1 || tb.Elem2 == elem2).ToList());
  93. item.NumGroup = numMatch();
  94. nullableValue();
  95. if (elem1 != null && item.Elem1 == null)
  96. {
  97. item.Elem1 = elem1;
  98. }
  99. else if (elem2 != null && item.Elem2 == null)
  100. {
  101. item.Elem2 = elem2;
  102. }
  103. else
  104. {
  105. Debug.WriteLine($"Elem with id '{elem.Id}' not found");
  106. return;
  107. }
  108. Matches.Add(item);
  109. if (item.Elem1 != null)
  110. {
  111. int i = ElementMatchGroup1.IndexOf(item.Elem1);
  112. ElementMatchGroup1[i].IsActive = true;
  113. ElementMatchGroup1[i].NumGroup = item.NumGroup;
  114. }
  115. if (item.Elem2 != null)
  116. {
  117. int i = ElementMatchGroup2.IndexOf(item.Elem2);
  118. ElementMatchGroup2[i].IsActive = true;
  119. ElementMatchGroup2[i].NumGroup = item.NumGroup;
  120. }
  121. }
  122. private int numMatch()
  123. {
  124. int value = -1;
  125. if (Matches.Count == 0)
  126. return 1;
  127. for(int i = 1; i <= ElementMatchGroup1.Count; i++)
  128. {
  129. if(Matches.Where(tb => tb.NumGroup == i).Count() == 0)
  130. return i;
  131. }
  132. return value;
  133. }
  134. private void nullableValue()
  135. {
  136. foreach(ElementOfGroup e in ElementMatchGroup1)
  137. {
  138. if(Matches.Where(tb => tb.Elem1 == e).Count() == 0)
  139. {
  140. int i = ElementMatchGroup1.IndexOf(e);
  141. ElementMatchGroup1[i].IsActive = false;
  142. if (Matches.Where(tb=>tb.Elem1 == e).Count() == 0)
  143. ElementMatchGroup1[i].NumGroup = 0;
  144. }
  145. }
  146. foreach (ElementOfGroup e in ElementMatchGroup2)
  147. {
  148. if (Matches.Where(tb => tb.Elem2 == e).Count() == 0)
  149. {
  150. int i = ElementMatchGroup2.IndexOf(e);
  151. ElementMatchGroup2[i].IsActive = false;
  152. if (Matches.Where(tb => tb.Elem2 == e).Count() == 0)
  153. ElementMatchGroup2[i].NumGroup = 0;
  154. }
  155. }
  156. }
  157. }
  158. }