public FightResult GenResult(my_appContext _context, Heros hero, Enemies enemy) { ItemResult resItem = null; int Exp = 0; if (this.IsOver && this.Loot.HasValue) { var ItemEq = _context.Items.FirstOrDefault(e => e.ItemId == this.Loot.Value); if (ItemEq == null) { throw new OperationException("lootErr", "Unknown looted item."); } resItem = (ItemResult)ItemEq; } if (this.IsOver && this.EnemyHp <= 0) { Exp = HeroCalculator.ExpForFight(hero.Lvl, enemy.Lvl); } return(new FightResult() { EnemyID = enemy.GraphicsId, EnemyLevel = enemy.Lvl, Hp = this.EnemyHp, HpMax = enemy.MaxHp, IsOver = this.IsOver, Loot = resItem, EnemyName = enemy.EnemyName, Log = new BattleLog[0], Experience = Exp, }); }
public static BattleLog[] MakeTurn(Fighting initial, Heros hero, Enemies enemy, AttackType action, my_appContext _context) { Fighter ally = HeroCalculator.GenHeroFightStats(hero, _context); Fighter vict = enemy.GetFighter(initial.EnemyHp); List <BattleLog> log = new List <BattleLog>(); int dmg = DmgDealt(ally, vict, action); log.Add(new BattleLog() { Damage = dmg, Target = 1, AttackType = action }); if (dmg >= vict.Hp) { initial.IsOver = true; initial.EnemyHp = 0; initial.Loot = GenerateLoot(enemy); return(log.ToArray()); } else { vict.Hp -= dmg; initial.Initiative -= InitiativeCost(ally, action); double armourMod = (action == AttackType.Defense) ? 1.4 : 1.0; int it = 0; while (initial.Initiative < 0) { int rand = Rand.Next(0, 10); AttackType aType = (rand == 0) ? AttackType.Strong : AttackType.Normal; dmg = DmgDealt(vict, ally, aType, armourMod); log.Add(new BattleLog() { Damage = dmg, Target = 0, AttackType = aType }); if (dmg >= hero.Hp) { initial.IsOver = true; hero.Hp = 0; initial.EnemyHp = vict.Hp; return(log.ToArray()); } else { hero.Hp -= dmg; initial.Initiative += InitiativeCost(vict, aType); } it++; if (it > 10) { throw new OperationException("battleErr", "Enemy is attacking in infinite loop."); } } } initial.EnemyHp = vict.Hp; return(log.ToArray()); }
public static bool CheckHeroHP(Heros hero, my_appContext _context) { var(hp, sl) = HeroCalculator.HPSLmax(hero, _context); if (hero.Hp > hp) { hero.Hp = hp; return(false); } return(true); }
public HeroResult GenResult(EquipmentResult eqRes, object locRes, object statusData = null) { return(new HeroResult() { Name = this.Name, Nickname = this.Nickname, Orders = this.Orders, Level = this.Lvl, Attributes = new int[8] { this.Strength, this.Endurance, this.Dexterity, this.Reflex, this.Wisdom, this.Intelligence, this.Charisma, this.Willpower }, Exp = this.Experience, Hp = this.Hp, Hpmax = HeroCalculator.BaseHP(this.Lvl), Sl = this.Sl, Slmax = this.Slbase, Equipment = eqRes, Location = locRes, Status = this.Status, StatusData = statusData, IsInvitational = this.Invitational, VelocityFactor = this.VelocityFactor, }); }
public static GeneralStatus GetHeroGeneralStatus(my_appContext _context, Heros hero, DateTime now) { object statusData = null; var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { throw new Exception("Location is not available."); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { throw new Exception("LocationData is not available."); } // TODO Location Type object locationResult = new LocationResult <object>(); int LocationType = descr.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(descr.Sketch); LocationState state = JsonConvert.DeserializeObject <LocationState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Status == 1) { Traveling travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (travel == null) { throw new Exception("Traveling hero without travel in DB."); } if (travel.HasEnded(now)) { state = description.MoveTo(travel.UpdatedLocationID(), state); hero.Status = 0; location.Description = JsonConvert.SerializeObject(state); _context.Traveling.Remove(travel); try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove travel."); } } else { statusData = travel.GenTravelResult(now); } } locationResult = description.GenLocalForm(state); } if (hero.Status == 2) { Healing heal = _context.Healing.FirstOrDefault(e => e.HeroId == hero.HeroId); if (heal == null) { throw new Exception("Healing hero without heal in DB."); } if (heal.HasEnded(now)) { int newHP = heal.FinalHealth(now); hero.Hp = newHP; HeroCalculator.CheckHeroHP(hero, _context); _context.Healing.Remove(heal); hero.Status = 0; try { _context.SaveChanges(); } catch (DbUpdateException) { throw new Exception("Failed to remove healing."); } } else { statusData = heal.GenHealingResult(now); } } if (hero.Status == 3) { Fighting fight = _context.Fighting.FirstOrDefault(e => e.HeroId == hero.HeroId); if (fight == null) { throw new Exception("Healing hero without heal in DB."); } statusData = fight.GenResult(_context, hero); } return(new GeneralStatus() { HeroStatus = hero.Status, Location = locationResult, StatusData = statusData, }); }