Facebook
From Fox, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 51
  1. using MDS.Controls;
  2. using MDS.DataModels;
  3. using MDS.Units;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. using System.Configuration;
  10. using System.Data;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Animation;
  20.  
  21. namespace MDS
  22. {
  23.     /// <summary>
  24.     /// Логика взаимодействия для App.xaml
  25.     /// </summary>
  26.     public partial class App : Application, INotifyPropertyChanged
  27.     {
  28.         public enum Mode { Demo, Study, Training, Exam, Quiz};
  29.  
  30.         private static string _client;
  31.         private static string _workplace;
  32.  
  33.         private static bool _checker;
  34.         public static bool Checker
  35.         {
  36.             set
  37.             {
  38.                 if (value != _checker)
  39.                 {
  40.                     _checker = value;
  41.                 }
  42.             }
  43.             get { return _checker; }
  44.         }
  45.  
  46.         private static ObservableCollection<Client> _clients = new ObservableCollection<Client>();
  47.         public static ObservableCollection<Client> Clients
  48.         {
  49.             get { return _clients; }
  50.         }
  51.  
  52.         public static Frame MainFrame;
  53.  
  54.         private static TelnetDotNet Telnet;
  55.  
  56.         public static Assembly CurrentDevice;
  57.  
  58.         private static ObservableCollection<Unit> _units;
  59.         public static ObservableCollection<Unit> Units
  60.         {
  61.             get { return _units; }
  62.             private set
  63.             {
  64.                 if (value != _units)
  65.                 {
  66.                     _units = value;
  67.                 }
  68.             }
  69.         }
  70.  
  71.         private static Mode _currentMode;
  72.         public static Mode CurrentMode
  73.         {
  74.             get { return _currentMode; }
  75.             set
  76.             {
  77.                 if (value != _currentMode)
  78.                 {
  79.                     _currentMode = value;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         private static Random rand = new Random();
  85.         public static Random Rand
  86.         {
  87.             get { return rand; }
  88.         }
  89.  
  90.         private static Tutorial activeTutorial;
  91.         public static Tutorial ActiveTutorial
  92.         {
  93.             get { return activeTutorial; }
  94.             set
  95.             {
  96.                 if (value != activeTutorial)
  97.                 {
  98.                     activeTutorial = value;
  99.                     if(activeTutorial.RandValues!=null)
  100.                     {
  101.                         foreach(var val in activeTutorial.RandValues)
  102.                         {
  103.                             val.Min = RandVarReplace(val.Min, activeTutorial.RandValues);
  104.                             val.Max = RandVarReplace(val.Max, activeTutorial.RandValues);
  105.  
  106.                             val.Value = Rand.Next(int.Parse(val.Min), int.Parse(val.Max));
  107.                             if (val.Mod != 0)
  108.                                 val.Value = val.Value - val.Value % val.Mod;
  109.                         }
  110.                     }
  111.                     activeTutorial.ExamDescription = RandVarReplace(activeTutorial.ExamDescription, activeTutorial.RandValues);
  112.                     foreach (var step in activeTutorial.Steps)
  113.                     {
  114.                         step.Title = RandVarReplace(step.Title, activeTutorial.RandValues);
  115.                         step.Command = RandVarReplace(step.Command, activeTutorial.RandValues);
  116.                         if(step.Conditions!=null)
  117.                         {
  118.                             foreach(var cond in step.Conditions)
  119.                                 cond.Value = RandVarReplace(cond.Value, activeTutorial.RandValues);
  120.                         }
  121.                     }
  122.  
  123.                 }
  124.             }
  125.         }
  126.  
  127.  
  128.  
  129.         public static string RandVarReplace(string text, List<RandValue> values)
  130.         {
  131.             string output = text;
  132.             if (values != null)
  133.             {
  134.                 for (int i = 0; i < values.Count(); i++)
  135.                     output = output.Replace("[" + i + "]", values[i].Value.ToString());
  136.             }
  137.             return output;
  138.         }
  139.  
  140.         public static Window TutorialWindow;
  141.  
  142.         public App()
  143.         {
  144.             Units = new ObservableCollection<Unit>();
  145.             Telnet = new TelnetDotNet();
  146.             Telnet.MessageRecieved += Telnet_MessageRecieved;
  147.             Current.Exit += Current_Exit;
  148.         }
  149.  
  150.         void Current_Exit(object sender, ExitEventArgs e)
  151.         {
  152.             Telnet.Close();
  153.         }
  154.  
  155.         public static void StartDemo(string workplace)
  156.         {
  157.             Task.Run(() =>
  158.             {
  159.                 Current.Dispatcher.BeginInvoke(new Action(() =>
  160.                 {
  161.                     MainFrame.Content = Activator.CreateInstance(CurrentDevice.GetType(workplace), Mode.Demo.ToString());
  162.                 }));
  163.             });
  164.         }
  165.  
  166.         public static void Start(string workplace)
  167.         {
  168.             Task.Run(() =>
  169.             {
  170.                 Current.Dispatcher.BeginInvoke(new Action(() =>
  171.                 {
  172.                     try
  173.                     {
  174.                         MainFrame.Content = Activator.CreateInstance(CurrentDevice.GetType(workplace), CurrentMode.ToString());
  175.                     }
  176.                     catch(Exception)
  177.                     {
  178.                         MainFrame.Content = Activator.CreateInstance(CurrentDevice.GetType(workplace));
  179.                     }
  180.                     switch (CurrentMode)
  181.                     {
  182.                         case Mode.Study:
  183.                             TutorialWindow = new TutorialWindow();
  184.                             TutorialWindow.Topmost = true;
  185.                             TutorialWindow.Show();
  186.                             break;
  187.                         case Mode.Exam:
  188.                             TutorialWindow = new ExamWindow();
  189.                             TutorialWindow.Topmost = true;
  190.                             TutorialWindow.Show();
  191.                             break;
  192.                     }
  193.                 }));
  194.             });
  195.         }
  196.  
  197.         public static void PrepareControls()
  198.         {
  199.             if (ActiveTutorial != null && ActiveTutorial.PrepareControls != null)
  200.             {
  201.                 foreach (var control in ActiveTutorial.PrepareControls)
  202.                 {
  203.                     CheckBox chkBox = FindChild(Current.MainWindow, control) as CheckBox;
  204.                     if (chkBox != null)
  205.                     {
  206.                         chkBox.IsChecked = !chkBox.IsChecked;
  207.                     }
  208.                     else
  209.                     {
  210.                         try
  211.                         { //TODO: програмное нажатие кнопки
  212.                             (App.MainFrame.Content as BaseFrame).prepareElement(control);
  213.                         }
  214.                         catch (Exception) { }
  215.                     }
  216.                 }
  217.             }
  218.         }
  219.  
  220.         public static void Stop()
  221.         {
  222.             if (TutorialWindow != null)
  223.                 TutorialWindow.Close();
  224.             Telnet.Close();
  225.             Current.Dispatcher.BeginInvoke(new Action(() =>
  226.             {
  227.                 if (App.Current.MainWindow.WindowStyle == WindowStyle.None)
  228.                 {
  229.                     App.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
  230.                     App.Current.MainWindow.ResizeMode = ResizeMode.CanResize;
  231.                     App.Current.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  232.                     App.Current.MainWindow.WindowState = WindowState.Normal;
  233.  
  234.                     App.Current.MainWindow.Hide();
  235.                     App.Current.MainWindow.Show();
  236.                 }
  237.                 MainFrame.Source = new Uri("MenuPage.xaml", UriKind.RelativeOrAbsolute);
  238.             }));
  239.             foreach (var u in Units)
  240.                 u.Dispose();
  241.             Units.Clear();
  242.         }
  243.  
  244.         public static bool BroadcastMessage(string Message, bool FromTelnet)
  245.         {
  246.             if (Message != null)
  247.             {
  248.                 bool MessageAccepted = false;
  249.                 foreach (Unit unit in Units)
  250.                 {
  251.                     if (unit.RecieveMessage(Message))
  252.                     {
  253.                         MessageAccepted = true;
  254.                     }
  255.                 }
  256.                 if (Telnet.IsConnected && !FromTelnet)
  257.                 {
  258.                     Telnet.Send(Message);
  259.                 }
  260.  
  261.                 if (CurrentMode == Mode.Exam || CurrentMode == Mode.Study)
  262.                 {
  263.                     if (ActiveTutorial.CurrentStep != null && ActiveTutorial.CurrentStep.Command != null)
  264.                     {
  265.                         if (Message.Contains(ActiveTutorial.CurrentStep.Command))
  266.                         {
  267.                             if (ActiveTutorial.CurrentStep.Conditions != null && ActiveTutorial.CurrentStep.Conditions.Count > 0)
  268.                             {
  269.                                 foreach (var cond in ActiveTutorial.CurrentStep.Conditions)
  270.                                 {
  271.                                     object unit = App.Units.FirstOrDefault(u => u.Name == cond.UnitName);
  272.                                     if (unit != null)
  273.                                     {
  274.                                         string[] path = cond.Path.Split('/');
  275.                                         PropertyInfo propertyInfo = null;
  276.                                         object property = unit;
  277.  
  278.                                         Type type = unit.GetType();
  279.                                         foreach (var segment in path)
  280.                                         {
  281.                                             if (segment[0] == '[')
  282.                                             {
  283.                                                 if (!segment.Contains('='))
  284.                                                 {
  285.                                                     int index;
  286.                                                     int.TryParse(segment.Substring(1, segment.Length - 2), out index);
  287.                                                     int i = 0;
  288.                                                     foreach (var element in property as IEnumerable)
  289.                                                     {
  290.                                                         if (index == i)
  291.                                                         {
  292.                                                             property = element;
  293.                                                             break;
  294.                                                         }
  295.                                                         i++;
  296.                                                     }
  297.                                                 }
  298.                                                 else
  299.                                                 {
  300.                                                     int eqSignPos = segment.IndexOf('=');
  301.                                                     string key = segment.Substring(1, eqSignPos - 1);
  302.                                                     string value = segment.Substring(eqSignPos + 1, segment.Length - eqSignPos - 2);
  303.                                                     foreach (var element in property as IEnumerable)
  304.                                                     {
  305.                                                         PropertyInfo elemInfo = element.GetType().GetProperty(key);
  306.                                                         if (elemInfo.GetValue(element).ToString() == value)
  307.                                                         {
  308.                                                             property = element;
  309.                                                             break;
  310.                                                         }
  311.                                                     }
  312.                                                 }
  313.                                             }
  314.                                             else
  315.                                             {
  316.                                                 propertyInfo = type.GetProperty(segment);
  317.                                                 if (propertyInfo != null)
  318.                                                     property = propertyInfo.GetValue(property);
  319.                                             }
  320.                                             if (property == null)
  321.                                                 break;
  322.                                             type = property.GetType();
  323.                                         }
  324.                                         if (cond.Tolerance != 0)
  325.                                         {
  326.                                             if (Math.Abs(double.Parse(property.ToString()) - double.Parse(cond.Value)) <= cond.Tolerance)
  327.                                                 cond.IsMet = true;
  328.                                         }
  329.                                         else
  330.                                         {
  331.                                             if (property != null)
  332.                                             {
  333.                                                 switch (type.Name)
  334.                                                 {
  335.                                                     case "bool":
  336.                                                         if (((bool)property == true && cond.Value == "True") ||
  337.                                                           ((bool)property == false && cond.Value == "False"))
  338.                                                             cond.IsMet = true;
  339.                                                         break;
  340.                                                     default:
  341.                                                         if (property.ToString() == (cond.Value))
  342.                                                             cond.IsMet = true;
  343.                                                         break;
  344.                                                 }
  345.                                             }
  346.                                         }
  347.                                     }
  348.                                 }
  349.                                 if (ActiveTutorial.CurrentStep.Conditions.Where(c => c.IsMet).Count() == ActiveTutorial.CurrentStep.Conditions.Count())
  350.                                     TutorialStepCompleted();
  351.                             }
  352.                             else TutorialStepCompleted();
  353.                         }
  354.                     }
  355.                 }
  356.                 return MessageAccepted;
  357.             }
  358.             else
  359.             {
  360.                 return false;
  361.             }
  362.         }
  363.  
  364.         private static void TutorialStepCompleted()
  365.         {
  366.             ActiveTutorial.NextStep();
  367.         }
  368.  
  369.         public static void Connect(string Server, string Client, string Workplace)
  370.         {
  371.             _client = Client;
  372.             _workplace = Workplace;
  373.             Telnet.Connected += Telnet_Connected;
  374.             Telnet.Connect(Server, 27027);
  375.         }
  376.  
  377.         static void Telnet_Connected(object sender, EventArgs e)
  378.         {
  379.             Telnet.Send(String.Format("Connect {0} {1}", _client, _workplace));
  380.             App.Current.Dispatcher.BeginInvoke(new Action(() => Start(_workplace)));
  381.         }
  382.  
  383.         public static void StartServer(int Port)
  384.         {
  385.             Telnet.StartServer(Port);
  386.         }
  387.  
  388.         static void Telnet_MessageRecieved(object sender, string e)
  389.         {
  390.             if (e != null)
  391.             {
  392.                 String[] Comms = e.Split('\n');
  393.  
  394.                 List<String> Commands = new List<string>(Comms);
  395.                 Commands.Sort();
  396.                 foreach (string command in Commands)
  397.                 {
  398.                     if (command != String.Empty)
  399.                     {
  400.                         String[] Message = command.Split(' ');
  401.                         switch (Message[0])
  402.                         {
  403.                             case "Connect":
  404.                                 //TODO
  405.                                 if (Checker == true && Telnet.ClientSocket == null)
  406.                                 {
  407.                                     string[] MessageSplitted = {""};
  408.                                     MessageSplitted = Message[2].Split(new string[] { "Big" }, StringSplitOptions.None);
  409.                                     if (MessageSplitted.Length < 2) {
  410.                                         MessageSplitted = Message[2].Split(new string[] { "Small" }, StringSplitOptions.None);
  411.                                     }
  412.  
  413.                                     if (Message[2].Contains("OperatorPage"))
  414.                                     {
  415.                                         int op_counter = 0;
  416.  
  417.                                         if (MainFrame.Content.ToString().Contains("OperatorPage"))
  418.                                             op_counter += 1;
  419.  
  420.                                         foreach (Client imposter in Clients)
  421.                                             if (imposter.Workspace.Contains("OperatorPage"))
  422.                                                 op_counter += 1;
  423.  
  424.                                         if (op_counter < 2)
  425.                                             Clients.Add(new Client(Message[1], Message[2]));
  426.                                         else
  427.                                             Telnet.Send(String.Format("You are impostor {0}", Message[1]));
  428.  
  429.                                     }
  430.                                     else
  431.                                     {
  432.                                         if(!MainFrame.Content.ToString().Contains(MessageSplitted[1]))
  433.                                         {
  434.                                             foreach (Client imposter in Clients)
  435.                                                 if (imposter.Workspace == Message[2])
  436.                                                 {
  437.                                                     Telnet.Send(String.Format("You are impostor {0}", Message[1]));
  438.                                                     break;
  439.                                                 }
  440.                                             Clients.Add(new Client(Message[1], Message[2]));
  441.                                         }
  442.                                         else
  443.                                             Telnet.Send(String.Format("You are impostor {0}", Message[1]));
  444.                                     }
  445.                                    
  446.                                 }
  447.                                 else
  448.                                     Clients.Add(new Client(Message[1], Message[2]));
  449.                                 break;
  450.  
  451.                             case "You":
  452.                                 if (Message[3] == Telnet.ClientSocket.LocalEndPoint.ToString().Split(':')[0])
  453.                                 {
  454.                                     App.Stop();
  455.                                     MessageBox.Show("Server closed connection", "Closed", MessageBoxButton.OK, MessageBoxImage.Information);
  456.                                 }
  457.                                 else
  458.                                 {
  459.                                     foreach (Client impostor in Clients)
  460.                                     {
  461.                                         if (Message[3] == impostor.IPAddress)
  462.                                         {
  463.                                             Clients.Remove(impostor);
  464.                                         }
  465.                                     }
  466.                                 }
  467.                                 break;
  468.  
  469.                             default:
  470.                                 if(command != "CommonInfoArrayNotif 53 0")
  471.                                     BroadcastMessage(command, true);
  472.                                 break;
  473.                         }
  474.                     }
  475.                 }
  476.             }
  477.         }
  478.  
  479.         public static DependencyObject FindChild(DependencyObject parent, string name)
  480.         {
  481.             if (parent == null || string.IsNullOrEmpty(name))
  482.                 return null;
  483.             if (parent is FrameworkElement && (parent as FrameworkElement).Name == name)
  484.                 return parent;
  485.             DependencyObject result = null;
  486.             if (parent is FrameworkElement)
  487.                 (parent as FrameworkElement).ApplyTemplate();
  488.             int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  489.             for (int i = 0; i < childrenCount; i++)
  490.             {
  491.                 var child = VisualTreeHelper.GetChild(parent, i);
  492.                 result = FindChild(child, name);
  493.                 if (result != null)
  494.                     break;
  495.             }
  496.             return result;
  497.         }
  498.  
  499.         public static double GetAngle(double x, double y)
  500.         {
  501.             if (x == 0)
  502.             {
  503.                 if (y > 0)
  504.                     return 180;
  505.                 else
  506.                     return 0;
  507.             }
  508.             else
  509.             {
  510.                 if (x > 0 && y < 0)
  511.                     return 90 - Math.Atan(Math.Abs(y) / x) * 180 / Math.PI;
  512.                 if (x > 0 && y >= 0)
  513.                     return Math.Atan(y / x) * 180 / Math.PI + 90;
  514.                 if (x < 0 && y >= 0)
  515.                     return -Math.Atan(y / Math.Abs(x)) * 180 / Math.PI + 270;
  516.                 if (x < 0 && y < 0)
  517.                     return Math.Atan(Math.Abs(y) / Math.Abs(x)) * 180 / Math.PI + 270;
  518.             }
  519.             return 0;
  520.         }
  521.  
  522.                                     #region INotify
  523.         public event PropertyChangedEventHandler PropertyChanged;
  524.  
  525.         public void NotifyPropertyChanged(string propertyName)
  526.         {
  527.             if (PropertyChanged != null)
  528.             {
  529.                 PropertyChanged(this,
  530.                     new PropertyChangedEventArgs(propertyName));
  531.             }
  532.         }
  533.                                     #endregion
  534.     }
  535. }
  536.