-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlayerSorter.cs
186 lines (170 loc) · 6.58 KB
/
PlayerSorter.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace NFL2K5Tool
{
public class PlayerSorter : PlayerParser, IComparer<string>
{
private DataTable Table = new DataTable();
public PlayerSorter(string key):base(key) { }
private string[] SortOrder = new string[] {
"Team",
"CB,", "DE,", "DT,", "FB,", "FS,", "G,", "RB,", "ILB,", "K,", "OLB,", "P,", "QB,", "SS,", "T,", "TE,", "WR,", "C,", "KR1,", "KR2,", "PR,", "LS,"
};
private Dictionary<string, string> mFormulas;
/// <summary>
/// Sorts the team passed in as a string
/// </summary>
/// <param name="team">the team to sort</param>
/// <returns>The team sorted by position, player ranking</returns>
public string SortTeam(string team)
{
string[] lines = team.Replace("\r\n", "\n").Split("\n".ToCharArray());
Array.Sort(lines, this);
StringBuilder builder = new StringBuilder();
foreach (string line in lines)
{
builder.Append(line);
builder.Append("\n");
}
return builder.ToString();
}
#region IComparer<string> Members
public int Compare(string x, string y)
{
int xPos = -1;
int yPos = -1;
for (int i = 0; i < SortOrder.Length; i++)
{
if (x.StartsWith(SortOrder[i]))
xPos = i;
if (y.StartsWith(SortOrder[i]))
yPos = i;
if (xPos > -1 && yPos > -1)
break;
}
int retVal = xPos.CompareTo(yPos);
if (retVal == 0 && x != y)
{
retVal = PositionCompare(x, y);
}
return retVal;
}
private int PositionCompare(string x, string y)
{
if (mFormulas == null)
InitFormulas();
string formula = GetFormula(x);
if (formula != "")
{
List<string> playerPartsX = InputParser.ParsePlayerLine(x);
try
{
List<string> playerPartsY = InputParser.ParsePlayerLine(y);
string xExpr = GetExpression(formula, playerPartsX);
string yExpr = GetExpression(formula, playerPartsY);
double xResult = (double)this.Table.Compute(xExpr, "");
double yResult = (double)this.Table.Compute(yExpr, "");
return yResult.CompareTo(xResult);
}
catch (Exception)
{
throw new Exception("Error comparing position: " + playerPartsX[0] + "\nCheck formula for position.");
}
}
return 0;
}
private string GetExpression(string formula, List<string> playerParts)
{
string work = formula;
for (int i = 0; i < mSkillsArray.Length; i++)
{
if (work.IndexOf(mSkillsArray[i]) > -1)
{
work = work.Replace(mSkillsArray[i], Get(playerParts, mSkillsArray[i]));
}
}
return work;
}
string[] mSkillsArray = new string[]{
"Agility","Jumping","PassRush","RunCoverage","PassBlocking",
"RunBlocking","Catch","RunRoute","BreakTackle","HoldOntoBall","PassAccuracy",
"PassArmStrength","PassReadCoverage","KickPower","KickAccuracy","Stamina","Durability",
"Leadership","Scramble","Composure","Consistency","Aggressiveness","Coverage","Speed","Strength","Tackle"
};
string GetFormula(string line)
{
string retVal = "";
string pos = "";
int index = line.IndexOf(",");
if (index > 0)
{
pos = line.Substring(0, index + 1);
}
if (mFormulas.ContainsKey(pos))
{
retVal = mFormulas[pos];
}
return retVal;
}
private void InitFormulas()
{
mFormulas = new Dictionary<string, string>(17);
int lineEnd = 0;
int index = 0;
string formula = "";
string pos = "";
for (int i = 1; i < SortOrder.Length; i++)
{
// get the formula for the pos
pos = SortOrder[i].Replace(",", "") + ":";
index = FormulasString.IndexOf(pos);
if (index > -1)
{
lineEnd = FormulasString.IndexOf("\n", index);
formula = FormulasString.Substring(index, lineEnd - index).Trim().Replace(pos, "");
mFormulas.Add(SortOrder[i], formula);
}
}
}
#endregion
/// <summary>
/// The default sort formulas
/// </summary>
public const string sFormulasString =
@"##You can use the following skills in the sort formulas below:
#Speed,Agility,Strength,Jumping,Coverage,PassRush,RunCoverage,PassBlocking,RunBlocking,Catch,
#RunRoute,BreakTackle,HoldOntoBall,PassAccuracy,PassArmStrength,PassReadCoverage,
#Tackle,KickPower,KickAccuracy,Stamina,Durability,Leadership,Scramble,Composure,Consistency,Aggressiveness
#
# Position sort Formulas:
QB: (PassAccuracy + PassArmStrength + Leadership + Consistency) /4
RB: (3 * Speed + ( HoldOntoBall + BreakTackle) + 2*Consistency)/ 7
FB: (Catch + 3*RunBlocking ) /4
WR: (3*Speed + 2*Catch + RunRoute + 2*Consistency)/7
TE: (2*Speed + 2*Catch + RunRoute + PassBlocking + RunBlocking+ Consistency)/8
C: (PassBlocking+RunBlocking+ Consistency)/3
G: (PassBlocking+RunBlocking+ Consistency)/3
T: (PassBlocking+RunBlocking+ Consistency)/3
DE: (3*Speed + PassRush + Strength+ Tackle+ Consistency) /7
DT: (RunCoverage + 2*Strength + PassRush+ Consistency) / 5
ILB: (PassRush + RunCoverage + 2*Speed + Consistency) /6
OLB: (3 * Speed + 2 * Tackle + PassRush + RunCoverage + Coverage + Consistency)/9
CB: (3*Speed + 3 * Coverage + Tackle + Consistency)/8
FS: (2* Coverage + Tackle + Speed + Consistency) / 5
SS: (2* Coverage + 2*Tackle + Speed + Consistency) / 6
K: (KickPower + 2*KickAccuracy)/3
P: ( 2 * KickPower + KickAccuracy)/3
";
private string mFormulasString = sFormulasString;
/// <summary>
/// The sort formula text to use.
/// </summary>
public string FormulasString
{
get { return mFormulasString; }
set { mFormulasString = value; }
}
}
}