Esempio n. 1
0
        public BattleCreateReturnModel GetBattle([FromBody] CreateBattleModel model,
                                                 [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new StoreContext();

                var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid session key!");
                }

                var secondUser = context.Users.FirstOrDefault(usr => usr.UserId == model.SecondUserId);

                var battle = context.Battles
                             .Where(b =>
                                    (b.SecondUser.UserId == model.SecondUserId && b.FirstUser.UserId == model.FirstUserId) ||
                                    (b.FirstUser.UserId == model.SecondUserId && b.SecondUser.UserId == model.FirstUserId)).FirstOrDefault();

                if (battle == null)
                {
                    throw new ArgumentNullException("Cannot find the requested battle!");
                }

                var createdBattleModel = new BattleCreateReturnModel()
                {
                    ID = battle.Id
                };

                return(createdBattleModel);
            });

            return(responseMsg);
        }
Esempio n. 2
0
        public HttpResponseMessage PerformAttackWithWeapon(int battleId, [FromBody] CreateBattleModel model,
                                                           [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new StoreContext();

                var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid session key!");
                }

                var secondUser = context.Users.FirstOrDefault(usr =>
                                                              (usr.UserId == model.SecondUserId && usr.UserId != user.UserId) ||
                                                              usr.UserId == model.FirstUserId && usr.UserId != user.UserId);

                var battle = context.Battles.FirstOrDefault(b => b.Id == battleId);

                if (battle == null)
                {
                    throw new ArgumentNullException("Invalid battle!");
                }

                if (battle.UserInTurn != user)
                {
                    throw new ArgumentException("You are not in turn!");
                }

                Attack(user.Hero.MeleAttack, secondUser.Hero.MeleDefense, secondUser);

                battle.UserInTurn = battle.UserInTurn == secondUser ? user : secondUser;
                context.SaveChanges();

                var createdBattleModel = new BattleCreateReturnModel()
                {
                    ID = battle.Id
                };

                var response =
                    this.Request.CreateResponse(HttpStatusCode.OK, createdBattleModel);
                return(response);
            });

            return(responseMsg);
        }
Esempio n. 3
0
        public HttpResponseMessage CreateBattle([FromBody] CreateBattleModel model,
                                                [ValueProvider(typeof(HeaderValueProviderFactory <string>))] string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new StoreContext();

                var user = context.Users.FirstOrDefault(usr => usr.SessionKey == sessionKey);
                if (user == null)
                {
                    throw new InvalidOperationException("Invalid session key!");
                }

                var secondUser = context.Users.FirstOrDefault(usr => usr.UserId == model.SecondUserId);

                var battle = new Battle
                {
                    FirstUser  = user,
                    SecondUser = secondUser,
                    UserInTurn = model.UserInTurnId == model.FirstUserId ? user : secondUser
                };

                context.Battles.Add(battle);
                context.SaveChanges();

                var createdBattleModel = new BattleCreateReturnModel()
                {
                    ID = battle.Id
                };

                var response =
                    this.Request.CreateResponse(HttpStatusCode.Created, createdBattleModel);
                return(response);
            });

            return(responseMsg);
        }