public bool RemoveSpellFromCharacter(Character character, int spellLevel, int spellIndex) { using var db = new PathfinderContext(); // KnownSpell spell = GetSpellFromSpellbook(character, spellLevel, spellIndex); Console.WriteLine("--------Trying to remove a spell to the character {0}", character.Name); //TODO Authorisation var query = from knownSpell in db.KnownSpells where knownSpell.CharacterId.Equals(character.Id) && knownSpell.SpellId.Equals(spellIndex) && knownSpell.SpellLevel.Equals(spellLevel) select knownSpell; if (query.Count() == 0 || query.First() == null) { Console.WriteLine("No spell found for that character."); return(false); } KnownSpell spell = query.First(); if (spell == null) { return(false); } db.KnownSpells.Remove(spell); db.SaveChanges(); return(true); }
public ActionResult GetSpecificSpell(int characterid, int spellLevel, int spellIndex) { Character character = GetCharacter(characterid).Value; if (character == null) { return(NotFound("No Character with this id")); } KnownSpell spell = ds.GetSpellFromSpellbook(character, spellLevel, spellIndex); if (spell == null) { return(NotFound(String.Format("Spell with index {0} was not found for {1}", spellIndex, character.Name))); } /* * if (character.SpellBook == null) return NotFound("This character doesn't have a spellbook"); * * if (spellLevel > character.SpellBook.SpellLevels.Length || spellLevel < 0) return BadRequest("spell level is out of bounds"); * * SpellLevel spellbookLevel = character.SpellBook.SpellLevels[spellLevel]; * * if (spellbookLevel == null) return NotFound(String.Format("{0} doesn't have any spells for {1} level spells", character.Name, spellLevel)); * * if (spellIndex > spellbookLevel.Spells.Count || spellIndex < 0) * return BadRequest(String.Format("spell with index {0} was not found", spellIndex)); * * KnownSpell spell = spellbookLevel.Spells[spellIndex]; */ return(Ok(spell)); }
/// <summary> /// Adds a spell to the character spell relation. /// </summary> /// <param name="character">The character that will learn the spell</param> /// <param name="spellId">The ID of the spell to be taught</param> /// <param name="spellLevel">The level the spell is taught at</param> /// <returns></returns> public KnownSpell AddSpellToCharacter(Character character, int spellId, int spellLevel) //, string note=null) { using var db = new PathfinderContext(); Console.WriteLine("--------Trying to add a spell to the character {0}", character.Name); //TODO Authorisation var spell = GetSpell(spellId); if (spell == null) { Console.WriteLine("Error: Spell not Found"); return(null); } Console.WriteLine("spell found! Trying to add {0}", spell.Name); var query = from knownSpell in db.KnownSpells where knownSpell.CharacterId.Equals(character.Id) && knownSpell.SpellId.Equals(spellId) select knownSpell; // Console.WriteLine("The query gave the following result for spell: {0}, level {}", query.FirstOrDefault().SpellId, query.FirstOrDefault().SpellLevel); if (query.Count() != 0) { Console.WriteLine("Character already knows this spell!"); //Change spell level? return(null); } Console.WriteLine("Spell is not already in use! Adding to character"); KnownSpell newSpell = new KnownSpell() { CharacterId = character.Id, SpellId = spell.Id, Spell = spell, SpellLevel = spellLevel, Prepared = null, Note = null }; db.KnownSpells.Add(newSpell); db.SaveChanges(); return(newSpell); }
public ActionResult AddSpell(int characterid, [FromBody] SpellToAddDTO _newSpell) { Character character = GetCharacter(characterid).Value; if (character == null) { return(NotFound("Character not found")); } KnownSpell newSpell = ds.AddSpellToCharacter(character, _newSpell.SpellId, _newSpell.SpellLevel); if (newSpell == null) { return(BadRequest(String.Format("{0} already knows this spell, or the spell doesn't exist", character.Name))); } int spellIndex = character.Spellbook.SpellLevels[_newSpell.SpellLevel].Spells.Count(); return(CreatedAtRoute(nameof(GetSpecificSpell), new { characterid = character.Id, _newSpell.SpellLevel, spellIndex }, newSpell)); }
/// <summary> /// Finds the index in which a spell is stored in the spellbook based on the spellId /// </summary> /// <param name="character"></param> /// <param name="spellLevel">The spell level you want to find the spell in</param> /// <param name="spellIndex">The index of the spell</param> /// <returns>The index of the spell</returns> public KnownSpell GetSpellFromSpellbook(Character character, int spellLevel, int spellIndex) { Spellbook spellbook = GetSpellBook(character); if (spellLevel > spellbook.SpellLevels.Length || spellLevel < 0) { return(null); } SpellLevel curLevel = spellbook.SpellLevels[spellLevel]; if (spellIndex >= curLevel.Spells.Count || spellIndex < 0) { return(null); } KnownSpell spell = curLevel.Spells[spellIndex]; if (spell == null) { return(null); } return(spell); }