public async Task <IActionResult> PostHeros([FromBody] PassedData <HeroPassed> data) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DateTime now = DateTime.UtcNow; if (data.UserToken == null) { return(BadRequest(new DataError("securityErr", "No authorization controll."))); } UserToken dbtoken = Security.CheckUserToken(this._context, data.UserToken); if (dbtoken == null) { return(BadRequest(new DataError("securityErr", "Your data has probably been stolen or modified manually. We suggest password's change."))); } else { if (!dbtoken.IsTimeValid(now)) { return(BadRequest(new DataError("timeoutErr", "You have been too long inactive. Relogin is required."))); } else { dbtoken.UpdateToken(now); } } int currheros = this._context.UsersHeros.Where(e => e.UserName == dbtoken.UserName).Count(); if (currheros >= ServerOptions.MaxHerosPerAccount) { return(BadRequest(new DataError("herolimitErr", "You have reached maximum amount of heros per account."))); } int ID = this._context.Heros.Select(x => x.HeroId).DefaultIfEmpty(0).Max(); Heros newly = new Heros() { Charisma = data.Data.Attributes[6], Country = data.Data.Country, // starting location of type?? CurrentLocation = 1, Dexterity = data.Data.Attributes[2], Endurance = data.Data.Attributes[1], Experience = 0, HeroId = ID + 1, Hp = HeroCalculator.PureMaxHP(HeroCalculator.BaseHP(1), data.Data.Attributes), Intelligence = data.Data.Attributes[5], Lvl = 1, Name = data.Data.Name, Nickname = data.Data.Nickname, Orders = 0, Origin = data.Data.Origin, Reflex = data.Data.Attributes[3], Sl = 0, Slbase = 0, Status = 0, Strength = data.Data.Attributes[0], Willpower = data.Data.Attributes[7], Wisdom = data.Data.Attributes[4], Invitational = true, VelocityFactor = 1, }; UsersHeros userheros = new UsersHeros() { HeroId = newly.HeroId, UserName = dbtoken.UserName, }; Equipment eq = Equipment.GenFreshEquipment(newly.HeroId); HerosLocations location = HerosLocations.GenInitialLocation(_context, newly.HeroId); _context.Heros.Add(newly); _context.UsersHeros.Add(userheros); _context.Equipment.Add(eq); _context.HerosLocations.Add(location); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("tokenErr", "Hero already exists."))); } return(Ok((HeroBrief)newly)); }
public async Task <IActionResult> PostFighting([FromBody] PassedGameData <bool> passedData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DateTime now = DateTime.UtcNow; if (passedData.UserToken == null || passedData.ActionToken == null) { return(BadRequest(new DataError("securityErr", "No authorization controll."))); } UserToken dbtoken = Security.CheckUserToken(this._context, passedData.UserToken); if (dbtoken == null) { return(BadRequest(new DataError("securityErr", "Your data has probably been stolen or modified manually. We suggest password's change."))); } else { if (!dbtoken.IsTimeValid(now)) { return(BadRequest(new DataError("timeoutErr", "You have been too long inactive. Relogin is required."))); } else { dbtoken.UpdateToken(now); } } Heros hero = _context.Heros.FirstOrDefault(e => e.Name == passedData.ActionToken.HeroName); ActionToken gametoken = Security.CheckActionToken(_context, passedData.ActionToken, hero.HeroId); if (gametoken == null) { return(BadRequest(new DataError("securityErr", "Your data has probably been stolen or modified manually. We suggest password's change."))); } else { if (!gametoken.IsTimeValid(now)) { return(BadRequest(new DataError("timeoutErr", "You have been too long inactive. Relogin is required."))); } else { gametoken.UpdateToken(now); } } // now do your stuff... if (hero.Status == 3) { // to pass object statusData = null; var Added = new List <EquipmentModifyResult.EquipmentModification>(); var newItems = new List <ItemResult>(); var fight = _context.Fighting.FirstOrDefault(e => e.HeroId == hero.HeroId); if (fight == null) { return(BadRequest(new DataError("fightErr", "Hero has no fight data."))); } if (!fight.IsOver) { return(BadRequest(new DataError("fightErr", "Fight has not been finished."))); } if (hero.Hp > 0) { hero.Status = 0; if (fight.Loot.HasValue && passedData.Data) { var Item = _context.Items.FirstOrDefault(e => e.ItemId == fight.Loot.Value); if (Item == null) { return(BadRequest(new DataError("itemErr", "Looted item not found."))); } newItems.Add((ItemResult)Item); var heroBackpack = _context.Backpack.Where(e => e.HeroId == hero.HeroId); int count = heroBackpack.Count(); if (count > 0) { var Equipment = _context.Equipment.FirstOrDefault(e => e.HeroId == hero.HeroId); if (Equipment == null) { return(BadRequest(new DataError("heroErr", "Hero without equipment."))); } if (Equipment.BackpackSize <= count) { return(BadRequest(new DataError("backpackErr", "In order to add this reward, you need to remove one item from backpack or resign from reward."))); } if (heroBackpack.Where(e => e.SlotNr == 0).Count() > 0) { var preSlot = heroBackpack.FirstOrDefault(e => heroBackpack.Where(f => f.SlotNr == e.SlotNr + 1).Count() == 0); _context.Backpack.Add(new Backpack() { HeroId = hero.HeroId, ItemId = fight.Loot.Value, SlotNr = preSlot.SlotNr + 1, }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fight.Loot.Value, Target = "Backpack" + (preSlot.SlotNr + 1) }); } else { _context.Backpack.Add(new Backpack() { HeroId = hero.HeroId, ItemId = fight.Loot.Value, SlotNr = 0, }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fight.Loot.Value, Target = "Backpack" + 0 }); } } else { _context.Backpack.Add(new Backpack() { HeroId = hero.HeroId, ItemId = fight.Loot.Value, SlotNr = 0, }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fight.Loot.Value, Target = "Backpack" + 0 }); } } var enemy = _context.Enemies.FirstOrDefault(e => e.EnemyId == fight.EnemyId); if (enemy == null) { return(BadRequest(new DataError("fightErr", "Enemy not found."))); } hero.Experience += HeroCalculator.ExpForFight(hero.Lvl, enemy.Lvl); bool update = false; while (hero.Experience >= HeroCalculator.ExpToLevel(hero.Lvl + 1)) { hero.Lvl += 1; update = true; } if (update) { var(hp, sl) = HeroCalculator.HPSLmax(hero, _context); hero.Hp = hp; } } else { hero.Status = 2; var(hp, sl) = HeroCalculator.HPSLmax(hero, _context); if (hero.Hp >= hp) { hero.Hp = hp; try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } } int time = HeroCalculator.RecoveryTime(hp, hero.Hp, hero.Lvl) / hero.VelocityFactor; Healing heal = new Healing() { EndTime = now.AddSeconds(time), HeroId = hero.HeroId, StartHp = hero.Hp, StartHpmax = hp, StartTime = now, }; this._context.Healing.Add(heal); statusData = heal.GenHealingResult(now); } // TODO update map! _context.Fighting.Remove(fight); try { var location = LocationHandler.InstanceClearCurrent(_context, hero); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } return(Ok(new { success = true, heroStatus = hero.Status, newHP = hero.Hp, newHPmax = HeroCalculator.BaseHP(hero.Lvl), newLvl = hero.Lvl, newExp = hero.Experience, location, statusData, added = Added.ToArray(), newItems = newItems.ToArray() })); } catch (OperationException e) { return(BadRequest(new DataError(e.ErrorClass, e.Message))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not in the fight."))); } }