Skip to content

Commit

Permalink
Global Edit Form (tewak)+ applyable formulas
Browse files Browse the repository at this point in the history
Drag/drop gamesave files into text area.
  • Loading branch information
BAD-AL committed Oct 14, 2020
1 parent 9fcca32 commit b3b8609
Show file tree
Hide file tree
Showing 17 changed files with 566 additions and 124 deletions.
7 changes: 7 additions & 0 deletions EnumDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,11 @@ public enum CoachOffsets
LoneBackPass = 0x8B,
EmptyPass = 0x8C
}

public enum FormulaMode
{
Normal = 0,
Add,
Percent
}
}
124 changes: 123 additions & 1 deletion GamesaveTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ public string GetPlayerTeam(int player)
/// </summary>
public void AutoUpdatePBP()
{
Console.WriteLine("AutoUpdatePBP");
string key, firstName, lastName, number, val;
for (int player = 0; player < MaxPlayers; player++)
{
Expand Down Expand Up @@ -719,6 +720,7 @@ public void AutoUpdatePBP()
/// </summary>
public void AutoUpdatePhoto()
{
Console.WriteLine("AutoUpdatePhoto");
string key, firstName, lastName, number, val;
for (int player = 0; player < MaxPlayers; player++)
{
Expand Down Expand Up @@ -804,6 +806,7 @@ public void SetPlayerPositionDepth(int player, byte depth)
/// </summary>
public void AutoUpdateDepthChart()
{
Console.WriteLine("AutoUpdateDepthChart");
for (int i = 0; i < 32; i++)
{
AutoUpdateDepthChartForTeam(mTeamsDataOrder[i]);
Expand Down Expand Up @@ -1063,6 +1066,10 @@ public bool LoadSaveFile(string fileName)
GameSaveData = StaticUtils.ExtractFileFromZip(fileName, null, "SAVEGAME.DAT");
mZipFile = fileName;
}
else
{
return false;
}
mColleges.Clear();
if (GameSaveData[0] == (byte)'R' && GameSaveData[1] == (byte)'O' &&
GameSaveData[2] == (byte)'S' && GameSaveData[3] == (byte)'T')
Expand Down Expand Up @@ -2753,7 +2760,120 @@ public List<int> FindPlayer(string pos, string firstName, string lastName)
return retVal;
}

#region GetPlayersByFormulaRegion
#region FormulaRegionStuff


/// <summary>
/// Aplies a formulaic change to players.
/// Formulas are printed to the console when the function is run so that the user can see the
/// formula when executed from the Global editor.
/// </summary>
/// <param name="formula">The formula used to select players.</param>
/// <param name="targetAttribute">The attribute you want to set.</param>
/// <param name="targetValue">The target value you want to set.</param>
/// <param name="positions">The list of positions to search.</param>
/// <param name="formulaMode"> The formula mode to use. </param>
/// <param name="applyChanges">false to not apply the changed (just want to see who's affected)</param>
/// <returns>
/// Null if no players are selected
/// string starting with 'Exception!' if an exception occured
/// string list of players changed if successful
/// </returns>
public string ApplyFormula(string formula,
string targetAttribute,
string targetValue,
List<string> positions,
FormulaMode formulaMode,
bool applyChanges )
{
string retVal = null;
try
{
if (formulaMode != FormulaMode.Normal)
{
Console.WriteLine("ApplyFormula('{0}','{1}','{2}', [{3}], {4})",
formula, targetAttribute, targetValue, String.Join(",", positions.ToArray()),
formulaMode
);
}
else
{
Console.WriteLine("ApplyFormula('{0}','{1}','{2}', [{3}])",
formula, targetAttribute, targetValue, String.Join(",", positions.ToArray()));
}

if ("Always".Equals(formula, StringComparison.InvariantCultureIgnoreCase))
formula = "true";
List<int> playerIndexes = GetPlayersByFormula(formula, positions);
string targetValueParam = targetValue; // save a copy for the 'usePercent' case

if (playerIndexes.Count > 0)
{
String tmp = "";
int temp_i = 0;
StringBuilder sb = new StringBuilder(30 * playerIndexes.Count);
int p = 0;

sb.Append("#Players affected = ");
sb.Append(playerIndexes.Count);
sb.Append("\n");
sb.Append("#Team,FirstName,LastName,");
sb.Append(targetAttribute);
sb.Append("\n");
int tmp_2 = 0;
for (int i = 0; i < playerIndexes.Count; i++)
{
p = playerIndexes[i];
if (formulaMode == FormulaMode.Add )
{
tmp = GetPlayerField(p, targetAttribute);
if (Int32.TryParse(tmp, out temp_i))
{
if( Int32.TryParse(targetValueParam, out tmp_2))
targetValue = (temp_i += tmp_2 ).ToString();
else
return String.Format("Exception! value '{0}' is not an integer!", targetValueParam);
}
else
return String.Format( "Exception! Field '{0}' is not a number!", targetAttribute);
}
else if ( formulaMode == FormulaMode.Percent)
{
tmp = GetPlayerField(p, targetAttribute);
if (Int32.TryParse(tmp, out temp_i))
{
double percent = double.Parse(targetValueParam) * 0.01;
//Console.WriteLine("Test: {0}", ((int)(temp_i * percent)).ToString());
targetValue = ((int)(temp_i * percent)).ToString();
}
else
return String.Format("Exception! Cannot take a percent of '{0}'!", targetAttribute);
}
if (applyChanges)
{
this.SetPlayerField(p, targetAttribute, targetValue);
}

sb.Append(GetPlayerTeam(p));
sb.Append(",");
sb.Append(GetPlayerFirstName(p));
sb.Append(",");
sb.Append(GetPlayerLastName(p));
sb.Append(",");
//sb.Append(GetPlayerField(p, targetAttribute));
sb.Append(targetValue);
sb.Append("\n");
}
retVal = sb.ToString();
}
}
catch (Exception ex)
{
retVal = "Exception! Check formula: " + ex.Message;
}
return retVal;
}

/// <summary>
/// Returns the indexes of the players matching the specified formula.
/// </summary>
Expand All @@ -2765,6 +2885,7 @@ public List<int> GetPlayersByFormula(string formula, List<string> positions)
List<int> retVal = new List<int>();
if (formula != null)
{
//Console.WriteLine("GetPlayersByFormula: '{0}'; [{1}] ", formula , String.Join(",", positions.ToArray()) );
System.Data.DataTable table = new System.Data.DataTable();
string evaluationString = "";
formula = formula.Replace("||", " or ").Replace("&&", " and ").Replace("=", " = ").Replace("!=", " <> ");
Expand All @@ -2789,6 +2910,7 @@ public List<int> GetPlayersByFormula(string formula, List<string> positions)
return retVal;
}


private string SubstituteAttributesForValues(int playerIndex, string formula)
{
string retVal = GetAppearanceAttributeExpression(formula, playerIndex);
Expand Down
136 changes: 41 additions & 95 deletions GlobalEditForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,130 +82,74 @@ private List<string> GetPositions()

private string GetFormula()
{
string formula = "true";
string f = mFormulaTextBox.Text.Trim();
if (f != "" && !f.Equals("Always", StringComparison.InvariantCultureIgnoreCase))
formula = f;
return formula;
return mFormulaTextBox.Text.Trim();
}

private FormulaMode GetFormulaMode()
{
FormulaMode retVal = FormulaMode.Normal;
if (intAttrControl1.Visible && mPercentCheckbox.Checked)
retVal = FormulaMode.Percent;
//else if (mIncreaseYearsPro)
// retVal = FormulaMode.Increment;
return retVal;
}

private void mShowAffectedPlayersButton_Click(object sender, EventArgs e)
{
ProcessFormula(false);
string result =
Tool.ApplyFormula(GetFormula(), GetTargetAttribute(), GetTargetValue(), GetPositions(), GetFormulaMode(), false);
ShowResults(result);
}

private void mSetAttributeButton_Click(object sender, EventArgs e)
{
ProcessFormula(true);
string result =
Tool.ApplyFormula(GetFormula(), GetTargetAttribute(), GetTargetValue(), GetPositions(), GetFormulaMode(), true);
ShowResults(result);
}


private bool mIncreaseYearsPro = false;

private void mAddOneYear_Click(object sender, EventArgs e)
{
mIncreaseYearsPro = true;
ProcessFormula(true);
mIncreaseYearsPro = false;
string result =
Tool.ApplyFormula(GetFormula(), "YearsPro", "1", GetPositions(), FormulaMode.Add, true);
ShowResults(result);
}

/// <summary>
/// get tle players that the formulaand positions checkboxes apply to.
/// Sets the players attributes when 'applyChanges' = true.
/// </summary>
private void ProcessFormula(bool applyChanges)
private void ShowResults(string results)
{
try
if (results == null)
{
List<int> playerIndexes = GetPlayerIntexesFor(GetFormula(), GetPositions());
if (playerIndexes.Count > 0)
{
String tmp = "";
int temp_i = 0;
StringBuilder sb = new StringBuilder(30 * playerIndexes.Count);
int p = 0;
string attr = mAttributeComboBox.Items[mAttributeComboBox.SelectedIndex].ToString();
if (mIncreaseYearsPro)
attr = "YearsPro";
string val = intAttrControl1.Visible ? intAttrControl1.Value + "" : stringSelectionControl1.Value;
sb.Append("Team,FirstName,LastName,");
sb.Append(attr);
sb.Append("\n");
for (int i = 0; i < playerIndexes.Count; i++)
{
p = playerIndexes[i];
if (applyChanges)
{
if (mIncreaseYearsPro)
{
tmp = Tool.GetPlayerField(p,attr);
temp_i = Int32.Parse(tmp);
temp_i++;
val = temp_i.ToString();
}
else if (intAttrControl1.Visible && mPercentCheckbox.Checked)
{
tmp = Tool.GetPlayerField(p, attr);
temp_i = Int32.Parse(tmp);
val = ((int)(temp_i * (intAttrControl1.Value * 0.01))).ToString();
}
this.Tool.SetPlayerField(p, attr, val);
}

sb.Append(Tool.GetPlayerTeam(p));
sb.Append(",");
sb.Append(Tool.GetPlayerFirstName(p));
sb.Append(",");
sb.Append(Tool.GetPlayerLastName(p));
sb.Append(",");
sb.Append(Tool.GetPlayerField(p, attr));
sb.Append("\n");
}
MessageForm.ShowMessage("Players affected = "+playerIndexes.Count, sb.ToString(), SystemIcons.Question, false, true);
}
else
{
MessageBox.Show(this,
MessageBox.Show(this,
"Check formula and Positions Check boxes ",
"No Players"
);
}
}
catch (Exception ex)
else if (results.StartsWith("Exception!"))
{
MessageBox.Show(this,
"Ensure the formula is correct: " + GetFormula() + "\n" + ex.Message,
results,
"Error, Check formula"
);
}
else
{
MessageForm.ShowMessage("Affected Players", results, SystemIcons.Question, false, false);
}
}

//private string GetPlayerText(List<int> playerIndexes)
//{
// int p = 0;
// StringBuilder sb = new StringBuilder();
// sb.Append("Number of players = ");
// sb.Append(playerIndexes.Count);
// sb.Append("\n");
// for (int i = 0; i < playerIndexes.Count; i++)
// {
// p = playerIndexes[i];
// sb.Append(Tool.GetPlayerTeam(p));
// sb.Append(",");
// sb.Append(Tool.GetPlayerFirstName(p));
// sb.Append(",");
// sb.Append(Tool.GetPlayerLastName(p));
// sb.Append("\n");
// }
// return sb.ToString();
//}

private List<int> GetPlayerIntexesFor(string formula, List<string> positions)
private string GetTargetAttribute()
{
List<int> playerIndexes = Tool.GetPlayersByFormula(formula, positions);
return playerIndexes;
string attr = mAttributeComboBox.Items[mAttributeComboBox.SelectedIndex].ToString();// GetTargetAttribute(); //include line below
return attr;
}

private string GetTargetValue()
{
string val = intAttrControl1.Visible ? intAttrControl1.Value + "" : stringSelectionControl1.Value; // GetTargetValue();
return val;
}

private void mCheckAllButton_Click(object sender, EventArgs e)
{
bool check = !mQBcheckBox.Checked;
Expand Down Expand Up @@ -242,8 +186,10 @@ private void mFormulaButton_Click(object sender, EventArgs e)
=
<> (Not Equal)
Check the Console output to see the formula the editor uses.
You can use these formulas in the Main Text area as well.
";
MessageForm.ShowMessage("Usage", howTo, SystemIcons.Question, false, true);
MessageForm.ShowMessage("Usage", howTo, SystemIcons.Question, false, false);
}

}
Expand Down
Binary file added ICSharpCode.SharpZipLib.dll
Binary file not shown.
Loading

0 comments on commit b3b8609

Please sign in to comment.