diff --git a/src/DnDCombatTracker.Core/CombatManagerService.cs b/src/DnDCombatTracker.Core/CombatManagerService.cs new file mode 100644 index 0000000..e75edc0 --- /dev/null +++ b/src/DnDCombatTracker.Core/CombatManagerService.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace DnDCombatTracker.Core +{ + public class CombatManagerService + { + public int RoundCount { get; set; } + + public List Combatants { get; set; } = new List(); + + public Character CurrentCharacter { get; set; } + + public Character NextCharacter + { + get + { + if (CurrentCharacter == null) + { + return null; + } + + var currentindex = Combatants.IndexOf(CurrentCharacter); + + if (currentindex + 1 == Combatants.Count || currentindex == -1) + { + return Combatants.First(); + } + else + { + return Combatants[Combatants.IndexOf(CurrentCharacter) + 1]; + } + + + } + } + + + public void SortCombatants() + { + Combatants = Combatants.OrderByDescending(x => x.Initiative).ToList(); + } + + public void AdvanceTurn() + { + if (Combatants.Count < 2) + { + return; + } + + Character currentCharacter = Combatants.Single(x => x.Name == CurrentCharacter.Name);//InitiativeList.SelectedValue as Character; + int currentIndex = Combatants.IndexOf(currentCharacter); + int indexToSet = currentIndex + 1 == Combatants.Count ? 0 : currentIndex + 1; //Check if wrap around is needed + + Character newCharacter = Combatants[indexToSet]; + Character newNextCharacter = Combatants[indexToSet + 1 == Combatants.Count ? 0 : indexToSet + 1]; + + if (indexToSet + 1 == Combatants.Count) + { + RoundCount++; + } + + CurrentCharacter = newCharacter; + } + + public void SaveCharacter(Character character) + { + if (Combatants.Any(x=>x.Name == character.Name)) + { + Combatants[Combatants.IndexOf(Combatants.ToList().Single(x => x.Name == character.Name))] = character; + } + else + { + Combatants.Add(character); + } + + if (Combatants.Count == 1) + { + CurrentCharacter = Combatants.First(); + } + + SortCombatants(); + } + + public void DeleteCharacter(Character character) + { + var characterToDelete = Combatants.FirstOrDefault(x => x.Name == character.Name); + + if (characterToDelete == null) + { + return; + } + + var currentCharacterIndex = Combatants.IndexOf(characterToDelete); + + Combatants.Remove(characterToDelete); + + if (Combatants.Count == 0) + { + CurrentCharacter = null; + return; + } + + int newIndex = Math.Max((Combatants.Count - 1) <= currentCharacterIndex ? currentCharacterIndex - 1 : currentCharacterIndex,0); + + CurrentCharacter = Combatants[newIndex]; + + SortCombatants(); + } + } +} diff --git a/src/DnDCombatTracker.Forms/OverviewForm.cs b/src/DnDCombatTracker.Forms/OverviewForm.cs index 21ec6a4..a0b51ad 100644 --- a/src/DnDCombatTracker.Forms/OverviewForm.cs +++ b/src/DnDCombatTracker.Forms/OverviewForm.cs @@ -12,7 +12,6 @@ namespace DnDCombatTracker { public partial class OverviewForm : Form { - private BindingList CharacterList { get; set; } private Dictionary ConditionsDictionary; private CheckBox[] ListDeathSavesSuccess; @@ -20,18 +19,21 @@ public partial class OverviewForm : Form private CheckBox[] DetailDeathSavesSuccess; private CheckBox[] DetailDeathSavesFail; + private CombatManagerService _combatManagerService { get; set; } + public OverviewForm() { InitializeComponent(); + _combatManagerService = new CombatManagerService(); + if (ApplicationDeployment.IsNetworkDeployed) { this.Text = string.Format("D&D 5e Combat Tracker - v{0}", ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4)); } - CharacterList = new BindingList(); - InitiativeList.DataSource = CharacterList; + InitiativeList.DataSource = _combatManagerService.Combatants; ListDeathSavesSuccess = new[] { Success1ListCheckbox, Success2ListCheckbox, Success3ListCheckbox }; ListDeathSavesFail = new[] { Fail1ListCheckbox, Fail2ListCheckbox, Fail3ListCheckbox }; @@ -64,23 +66,13 @@ public OverviewForm() private void NextButton_Click(object sender, EventArgs e) { - if (CharacterList.Count < 2) - { - return; - } - - Character currentCharacter = CharacterList.ToList().Single(x => x.Name == NamePlaceholderLabel.Text);//InitiativeList.SelectedValue as Character; - int currentIndex = CharacterList.IndexOf(currentCharacter); - int indexToSet = currentIndex + 1 == CharacterList.Count ? 0 : currentIndex + 1; //Check if wrap around is needed + _combatManagerService.AdvanceTurn(); - Character newCharacter = CharacterList[indexToSet]; - Character newNextCharacter = CharacterList[indexToSet + 1 == CharacterList.Count ? 0 : indexToSet + 1]; + UpdateDetailsTab(_combatManagerService.CurrentCharacter); - UpdateDetailsTab(newCharacter); - - if (CharacterList.Count > 1) + if (_combatManagerService.Combatants.Count > 1) { - UpdateNextTab(newNextCharacter); + UpdateNextTab(_combatManagerService.NextCharacter); } else { @@ -91,69 +83,37 @@ private void NextButton_Click(object sender, EventArgs e) NextTempHPPlaceholderLabel.Text = string.Empty; } + + //Change the index of the InitiativeList so that event will automatically update the Information tab and stuff - InitiativeList.SelectedIndex = indexToSet; + InitiativeList.SelectedIndex = _combatManagerService.Combatants.IndexOf(_combatManagerService.CurrentCharacter); } private void DeleteButton_Click(object sender, EventArgs e) { - if (CharacterList.Count == 2) - { - CharacterList.Remove(InitiativeList.SelectedValue as Character ?? CharacterList[0]); + + //calculate new index + int currentIndex = InitiativeList.SelectedIndex; + //remove selected character and update listbox + _combatManagerService.DeleteCharacter(InitiativeList.SelectedValue as Character ?? _combatManagerService.Combatants.FirstOrDefault()); + InitiativeList.DataSource = null; + InitiativeList.DataSource = _combatManagerService.Combatants; + InitiativeList.SelectedItem = _combatManagerService.CurrentCharacter; + + if (_combatManagerService.Combatants.Count != 0) + { Character newSelectedCharacter = InitiativeList.SelectedValue as Character; UpdateDetailsTab(newSelectedCharacter); UpdateInfoTab(newSelectedCharacter); - - UpdateNextTab(null); - + UpdateNextTab(_combatManagerService.CurrentCharacter == _combatManagerService.NextCharacter ? null : _combatManagerService.NextCharacter); } - else if (CharacterList.Count == 1) + else { - CharacterList.Remove(InitiativeList.SelectedValue as Character ?? CharacterList[0]); - UpdateDetailsTab(null); UpdateNextTab(null); } - else if (CharacterList.Count != 0) - { - CharacterList.Remove(InitiativeList.SelectedValue as Character ?? CharacterList[0]); - - - var characterDetail = CharacterList.SingleOrDefault(x => x.Name == NamePlaceholderLabel.Text); - var characterNext = CharacterList.SingleOrDefault(x => x.Name == NextNamePlaceholderLabel.Text); - if (characterDetail == null) - { - //We are deleting the item displayed in the detail tab. - //Update that! - Character newSelectedCharacter = InitiativeList.SelectedValue as Character; - UpdateDetailsTab(newSelectedCharacter); - - int currentIndex = CharacterList.IndexOf(newSelectedCharacter); - int newIndex = currentIndex + 1 == CharacterList.Count ? 0 : currentIndex + 1; - UpdateNextTab(CharacterList[newIndex]); - - UpdateInfoTab(newSelectedCharacter); - } - //We are not deleting the current item - else if (characterNext == null) //Are we deleting the next item? If so, we should update the next item! - { - //We are deleting the next item, we should update that to the new next item! - int currentIndex = CharacterList.IndexOf(CharacterList.Single(x => x.Name == NamePlaceholderLabel.Text)); - int newIndex = currentIndex + 1 == CharacterList.Count ? 0 : currentIndex + 1; - UpdateNextTab(CharacterList[newIndex]); - } - else - { - //Update the selected item - InitiativeList.SelectedItem = CharacterList.Single(y => y.Name == NamePlaceholderLabel.Text); - UpdateInfoTab(InitiativeList.SelectedItem as Character); - - } - //Update the selected item - InitiativeList.SelectedItem = CharacterList.Single(x => x.Name == NamePlaceholderLabel.Text); - } } private void SaveButton_Click(object sender, EventArgs e) @@ -362,16 +322,7 @@ where item.Key.Checked Character character = new Character(name, initiative, maxHP, hp, tempHP, fails, successes, chosenConditions, notes); //Update or insert the character - if (CharacterList.ToList().Exists(x => x.Name == name)) - { - //Update - CharacterList[CharacterList.IndexOf(CharacterList.ToList().Single(x => x.Name == name))] = character; - } - else - { - //New - CharacterList.Add(character); - } + _combatManagerService.SaveCharacter(character); Sort(); @@ -388,17 +339,15 @@ where item.Key.Checked } //Now update the next info thing - if (CharacterList.Count > 1) + if (_combatManagerService.Combatants.Count > 1) { //Get the index of the current detail character(It should be up to date as we just updated it if necesarry - Character current = CharacterList.Single(x => x.Name == NamePlaceholderLabel.Text); - int currentIndex = CharacterList.IndexOf(current); - - int nextIndex = currentIndex + 1 == CharacterList.Count ? 0 : currentIndex + 1; //Check if wrap around is needed + Character current = _combatManagerService.Combatants.Single(x => x.Name == NamePlaceholderLabel.Text); + int currentIndex = _combatManagerService.Combatants.IndexOf(current); - Character nextChar = CharacterList[nextIndex]; + int nextIndex = currentIndex + 1 == _combatManagerService.Combatants.Count ? 0 : currentIndex + 1; //Check if wrap around is needed - UpdateNextTab(nextChar); + UpdateNextTab(_combatManagerService.NextCharacter); } } } @@ -407,14 +356,12 @@ private void Sort() { var currentSelected = InitiativeList.SelectedItem as Character; - var items = CharacterList.ToList(); - items.Sort((a, b) => -1 * a.CompareTo(b)); - CharacterList = new BindingList(items); + _combatManagerService.SortCombatants(); InitiativeList.DataSource = null; - InitiativeList.DataSource = CharacterList; + InitiativeList.DataSource = _combatManagerService.Combatants; - InitiativeList.SelectedItem = currentSelected ?? CharacterList[0]; + InitiativeList.SelectedItem = currentSelected ?? _combatManagerService.CurrentCharacter; } @@ -425,7 +372,7 @@ private void Sort() /// private void InitiativeList_SelectedIndexChanged(object sender, EventArgs e) { - var character = InitiativeList.SelectedValue as Character ?? (CharacterList.Count > 0 ? CharacterList[0] : null); + var character = InitiativeList.SelectedValue as Character ?? (_combatManagerService.Combatants.Count > 0 ? _combatManagerService.Combatants.First() : null); UpdateInfoTab(character); }