/
Raid.cs
1393 lines (1234 loc) · 62.4 KB
/
Raid.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections;
using System.Linq;
using Newtonsoft.Json;
using ES_DKP_Utils.Models;
namespace ES_DKP_Utils
{
public class Raid
{
#region Declarations
private frmMain owner;
private string _RaidName;
public string RaidName
{
get { return _RaidName; }
set
{
DataRow[] rs = dbRaid.Select("EventNameOrLoot='" + _RaidName + "' OR LootRaid='" + _RaidName + "'");
foreach (DataRow r in rs)
{
if (r["EventNameOrLoot"].Equals(_RaidName))
{
r["EventNameOrLoot"] = value;
}
if (r["LootRaid"].Equals(_RaidName))
{
r["LootRaid"] = value;
}
}
_RaidName = value;
}
}
private DateTime _RaidDate;
public DateTime RaidDate
{
get { return _RaidDate; }
set
{
DataRow[] rs = dbRaid.Select("Date =#" + _RaidDate.Month + "/" + _RaidDate.Day + "/" + _RaidDate.Year + "#");
foreach (DataRow r in rs)
{
r["Date"] = value;
}
_RaidDate = value;
}
}
private bool _AttendanceSynced;
public bool AttendanceSynced
{
get
{
return _AttendanceSynced;
}
set
{
_AttendanceSynced = value;
}
}
private bool _Loaded;
public bool Loaded
{
get
{
return _Loaded;
}
set
{
_Loaded = value;
}
}
private bool _DoubleTier;
public bool DoubleTier
{
get
{
return _DoubleTier;
}
set
{
_DoubleTier = value;
}
}
private System.Double _TotalDKP;
public System.Double TotalDKP
{
get
{
return _TotalDKP;
}
set
{
_TotalDKP = value;
}
}
private int _Attendees;
public int Attendees
{
get
{
return _Attendees;
}
set
{
_Attendees = value;
}
}
private DataTable dbRaid;
private DataTable dbPeople;
private DataTable dbAlts;
private OleDbConnection dbConnect;
private OleDbDataAdapter dkpDA;
private OleDbDataAdapter altDA;
private OleDbCommandBuilder cmdBld;
private OleDbCommandBuilder altBld;
private ArrayList ignore;
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
#region Constuctor
public Raid(frmMain owner)
{
log.Debug("Begin Method: Raid.Raid(frmMain) (" + owner.ToString() + ")");
this.owner = owner;
this.ignore = new ArrayList();
DoubleTier = false;
string connectionStr;
connectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + owner.DBString;
try { dbConnect = new OleDbConnection(connectionStr); }
catch (Exception ex)
{
log.Error("Failed to create data connection: " + ex.Message);
MessageBox.Show("Could not create data connection. \n(" + ex.Message + ")", "Error");
}
dkpDA = new OleDbDataAdapter();
dbRaid = new DataTable("Raid");
dbRaid.Columns.Add("Name", System.Type.GetType("System.String"));
dbRaid.Columns.Add("Date", System.Type.GetType("System.DateTime"));
dbRaid.Columns.Add("EventNameOrLoot", System.Type.GetType("System.String"));
dbRaid.Columns.Add("PTS", System.Type.GetType("System.Double"));
dbRaid.Columns.Add("Comment", System.Type.GetType("System.String"));
dbRaid.Columns.Add("LootRaid", System.Type.GetType("System.String"));
dbPeople = new DataTable("People");
try
{
string query = "SELECT DISTINCT Name FROM DKS WHERE (Name<>\"zzzDKP Retired\")";
dkpDA.SelectCommand = new OleDbCommand(query, dbConnect);
dbConnect.Open();
dkpDA.Fill(dbPeople);
}
catch (Exception ex)
{
log.Error("Failed to load people table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
log.Debug("End Method: Raid.Raid()");
}
#endregion
#region Methods
public void LoadRaid()
{
log.Debug("Begin Method: Raid.LoadRaid()");
try
{
// TODO: make raid names SQL safe
// erroring raid 12/21/2004 "L'Fay Epic" because it screws up quotation marks:
// 2023-01-16 13:55:26,717 [1] ERROR ES_DKP_Utils.Raid [(null)] - Failed to load raid: Syntax error (missing operator) in query expression '(Date=#12/21/2004# AND (((EventNameOrLoot LIKE 'L'Fay Epic%' AND LootRaid NOT LIKE '__%') OR LootRaid LIKE 'L'Fay Epic%') AND EventNameOrLoot NOT LIKE '%1') )'.
string query = "SELECT * FROM DKS WHERE (Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND (((EventNameOrLoot LIKE '" + RaidName + "%' AND LootRaid NOT LIKE '__%') OR LootRaid LIKE '" + RaidName + "%') AND EventNameOrLoot NOT LIKE '%1') )";
dkpDA.SelectCommand = new OleDbCommand(query, dbConnect);
dbConnect.Open();
dkpDA.Fill(dbRaid);
cmdBld = new OleDbCommandBuilder(dkpDA);
Loaded = true;
DataRow[] tiercheck = dbRaid.Select("EventNameOrLoot LIKE '%0' OR LootRaid LIKE '%0'");
if (tiercheck.Length > 0)
{
DoubleTier = true;
foreach (DataRow r in tiercheck)
{
bool isloot = (double)r.ItemArray[3] < 0;
if (isloot)
{
string s = (string)r.ItemArray[5];
s = s.TrimEnd(new char[] { '0' });
r.ItemArray = new object[] {
r.ItemArray[0], r.ItemArray[1],
r.ItemArray[2], r.ItemArray[3],
r.ItemArray[4], s };
}
else
{
string s = (string)r.ItemArray[2];
s = s.TrimEnd(new char[] { '0' });
r.ItemArray = new object[] {
r.ItemArray[0], r.ItemArray[1],
s, r.ItemArray[3],
r.ItemArray[4], r.ItemArray[5] };
}
}
}
}
catch (Exception ex)
{
log.Error("Failed to load raid: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
log.Debug("End Method: Raid.LoadRaid()");
}
public void ShowAttendees()
{
log.Debug("Begin Method: Raid.ShowAttendees()");
DataRow[] attendees = dbRaid.Select("(PTS>=0)");
DataTable newTable = rowsToTable(dbRaid, attendees);
TableViewer table = new TableViewer(owner, newTable, new TableViewer.UpdateDel(UpdateAttendees));
table.Show();
log.Debug("End Method: Raid.ShowAttendees()");
}
public void UpdateAttendees(DataTable dt)
{
log.Debug("Begin Method: Raid.UpdateAttendees(DataTable) (" + dt.ToString() + ")");
foreach (DataRow r in dbRaid.Select("(PTS>=0)")) { dbRaid.Rows.Remove(r); }
foreach (DataRow r in dt.Rows) { dbRaid.Rows.Add(r.ItemArray); }
log.Debug("End Method: Raid.UpdateAttendees()");
}
public void ShowLoot()
{
log.Debug("Begin Method: Raid.ShowLoot()");
DataRow[] loot = dbRaid.Select("(PTS<0)");
DataTable newTable = rowsToTable(dbRaid, loot);
TableViewer table = new TableViewer(owner, newTable, new TableViewer.UpdateDel(UpdateLoot));
table.Show();
log.Debug("End Method: Raid.ShowLoot()");
}
public void UpdateLoot(DataTable dt)
{
log.Debug("Begin Method: Raid.UpdateLoot(DataTable) (" + dt.ToString() + ")");
foreach (DataRow r in dbRaid.Select("(PTS<0)")) { dbRaid.Rows.Remove(r); }
foreach (DataRow r in dt.Rows) { dbRaid.Rows.Add(r.ItemArray); }
log.Debug("End Method: Raid.UpdateLoot()");
}
public void InputPerson(string s)
{
log.Debug("Begin Method: Raid.InputPerson(string) (" + s.ToString() + ")");
DataRow[] rows = dbPeople.Select("Name='" + s + "'");
if (rows.Length > 0)
{
DataRow[] rs = dbRaid.Select("Name='" + s + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { s, RaidDate, RaidName, 0, null, null });
log.Debug("Added person: " + s);
}
}
else
{
string t = GetAlt(s);
if ((t != null && t.Length != 0))
{
if (dbRaid.Select("Name='" + t + "' AND PTS>=0").Length == 0)
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added person: " + t);
}
}
}
else
{
if (!ignore.Contains(t))
{
AppAltDialog alt = new AppAltDialog(owner, s, dbPeople);
t = alt.GetName();
if ((t != null && t.Length != 0))
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added person: " + t);
}
if (t != s) dbAlts.Rows.Add(new object[] { s, t });
altDA.Update(dbAlts);
}
else { ignore.Add(t); }
}
}
}
}
public void ParseRaidDump(string logFile)
{
log.Debug("Begin Method: Raid.ParseRaidDump(string) (" + logFile.ToString() + ")");
FileStream input;
StreamReader sr;
string line;
char[] delimiters = new char[] { '\t' };
ArrayList people = new ArrayList();
try
{
input = new FileStream(logFile, FileMode.Open, FileAccess.Read);
sr = new StreamReader(input);
}
catch (Exception ex)
{
log.Error("Failed to open raid dump file to parse: " + ex.Message);
MessageBox.Show("File IO Error.\n(" + ex.Message + ")");
return;
}
do
{
line = sr.ReadLine();
if (line == null)
{
log.Debug("Line in file empty, skipping");
break;
}
string[] parts = line.Split(delimiters);
people.Add(parts[1]);
} while (line != null);
sr.Close();
input.Close();
if (people.Count > 0)
{
MessageBox.Show("Found " + people.Count + " raiders including " + people[0] + ".");
}
foreach (string s in people)
{
DataRow[] rows = dbPeople.Select("Name='" + s + "'");
if (rows.Length > 0)
{
DataRow[] rs = dbRaid.Select("Name='" + s + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { s, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + s);
}
}
else
{
string t = GetAlt(s);
if ((t != null && t.Length != 0))
{
if (!people.Contains(t))
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + t + " (Alt: " + s + ")");
}
}
}
else
{
AppAltDialog alt = new AppAltDialog(owner, s, dbPeople);
t = alt.GetName();
if ((t != null && t.Length != 0))
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + t);
}
if (t != s) dbAlts.Rows.Add(new object[] { s, t });
altDA.Update(dbAlts);
}
}
}
}
log.Debug("End Method: Raid.ParseRaidDump()");
}
public void ParseAttendance(string logFile, string zo)
{
log.Debug("Begin Method: Raid.ParseAttendance(string,string) (" + logFile.ToString() + "," + zo.ToString() + ")");
string[] zones = zo.Split(new char[] { ' ' });
string line;
FileStream input;
StreamReader sr;
Regex regex = new Regex(@"\[.*\] \[.*\] (?<name>\S+).*<(?<guild>.*)> ZONE: (?<zone>.*)$");
Match m;
ArrayList people = new ArrayList();
foreach (DataRow z in dbRaid.Rows) people.Add(z.ItemArray[0]);
try
{
input = new FileStream(logFile, FileMode.Open, FileAccess.Read);
sr = new StreamReader(input);
}
catch (Exception ex)
{
log.Error("Failed to open log to parse: " + ex.Message);
MessageBox.Show("File IO Error.\n(" + ex.Message + ")");
return;
}
do
{
line = sr.ReadLine();
if (line != null)
{
m = regex.Match(line);
if (m.Success)
{
string _guild = m.Groups["guild"].ToString();
string _zone = m.Groups["zone"].ToString();
string _name = m.Groups["name"].ToString();
if (owner.GuildNames.Contains(_guild) && zo.Contains(_zone) && !people.Contains(_name))
{
log.Debug(_name + " <" + _guild + "> is in zone " + _zone + ", adding to people array.");
people.Add(_name);
}
}
}
} while (line != null);
sr.Close();
input.Close();
people.Sort();
foreach (string s in people)
{
DataRow[] rows = dbPeople.Select("Name='" + s + "'");
if (rows.Length > 0)
{
DataRow[] rs = dbRaid.Select("Name='" + s + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { s, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + s);
}
}
else
{
string t = GetAlt(s);
if ((t != null && t.Length != 0))
{
if (!people.Contains(t))
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + t + " (Alt: " + s + ")");
}
}
}
else
{
AppAltDialog alt = new AppAltDialog(owner, s, dbPeople);
t = alt.GetName();
if ((t != null && t.Length != 0))
{
DataRow[] rs = dbRaid.Select("Name='" + t + "' AND PTS>=0");
if (rs.Length == 0)
{
dbRaid.Rows.Add(new object[] { t, RaidDate, RaidName, 0, null, null });
log.Debug("Added attendance row for " + t);
}
if (t != s) dbAlts.Rows.Add(new object[] { s, t });
altDA.Update(dbAlts);
}
}
}
}
log.Debug("Begin Method: Raid.ParseAttendance()");
}
public string GetAlt(string s)
{
log.Debug("Begin Method: Raid.GetAlt(string) (" + s.ToString() + ")");
dbAlts = new DataTable("Alts");
string query = "SELECT * FROM ALTS";
try
{
altDA = new OleDbDataAdapter(query, dbConnect);
dbConnect.Open();
altDA.Fill(dbAlts);
altBld = new OleDbCommandBuilder(altDA);
}
catch (Exception ex)
{
log.Error("Failed to load alts table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
DataRow[] rows = dbAlts.Select("AltName='" + s + "'");
if (rows.Length > 0)
{
log.Debug("End Method: Raid.GetAlt(), returning " + (string)rows[0].ItemArray[1]); ;
return (string)rows[0].ItemArray[1];
}
log.Debug("End Method: Raid.GetAlt(), returning ");
return "";
}
public string[] ParseLine(string line)
{
/* Whatever is using this function is expecting a string array to come back with these elements:
* 0: Raider's Name
* 1: Zone
*
* I've added guild as paramater [2], in case we want to use it in guild name matching.
*/
log.Debug("Begin Method: Raid.ParseLine(string) (" + line.ToString() + ")");
Regex r = new Regex(@"\[.*\] \[.*\] (?<name>\S+).*<(?<guild>.*)> ZONE: (?<zone>.*)$");
Match m = r.Match(line.Trim());
string[] results = { m.Groups["name"].ToString(),
m.Groups["zone"].ToString(),
m.Groups["guild"].ToString() };
log.Debug("End Method: Raid.ParseLine(), returning {" + results[0] + "," + results[1] + "}");
return results;
}
public void AddRowToLocalTable(object[] o)
{
log.Debug("Begin Method: Raid.AddRowToLocalTable(object[]) (" + o.ToString() + ")");
string s = "Creating new row: ";
try
{
foreach (object k in o)
{
if (k != null)
s += k.ToString() + ",";
else s += "null,";
}
log.Debug(s.TrimEnd(new char[] { ',' }));
}
catch (Exception ex) {
log.Error("Error adding row to local table: " + ex.Message);
}
DataRow r = dbRaid.NewRow();
r.ItemArray = o;
dbRaid.Rows.Add(r);
log.Debug("End Method: Raid.AddRowToLocalTable()");
}
public void DivideDKP()
{
log.Debug("Begin Method: Raid.DivideDKP()");
double dkp = 0.0;
DataRow[] rows = dbRaid.Select("PTS<0 AND LootRaid='" + RaidName + "'");
foreach (DataRow r in rows) { dkp += Math.Abs((double)r.ItemArray[3]); }
log.Info("Total dkp to distribute: " + dkp);
rows = dbRaid.Select("PTS>=0 AND EventNameOrLoot='" + RaidName + "'");
if (rows.Length == 0) rows = dbRaid.Select("PTS>=0 AND EventNameOrLoot='" + RaidName + "0'");
if (rows.Length == 0) return;
Attendees = rows.Length;
log.Info("Total attendees: " + Attendees);
TotalDKP = dkp;
dkp *= (1 - owner.DKPTax);
dkp /= rows.Length;
log.Info("DKP per person: " + dkp);
foreach (DataRow r in rows)
{
r.ItemArray = new object[] { r.ItemArray[0], r.ItemArray[1], r.ItemArray[2], dkp, r.ItemArray[4], r.ItemArray[5] };
}
log.Debug("End Method: Raid.DivideDKP()");
}
public void SyncData()
{
int k = 0;
log.Debug("Begin Method: Raid.SyncData()");
owner.StatusMessage = "Saving raid...";
owner.PBMin = 0;
owner.PBMax = dbRaid.Rows.Count + 1;
owner.PBVal = 0;
string query = "UPDATE DKS SET Comment='DeleteMe' WHERE Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND ( (EventNameOrLoot LIKE '" + RaidName + "%' AND PTS>=0)OR (LootRaid LIKE '" + RaidName + "%' AND PTS<0))";
OleDbCommand updateCommand;
OleDbCommand insertCommand;
OleDbCommand deleteCommand;
try
{
updateCommand = new OleDbCommand(query, dbConnect);
dbConnect.Open();
updateCommand.ExecuteNonQuery();
log.Info("Updated " + k + " rows, flagged for deletion in DKS");
query = "UPDATE EventLog SET DKPValue=-1 WHERE EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND ( EventName LIKE '" + RaidName + "%' )";
updateCommand = new OleDbCommand(query, dbConnect);
k = updateCommand.ExecuteNonQuery();
log.Info("Updated " + k + " rows, flagged for deletion in EventLog");
}
catch (Exception ex)
{
log.Error("Failed to load either DKS or EventLog: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
DataRow[] ppl = dbRaid.Select("PTS>=0");
DataRow[] loot = dbRaid.Select("PTS<0");
string lootstr;
if (!DoubleTier)
{
try
{
dbConnect.Open();
foreach (DataRow r in ppl)
{
query = "UPDATE DKS SET EventNameOrLoot=\"" + r.ItemArray[2] + "\", PTS=" + r.ItemArray[3] + ", Comment=\"" + r.ItemArray[4] + "\", LootRaid=\"" + r.ItemArray[5] + "\" WHERE (Name=\"" + r.ItemArray[0] + "\" AND PTS>=0 AND Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventNameOrLoot=\"" + RaidName + "\")";
updateCommand = new OleDbCommand(query, dbConnect);
if ((k = updateCommand.ExecuteNonQuery()) == 0)
{
query = "INSERT INTO DKS VALUES (\"" + r.ItemArray[0] + "\",#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + r.ItemArray[2] + "\"," + r.ItemArray[3] + ",\"" + r.ItemArray[4] + "\",\"" + r.ItemArray[5] + "\")";
insertCommand = new OleDbCommand(query, dbConnect);
k = insertCommand.ExecuteNonQuery();
}
owner.PBVal++;
}
foreach (DataRow r in loot)
{
lootstr = (string)r.ItemArray[2];
query = "INSERT INTO DKS VALUES (\"" + r.ItemArray[0] + "\",#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + lootstr + "\"," + r.ItemArray[3] + ",\"" + r.ItemArray[4] + "\",\"" + r.ItemArray[5] + "\")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
owner.PBVal++;
}
DataTable temp = new DataTable();
query = "SELECT EventName FROM EventLog WHERE EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventName NOT LIKE '%1'";
OleDbDataAdapter tempda = new OleDbDataAdapter(query, dbConnect);
tempda.Fill(temp);
query = "UPDATE EventLog ";
query += "SET Points=" + (TotalDKP / Attendees) * (1 - owner.DKPTax) + ", DKPValue=" + TotalDKP + " ";
query += "WHERE (EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventName=\"" + RaidName + "\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO EventLog Values (#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + RaidName + "\"," + (TotalDKP / Attendees) * (1 - owner.DKPTax) + "," + TotalDKP + "," + (int)((int)temp.Rows.Count + 1) + ")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
owner.PBVal++;
query = "DELETE FROM DKS WHERE Comment='DeleteMe'";
deleteCommand = new OleDbCommand(query, dbConnect);
deleteCommand.ExecuteNonQuery();
query = "DELETE FROM EventLog WHERE DKPValue=-1";
deleteCommand = new OleDbCommand(query, dbConnect);
deleteCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
log.Error("Failed to update DKS or EventLog: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
}
else
{
try
{
dbConnect.Open();
foreach (DataRow r in ppl)
{
query = "UPDATE DKS SET EventNameOrLoot=\"" + r.ItemArray[2] + "0\", PTS=" + r.ItemArray[3] + ", Comment=\"" + r.ItemArray[4] + "\", LootRaid=\"" + r.ItemArray[5] + "\" WHERE (Name=\"" + r.ItemArray[0] + "\" AND PTS>=0 AND Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventNameOrLoot=\"" + RaidName + "0\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO DKS VALUES (\"" + r.ItemArray[0] + "\",#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + r.ItemArray[2] + "0\"," + r.ItemArray[3] + ",\"" + r.ItemArray[4] + "\",\"" + r.ItemArray[5] + "\")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
query = "UPDATE DKS SET EventNameOrLoot=\"" + r.ItemArray[2] + "1\", PTS=0, Comment=\"" + r.ItemArray[4] + "\", LootRaid=\"" + r.ItemArray[5] + "\" WHERE (Name=\"" + r.ItemArray[0] + "\" AND PTS>=0 AND Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventNameOrLoot=\"" + RaidName + "1\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO DKS VALUES (\"" + r.ItemArray[0] + "\",#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + r.ItemArray[2] + "1\",0,\"" + r.ItemArray[4] + "\",\"" + r.ItemArray[5] + "\")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
owner.PBVal++;
}
foreach (DataRow r in loot)
{
lootstr = (string)r.ItemArray[2];
query = "UPDATE DKS ";
query += "SET PTS=" + r.ItemArray[3] + ", Comment=\"" + r.ItemArray[4] + "\" ";
query += "WHERE (Name=\"" + r.ItemArray[0] + "\" AND PTS<0 AND Date=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND LootRaid=\"" + RaidName + "0\" AND EventNameOrLoot=\"" + lootstr + "\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO DKS VALUES (\"" + r.ItemArray[0] + "\",#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + lootstr + "\"," + r.ItemArray[3] + ",\"" + r.ItemArray[4] + "\",\"" + r.ItemArray[5] + "0\")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
owner.PBVal++;
}
DataTable temp = new DataTable();
query = "SELECT EventName FROM EventLog WHERE EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventName NOT LIKE '%1'";
OleDbDataAdapter tempda = new OleDbDataAdapter(query, dbConnect);
tempda.Fill(temp);
query = "UPDATE EventLog ";
query += "SET Points=" + (TotalDKP / Attendees) * (1 - owner.DKPTax) + ", DKPValue=" + TotalDKP + " ";
query += "WHERE (EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventName=\"" + RaidName + "0\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO EventLog Values (#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + RaidName + "0\"," + (TotalDKP / Attendees) * (1 - owner.DKPTax) + "," + TotalDKP + "," + (int)((int)temp.Rows.Count + 1) + ")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
query = "UPDATE EventLog ";
query += "SET Points=0, DKPValue=0 ";
query += "WHERE (EventDate=#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "# AND EventName=\"" + RaidName + "1\")";
updateCommand = new OleDbCommand(query, dbConnect);
if (updateCommand.ExecuteNonQuery() == 0)
{
query = "INSERT INTO EventLog Values (#" + RaidDate.Month + "/" + RaidDate.Day + "/" + RaidDate.Year + "#,\"" + RaidName + "1\"," + 0 + "," + 0 + "," + (int)((int)temp.Rows.Count + 1) + ")";
insertCommand = new OleDbCommand(query, dbConnect);
insertCommand.ExecuteNonQuery();
}
owner.PBVal++;
query = "DELETE FROM DKS WHERE Comment='DeleteMe'";
deleteCommand = new OleDbCommand(query, dbConnect);
deleteCommand.ExecuteNonQuery();
query = "DELETE FROM EventLog WHERE DKPValue=-1";
deleteCommand = new OleDbCommand(query, dbConnect);
deleteCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
log.Error("Failed to update DKS or EventLog: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
}
if (owner.AutosaveJsonRaidModels)
{
SaveJsonRaidModel();
}
log.Debug("End Method: Raid.SyncRaid()");
}
/// <summary>
/// Saves a JSON file of the current raid as represented by a freshly-generated RaidModel
/// </summary>
public void SaveJsonRaidModel()
{
RaidModel raidModel = GenerateRaidModel();
string fileName = $"{this.RaidDate.ToString("yyyy-MM-dd")}-{this.RaidName}.json";
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '_');
}
try
{
Directory.CreateDirectory(owner.JsonRaidModelDirectory);
File.WriteAllText(
Path.Combine(owner.JsonRaidModelDirectory, fileName),
JsonConvert.SerializeObject(raidModel)
);
}
catch (Exception ex)
{
log.Error("Failed to save JSON Raid Model: " + ex.Message);
MessageBox.Show("Failed to save JSON Raid Model: " + ex.Message, "Error");
}
}
/// <summary>
/// Generates a RaidModel object from the current form's dbRaid object.
/// </summary>
/// <returns>
/// RaidModel generated from current dbRaid
/// </returns>
private RaidModel GenerateRaidModel()
{
RaidModel raidModel = new RaidModel
{
Name = RaidName,
Date = RaidDate,
Attendance = DoubleTier ? 2.0 : 1.0
};
DataRow[] modelLoader = dbRaid.Select();
// These rows are as follows:
// ItemArray[0] - string Name (of raider)
// ItemArray[1] - DateTime Date (of raid)
// ItemArray[2] - string EventNameOrLoot
// ItemArray[3] - double PTS
// ItemArray[4] - string Comment
// ItemArray[5] - string LootRaid
foreach (var row in modelLoader)
{
if ((double)row.ItemArray[3] < 0)
{
raidModel.Loots.Add(
new LootModel(
(string)row.ItemArray[2],
(string)row.ItemArray[0],
Math.Abs((double)row.ItemArray[3])
)
);
}
else
{
if (!raidModel.Raiders.Contains((string)row.ItemArray[0]))
{
raidModel.Raiders.Add((string)row.ItemArray[0]);
}
}
}
return raidModel;
}
public void FigureTiers()
{
log.Debug("Begin Method: Raid.FigureTiers()");
string query = "SELECT DISTINCT EventDate FROM EventLog WHERE EventDate <= #" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year + "#";
DataTable dates = null;
DataTable nameCount = null;
try
{
dates = new DataTable("Date");
dkpDA = new OleDbDataAdapter(query, dbConnect);
dbConnect.Open();
dkpDA.Fill(dates);
}
catch (Exception ex)
{
log.Error("Failed to fill dates table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
DateTime tierwindow = (DateTime)dates.Rows[dates.Rows.Count - 20].ItemArray[0];
int totalraids = 0;
try
{
OleDbCommand deleteCmd = new OleDbCommand("DELETE * FROM NamesTiers", dbConnect);
dbConnect.Open();
deleteCmd.ExecuteNonQuery();
nameCount = new DataTable("DKS");
query = "SELECT DKS.Name, Count(DKS.Name) as NameCount FROM DKS WHERE DKS.Date>=#" + tierwindow.Month + "/" + tierwindow.Day + "/" + tierwindow.Year + "# AND DKS.PTS>=0 GROUP BY DKS.Name";
dkpDA = new OleDbDataAdapter(query, dbConnect);
dkpDA.Fill(nameCount);
query = "SELECT Count(EventLog.EventName) as TotalRaids FROM EventLog WHERE EventLog.EventDate>=#" + tierwindow.Month + "/" + tierwindow.Day + "/" + tierwindow.Year + "#";
dkpDA = new OleDbDataAdapter(query, dbConnect);
DataTable t = new DataTable("temp");
dkpDA.Fill(t);
totalraids = (int)t.Rows[0].ItemArray[0];
}
catch (Exception ex)
{
log.Error("Failed to fill attendance count or raid count table: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
owner.PBMin = 0;
owner.PBMax = nameCount.Rows.Count + nameCount.Rows.Count / 10;
owner.PBVal = nameCount.Rows.Count / 10;
DataRow[] rows = nameCount.Select("Name<>'zzzDKP Retired'");
foreach (DataRow r in rows)
{
int numraids = (int)r.ItemArray[1];
char c;
double d;
string q;
d = (double)numraids / totalraids;
if (d >= owner.TierAPct)
c = 'A';
else if (d >= owner.TierBPct)
c = 'B';
else if (d >= owner.TierCPct)
c = 'C';
else
c = 'E';
q = "INSERT INTO NamesTiers VALUES (\"" + r.ItemArray[0] + "\",\"" + c + "\"," + numraids + "," + d * 100 + ")";
try
{
OleDbCommand dbc = new OleDbCommand(q, dbConnect);
dbConnect.Open();
dbc.ExecuteNonQuery();
owner.PBVal++;
}
catch (Exception ex)
{
log.Error("Failed to write NamesTiers row: " + ex.Message);
MessageBox.Show("Could not open data connection. \n(" + ex.Message + ")", "Error");
}
finally { dbConnect.Close(); }
}
log.Debug("End Method: Raid.FigureTiers()");
}
public void AddNameClass(string s, string t)
{
log.Debug("Begin Method: Raid.AddNameClass(string,string) (" + s.ToString() + "," + t.ToString() + ")");