Imitation.cs
9.69 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Timers;
using System.Windows.Forms;
//using System.Threading;
namespace GeneralLib
{
/// <summary>
/// 模拟数据产生事件的委托
/// </summary>
/// <param name="statusStr"></param>
public delegate void DemoCreatEvt(DemoResult result);
/// <summary>
/// 模拟结果
/// </summary>
public class DemoResult
{
public string KeyID = "";
public string Value = "";
public double Speed = 0;
}
/// <summary>
/// 演示模式接口
/// </summary>
public interface IDemo
{
/// <summary>
/// 获取模拟结果
/// </summary>
/// <returns></returns>
string GetValue();
}
/// <summary>
/// 基本模式,如签到,只需事件,值自定义
/// </summary>
public class BaseDemo : IDemo
{
/// <summary>
/// 模拟事件返回值指定
/// </summary>
public string ResultValue = "";
/// <summary>
/// 获取模拟结果
/// </summary>
/// <returns></returns>
public string GetValue()
{
return ResultValue;
}
}
/// <summary>
/// 选择模式
/// </summary>
public class ChoiceDemo : IDemo
{
/// <summary>
/// 选项个数
/// </summary>
public int OptionCount = 4;
/// <summary>
/// 可选个数
/// </summary>
public int LimitCount = 1;
/// <summary>
/// 是否选满可选个数
/// </summary>
public bool IsForced = false;
/// <summary>
/// 选项格式是否ABCD
/// </summary>
public bool IsABC = false;
/// <summary>
/// 结果是否排序
/// </summary>
public bool IsOrderly = false;
/// <summary>
/// 获取模拟结果
/// </summary>
/// <returns></returns>
public string GetValue()
{
string res = "";
Random rnd = new Random();
int sel = LimitCount;
if (!IsForced)
{
sel = (int)(LimitCount * rnd.NextDouble()) + 1;
}
List<int> listOption = new List<int>();
for (int i = 1; i <= OptionCount; i++)
{
listOption.Add(i);
}
List<int> listRes = new List<int>();
for (int i = 1; i <= sel; i++)
{
int n = (int)(listOption.Count * rnd.NextDouble());
int val = listOption[n];
listOption.Remove(val);
listRes.Add(val);
}
if (listRes.Count > 0)
{
if (IsOrderly)
listRes.Sort();
string[] aryRes = new string[listRes.Count];
if (IsABC)
{
for (int i = 0; i < listRes.Count; i++)
aryRes[i] = Convert.ToChar(64 + listRes[i]).ToString();
}
else
{
for (int i = 0; i < listRes.Count; i++)
aryRes[i] = listRes[i].ToString();
}
res = string.Join(",", aryRes);
}
return res;
}
}
/// <summary>
/// 数值模式
/// </summary>
public class NumberDemo : IDemo
{
/// <summary>
/// 最大值
/// </summary>
public int MaxVal = 100;
/// <summary>
/// 最小值
/// </summary>
public int MinVal = -100;
//public int MaxLen = 0;
/// <summary>
/// 小数位,-1为任意小数位
/// </summary>
public int Decimal = 0;
/// <summary>
/// 获取模拟结果
/// </summary>
/// <returns></returns>
public string GetValue()
{
string res = "";
Random rnd = new Random();
int v1 = MinVal;
int v2 = MaxVal;
if (MaxVal < MinVal)
{
v1 = MaxVal;
v2 = MinVal;
}
int vRnd = 0;
int dec = 0;
if (Decimal == -1)
dec = (int)(5 * rnd.NextDouble());
else if (Decimal > 0)
dec = Decimal;
if (dec > 0)
{
double pow = Math.Pow(10, dec);
v1 = v1 * (int)pow;
v2 = v2 * (int)pow;
vRnd = (int)((v2 - v1 + 1) * rnd.NextDouble()) + v1;
res = ((double)vRnd / pow).ToString("F" + dec);
}
else
{
vRnd = (int)((v2 - v1 + 1) * rnd.NextDouble()) + v1;
res = vRnd.ToString();
}
return res;
}
}
/// <summary>
/// 模拟控制类
/// </summary>
public class Imitation
{
/// <summary>
/// 键盘列表
/// </summary>
public List<string> KeyIDList = new List<string>();
///// <summary>
///// 预定值列表
///// </summary>
//public List<string> ValueList = new List<string>();
///// <summary>
///// 结果仅从预定值列表产生
///// </summary>
//public bool OnlyValueList = false;
/// <summary>
/// 模拟数据产生事件
/// </summary>
public event DemoCreatEvt DemoCreat;
/// <summary>
/// 演示模式
/// </summary>
public IDemo DemoType = null;
/// <summary>
/// 控制计时器,产生数据事件
/// </summary>
private Timer TmrDemo = new Timer();
/// <summary>
/// 启动时间,用来计算速度
/// </summary>
private int timeStart = 0;
public Imitation()
{
TmrDemo.Interval = 1000;
TmrDemo.Enabled = false;
TmrDemo.Tick += new EventHandler(TmrDemo_Tick);
}
~Imitation()
{
TmrDemo.Stop();
TmrDemo.Enabled = false;
}
/// <summary>
/// 事件计时间隔
/// </summary>
public int Interval
{
get { return TmrDemo.Interval; }
set { TmrDemo.Interval = value; }
}
/// <summary>
/// 已经产生的模拟数据,若重新开始模拟,需要清空,否则就是继续
/// </summary>
public Dictionary<string, DemoResult> ResultList = new Dictionary<string, DemoResult>();
/// <summary>
/// 未模拟过的键盘
/// </summary>
List<string> KeyIDListCreate = new List<string>();
/// <summary>
/// 启动模拟
/// </summary>
public void Start()
{
//初始化未模拟ID的列表
KeyIDListCreate.Clear();
foreach (var key in KeyIDList)
{
if (!ResultList.ContainsKey(key))
KeyIDListCreate.Add(key);
}
TmrDemo.Start();//开始模拟
timeStart = Environment.TickCount;//记住开始时间
}
/// <summary>
/// 停止模拟
/// </summary>
public void Stop()
{
TmrDemo.Stop();
}
/// <summary>
/// 产生模拟数据
/// </summary>
void CreateData()
{
try
{
if (!TmrDemo.Enabled) return;
if (DemoCreat != null)
{
DemoResult demoRes = new DemoResult();
if (KeyIDListCreate.Count > 0)
{
Random rnd = new Random();
int nRnd = (int)(KeyIDListCreate.Count * rnd.NextDouble());
demoRes.KeyID = KeyIDListCreate[nRnd];
//KeyIDListCreate.Remove(demoRes.KeyID);//放在最后。杨斌 2014-06-06
}
else
{
Random rnd = new Random();
int nRnd = (int)(KeyIDList.Count * rnd.NextDouble());
demoRes.KeyID = KeyIDList[nRnd];
////杨斌 2014-06-06
//List<int> lstNoVote = new List<int>();
//foreach (int key in KeyIDList)
//{
// if (!ResultList.ContainsKey(key))
// lstNoVote.Add(key);
//}
//if (lstNoVote.Count > 0)
//{
// nRnd = (int)(lstNoVote.Count * rnd.NextDouble());
// demoRes.KeyID = lstNoVote[nRnd];
//}
}
demoRes.Value = DemoType.GetValue();
demoRes.Speed = (Environment.TickCount - timeStart) / (double)1000;
if (ResultList.ContainsKey(demoRes.KeyID))
ResultList[demoRes.KeyID] = demoRes;
else
ResultList.Add(demoRes.KeyID, demoRes);
if (KeyIDListCreate.Contains(demoRes.KeyID))//杨斌 2014-06-06
KeyIDListCreate.Remove(demoRes.KeyID);
DemoCreat(demoRes);
}
}
catch(Exception ex)
{
//SystemLog.WriterLog(ex, false);
}
}
//计时器事件产生模拟事件
void TmrDemo_Tick(object sender, EventArgs e)
{
CreateData();
}
}
}