private void CheckTraveling(string username, Player player) { if (player.task != null) { if (player.task is Traveling) { Traveling travelTask = player.task as Traveling; if (travelTask.hoursPassed >= 1) { player.EndTask(username, null); } } } }
// GET: Travel public ActionResult Index() { Traveling travel = _provider.GetTravel(); return(Ok(travel.Adapt <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> 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."))); } }