-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
219 lines (183 loc) · 5.91 KB
/
MainWindow.xaml.cs
File metadata and controls
219 lines (183 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MahApps.Metro;
using MahApps.Metro.Controls;
namespace CyberCrew
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
class AppFrame
{
public static Frame frameMain;
}
public static class Hash
{
public static string HashPassword(string password)
{
using (var sha256 = SHA256.Create())
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] hashBytes = sha256.ComputeHash(passwordBytes);
return Convert.ToBase64String(hashBytes);
}
}
}
public static class ShortName
{
public static string ShortenName(DB.Employee employee)
{
string res = $"{employee.Surname} {employee.FirstName.Substring(0, 1)}.";
if (!String.IsNullOrEmpty(employee.Patronymic))
{
res += employee.Patronymic.Substring(0, 1) + ".";
}
return res;
}
}
class DBConnection
{
public static DB.CyberCrewEntities modelOdb;
}
public class Config
{
public string Login { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string EmailPassword { get; set; }
public string SmtpAddress { get; set; }
public int SmtpPortNumber { get; set; }
public string OrganizationName { get; set; }
}
public class AppsManager
{
public static DB.Client User;
private static string DirPath = "A:\\Clones\\CyberCrew\\CyberCrew";
//private static string DirPath = "C:\\Projects\\CyberCrew";
public static DispatcherTimer timer = new DispatcherTimer();
public static List<string> runningApps;
public static void KillRunningApps()
{
foreach (var app in runningApps)
{
foreach (var process in Process.GetProcessesByName(app))
{
try
{
process.Kill();
}
catch
{
}
}
}
timer.Stop();
runningApps.Clear();
}
private static void CheckProcesses()
{
var localProcesses = Process.GetProcesses().Select(x => x.ProcessName.ToLower()).ToHashSet();
List<string> currentApps = runningApps.ToList();
foreach (string app in currentApps)
{
if (!localProcesses.Contains(app))
{
runningApps.Remove(app);
}
}
if (runningApps.Count == 0)
{
timer.Stop();
}
}
public static void ReduceUserBalance_Tick(object sender, EventArgs e)
{
CheckProcesses();
if (runningApps.Count == 0 || User.Balance == 0)
{
timer.Stop();
return;
}
if (User.Balance <= 2)
{
User.Balance = 0;
DBConnection.modelOdb.Client.AddOrUpdate(User);
DBConnection.modelOdb.SaveChanges();
KillRunningApps();
return;
}
try
{
User.Balance -= Convert.ToDecimal(1);
DBConnection.modelOdb.Client.AddOrUpdate(User);
DBConnection.modelOdb.SaveChanges();
}
catch (Exception ex)
{
User.Balance += Convert.ToDecimal(1);
MessageBox.Show(ex.Message);
}
}
public static void RunApp(string appName, string pathToExe)
{
if (User.Balance <= 1)
{
new MessageWindow("Недостатно средств на балансе.").ShowDialog();
return;
}
try
{
Process.Start(DirPath + pathToExe);
if (!timer.IsEnabled)
{
timer.Start();
}
runningApps.Add(appName.ToLower());
}
catch
{
new MessageWindow("Отсутствует исполняемый файл.").ShowDialog();
}
}
}
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
AppFrame.frameMain = FrmMain;
DBConnection.modelOdb = new DB.CyberCrewEntities();
AppsManager.runningApps = new List<string>();
AppsManager.timer.Tick += new EventHandler(AppsManager.ReduceUserBalance_Tick);
AppsManager.timer.Interval = new TimeSpan(0, 0, 1);
LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
FrmMain.Navigate(new LoginPage());
}
private void FrmMain_KeyDown(object sender, KeyEventArgs e)
{
if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
&& (Keyboard.IsKeyDown(Key.Left)))
{
e.Handled = true;
}
}
}
}