-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMainCode.cs
558 lines (468 loc) · 16.3 KB
/
frmMainCode.cs
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
using System;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using DowReplayManager.NET.Types;
using DowReplayManager.NET.Readers;
using DowReplayManager.NET.Handlers;
using System.Diagnostics;
namespace DowReplayManager.NET.Code
{
/// <summary>
/// Summary description for frmMainCode.
/// </summary>
public class Methods
{
public static string globalfilter = null;
public static Hashtable htAvailable = null;
public Methods()
{
}
/// <summary>
/// Creates a DataView of the Player details which can be sorted, filtered and adjusted
/// </summary>
/// <param name="storeReader"></param>
/// <param name="recs"></param>
/// <returns>DataView of all Players sorted by Games DESC</returns>
public static DataView CreatePlayerDataView(StoreReader storeReader, ReplayStore recs)
{
DataTable table = new DataTable("Players");
table.Columns.Add("Player", typeof(string));
table.Columns.Add("Games", typeof(int));
table.Columns.Add("ReplayID", typeof(int));
Debug.WriteLine(table);
DataRow dvRow = null;
//create a hash table to store the unique player names
foreach (ReplayStore.PlayerRow row in recs.Player)
{
object reps = storeReader.PlayerReplays(row.Name);
if (reps != System.DBNull.Value)
{
dvRow = table.NewRow();
dvRow["Player"] = row.Name;
dvRow["Games"] = (int)reps;
dvRow["ReplayID"] = row.ReplayID;
table.Rows.Add(dvRow);
}
}
DataView dvPlayers = new DataView(table, "", "Games DESC", DataViewRowState.CurrentRows);
dvPlayers.Table.CaseSensitive = false;
return dvPlayers;
}
/// <summary>
/// Adds Players contained in the supplied DataView to the ListView
/// </summary>
/// <param name="lvPlayers"></param>
/// <param name="dvPlayers"></param>
/// <param name="filter"></param>
/// <param name="sort"></param>
public static void PopulatePlayersView(ListView lvPlayers, DataView dvPlayers, string filter, string sort)
{
lvPlayers.BeginUpdate();
lvPlayers.Items.Clear();
if (globalfilter != null && filter.Length > 0)
{
filter = globalfilter + " AND " + filter;
}
else if (globalfilter != null)
{
filter = globalfilter;
}
FormatFilter(ref filter);
dvPlayers.RowFilter = filter;
dvPlayers.Sort = sort;
dvPlayers.RowStateFilter = DataViewRowState.CurrentRows;
//use a hash table to make sure there are only unique entries
Hashtable hash = new Hashtable();
int index = 1;
foreach (DataRowView dvr in dvPlayers)
{
if (!hash.ContainsKey(dvr["Player"]))
{
ListViewItem item =
new ListViewItem(new string[] {index.ToString(),
dvr["Player"].ToString(),
dvr["Games"].ToString()});
lvPlayers.Items.Add(item);
hash.Add(dvr["Player"], null);
index++;
}
}
lvPlayers.EndUpdate();
}
/// <summary>
/// Creates a DataView of the Player details which can be sorted, filtered and adjusted
/// </summary>
/// <param name="storeReader"></param>
/// <param name="recs"></param>
/// <returns>DataView of all Maps sorted by Games DESC</returns>
public static DataView CreateMapDataView(StoreReader storeReader, ReplayStore recs)
{
DataTable table = new DataTable("Maps");
table.Columns.Add("Map", typeof(string));
table.Columns.Add("Games", typeof(int));
table.Columns.Add("ReplayID", typeof(int));
DataRow dvRow;
foreach (ReplayStore.MapRow row in recs.Map)
{
object reps = storeReader.MapReplays(row.Name);
if (reps != System.DBNull.Value)
{
dvRow = table.NewRow();
dvRow["Map"] = row.Name;
dvRow["Games"] = (int)reps;
dvRow["ReplayID"] = row.ReplayID;
table.Rows.Add(dvRow);
}
}
DataView dvMaps = new DataView(table, "", "Games DESC", DataViewRowState.CurrentRows);
dvMaps.Table.CaseSensitive = false;
return dvMaps;
}
/// <summary>
/// Adds Players contained in the supplied DataView to the ListView
/// </summary>
/// <param name="lvMaps"></param>
/// <param name="dvMaps"></param>
/// <param name="filter"></param>
/// <param name="sort"></param>
public static void PopulateMapsView(ListView lvMaps, DataView dvMaps, string filter, string sort)
{
lvMaps.BeginUpdate();
lvMaps.Items.Clear();
if (globalfilter != null && filter.Length > 0)
{
filter = globalfilter + " AND " + filter;
}
else if (globalfilter != null)
{
filter = globalfilter;
}
FormatFilter(ref filter);
dvMaps.RowFilter = filter;
dvMaps.Sort = sort;
dvMaps.RowStateFilter = DataViewRowState.CurrentRows;
//use a hash table to make sure there are only unique entries
Hashtable hash = new Hashtable();
int index = 1;
foreach (DataRowView dvr in dvMaps)
{
if (!hash.ContainsKey(dvr["Map"]))
{
ListViewItem item = new ListViewItem(
new string[] { index.ToString(),
dvr["Map"].ToString(),
dvr["Games"].ToString()});
lvMaps.Items.Add(item);
hash.Add(dvr["Map"], null);
index++;
}
}
lvMaps.EndUpdate();
}
public static DataView CreateReplayDataView(StoreReader storeReader, ReplayStore recs)
{
DataTable table = new DataTable("Replays");
table.Columns.Add("ReplayID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Modified", typeof(DateTime));
table.Columns.Add("Added", typeof(DateTime));
table.Columns.Add("Filename", typeof(string));
table.Columns.Add("CategoryID", typeof(int));
DataRow dvRow;
foreach (ReplayStore.ReplayRow row in recs.Replay)
{
dvRow = table.NewRow();
dvRow["ReplayID"] = row.ID;
dvRow["Name"] = row.Name;
dvRow["Modified"] = row.DateModified;
dvRow["Added"] = row.DateAdded;
dvRow["Filename"] = row.Filename;
dvRow["CategoryID"] = row.CategoryID;
table.Rows.Add(dvRow);
}
DataView dvReplays = new DataView(table, "", "Added DESC", DataViewRowState.CurrentRows);
dvReplays.Table.CaseSensitive = false;
return dvReplays;
}
public static void PopulateReplaysView(ListView lvReplays, DataView dvReplays, string filter, string sort)
{
lvReplays.BeginUpdate();
lvReplays.Items.Clear();
if (globalfilter != null && filter.Length > 0)
{
filter = globalfilter + " AND " + filter;
}
else if (globalfilter != null)
{
filter = globalfilter;
}
FormatFilter(ref filter);
dvReplays.RowFilter = filter;
dvReplays.Sort = sort;
dvReplays.RowStateFilter = DataViewRowState.CurrentRows;
int index = 1;
foreach (DataRowView dvr in dvReplays)
{
ListViewItem item = new ListViewItem(
new string[] {
dvr["Name"].ToString(), dvr["Modified"].ToString(),
dvr["Added"].ToString(), dvr["Filename"].ToString()});
if (htAvailable != null && htAvailable.ContainsKey(dvr["Filename"].ToString()))
item.BackColor = System.Drawing.Color.PowderBlue;
item.Tag = dvr["ReplayID"];
lvReplays.Items.Add(item);
index++;
}
lvReplays.EndUpdate();
}
/// <summary>
/// Get a list of ReplayIDs using the supplied player name
/// </summary>
/// <param name="dvPlayers"></param>
/// <param name="name"></param>
/// <returns></returns>
public static object[] GetReplaysByPlayerName(DataView dvPlayers, string name)
{
string old_filter = dvPlayers.RowFilter;
dvPlayers.RowFilter = "Player = '" + name + "'";
System.Collections.ArrayList list = new ArrayList();
foreach (DataRowView dvr in dvPlayers)
{
list.Add(dvr["ReplayID"]);
}
dvPlayers.RowFilter = old_filter;
return list.ToArray();
}
public static object[] GetReplaysByMapName(DataView dvMaps, string name)
{
string old_filter = dvMaps.RowFilter;
dvMaps.RowFilter = "Map = '" + name + "'";
System.Collections.ArrayList list = new ArrayList();
foreach (DataRowView dvr in dvMaps)
{
list.Add(dvr["ReplayID"]);
}
dvMaps.RowFilter = old_filter;
return list.ToArray();
}
public static object[] GetReplaysByCategoryID(DataView dvReplays, int id)
{
string old_filter = dvReplays.RowFilter;
if (id != 1)
dvReplays.RowFilter = "CategoryID = " + id;
System.Collections.ArrayList list = new ArrayList();
foreach (DataRowView dvr in dvReplays)
{
list.Add(dvr["ReplayID"]);
}
dvReplays.RowFilter = old_filter;
return list.ToArray();
}
public static string GetReplayFilenameByReplayID(DataView dvReplays, int id)
{
string old_filter = dvReplays.RowFilter;
dvReplays.RowFilter = "ReplayID = " + id;
string filename = null;
foreach (DataRowView dvr in dvReplays)
{
filename = (string)dvr["Filename"];
}
dvReplays.RowFilter = old_filter;
return filename;
}
public static void GlobalReplayFilter(DataView dvReplays, DataView dvPlayers, DataView dvMaps, string filter)
{
globalfilter = filter;
}
/// <summary>
/// Changes the supplied Replays' Category ID to the NewCategoryID
/// </summary>
/// <param name="ReplayIDs"></param>
/// <param name="NewCategoryID"></param>
/// <param name="storeReader"></param>
public static void AlterReplayCategory(object[] ReplayIDs, int NewCategoryID, StoreReader storeReader)
{
System.Text.StringBuilder s_Replayids = new System.Text.StringBuilder();
if (ReplayIDs.Length > 0)
{
foreach (object replayid in ReplayIDs)
{
if (s_Replayids.Length == 0)
s_Replayids.Append("ID = " + replayid);
else
s_Replayids.Append(" OR ID = " + replayid);
}
DataRow[] drRows = storeReader.Replays.Tables["Replay"].Select(s_Replayids.ToString());
foreach (ReplayStore.ReplayRow row in drRows)
{
row.CategoryID = NewCategoryID;
}
storeReader.Replays.Merge(drRows);
//save changes
if (!storeReader.SaveReplays("rec.dat"))
MessageBox.Show(null, StoreReader.STORE_REPLAYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Fetches the category tree node using the category tag ID
/// </summary>
/// <param name="tag">Tag to search for</param>
/// <param name="nodes">Node collection that is searched</param>
/// <returns>A TreeNode object if successful or null</returns>
private static TreeNode GetNodeByTag(int tag, TreeNodeCollection nodes)
{
for (int rooti = 0; rooti < nodes.Count; rooti++)
{
if (nodes[rooti].Tag.ToString() == tag.ToString())
return nodes[rooti];
else if (nodes[rooti].GetNodeCount(true) > 0)
{
TreeNode node = GetNodeByTag(tag, nodes[rooti].Nodes);
if (node != null)
return node;
}
}
return null;
}
public static void PopulateCategoriesView(TreeView tvCategories, StoreReader storeReader)
{
tvCategories.Nodes.Clear();
TreeNode node;
foreach (CategoryStore.CategoryRow row in storeReader.Categories.Category)
{
node = new TreeNode();
node.Tag = row.ID;
node.Text = row.Name;
if (row.ParentID != 0)
{
TreeNode pNode = GetNodeByTag(row.ParentID, tvCategories.Nodes);
if (pNode != null)
pNode.Nodes.Add(node);
}
else
{
tvCategories.Nodes.Add(node);
}
}
tvCategories.ExpandAll();
}
public static void CreateNewCategory(StoreReader storeReader, string NewCategoryName, int ParentID)
{
CategoryStore.CategoryRow row = storeReader.Categories.Category.NewCategoryRow();
row["Name"] = NewCategoryName;
row["AllowDelete"] = true;
row["ParentID"] = ParentID;
row["HasChildren"] = false;
int id = (int)row["ID"];
storeReader.Categories.Category.AddCategoryRow(row);
if (!storeReader.SaveCategories("cat.dat"))
MessageBox.Show(null, StoreReader.STORE_CATEGORYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void RenameCategory(StoreReader storeReader, int CategoryID, string NewCategoryName)
{
DataRow[] drRows = storeReader.Categories.Category.Select("ID = " + CategoryID);
foreach (CategoryStore.CategoryRow row in drRows)
{
row.Name = NewCategoryName;
}
storeReader.Categories.Merge(drRows);
if (!storeReader.SaveCategories("cat.dat"))
MessageBox.Show(null, StoreReader.STORE_CATEGORYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void DeleteCategory(StoreReader storeReader, int CategoryID)
{
DataRow[] drRows = storeReader.Categories.Category.Select("ID = " + CategoryID);
foreach (CategoryStore.CategoryRow row in drRows)
{
storeReader.Categories.Category.RemoveCategoryRow(row);
}
if (!storeReader.SaveCategories("cat.dat"))
MessageBox.Show(null, StoreReader.STORE_CATEGORYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void RenameReplayFile(StoreReader storeReader, int ReplayID, string NewFileName)
{
DataRow[] drRows = storeReader.Replays.Replay.Select("ID = " + ReplayID);
foreach (ReplayStore.ReplayRow row in drRows)
{
row.Filename = NewFileName;
}
storeReader.Replays.Merge(drRows);
if (!storeReader.SaveReplays("rec.dat"))
MessageBox.Show(null, StoreReader.STORE_REPLAYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static void RenameReplay(StoreReader storeReader, int ReplayID, string NewName)
{
DataRow[] drRows = storeReader.Replays.Replay.Select("ID = " + ReplayID);
foreach (ReplayStore.ReplayRow row in drRows)
{
row.Name = NewName;
}
storeReader.Replays.Merge(drRows);
if (!storeReader.SaveReplays("rec.dat"))
MessageBox.Show(null, StoreReader.STORE_REPLAYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
public static object[] DeleteReplay(StoreReader storeReader, object[] ReplayIDs)
{
DialogResult result = MessageBox.Show(null,
"Deleted replays will be totally removed from your system! Are you sure?",
"Delete replay(s)...",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
//store all the replay filenames that we're gonna delete in this Array
ArrayList list = new ArrayList();
if (result == DialogResult.Yes)
{
System.Text.StringBuilder s_Replayids = new System.Text.StringBuilder();
if (ReplayIDs.Length > 0)
{
foreach (object replayid in ReplayIDs)
{
if (s_Replayids.Length == 0)
s_Replayids.Append("ID = " + replayid);
else
s_Replayids.Append(" OR ID = " + replayid);
}
}
//first remove the replay from the store
DataRow[] drReplay = storeReader.Replays.Replay.Select(s_Replayids.ToString());
foreach (ReplayStore.ReplayRow row in drReplay)
{
list.Add(row.Filename);
storeReader.Replays.Replay.RemoveReplayRow(row);
}
//remove from the map store
DataRow[] drMap = storeReader.Replays.Map.Select(s_Replayids.ToString().Replace("ID", "ReplayID"));
foreach (ReplayStore.MapRow row in drMap)
{
storeReader.Replays.Map.RemoveMapRow(row);
}
//remove from the player store
DataRow[] drPlayers = storeReader.Replays.Player.Select(s_Replayids.ToString().Replace("ID", "ReplayID"));
foreach (ReplayStore.PlayerRow row in drPlayers)
{
storeReader.Replays.Player.RemovePlayerRow(row);
}
//attempt to save the changes to the store
if (!storeReader.SaveReplays("rec.dat"))
MessageBox.Show(null, StoreReader.STORE_REPLAYSAVE_FAIL, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
return list.ToArray();
}
return list.ToArray();
}
/// <summary>
/// Formats the filter correctly for use with special characters
/// </summary>
/// <param name="filter"></param>
public static void FormatFilter(ref string filter)
{
if (filter != null && filter.Length > 0)
{
if (filter.IndexOf("[") > 0)
filter = filter.Replace(@"[", @"[[]");
if (filter.IndexOf("]", filter.IndexOf("]") + 1) > 0)
filter = filter.Replace(@"]", @"[]]");
}
}
}
}