Example #1
0
        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.")));
            }
        }