public async Task <IActionResult> PostHealing([FromBody] PassedGameData <int?> 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 == 0) { var(hp, sl) = HeroCalculator.HPSLmax(hero, _context); if (hero.Hp >= hp) { hero.Hp = hp; try { await _context.SaveChangesAsync(); return(BadRequest(new DataError("heroHpSucc", "Hero has already full HP."))); } 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); hero.Status = 2; try { await _context.SaveChangesAsync(); HealingResult result = heal.GenHealingResult(now); return(Ok(new { success = true, healing = result })); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not able to change state."))); } }
public async Task <IActionResult> PostHerosLocations([FromBody] PassedGameData <int?> 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); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } // can do stuff var Equipment = _context.Equipment.FirstOrDefault(e => e.HeroId == hero.HeroId); if (Equipment == null) { return(BadRequest(new DataError("equipmentErr", "Hero is without equipment."))); } List <int?> used = new List <int?> { Equipment.Armour, Equipment.Bracelet, Equipment.FirstHand, Equipment.Gloves, Equipment.Neckles, Equipment.Ring1, Equipment.Ring2, Equipment.SecondHand, Equipment.Shoes, Equipment.Trousers }; var ItemsOn = used.Where(e => e.HasValue).Select(e => e.Value).ToList(); var Backpack = _context.Backpack.Where(e => e.HeroId == hero.HeroId); var UsedItems = Backpack.Select(e => e.ItemId).Distinct().ToList(); UsedItems.AddRange(ItemsOn); UsedItems = UsedItems.Distinct().OrderBy(e => e).ToList(); var ItemsInUse = _context.Items.Join(UsedItems, e => e.ItemId, e => e, (a, b) => a).ToArray(); EquipmentResult EQ = Equipment.GenResult(ItemsInUse.ToArray(), Backpack.ToList()); return(Ok(new { success = true, equipment = EQ })); }
public async Task <IActionResult> PostTraveling([FromBody] PassedGameData <int> 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); } } // if can go there if (hero.Status == 0) { var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { return(BadRequest(new DataError("LocationErr", "LocationData is not available."))); } try { // TODO check location type AstarResult astar = null; int TravelScale = 0; int Start = -1; string StartName = ""; int Target = -1; string TargetName = ""; 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; int GlobalNodeID = description.GlobalMainNodeID(passedData.Data, state); if (GlobalNodeID == state.CurrentLocation) { throw new Exception("Moving nowhere"); } astar = LocationHandler.DistanceToMove(description, state, passedData.Data); TravelScale = description.TravelScale; Start = state.CurrentLocation; StartName = description.MainNodes.First(e => e.NodeID == state.CurrentLocation).Name; Target = GlobalNodeID; TargetName = description.MainNodes.First(e => e.NodeID == GlobalNodeID).Name; } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; int GlobalNodeID = description.GlobalMainNodeID(passedData.Data, state); if (GlobalNodeID == state.CurrentLocation) { throw new Exception("Moving nowhere"); } astar = LocationHandler.DistanceToMove(description, state, passedData.Data); TravelScale = description.TravelScale; Start = state.CurrentLocation; Target = GlobalNodeID; TargetName = passedData.Data.ToString(); } double TravelTime = LocationHandler.TimeTravel(astar.Distance, TravelScale, 18 * hero.VelocityFactor); Traveling travel = new Traveling() { EndTime = now.AddSeconds(TravelTime), HeroId = hero.HeroId, IsReverse = false, ReverseTime = null, Start = Start, StartName = StartName, StartTime = now, Target = Target, TargetName = TargetName, }; hero.Status = 1; _context.Traveling.Add(travel); TravelResult travelResult = travel.GenTravelResult(now); try { await _context.SaveChangesAsync(); return(Ok(new { success = true, travel = travelResult })); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember travel."))); } } catch { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not able to travel."))); } }
public async Task <IActionResult> PostHerosLocations([FromBody] PassedGameData <int?> 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); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } // can load location status - update status -> function (?) var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { return(BadRequest(new DataError("LocationErr", "LocationData is not available."))); } try { // TODO check location type -> vitual class or what? int LocationType = descr.LocationGlobalType; if (LocationType != 2) { LocationDescription description = JsonConvert.DeserializeObject <LocationDescription>(descr.Sketch); LocationState state = JsonConvert.DeserializeObject <LocationState>(location.Description); description.LocationGlobalType = LocationType; 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 { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remove travel."))); } LocationResult <MainNodeResult> locationResult = description.GenLocalForm(state); return(Ok(new { success = true, location = locationResult })); } else { return(BadRequest(new DataError("LocationErr", "Travel is not finished"))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not in travel mode"))); } } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = LocationType; 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 { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remove travel."))); } LocationResult <InstanceNodeResult> locationResult = description.GenLocalForm(state); return(Ok(new { success = true, location = locationResult })); } else { return(BadRequest(new DataError("LocationErr", "Travel is not finished"))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not in travel mode"))); } } } catch { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } }
public async Task <IActionResult> PostFighting([FromBody] PassedGameData <FightingCalculator.AttackType> 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) { var fighting = _context.Fighting.FirstOrDefault(e => e.HeroId == hero.HeroId); if (fighting == null) { return(BadRequest(new DataError("fightingErr", "Failed to load fight. Hero is not in fight."))); } if (fighting.IsOver) { return(BadRequest(new DataError("fightingErr", "Fight is already finished!"))); } var enemy = _context.Enemies.FirstOrDefault(e => e.EnemyId == fighting.EnemyId); if (enemy == null) { return(BadRequest(new DataError("fightingErr", "Failed to load enemy."))); } var Logs = FightingCalculator.MakeTurn(fighting, hero, enemy, passedData.Data, this._context); try { await _context.SaveChangesAsync(); var result = fighting.GenResult(_context, hero, enemy); result.Log = Logs; return(Ok(new { success = true, fightingData = result, heroHP = hero.Hp })); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not fighting."))); } }
public async Task <IActionResult> PostTraveling([FromBody] PassedGameData <int?> 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); } } // if can go there if (hero.Status == 1) { try { var Travel = _context.Traveling.FirstOrDefault(e => e.HeroId == hero.HeroId); if (Travel == null || Travel.HasEnded(now) || Travel.IsReverse) { throw new Exception(); } Travel.IsReverse = true; Travel.ReverseTime = now; TravelResult travelResult = Travel.GenTravelResult(now); try { await _context.SaveChangesAsync(); return(Ok(new { success = true, travel = travelResult })); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember travel."))); } } catch { return(BadRequest(new DataError("TravelErr", "Travel reverse is not available."))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not in the travel."))); } }
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."))); } }
public async Task <IActionResult> PostHealing([FromBody] PassedGameData <int?> 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 == 2) { var heal = _context.Healing.FirstOrDefault(e => e.HeroId == hero.HeroId); if (heal == null) { return(BadRequest(new DataError("heroHpSucc", "Hero has no healing progress."))); } int newHP = heal.FinalHealth(now); hero.Hp = newHP; HeroCalculator.CheckHeroHP(hero, _context); _context.Healing.Remove(heal); hero.Status = 0; try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } return(Ok(new { success = true, newHP = hero.Hp })); } else { return(BadRequest(new DataError("LocationErr", "Hero is not healing."))); } }
public async Task <IActionResult> PostLocationAction([FromBody] PassedGameData <int> 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 == 0) { var location = _context.HerosLocations.FirstOrDefault(e => (e.HeroId == hero.HeroId) && (e.LocationIdentifier == hero.CurrentLocation)); if (location == null) { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } var descr = _context.LocationsDb.FirstOrDefault(e => e.LocationIdentifier == location.LocationIdentifier); if (descr == null) { return(BadRequest(new DataError("LocationErr", "LocationData is not available."))); } try { // TODO check location type 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; var CurrentNode = description.MainNodes.FirstOrDefault(e => e.NodeID == state.CurrentLocation); if (CurrentNode.Data == -1) { try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember travel."))); } return(BadRequest(new DataError("notImplementedErr", "This feature has not been implemented yet. We are working on it!"))); } try { if (!LocationHandler.OptionsForLocation.ContainsKey(CurrentNode.LocationType)) { throw new Exception(); } if (!LocationHandler.LocationTypeFunctions.ContainsKey(LocationHandler.OptionsForLocation[CurrentNode.LocationType][passedData.Data])) { throw new Exception(); } var func = LocationHandler.LocationTypeFunctions[LocationHandler.OptionsForLocation[CurrentNode.LocationType][passedData.Data]]; func(_context, hero, CurrentNode.Data); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember action."))); } } catch (OperationException e) { return(BadRequest(new DataError(e.ErrorClass, e.Message))); } catch { return(BadRequest(new DataError("notImplementedErr", "This feature has not been implemented yet. We are working on it!"))); } } else { InstanceDescription description = JsonConvert.DeserializeObject <InstanceDescription>(descr.Sketch); InstanceState state = JsonConvert.DeserializeObject <InstanceState>(location.Description); description.LocationGlobalType = descr.LocationGlobalType; var CurrentNode = description.MainNodes.FirstOrDefault(e => e.NodeID == state.CurrentLocation); if (CurrentNode.Data == -1) { try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember travel."))); } return(BadRequest(new DataError("notImplementedErr", "This feature has not been implemented yet. We are working on it!"))); } try { if (!LocationHandler.OptionsForInstances.ContainsKey(CurrentNode.InstanceType)) { throw new Exception(); } if (!LocationHandler.InstanceTypeFunctions.ContainsKey(LocationHandler.OptionsForInstances[CurrentNode.InstanceType][passedData.Data])) { throw new Exception(); } var func = LocationHandler.InstanceTypeFunctions[LocationHandler.OptionsForInstances[CurrentNode.InstanceType][passedData.Data]]; func(_context, hero, CurrentNode.Data); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to remember action."))); } } catch { return(BadRequest(new DataError("notImplementedErr", "This feature has not been implemented yet. We are working on it!"))); } } // load new hero status try { var heroStatus = LocationHandler.GetHeroGeneralStatus(_context, hero, now); return(Ok(new { success = true, location = heroStatus.Location, statusData = heroStatus.StatusData, heroStatus = heroStatus.HeroStatus })); } catch (Exception e) { return(BadRequest(new DataError("statusErr", e.Message))); } } catch { return(BadRequest(new DataError("LocationErr", "Location is not available."))); } } else { return(BadRequest(new DataError("LocationErr", "Hero is not able to change state."))); } }
public async Task <IActionResult> PostHeros([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.Invitational) { try { if (passedData.Data[0]) { hero.VelocityFactor = 10; } if (passedData.Data[1]) { hero.Lvl = 5; hero.Experience = HeroCalculator.ExpToLevel(5); } if (passedData.Data[2]) { var list = new List <Backpack>(); for (int i = 1; i <= 16; i++) { list.Add(new Backpack() { HeroId = hero.HeroId, ItemId = i, SlotNr = i - 1, }); } _context.Backpack.AddRange(list); } hero.Invitational = false; await _context.SaveChangesAsync(); return(Ok(new { success = true })); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update hero."))); } } else { return(BadRequest(new DataError("securityErr", "This option should be impossible to reach."))); } }
public async Task <IActionResult> PostEquipment([FromBody] PassedGameData <PassedChangeEqData> 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); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update tokens."))); } // can do stuff var Equipment = _context.Equipment.FirstOrDefault(e => e.HeroId == hero.HeroId); if (Equipment == null) { return(BadRequest(new DataError("equipmentErr", "Hero is without equipment."))); } var Backpack = _context.Backpack.Where(e => e.HeroId == hero.HeroId); var Added = new List <EquipmentModifyResult.EquipmentModification>(); var Removed = new List <EquipmentModifyResult.EquipmentModification>(); if (passedData.Data.From == passedData.Data.To) { return(Ok(new { success = true, changes = new EquipmentModifyResult() { Added = Added.ToArray(), Removed = Removed.ToArray() } })); } // BP -> BP if (passedData.Data.From.StartsWith("Backpack") && passedData.Data.To.StartsWith("Backpack")) { int fromSlot = int.Parse(passedData.Data.From.Substring(8)); int toSlot = int.Parse(passedData.Data.To.Substring(8)); var fromItem = _context.Backpack.FirstOrDefault(e => e.HeroId == hero.HeroId && e.SlotNr == fromSlot); if (fromItem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } // remove from current and add to new Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fromItem.ItemId, Target = passedData.Data.To }); var toItem = _context.Backpack.FirstOrDefault(e => e.HeroId == hero.HeroId && e.SlotNr == toSlot); if (toItem == null) { _context.Backpack.Remove(fromItem); _context.Backpack.Add(new Backpack() { HeroId = hero.HeroId, ItemId = fromItem.ItemId, SlotNr = toSlot, }); } else { Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.To }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = toItem.ItemId, Target = passedData.Data.From }); int mem = toItem.ItemId; toItem.ItemId = fromItem.ItemId; fromItem.ItemId = mem; } } // BP -> EQ if (passedData.Data.From.StartsWith("Backpack") && passedData.Data.To.StartsWith("Inventory")) { int fromSlot = int.Parse(passedData.Data.From.Substring(8)); int toSlot = int.Parse(passedData.Data.To.Substring(9)); var fromItem = _context.Backpack.FirstOrDefault(e => e.HeroId == hero.HeroId && e.SlotNr == fromSlot); if (fromItem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } var passeditem = _context.Items.FirstOrDefault(e => e.ItemId == fromItem.ItemId); if (passeditem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } if (!EquipmentControll.CanEquip(passeditem, hero, toSlot)) { return(BadRequest(new DataError("changeEqErr", "Items requirements are not fullfilled."))); } Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fromItem.ItemId, Target = passedData.Data.To }); int?Target = null; switch (toSlot) { case 0: Target = Equipment.Helmet; break; case 1: Target = Equipment.Ring1; break; case 2: Target = Equipment.Neckles; break; case 3: Target = Equipment.Ring2; break; case 4: Target = Equipment.Gloves; break; case 5: Target = Equipment.Armour; break; case 6: Target = Equipment.Bracelet; break; case 7: Target = Equipment.FirstHand; break; case 8: Target = Equipment.Trousers; break; case 9: Target = Equipment.SecondHand; break; case 10: Target = Equipment.Shoes; break; } var toItem = Target; // update inventory switch (toSlot) { case 0: Equipment.Helmet = fromItem.ItemId; break; case 1: Equipment.Ring1 = fromItem.ItemId; break; case 2: Equipment.Neckles = fromItem.ItemId; break; case 3: Equipment.Ring2 = fromItem.ItemId; break; case 4: Equipment.Gloves = fromItem.ItemId; break; case 5: Equipment.Armour = fromItem.ItemId; break; case 6: Equipment.Bracelet = fromItem.ItemId; break; case 7: Equipment.FirstHand = fromItem.ItemId; break; case 8: Equipment.Trousers = fromItem.ItemId; break; case 9: Equipment.SecondHand = fromItem.ItemId; break; case 10: Equipment.Shoes = fromItem.ItemId; break; } // update backpack if (toItem == null) { _context.Backpack.Remove(fromItem); } else { Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.To }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = toItem, Target = passedData.Data.From }); fromItem.ItemId = toItem.Value; } } // EQ -> BP if (passedData.Data.From.StartsWith("Inventory") && passedData.Data.To.StartsWith("Backpack")) { int fromSlot = int.Parse(passedData.Data.From.Substring(9)); int toSlot = int.Parse(passedData.Data.To.Substring(8)); int?Target = null; switch (fromSlot) { case 0: Target = Equipment.Helmet; break; case 1: Target = Equipment.Ring1; break; case 2: Target = Equipment.Neckles; break; case 3: Target = Equipment.Ring2; break; case 4: Target = Equipment.Gloves; break; case 5: Target = Equipment.Armour; break; case 6: Target = Equipment.Bracelet; break; case 7: Target = Equipment.FirstHand; break; case 8: Target = Equipment.Trousers; break; case 9: Target = Equipment.SecondHand; break; case 10: Target = Equipment.Shoes; break; } var fromItem = Target; if (fromItem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } var toItem = _context.Backpack.FirstOrDefault(e => e.HeroId == hero.HeroId && e.SlotNr == toSlot); if (toItem != null) { // slot is being used var passeditem = _context.Items.FirstOrDefault(e => e.ItemId == toItem.ItemId); if (passeditem == null) { return(BadRequest(new DataError("changeEqErr", "No target item in DB."))); } if (!EquipmentControll.CanEquip(passeditem, hero, fromSlot)) { return(BadRequest(new DataError("changeEqErr", "Items requirements are not fullfilled."))); } } Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fromItem.Value, Target = passedData.Data.To }); // null if prev was empty lub itemID if existed int?invRes = toItem?.ItemId; // update inventory switch (fromSlot) { case 0: Equipment.Helmet = invRes; break; case 1: Equipment.Ring1 = invRes; break; case 2: Equipment.Neckles = invRes; break; case 3: Equipment.Ring2 = invRes; break; case 4: Equipment.Gloves = invRes; break; case 5: Equipment.Armour = invRes; break; case 6: Equipment.Bracelet = invRes; break; case 7: Equipment.FirstHand = invRes; break; case 8: Equipment.Trousers = invRes; break; case 9: Equipment.SecondHand = invRes; break; case 10: Equipment.Shoes = invRes; break; } // update backpack if (toItem == null) { _context.Backpack.Add(new Backpack() { HeroId = hero.HeroId, ItemId = fromItem.Value, SlotNr = toSlot, }); } else { Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.To }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = toItem.ItemId, Target = passedData.Data.From }); toItem.ItemId = fromItem.Value; } } // EQ -> EQ if (passedData.Data.From.StartsWith("Inventory") && passedData.Data.To.StartsWith("Inventory")) { int fromSlot = int.Parse(passedData.Data.From.Substring(9)); int toSlot = int.Parse(passedData.Data.To.Substring(9)); int?Target = null; switch (fromSlot) { case 0: Target = Equipment.Helmet; break; case 1: Target = Equipment.Ring1; break; case 2: Target = Equipment.Neckles; break; case 3: Target = Equipment.Ring2; break; case 4: Target = Equipment.Gloves; break; case 5: Target = Equipment.Armour; break; case 6: Target = Equipment.Bracelet; break; case 7: Target = Equipment.FirstHand; break; case 8: Target = Equipment.Trousers; break; case 9: Target = Equipment.SecondHand; break; case 10: Target = Equipment.Shoes; break; } var fromItem = Target; if (fromItem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } Target = null; switch (toSlot) { case 0: Target = Equipment.Helmet; break; case 1: Target = Equipment.Ring1; break; case 2: Target = Equipment.Neckles; break; case 3: Target = Equipment.Ring2; break; case 4: Target = Equipment.Gloves; break; case 5: Target = Equipment.Armour; break; case 6: Target = Equipment.Bracelet; break; case 7: Target = Equipment.FirstHand; break; case 8: Target = Equipment.Trousers; break; case 9: Target = Equipment.SecondHand; break; case 10: Target = Equipment.Shoes; break; } var toItem = Target; var passeditem = _context.Items.FirstOrDefault(e => e.ItemId == fromItem.Value); if (passeditem == null) { return(BadRequest(new DataError("changeEqErr", "No target item in DB."))); } if (!EquipmentControll.CanEquip(passeditem, hero, toSlot)) { return(BadRequest(new DataError("changeEqErr", "Items requirements are not fullfilled."))); } if (toItem != null) { var passeditem2 = _context.Items.FirstOrDefault(e => e.ItemId == toItem.Value); if (passeditem2 == null) { return(BadRequest(new DataError("changeEqErr", "No target item in DB."))); } if (!EquipmentControll.CanEquip(passeditem2, hero, fromSlot)) { return(BadRequest(new DataError("changeEqErr", "Items requirements are not fullfilled."))); } } Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = fromItem.Value, Target = passedData.Data.To }); // update inventory switch (fromSlot) { case 0: Equipment.Helmet = toItem; break; case 1: Equipment.Ring1 = toItem; break; case 2: Equipment.Neckles = toItem; break; case 3: Equipment.Ring2 = toItem; break; case 4: Equipment.Gloves = toItem; break; case 5: Equipment.Armour = toItem; break; case 6: Equipment.Bracelet = toItem; break; case 7: Equipment.FirstHand = toItem; break; case 8: Equipment.Trousers = toItem; break; case 9: Equipment.SecondHand = toItem; break; case 10: Equipment.Shoes = toItem; break; } switch (toSlot) { case 0: Equipment.Helmet = fromItem; break; case 1: Equipment.Ring1 = fromItem; break; case 2: Equipment.Neckles = fromItem; break; case 3: Equipment.Ring2 = fromItem; break; case 4: Equipment.Gloves = fromItem; break; case 5: Equipment.Armour = fromItem; break; case 6: Equipment.Bracelet = fromItem; break; case 7: Equipment.FirstHand = fromItem; break; case 8: Equipment.Trousers = fromItem; break; case 9: Equipment.SecondHand = fromItem; break; case 10: Equipment.Shoes = fromItem; break; } if (toItem != null) { Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.To }); Added.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = toItem.Value, Target = passedData.Data.From }); } } // Remove item if (passedData.Data.To == "Trash") { if (passedData.Data.From.StartsWith("Backpack")) { int fromSlot = int.Parse(passedData.Data.From.Substring(8)); var fromItem = _context.Backpack.FirstOrDefault(e => e.HeroId == hero.HeroId && e.SlotNr == fromSlot); if (fromItem == null) { return(BadRequest(new DataError("changeEqErr", "No initial item."))); } // remove from current and add to new _context.Backpack.Remove(fromItem); Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); } if (passedData.Data.From.StartsWith("Inventory")) { int fromSlot = int.Parse(passedData.Data.From.Substring(9)); switch (fromSlot) { case 0: Equipment.Helmet = null; break; case 1: Equipment.Ring1 = null; break; case 2: Equipment.Neckles = null; break; case 3: Equipment.Ring2 = null; break; case 4: Equipment.Gloves = null; break; case 5: Equipment.Armour = null; break; case 6: Equipment.Bracelet = null; break; case 7: Equipment.FirstHand = null; break; case 8: Equipment.Trousers = null; break; case 9: Equipment.SecondHand = null; break; case 10: Equipment.Shoes = null; break; } Removed.Add(new EquipmentModifyResult.EquipmentModification() { ItemID = null, Target = passedData.Data.From }); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new DataError("databaseErr", "Failed to update equipment."))); } return(Ok(new { success = true, changes = new EquipmentModifyResult() { Added = Added.ToArray(), Removed = Removed.ToArray() } })); }