123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Windows;
- using System.Collections.Generic;
- using EntranseTesting.Models;
- using System.Collections.ObjectModel;
- using System.Runtime.InteropServices;
- using ReactiveUI;
- using System.Linq;
- using Avalonia.Input;
- using System.Diagnostics;
- using Avalonia.Interactivity;
- using Avalonia.Controls;
- using System.Reactive;
- using CommunityToolkit.Mvvm.Input;
- using Avalonia;
- using Microsoft.EntityFrameworkCore;
- using System.Text.RegularExpressions;
- using DynamicData;
- using CommunityToolkit.Mvvm.ComponentModel;
- using System.ComponentModel;
- using System.Xml.Linq;
- namespace EntranseTesting.ViewModels
- {
- public partial class TestMatchTheElementViewModel : ObservableObject, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- EntranceTestingContext baseConnection = new EntranceTestingContext();
- [ObservableProperty]int numberTask;
- [ObservableProperty] string question;
- [ObservableProperty] string nameGroup1;
- [ObservableProperty] string nameGroup2;
- ObservableCollection<ElementOfGroup> elementMatchGroup1 = new ObservableCollection<ElementOfGroup>();
- ObservableCollection<ElementOfGroup> elementMatchGroup2 = new ObservableCollection<ElementOfGroup>();
- [ObservableProperty] List<ItemMatch> matches = new List<ItemMatch>();
- [ObservableProperty] List<QuestionImage> qImage = new List<QuestionImage>();
- public TestMatchTheElementViewModel(int numberTask)
- {
- this.numberTask = numberTask;
- Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
- QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
- List<Models.Group> _group = baseConnection.Groups.Where(tb => tb.IdQuestion == numberTask).ToList();
- NameGroup1 = _group[0].Name;
- NameGroup2 = _group[1].Name;
- List<ElementOfGroup> _list1 = baseConnection.ElementOfGroups.Include(tb => tb.IdGroupNavigation).Where(tb => tb.IdGroup == _group[0].Id).ToList();
- Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list1));
- foreach (ElementOfGroup elem in _list1)
- ElementMatchGroup1.Add(elem);
- List<ElementOfGroup> _list2 = baseConnection.ElementOfGroups.Include(tb => tb.IdGroupNavigation).Where(tb => tb.IdGroup == _group[1].Id).ToList();
- Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list2));
- foreach (ElementOfGroup elem in _list2)
- ElementMatchGroup2.Add(elem);
- //çàïîëíÿåì äàííûå
- int responseIndex = Response.IndexResponse(numberTask);
- if (Response.responseUsers[responseIndex].UserResponseMatchTheElements.Count > 0)//åñëè ïîëüçîâàòåëü íå îòâå÷àë
- {
- Matches.Clear();
- List<UserResponseMatchTheElement> _response = Response.responseUsers[responseIndex].UserResponseMatchTheElements.ToList();
- int i = 1;
- foreach (UserResponseMatchTheElement item in _response)
- {
- int index1 = _list1.IndexOf(_list1.FirstOrDefault(tb => tb.Id == item.IdElement1));
- int index2 = _list2.IndexOf(_list2.FirstOrDefault(tb => tb.Id == item.IdElement2));
- _list1[index1].IsActive = true;
- _list2[index2].IsActive = true;
- _list1[index1].NumGroup = i;
- _list2[index2].NumGroup = i;
- ItemMatch elem = new ItemMatch()
- {
- Elem1 = _list1[index1],
- Elem2 = _list2[index2],
- NumGroup = i++
- };
- Matches.Add(elem);
- }
- }
- }
- public ObservableCollection<ElementOfGroup> ElementMatchGroup1 { get => elementMatchGroup1; set { elementMatchGroup1 = value; OnPropertyChanged(); } }
- public ObservableCollection<ElementOfGroup> ElementMatchGroup2 { get => elementMatchGroup2; set { elementMatchGroup2 = value; OnPropertyChanged(); } }
- public void MatchLine(ElementOfGroup elem, ref Border border)
- {
- ItemMatch item = Matches.FirstOrDefault(tb => tb.Elem1 == null || tb.Elem2 == null);
- if (item == null)
- item = new ItemMatch();
- else
- Matches.Remove(item);
- ElementOfGroup elem1 = ElementMatchGroup1.FirstOrDefault(tb => tb.Id == elem.Id);
- ElementOfGroup elem2 = ElementMatchGroup2.FirstOrDefault(tb => tb.Id == elem.Id);
- Matches.RemoveMany(matches.Where(tb => tb.Elem1 == elem1 || tb.Elem2 == elem2).ToList());
- item.NumGroup = numMatch();
- nullableValue();
- if (elem1 != null && item.Elem1 == null)
- {
- item.Elem1 = elem1;
- }
- else if (elem2 != null && item.Elem2 == null)
- {
- item.Elem2 = elem2;
- }
- else
- {
- Debug.WriteLine($"Elem with id '{elem.Id}' not found");
- return;
- }
-
- Matches.Add(item);
- if (item.Elem1 != null)
- {
- int i = ElementMatchGroup1.IndexOf(item.Elem1);
- ElementMatchGroup1[i].IsActive = true;
- ElementMatchGroup1[i].NumGroup = item.NumGroup;
- }
- if (item.Elem2 != null)
- {
- int i = ElementMatchGroup2.IndexOf(item.Elem2);
- ElementMatchGroup2[i].IsActive = true;
- ElementMatchGroup2[i].NumGroup = item.NumGroup;
- }
-
- }
-
- private int numMatch()
- {
- int value = -1;
- if (Matches.Count == 0)
- return 1;
- for(int i = 1; i <= ElementMatchGroup1.Count; i++)
- {
- if(Matches.Where(tb => tb.NumGroup == i).Count() == 0)
- return i;
- }
- return value;
- }
- private void nullableValue()
- {
- foreach(ElementOfGroup e in ElementMatchGroup1)
- {
- if(Matches.Where(tb => tb.Elem1 == e).Count() == 0)
- {
- int i = ElementMatchGroup1.IndexOf(e);
- ElementMatchGroup1[i].IsActive = false;
- if (Matches.Where(tb=>tb.Elem1 == e).Count() == 0)
- ElementMatchGroup1[i].NumGroup = 0;
- }
- }
- foreach (ElementOfGroup e in ElementMatchGroup2)
- {
- if (Matches.Where(tb => tb.Elem2 == e).Count() == 0)
- {
- int i = ElementMatchGroup2.IndexOf(e);
- ElementMatchGroup2[i].IsActive = false;
- if (Matches.Where(tb => tb.Elem2 == e).Count() == 0)
- ElementMatchGroup2[i].NumGroup = 0;
- }
- }
- }
- }
- }
|