public async Task <IActionResult> PostHerosLocations([FromBody] HerosLocations herosLocations)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.HerosLocations.Add(herosLocations);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
            }

            return(CreatedAtAction("GetHerosLocations", new { id = herosLocations.LocationId }, herosLocations));
        }
Ejemplo n.º 2
0
        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));
        }