public static HerosLocations GenInitialLocation(my_appContext _context, int heroID, int locationID = 1) { var locSk = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == locationID); if (locSk == null) { throw new Exception("Starting in unknown location."); } string Description = ""; int LocationType = locSk.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(locSk.Sketch); Description = JsonConvert.SerializeObject(description.InitializeLocation()); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(locSk.Sketch); Description = JsonConvert.SerializeObject(description.InitializeLocation()); } return(new HerosLocations() { Description = Description, HeroId = heroID, LocationIdentifier = locationID, LocationId = 1000 * heroID + locationID, }); }
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 int StartFight(my_appContext _context, Heros hero, int enemyID) { if (hero.Status == 0) { var enemy = _context.Enemies.FirstOrDefault(e => e.EnemyId == enemyID); if (enemy == null) { throw new OperationException("fightErr", "Unknown emeny to enter battle."); } Fighting fight = new Fighting() { EnemyHp = enemy.MaxHp, EnemyId = enemy.EnemyId, HeroId = hero.HeroId, IsOver = false, Loot = null, Initiative = 0, }; _context.Fighting.Add(fight); hero.Status = 3; try { _context.SaveChanges(); } catch (DbUpdateException) { throw new OperationException("databaseErr", "Failed to add healing."); } } else { throw new OperationException("LocationErr", "Hero is not able to change state."); } return(0); }
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 FightResult GenResult(my_appContext _context, Heros hero) { Enemies enemy = _context.Enemies.FirstOrDefault(e => e.EnemyId == this.EnemyId); if (enemy == null) { throw new OperationException("fightingErr", "Unknown enemy."); } // TODO item? return(GenResult(_context, hero, enemy)); }
public static int MoveHeroToLocation(my_appContext _context, Heros hero, int locationID) { if (hero.Status != 0) { throw new Exception("Hero is not able to move."); } var globalStatus = _context.HerosLocations.FirstOrDefault(e => e.HeroId == hero.HeroId && e.LocationIdentifier == locationID); if (globalStatus == null) { HerosLocations location = HerosLocations.GenInitialLocation(_context, hero.HeroId, locationID); _context.HerosLocations.Add(location); } hero.CurrentLocation = locationID; return(0); }
public static ActionToken GenerateActionToken(int heroid, my_appContext _context) { DateTime time = DateTime.UtcNow; ActionToken token; if ((token = _context.ActionToken.FirstOrDefault(e => e.HeroId == heroid)) == null) { token = ActionToken.GenToken(heroid, time); _context.ActionToken.Add(token); } else { ActionToken ntoken = ActionToken.GenToken(heroid, time); token.HashedToken = ntoken.HashedToken; token.ExpireDate = ntoken.ExpireDate; } return(token); }
public static UserToken GenerateUsersToken(string username, my_appContext _context) { DateTime time = DateTime.UtcNow; UserToken token; if ((token = _context.UserToken.FirstOrDefault(e => e.UserName == username)) == null) { token = UserToken.GenToken(username, time); _context.UserToken.Add(token); } else { UserToken ntoken = UserToken.GenToken(username, time); token.HashedToken = ntoken.HashedToken; token.ExpireDate = ntoken.ExpireDate; } return(token); }
public static (int hp, int sl) HPSLmax(Heros hero, my_appContext _context) { Equipment Equipment = _context.Equipment.FirstOrDefault(e => e.HeroId == hero.HeroId); List <Items> itemsOn = new List <Items>(); List <int?> itemsPossible = new List <int?>() { Equipment.Armour, Equipment.Bracelet, Equipment.FirstHand, Equipment.Gloves, Equipment.Helmet, Equipment.Neckles, Equipment.Ring1, Equipment.Ring2, Equipment.SecondHand, Equipment.Shoes, Equipment.Trousers }; int[] attributes = new int[8] { hero.Strength, hero.Endurance, hero.Dexterity, hero.Reflex, hero.Wisdom, hero.Intelligence, hero.Charisma, hero.Willpower }; foreach (int?it in itemsPossible) { if (it.HasValue) { var found = _context.Items.FirstOrDefault(e => e.ItemId == it.Value); if (found == null) { throw new Exception("Unknown item in equipment"); } itemsOn.Add(found); } } foreach (Items it in itemsOn) { int[] itAttr = new int[8] { it.Strength, it.Endurance, it.Dexterity, it.Reflex, it.Wisdom, it.Intelligence, it.Charisma, it.Willpower }; for (int i = 0; i < 8; i++) { attributes[i] += itAttr[i]; } } return(BaseHP(hero.Lvl) + (int)Math.Ceiling(attributes[1] / 2.0), (int)Math.Ceiling(hero.Slbase * (1.0 + attributes[4] / 100))); }
public static LocationResult <InstanceNodeResult> InstanceClearCurrent(my_appContext _context, Heros hero) { 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."); } int LocationType = descr.LocationGlobalType; if (LocationType != 2) { throw new OperationException("locationErr", "Fight outside the location"); } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; if (hero.Hp > 0) { state.IsCleared[state.CurrentLocation] = true; location.Description = JsonConvert.SerializeObject(state); } return(description.GenLocalForm(state)); } }
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 ActionToken CheckActionToken(my_appContext context, ActionTokenResult token, int HeroID) { return(context.ActionToken.FirstOrDefault(e => (e.HeroId == HeroID && e.HashedToken == token.Token))); }
public static UserToken CheckUserToken(my_appContext context, UserTokenResult token) { return(context.UserToken.FirstOrDefault(e => (e.UserName == token.UserName && e.HashedToken == token.Token))); }
public static Fighter GenHeroFightStats(Heros hero, my_appContext _context) { Equipment Equipment = _context.Equipment.FirstOrDefault(e => e.HeroId == hero.HeroId); List <Items> itemsOn = new List <Items>(); List <int?> itemsPossible = new List <int?>() { Equipment.Armour, Equipment.Bracelet, Equipment.FirstHand, Equipment.Gloves, Equipment.Helmet, Equipment.Neckles, Equipment.Ring1, Equipment.Ring2, Equipment.SecondHand, Equipment.Shoes, Equipment.Trousers }; int[] attributes = new int[8] { hero.Strength, hero.Endurance, hero.Dexterity, hero.Reflex, hero.Wisdom, hero.Intelligence, hero.Charisma, hero.Willpower }; int AttackMin = 0; int AttackMax = 0; int Armour = 0; foreach (int?it in itemsPossible) { if (it.HasValue) { var found = _context.Items.FirstOrDefault(e => e.ItemId == it.Value); if (found == null) { throw new Exception("Unknown item in equipment"); } itemsOn.Add(found); } } foreach (Items it in itemsOn) { int[] itAttr = new int[8] { it.Strength, it.Endurance, it.Dexterity, it.Reflex, it.Wisdom, it.Intelligence, it.Charisma, it.Willpower }; for (int i = 0; i < 8; i++) { attributes[i] += itAttr[i]; } AttackMin += it.DmgMin; AttackMax += it.DmgMax; Armour += it.Armour; } AttackMin += (int)Math.Ceiling(attributes[0] * 0.1); AttackMax += (int)Math.Ceiling(attributes[0] * 0.1); Armour += (int)Math.Floor(attributes[1] * 0.1); return(new Fighter() { Strength = attributes[0], Endurance = attributes[1], Dexterity = attributes[2], Reflex = attributes[3], Wisdom = attributes[4], Intelligence = attributes[5], Charisma = attributes[6], Willpower = attributes[7], AttackMax = AttackMax, AttackMin = AttackMin, Armour = Armour, Hp = hero.Hp, }); }
public static int MoveHeroToGlobal(my_appContext _context, Heros hero, int locationID) { MoveHeroToLocation(_context, hero, 0); return(0); }
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, }); }