Skip to content

Commit

Permalink
Added combat manager service and moved core functionality to Core Lib…
Browse files Browse the repository at this point in the history
…rary. sander1095#5
  • Loading branch information
herrozerro committed Oct 9, 2019
1 parent 02a1371 commit 4b89c86
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 88 deletions.
113 changes: 113 additions & 0 deletions src/DnDCombatTracker.Core/CombatManagerService.cs
Original file line number Diff line number Diff line change
@@ -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<Character> Combatants { get; set; } = new List<Character>();

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();
}
}
}
123 changes: 35 additions & 88 deletions src/DnDCombatTracker.Forms/OverviewForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ namespace DnDCombatTracker
{
public partial class OverviewForm : Form
{
private BindingList<Character> CharacterList { get; set; }
private Dictionary<CheckBox, Conditions> ConditionsDictionary;

private CheckBox[] ListDeathSavesSuccess;
private CheckBox[] ListDeathSavesFail;
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<Character>();
InitiativeList.DataSource = CharacterList;
InitiativeList.DataSource = _combatManagerService.Combatants;

ListDeathSavesSuccess = new[] { Success1ListCheckbox, Success2ListCheckbox, Success3ListCheckbox };
ListDeathSavesFail = new[] { Fail1ListCheckbox, Fail2ListCheckbox, Fail3ListCheckbox };
Expand Down Expand Up @@ -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
{
Expand All @@ -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)
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}
}
Expand All @@ -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<Character>(items);
_combatManagerService.SortCombatants();

InitiativeList.DataSource = null;
InitiativeList.DataSource = CharacterList;
InitiativeList.DataSource = _combatManagerService.Combatants;

InitiativeList.SelectedItem = currentSelected ?? CharacterList[0];
InitiativeList.SelectedItem = currentSelected ?? _combatManagerService.CurrentCharacter;

}

Expand All @@ -425,7 +372,7 @@ private void Sort()
/// <param name="e"></param>
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);
}
Expand Down

0 comments on commit 4b89c86

Please sign in to comment.