private const string CharactersFile = "CharacterChreationModels.csv"; // the value inside a const will never change. public CharacterCreationModel SaveCharacter(CharacterCreationModel model) { // Load the textfile and convert the text to List<CharacterCreationModel> List <CharacterCreationModel> characters = CharactersFile.FullFilePath().LoadFile().ConvertToCharacterCreationModels(); // Fin the max ID int currentId = 1; if (characters.Count > 0) { currentId = characters.OrderByDescending(x => x.Id).First().Id + 1; } model.Id = currentId; //CODE ADDON currentId += 1; <--This code can be added if you want to write more than one record at the time to this List. // Add the new record with the new ID (max + 1) characters.Add(model); // Convert the Characters to list<string> // Save the list<string> to the text file characters.SaveToCharacterFile(CharactersFile); // The model is were we added the currentId to, and now, it's the CharacterCreationModel fully formed with that Id. return(model); }
private void saveCharacterButton_Click(object sender, EventArgs e) { if (ValidateForm()) { string k = "0"; // This uses the overloaded constructor in CharacterCreationModel and aplying it to the text boxes in the form. CharacterCreationModel model = new CharacterCreationModel( nameTextBox.Text, healthTextBox.Text, iniModTextBox.Text, noteTextBox.Text, k); // GlobalConfig.Connections.SaveCharacter(model); foreach (IDataConnection db in GlobalConfig.Connections) //This creates a list of IDataConnection connections. { // This passes in model and returns a new model. Which can be saved or ignored. db.SaveCharacter(model); } nameTextBox.Text = ""; healthTextBox.Text = "0"; iniModTextBox.Text = "0"; noteTextBox.Text = ""; if (RefreshList != null) { RefreshList(this, new EventArgs()); } } else { MessageBox.Show("This form is invalid"); } }
private void deleteCharacterButton_Click(object sender, EventArgs e) { CharacterCreationModel selected = (CharacterCreationModel)characterListBox.SelectedItem; int i = currentCharacters.IndexOf(selected); currentCharacters.RemoveAt(i); currentCharacters.SaveToCharacterFile(CharactersFile); ListWireUp(); }
private void initiativeListBox_SelectedIndexChanged(object sender, EventArgs e) { CharacterCreationModel cdisplay = (CharacterCreationModel)initiativeListBox.SelectedItem; if (cdisplay != null) { characterName.Text = cdisplay.CharName.ToString(); healthAmount.Text = cdisplay.CharHealth.ToString(); iniMod.Text = cdisplay.CharIniMod.ToString() + "+" + cdisplay.CharHiddenNr.ToString(); notes.Text = cdisplay.CharNote.ToString(); } }
private void removeCharacterButton_Click(object sender, EventArgs e) { CharacterCreationModel c = (CharacterCreationModel)initiativeListBox.SelectedItem; if (c != null) { selectedCharacters.Remove(c); availableCharacters.Add(c); WireUpList(); } }
private void addCharacterButton_Click(object sender, EventArgs e) { // This creates an instance of the selected character from CharacterCreationModel CharacterCreationModel c = (CharacterCreationModel)characterDropDown.SelectedItem; //This removes the instance c from the availableCharacters List and adds it to selectedCharacters List if (c != null) { availableCharacters.Remove(c); selectedCharacters.Add(c); WireUpList(); } }
/// <summary> /// Creates a new character. /// </summary> /// <param name="characterCreationModel">The character to create.</param> /// <returns>Info on success/failure of character creation.</returns> public async Task <ServiceResult> CreateCharacterAsync(CharacterCreationModel characterCreationModel) { try { var playerCharacter = _Mapper.Map <PlayerCharacter>(characterCreationModel); await _DatabaseContext.ExecuteAsync(async u => { var account = await u.AccountRepository.GetAccountByIdAsync(_SessionModel.Account.AccountId); playerCharacter.Account = account; await u.CharacterRepository.AddNewCharacterAsync(playerCharacter); }); return(new ServiceResult(true)); } catch (Exception ex) { _Logger.LogError(ex, "Failed to create character {0}", characterCreationModel); return(new ServiceResult <CharacterInfoModel[]>(false, "Server error.")); } }
private void rollInitiativeButton_Click(object sender, EventArgs e) { CharacterCreationModel Selected = (CharacterCreationModel)initiativeListBox.SelectedItem; int n = initiativeListBox.Items.Count; int y = initiativeListBox.Items.Count; Random rng = new Random(); int x; int z = 0; while (n > 0) { // This goes through all items in initiativeListBox and add a random number to CharHiddenNr based on CharIniMod. n--; CharacterCreationModel index = (CharacterCreationModel)initiativeListBox.Items[z]; x = rng.Next(y + index.CharIniMod); index.CharHiddenNr = x; z++; } }
//this is gonna take in a list of string from the lines from the text files. It's gonna spit out a list of CharacterCreationModel. Extension Method public static List <CharacterCreationModel> ConvertToCharacterCreationModels(this List <string> lines) { List <CharacterCreationModel> output = new List <CharacterCreationModel>(); // our output // This is gonna loop through every line in our text file. If there is no lines: "if (!File.Exists(file))" as above, it's gonna skip through the foreach loop. foreach (string line in lines) { string[] colums = line.Split(','); // for each line we are gonna have a comma seperate it. Split the line on line values and thats gonna put it in an array called colums. CharacterCreationModel cc = new CharacterCreationModel(); cc.Id = int.Parse(colums[0]); // this is anly gonna take intergers, so if it's given anything else, it's gonna crash. cc.CharName = colums[1]; cc.CharHealth = int.Parse(colums[2]); cc.CharIniMod = int.Parse(colums[3]); cc.CharNote = colums[4]; cc.CharHiddenNr = int.Parse(colums[5]); output.Add(cc); } return(output); }
// Integer private void addDamageButton_Click(object sender, EventArgs e) { CharacterCreationModel damage = (CharacterCreationModel)initiativeListBox.SelectedItem; int x; if (damage != null) { if (validateForm()) { x = Int32.Parse(damageTextBox.Text); // set CharHealth to ("currentHealth" - "CurrentDamage") damage.CharHealth = (damage.CharHealth - x); damageTextBox.Text = "0"; //initiativeListBox.Select(); } } else { MessageBox.Show("This form is invalid"); } }
private void sortByIniModButton_Click(object sender, EventArgs e) { string tempName; int tempHealth; int tempIniMod; string tempNote; int tempHiddenNr; for (int i = 0; i < selectedCharacters.Count; i++) { CharacterCreationModel index1 = (CharacterCreationModel)selectedCharacters[i]; for (int q = i + 1; q < selectedCharacters.Count; q++) { CharacterCreationModel index2 = (CharacterCreationModel)selectedCharacters[q]; int indexi = index1.CharIniMod + index1.CharHiddenNr; int indexq = index2.CharIniMod + index2.CharHiddenNr; if (indexi <= indexq) { if (indexi == indexq) { if (index1.CharIniMod < index2.CharIniMod) { tempName = selectedCharacters[i].CharName; tempHealth = selectedCharacters[i].CharHealth; tempIniMod = selectedCharacters[i].CharIniMod; tempNote = selectedCharacters[i].CharNote; tempHiddenNr = selectedCharacters[i].CharHiddenNr; index1.CharName = selectedCharacters[q].CharName; index1.CharHealth = selectedCharacters[q].CharHealth; index1.CharIniMod = selectedCharacters[q].CharIniMod; index1.CharNote = selectedCharacters[q].CharNote; index1.CharHiddenNr = selectedCharacters[q].CharHiddenNr; index2.CharName = tempName; index2.CharHealth = tempHealth; index2.CharIniMod = tempIniMod; index2.CharNote = tempNote; index2.CharHiddenNr = tempHiddenNr; } } else { tempName = selectedCharacters[i].CharName; tempHealth = selectedCharacters[i].CharHealth; tempIniMod = selectedCharacters[i].CharIniMod; tempNote = selectedCharacters[i].CharNote; tempHiddenNr = selectedCharacters[i].CharHiddenNr; index1.CharName = selectedCharacters[q].CharName; index1.CharHealth = selectedCharacters[q].CharHealth; index1.CharIniMod = selectedCharacters[q].CharIniMod; index1.CharNote = selectedCharacters[q].CharNote; index1.CharHiddenNr = selectedCharacters[q].CharHiddenNr; index2.CharName = tempName; index2.CharHealth = tempHealth; index2.CharIniMod = tempIniMod; index2.CharNote = tempNote; index2.CharHiddenNr = tempHiddenNr; } WireUpList(); } } } }