Example #1
0
        public UserFarmHistoryModel(UserFarmHistory userFarm)
        {
            this.PetName = userFarm.Animal.Breed;
            if (userFarm.PetStatus.Trim().EndsWith("P"))
            {
                this.Object = "Pet";
            }
            else if (userFarm.PetStatus.Trim().EndsWith("H"))
            {
                this.Object = "Hatching Egg";
            }
            else if (userFarm.PetStatus.Trim().EndsWith("E"))
            {
                this.Object = "Eggs In Stock";
            }
            else
            {
                this.Object = "No Info";
            }
            if (userFarm.PetStatus.Trim().Contains("ADD"))
            {
                this.Action = "Added";
            }
            else if (userFarm.PetStatus.Trim().Contains("SELL"))
            {
                this.Action = "Sold";
            }
            else if (userFarm.PetStatus.Trim().Contains("LOSE"))
            {
                this.Action = "Lost";
            }
            else this.Action = "No Info";

            this.Price = userFarm.PetPrice;
            this.Count = userFarm.PetCount;
        }
Example #2
0
        public HttpResponseMessage UpdateFarmEggsHistoryByUser(JObject obj)
        {
            var msg = PerformOperation(() =>
            {
                string sessionId = obj["sessionId"].Value<string>();
                ValidateSessionId(sessionId);
                User currentUser = db.Users.SingleOrDefault(x => x.SessionId == sessionId);

                int petId = obj["petId"].Value<int>();
                Animal pet = ValidatePetId(petId);

                string action = obj["action"].Value<string>();
                if (action != "ADDE" && action != "SELLE" && action != "LOSEE")
                {
                    throw BuildHttpResponseException("Invalid Eggs Action", "ERR_EGG_ACT");
                }

                int count = obj["count"].Value<int>();
                if (count < 0)
                {
                    throw BuildHttpResponseException("Invalid Eggs Count", "ERR_EGG_CNT");
                }

                UserFarm uf = db.UserFarms.SingleOrDefault(x => x.PetId == petId && x.UserId == currentUser.Id);

                double price = obj["price"].Value<double>();

                switch (action)
                {
                    case "ADDE":
                        {
                            if (price < 0)
                            {
                                throw BuildHttpResponseException("Invalid Eggs Price", "ERR_EGG_PRC");
                            }
                            else
                            {
                                if (uf == null)
                                {
                                    UserFarm tempUF = new UserFarm()
                                    {
                                        UserId = currentUser.Id,
                                        PetId = petId,
                                        EggsInStock = count,
                                        EggsHatching = 0,
                                        PetCount = 0
                                    };
                                    db.UserFarms.Add(tempUF);
                                }
                                else
                                {
                                    uf.EggsInStock += count;
                                }
                                UserFarmHistory tempFarmHistory = new UserFarmHistory()
                                {
                                    PetCount = 0,
                                    PetId = petId,
                                    PetStatus = action,
                                    EggsHatching = 0,
                                    EggsStocked = count,
                                    UserId = currentUser.Id,
                                    ActionDate = DateTime.Now,
                                    PetPrice = price
                                };
                                db.UserFarmHistories.Add(tempFarmHistory);
                                db.SaveChanges();
                            }
                            break;
                        }
                    case "SELLE":
                        {
                            if (price < 0)
                            {
                                throw BuildHttpResponseException("Invalid Eggs Price", "ERR_EGG_PRC");
                            }
                            else
                            {
                                if (uf == null)
                                {
                                    throw BuildHttpResponseException("Can not sell eggs of pet you dont have", "ERR_EGG_INV");
                                }
                                else
                                {
                                    uf.EggsInStock -= count;
                                    if (uf.EggsInStock < 0)
                                    {
                                        throw BuildHttpResponseException("Can not sell more eggs than you have", "ERR_HTC_CNT");
                                    }
                                }
                                UserFarmHistory tempFarmHistory = new UserFarmHistory()
                                {
                                    PetCount = 0,
                                    PetId = petId,
                                    PetStatus = action,
                                    EggsHatching = 0,
                                    EggsStocked = count,
                                    UserId = currentUser.Id,
                                    ActionDate = DateTime.Now,
                                    PetPrice = price
                                };
                                db.UserFarmHistories.Add(tempFarmHistory);
                                db.SaveChanges();
                            }
                            break;
                        }
                    case "LOSEE":
                        {
                            if (uf == null)
                            {
                                throw BuildHttpResponseException("Can not lose eggs of pet you dont have", "ERR_HTC_PET");
                            }
                            else
                            {
                                uf.EggsInStock -= count;
                                if (uf.EggsInStock < 0)
                                {
                                    throw BuildHttpResponseException("Can not lose more hatchings eggs that you have", "ERR_HTC_CNT");
                                }
                            }
                            UserFarmHistory tempFarmHistory = new UserFarmHistory()
                            {
                                PetCount = 0,
                                PetId = petId,
                                PetStatus = action,
                                EggsHatching = 0,
                                EggsStocked = count,
                                UserId = currentUser.Id,
                                ActionDate = DateTime.Now,
                                PetPrice = 0
                            };
                            db.UserFarmHistories.Add(tempFarmHistory);
                            db.SaveChanges();
                            break;
                        }
                    default:
                        break;
                }
                return "Success";
            });
            return msg;
        }