public async Task <IActionResult> PostHeros([FromBody] PassedData <PassedRemoveCharacter> passedData)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            DateTime now = DateTime.UtcNow;

            if (passedData.UserToken == 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);
                }
            }
            Users user = _context.Users.FirstOrDefault(e => e.Name == dbtoken.UserName);

            if (user.Password != HashClass.GenHash(passedData.Data.Password))
            {
                return(BadRequest(new DataError("passwordErr", "Password is incorrect.")));
            }
            Heros      herotoremove      = _context.Heros.FirstOrDefault(e => e.Name == passedData.Data.HeroName);
            UsersHeros conntoremove      = _context.UsersHeros.FirstOrDefault(e => e.UserName == dbtoken.UserName && e.HeroId == herotoremove.HeroId);
            var        tokentoremove     = _context.ActionToken.Where(e => e.HeroId == herotoremove.HeroId);
            var        locationstoremove = _context.HerosLocations.Where(e => e.HeroId == herotoremove.HeroId);
            var        travelingtoremove = _context.Traveling.Where(e => e.HeroId == herotoremove.HeroId);
            var        equipmenttoremove = _context.Equipment.Where(e => e.HeroId == herotoremove.HeroId);
            var        backpacktoremove  = _context.Backpack.Where(e => e.HeroId == herotoremove.HeroId);
            var        healingremove     = _context.Healing.Where(e => e.HeroId == herotoremove.HeroId);
            var        fightingremove    = _context.Fighting.Where(e => e.HeroId == herotoremove.HeroId);

            // TODO: remove other features

            if (tokentoremove.Count() > 0)
            {
                _context.ActionToken.RemoveRange(tokentoremove);
            }
            if (locationstoremove.Count() > 0)
            {
                _context.HerosLocations.RemoveRange(locationstoremove);
            }
            if (travelingtoremove.Count() > 0)
            {
                _context.Traveling.RemoveRange(travelingtoremove);
            }
            if (equipmenttoremove.Count() > 0)
            {
                _context.Equipment.RemoveRange(equipmenttoremove);
            }
            if (backpacktoremove.Count() > 0)
            {
                _context.Backpack.RemoveRange(backpacktoremove);
            }
            if (healingremove.Count() > 0)
            {
                _context.Healing.RemoveRange(healingremove);
            }
            if (fightingremove.Count() > 0)
            {
                _context.Fighting.RemoveRange(fightingremove);
            }

            _context.Heros.Remove(herotoremove);
            _context.UsersHeros.Remove(conntoremove);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                return(BadRequest(new DataError("serverErr", "Failed to remove hero.")));
            }
            return(Ok(new { success = true, removedHero = herotoremove.Name }));
        }
Пример #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));
        }