Пример #1
0
        public IQueryable<ReturnRaceModel> GetAll(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = BasePersister.GetUserBySessionKey(sessionKey, context);

                if (user == null)
                {
                    throw new NullReferenceException();
                }
                var races = context.Races;

                var models =
                    (from r in races
                     select new ReturnRaceModel
                     {
                        Id = r.Id,
                        Name = r.Name
                     });

                return models;
            });
        }
Пример #2
0
 private static void ValidateRace(int raceName, GameContext context)
 {
     var result = context.Races.FirstOrDefault(r => r.Id == raceName);
     if (result == null)
     {
         throw new ArgumentNullException("The Race is not valid!");
     }
 }
Пример #3
0
        public HttpResponseMessage LogoutUser(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = UserPersister.GetUserBySessionKey(sessionKey, context);
                user.SessionKey = null;
                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });
        }
Пример #4
0
        public HttpResponseMessage LoginUser(LoginUserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                if (model == null)
                {
                    throw new FormatException("invalid username and/or password");
                }

                UserPersister.ValidateLoginUser(model);

                var context = new GameContext();

                var user = context.Users.FirstOrDefault(u => u.Username == model.Username.ToLower()
                    && u.AuthCode == model.AuthCode);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password");
                }

                if (user.SessionKey == null)
                {
                    user.SessionKey = UserPersister.GenerateSessionKey(user.Id);
                    context.SaveChanges();
                }

                var responseModel = new UserResponseModel()
                {
                    Nickname = user.Nickname,
                    SesionKey = user.SessionKey
                };

                var response = this.Request.CreateResponse(HttpStatusCode.OK, responseModel);
                return response;
            });
        }
Пример #5
0
        public HttpResponseMessage GetAllHeros(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = BasePersister.GetUserBySessionKey(sessionKey, context);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password!");
                }

                var heroes = user.Heroes;

                var models = (
                    from h in heroes
                    select new ViewHeroModel
                    {
                        Id = h.Id,
                        Name = h.Name,
                        Image = h.Image,
                        Money = h.Money,
                        NumberOfWins = h.NumberOfWins,
                        NumberOfLoses = h.NumberOfLoses,
                        Level = h.Level,
                        Race = h.Race.Name,
                        Points = h.Points
                    });

                var response = this.Request.CreateResponse(HttpStatusCode.OK, models);

                return response;
            });
        }
Пример #6
0
        public HttpResponseMessage CreateHeroe(CreateHeroeModel model,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = BasePersister.GetUserBySessionKey(sessionKey, context);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password!");
                }

                HeroePersister.ValidateCreateHeroe(model, context);

                Hero newHeroe = new Hero()
                 {
                     Name = model.Name,
                     Race = context.Races.First(r => r.Id == model.Race),
                     Level = StartHeroeLevel,
                     Points = StartHeroePoints,
                     Money = StartHeroeMoney,
                     NumberOfLoses = 0,
                     NumberOfWins = 0,
                     User = BasePersister.GetUserBySessionKey(sessionKey, context),
                     Units = new HashSet<Unit>()
                 };
                context.Heroes.Add(newHeroe);

                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });
        }
Пример #7
0
 public static User GetUserByUsernameAndDisplayName(string username, string nickname, GameContext context)
 {
     return context.Users.FirstOrDefault(u => u.Username == username.ToLower() && u.Nickname == nickname);
 }
Пример #8
0
 public static User GetUserBySessionKey(string sessionKey, GameContext context)
 {
     return context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
 }
Пример #9
0
 public static void ValidateCreateHeroe(CreateHeroeModel model, GameContext context)
 {
     ValidateName(model.Name);
     ValidateRace(model.Race, context);
 }
Пример #10
0
        public HttpResponseMessage GetHeroUnits(int id,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = BasePersister.GetUserBySessionKey(sessionKey, context);

                if (user == null)
                {
                    throw new InvalidOperationException("Invalid username or password!");
                }

                var units = context.Units.Where(h => h.Hero.Id == id);

                var models = (
                    from u in units
                    select new ViewUnitModel
                    {
                        Id = u.Id,
                        Name = u.Name,
                        Image = u.Image,
                        Health = u.Health,
                        Attack = u.Attack,
                        Defense = u.Defense,
                        Damage = u.Damage,
                        Speed = u.Speed
                    });

                var response = this.Request.CreateResponse(HttpStatusCode.OK, models);

                return response;
            });
        }
Пример #11
0
        public HttpResponseMessage RegisterUser(RegisterUserModel model)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var dbUser = UserPersister.GetUserByUsernameAndDisplayName(model.Username, model.Nickname, context);
                if (dbUser != null)
                {
                    throw new InvalidOperationException("This user already exists in the database");
                }

                dbUser = new User()
                {
                    Username = model.Username.ToLower(),
                    Nickname = model.Nickname,
                    AuthCode = model.AuthCode,
                    Role = context.Roles.Where(r => r.Name == "user").FirstOrDefault()
                };
                context.Users.Add(dbUser);
                dbUser.SessionKey = UserPersister.GenerateSessionKey(dbUser.Id);

                context.SaveChanges();

                var responseModel = new UserResponseModel()
                {
                    Nickname = dbUser.Nickname,
                    SesionKey = dbUser.SessionKey
                };

                var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                return response;
            });
        }
Пример #12
0
        public HttpResponseMessage UserDetails(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new GameContext();
                var user = UserPersister.GetUserBySessionKey(sessionKey, context);

                var model = new UserDetails
                {
                    Nickname = user.Nickname,
                    Username = user.Username,
                    Heroes = (
                    from h in user.Heroes
                    select new ViewHeroModel
                    {
                        Id = h.Id,
                        Name = h.Name
                    }),
                    Avatar = user.Avatar
                };

                var response = this.Request.CreateResponse(HttpStatusCode.OK, model);
                return response;
            });
        }
Пример #13
0
        public HttpResponseMessage UploadAvatar()
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var httpRequest = HttpContext.Current.Request;

                var context = new GameContext();
                var dbUser = UserPersister.GetUserByUsernameAndDisplayName(httpRequest.Form["username"], httpRequest.Form["nickname"], context);
                if (dbUser == null)
                {
                    throw new InvalidOperationException("This user already exists in the database");
                }

                if (httpRequest.Files.Count > 0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];

                        var configuration = new AccountConfiguration("djlwcsyiz", "781383948985498", "Vh5BQmeTxvSKvTGTg-wRDYKqPz4");

                        var uploader = new Uploader(configuration);
                        string publicId = Path.GetFileNameWithoutExtension(postedFile.FileName);
                        var uploadResult = uploader.Upload(new UploadInformation(postedFile.FileName, postedFile.InputStream)
                        {
                            PublicId = publicId,
                            Format = postedFile.FileName.Substring(postedFile.FileName.Length - 3),
                        });

                        dbUser.Avatar = uploadResult.Url;
                    }
                }

                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });
        }